
org.tinystruct.system.HttpProxyServer Maven / Gradle / Ivy
package org.tinystruct.system;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.tinystruct.ApplicationException;
import org.tinystruct.handler.HttpProxyHandler;
import org.tinystruct.system.cli.CommandOption;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class HttpProxyServer extends ProxyServer implements Bootstrap {
private int port = 81;
private String remoteHost = "localhost";
private int remotePort = 80;
private final Logger logger = Logger.getLogger(HttpProxyServer.class.getName());
public HttpProxyServer() {
}
public void init() {
this.setAction("start", "start");
List options = new ArrayList();
options.add(new CommandOption("server-port", "", "Server port"));
options.add(new CommandOption("remote-server-host", "", "Remote Server host"));
options.add(new CommandOption("remote-server-port", "", "Remote Server port"));
this.commandLines.get("start").setOptions(options);
this.setTemplateRequired(false);
}
@Override
public String version() {
return null;
}
@Override
public void start() throws ApplicationException {
if (this.context != null) {
if (this.context.getAttribute("--server-port") != null) {
this.port = Integer.parseInt(this.context.getAttribute("--server-port").toString());
}
if (this.context.getAttribute("--remote-server-port") != null) {
this.remotePort = Integer.parseInt(this.context.getAttribute("--remote-server-port").toString());
}
if (this.context.getAttribute("--remote-server-host") != null) {
this.remoteHost = this.context.getAttribute("--remote-server-host").toString();
}
}
try {
// Configure SSL.
final SslContext sslCtx;
if (this.remotePort == 443) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
ServerBootstrap bootstrap = new ServerBootstrap().group(bossgroup, workgroup)
.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class
: NioServerSocketChannel.class)
.handler(new LoggingHandler())
.childHandler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) {
if (sslCtx != null) {
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast(new LoggingHandler());
ch.pipeline().addLast(new HttpRequestDecoder());
ch.pipeline().addLast(new HttpProxyHandler(remoteHost, remotePort));
}
})
.childOption(ChannelOption.AUTO_READ, false);
// Bind and start to accept incoming connections.
ChannelFuture future = bootstrap.bind(port).sync();
logger.info("Proxy server(" + port + ") started.");
// Wait until the server socket is closed.
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.stop();
}
}
@Override
public void stop() {
bossgroup.shutdownGracefully();
workgroup.shutdownGracefully();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy