/
DungProxy.java
125 lines (114 loc) · 3.73 KB
/
DungProxy.java
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
import java.io.*;
import java.net.*;
import java.util.regex.*;
public class DungProxy {
public static void main(String[] args) throws IOException {
try {
String host = "Dung Proxy Server";
int remoteport = 80;
int localport = 8585;
if (args.length > 0) {
try {
localport = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Port must be an integer");
System.exit(1);
}
}
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" + remoteport + " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
} catch (Exception e) {
System.err.println(e);
}
}
public static String errReturn(String err, String url) {
return "dung_beetle.proxyError('"+err+"', '"+url+"')";
}
public static String cssReturn(String css, String url) {
return "dung_beetle.catchCSS('"+escapeStr(css)+"', '"+url+"')";
}
public static String jsReturn(String js, String url) {
return "dung_beetle.catchJS('"+escapeStr(js)+"', '"+url+"')";
}
/**
* Escape a string to be put into a single quoted javascript function
*/
public static String escapeStr(String s) {
Pattern p = Pattern.compile("[\r|\n]+");
String[] spt = p.split(s.replace("\\", "\\\\").replace("'", "\\'"));
StringBuilder sb = new StringBuilder();
for(int x=0; x<spt.length; x++) {
sb.append(spt[x] + "~$~");
}
return sb.toString();
}
/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
*/
public static void runServer(String host, int remoteport, int localport) throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
while (true) {
Socket client = null;
try {
// Wait for a connection on the local port
client = ss.accept();
final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();
PrintWriter out = new PrintWriter(streamToClient);
StringBuilder sb = new StringBuilder();
String line = null, url = "", userAgent = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(streamFromClient , "UTF-8"));
while ((line = reader.readLine()) != null && (url.length() == 0 && userAgent.length() == 0)) {
if(line.indexOf("GET") > -1 && url.length() == 0) {
String[] spl = line.split(" ");
url = removeFirst(spl[1], '/');
}
if(line.indexOf("User-Agent") > -1 && userAgent.length() == 0) {
userAgent = line.replace("User-Agent ", "");
}
}
} finally {
}
System.out.println(url);
URL req = new URL(url);
URLConnection urlConn = req.openConnection();
urlConn.setRequestProperty("User-Agent", userAgent);
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuilder osb = new StringBuilder();
while((line = in.readLine()) != null) {
osb.append(line);
}
streamToClient.write(cssReturn(osb.toString(), url).getBytes());
in.close();
streamToClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
}
}
}
}
public static String removeFirst(String s, char c) {
String r = "";
boolean found = false;
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) != c || found) {
r += s.charAt(i);
found = true;
}
}
return r;
}
}