-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeProxy.java
More file actions
58 lines (46 loc) · 1.94 KB
/
GeProxy.java
File metadata and controls
58 lines (46 loc) · 1.94 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
package http_proxy_sorta;
import com.sun.net.httpserver.*;
import java.net.InetSocketAddress;
import java.io.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class GeProxy {
public static void main(String[] args) throws Exception {
System.out.println("Listening on port 8000");
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new MyHandler()); //don't touch, it somehow works
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String temp = t.getRequestURI().getPath();
String urli="";
for(int i=1; i<temp.length(); i++)
urli += temp.charAt(i);
//now urli contains the url we want to visit through this not-so-proxy
String response="";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(urli);
try{
HttpResponse hresponse = client.execute(request);
HttpEntity entity = hresponse.getEntity();
response += EntityUtils.toString(entity);
}catch (IOException e){
e.printStackTrace();
}
//response now contains the html
//modify body
response = response.replace("DEADNAME","CHOSENNAME"); //works
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}