[FEATURE} IP Forwarding

This commit is contained in:
Gravit 2019-05-09 20:00:10 +07:00
parent bb04ed849c
commit a66d7a7164
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 57 additions and 2 deletions

View file

@ -275,6 +275,7 @@ public class LauncherConf
public class NettyConfig { public class NettyConfig {
public boolean fileServerEnabled; public boolean fileServerEnabled;
public boolean sendExceptionEnabled; public boolean sendExceptionEnabled;
public boolean ipForwarding;
public String launcherURL; public String launcherURL;
public String downloadURL; public String downloadURL;
public String launcherEXEURL; public String launcherEXEURL;

View file

@ -40,13 +40,15 @@ public LauncherNettyServer() {
@Override @Override
public void initChannel(NioSocketChannel ch) { public void initChannel(NioSocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
NettyConnectContext context = new NettyConnectContext();
//p.addLast(new LoggingHandler(LogLevel.INFO)); //p.addLast(new LoggingHandler(LogLevel.INFO));
pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler()); pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true)); pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
if (LaunchServer.server.config.netty.fileServerEnabled) pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true)); if (LaunchServer.server.config.netty.fileServerEnabled) pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true));
pipeline.addLast(new WebSocketFrameHandler()); if (LaunchServer.server.config.netty.ipForwarding) pipeline.addLast(new NettyIpForwardHandler(context));
pipeline.addLast(new WebSocketFrameHandler(context));
} }
}); });
if(config.proxy != null && config.proxy.enabled) if(config.proxy != null && config.proxy.enabled)

View file

@ -0,0 +1,5 @@
package ru.gravit.launchserver.websocket;
public class NettyConnectContext {
public String ip = null;
}

View file

@ -0,0 +1,41 @@
package ru.gravit.launchserver.websocket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import ru.gravit.utils.helper.LogHelper;
public class NettyIpForwardHandler extends ChannelInboundHandlerAdapter {
private NettyConnectContext context;
public NettyIpForwardHandler(NettyConnectContext context) {
super();
this.context = context;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
if(context.ip != null) return;
if(msg instanceof DefaultHttpRequest)
{
DefaultHttpRequest http = (DefaultHttpRequest) msg;
HttpHeaders headers = http.headers();
String realIP = null;
if(headers.contains("X-Forwarded-For"))
{
realIP = headers.get("X-Forwarded-For");
}
if(headers.contains("X-Real-IP"))
{
realIP = headers.get("X-Real-IP");
}
if(realIP != null) {
LogHelper.dev("Real IP address %s", realIP);
context.ip = realIP;
}
}
}
}

View file

@ -100,7 +100,7 @@ public void run() {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }*/
LogHelper.info("Starting server socket thread"); LogHelper.info("Starting netty server socket thread");
//SSLEngine engine = sc.createSSLEngine(); //SSLEngine engine = sc.createSSLEngine();
//engine.setUseClientMode(false); //engine.setUseClientMode(false);
WebSocketFrameHandler.server = LaunchServer.server; WebSocketFrameHandler.server = LaunchServer.server;

View file

@ -17,6 +17,12 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocket
public static LaunchServer server; public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder(); public static GsonBuilder builder = new GsonBuilder();
public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server, builder); public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server, builder);
public NettyConnectContext context;
public WebSocketFrameHandler(NettyConnectContext context) {
this.context = context;
}
private Client client; private Client client;
static { static {