-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyd.java
More file actions
233 lines (191 loc) · 6.78 KB
/
Copy pathproxyd.java
File metadata and controls
233 lines (191 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class proxyd {
static final int DEFAULT_SERVER_PORT = 80;
static final String REQUEST_END = "\r\n\r\n";
static final String LINE_END = "\r\n";
static final int CACHE_TIME = 30000;
static int portNum = 5023;
private static Map<String, InetAddress> ips = new HashMap<String, InetAddress>();
private static Map<Long, String> dnsTimes = new HashMap<Long,String>();
public static void main(String[] args) {
if(args.length == 2) {
if(args[0].equals("-port")) {
portNum = Integer.parseInt(args[1]);
}
else {
usageErr();
}
}
else if(args.length == 1) {
usageErr();
}
else if(args.length != 0) {
usageErr();
}
try {
ServerSocket clientSocket = new ServerSocket(portNum);
run(clientSocket);
clientSocket.close();
} catch (IOException e) {
System.err.println("Encountered an I/O exception; exiting");
System.exit(-1);
}
}
private static void usageErr() {
System.err.println("usage: proxyd [-port portnum]");
System.exit(1);
}
private static void run(ServerSocket clientSocket) throws IOException {
DNSCacheThread cacheThread = new DNSCacheThread();
cacheThread.start();
while(true) {
Socket connectionSocket = clientSocket.accept();
RequestThread requestThread = new RequestThread(connectionSocket);
requestThread.start();
}
}
protected static void handleRequest(Socket connectionSocket) throws IOException {
DataInputStream clientInStream = new DataInputStream(connectionSocket.getInputStream());
ByteBuffer clientInBuffer = ByteBuffer.allocate(connectionSocket.getReceiveBufferSize());
//read input from the client
byte[] inBuffBytes = {};
while(isBlank(new String(inBuffBytes))) {
while(clientInStream.available() > 0) {
clientInBuffer.put(clientInStream.readByte());
}
inBuffBytes = clientInBuffer.array();
}
String clientInStr = new String(inBuffBytes);
String [] requests = clientInStr.split(REQUEST_END);
for(String request : requests) {
if(isBlank(request)) {
break;
}
//stop if this is a CONNECT request - no support for that!
if(request.contains("CONNECT")) {
System.err.println("This proxy does not accept CONNECT requests, sorry!");
break;
}
String[] lines = request.split(LINE_END);
//do some minimal parsing!
boolean connectFound = false;
String line;
for(int i = 0; i < lines.length; i++) {
line = lines[i];
//we'll want to close this connection once we've received the data
if(line.equalsIgnoreCase("connection: keep-alive") ||
line.equalsIgnoreCase("proxy-connection: keep-alive")) {
lines[i] = "Connection: close";
connectFound = true;
}
}
StringBuffer sb = new StringBuffer();
for(int i = 0; i < lines.length; i++) {
sb.append(lines[i] + LINE_END);
}
if(!connectFound) {
sb.append("Connection: close" + LINE_END);
}
request = sb.toString();
request += REQUEST_END;
byte[] requestBytes = request.getBytes();
//get the IP address
String hostname = getValue(request, "Host: ");
int serverPort = DEFAULT_SERVER_PORT;
//check if there's a port number specified
if(hostname.contains(":")) {
String[] hostParts = hostname.split(":");
hostname = hostParts[0];
serverPort = Integer.parseInt(hostParts[1]);
}
InetAddress ip = getInetAddress(hostname);
//connect a new socket to the appropriate server
Socket serverSocket = new Socket(ip, serverPort);
//start the thread for reading responses from this server
ResponseThread responseThread = new ResponseThread(serverSocket, connectionSocket);
responseThread.start();
DataOutputStream serverOutStream = new DataOutputStream(serverSocket.getOutputStream());
//write the request to the server
serverOutStream.write(requestBytes, 0, requestBytes.length);
serverOutStream.flush();
}
}
private static String getValue(String request, String header) {
//get the value of the given header field
int startIndex = request.indexOf(header) + header.length();
int endIndex = request.indexOf(LINE_END, startIndex);
return request.substring(startIndex, endIndex);
}
private static InetAddress getInetAddress(String hostname) throws UnknownHostException {
//check the map for the hostname first
if(ips.containsKey(hostname)) {
InetAddress addr = ips.get(hostname);
return addr;
}
//if the hostname isn't cached, find the associated IP (and cache it)
InetAddress addr = InetAddress.getAllByName(hostname)[0];
ips.put(hostname, addr);
dnsTimes.put(new Date().getTime(), hostname);
return addr;
}
protected static void handleDNS() {
//remove mappings that were created >= 30 seconds ago
long longestTime = new Date().getTime() - CACHE_TIME;
Set<Long> times = dnsTimes.keySet();
Iterator<Long> itr = times.iterator();
Set<Long> removeTimes = new HashSet<Long>();
//iterate over all times; keep track of the ones that are more than 30 seconds ago
while(itr.hasNext()) {
Long next = itr.next();
if(next <= longestTime) {
removeTimes.add(next);
}
}
Iterator<Long> removeItr = removeTimes.iterator();
//remove the ips associated with times that are more than 30 seconds ago
while(removeItr.hasNext()) {
Long next = removeItr.next();
//remove this instance from both removeTimes and ips
ips.remove(dnsTimes.remove(next));
}
}
protected static void handleResponse(Socket serverSocket, Socket clientSocket) throws IOException {
InputStream serverInStream = serverSocket.getInputStream();
ByteBuffer serverInBuffer = ByteBuffer.allocate(serverSocket.getReceiveBufferSize());
byte[] responseBytes = {};
//keep trying to read from the server until it sends something
while(isBlank(new String(responseBytes))) {
//read input from the server
byte nextByte = (byte)serverInStream.read();
while(nextByte != -1) {
serverInBuffer.put(nextByte);
nextByte = (byte)serverInStream.read();
}
responseBytes = serverInBuffer.array();
}
DataOutputStream clientOutStream = new DataOutputStream(clientSocket.getOutputStream());
//write the response to the client
clientOutStream.write(responseBytes, 0, responseBytes.length);
clientOutStream.flush();
clientOutStream.close();
}
private static boolean isBlank(String str) {
// if there's nothing but white space, consider the string blank
return str.trim().length() == 0;
}
}