/
Main.java
58 lines (53 loc) · 2.39 KB
/
Main.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
package com.zanclus.simple.proxy.example;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.impl.LoggerFactory;
import io.vertx.core.streams.Pump;
import io.vertx.ext.apex.Router;
/**
*
* @author <a href="https://github.com/InfoSec812">Deven Phillips</a>
*/
public class Main extends AbstractVerticle {
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
LOG.debug("Deploying Main verticle.");
Vertx.vertx().deployVerticle("com.zanclus.simple.proxy.example.Main");
}
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
HttpClient client = vertx.createHttpClient();
router.route().handler(ctx -> {
LOG.error("Sending proxied request.");
HttpClientRequest clientReq = client.request(ctx.request().method(), 80, "www.reddit.com", ctx.request().uri());
clientReq.headers().addAll(ctx.request().headers().remove("Host"));
clientReq.putHeader("Host", "www.reddit.com");
if (ctx.request().method().equals(HttpMethod.POST) || ctx.request().method().equals(HttpMethod.PUT)) {
if (ctx.request().headers().get("Content-Length")==null) {
clientReq.setChunked(true);
}
}
clientReq.handler(pResponse -> {
LOG.error("Getting response from target");
ctx.response().headers().addAll(pResponse.headers());
if (pResponse.headers().get("Content-Length") == null) {
ctx.response().setChunked(true);
}
ctx.response().setStatusCode(pResponse.statusCode());
ctx.response().setStatusMessage(pResponse.statusMessage());
Pump targetToProxy = Pump.pump(pResponse, ctx.response());
targetToProxy.start();
pResponse.endHandler(v -> ctx.response().end());
});
Pump proxyToTarget = Pump.pump(ctx.request(), clientReq);
proxyToTarget.start();
ctx.request().endHandler(v -> clientReq.end());
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}