|
|
|
@ -20,7 +20,6 @@ import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap; |
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
import io.netty.channel.ChannelFutureListener; |
|
|
|
|
import io.netty.channel.ChannelHandler; |
|
|
|
|
import io.netty.channel.ChannelHandlerContext; |
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
@ -29,9 +28,12 @@ import io.netty.channel.socket.SocketChannel; |
|
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel; |
|
|
|
|
import io.netty.handler.ssl.SslHandler; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 客户端代理容器 |
|
|
|
|
*/ |
|
|
|
|
public class ProxyClientContainer implements Container, ChannelStatusListener { |
|
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ProxyClientContainer.class); |
|
|
|
|
private static Logger LOGGER = LoggerFactory.getLogger(ProxyClientContainer.class); |
|
|
|
|
|
|
|
|
|
private static final int MAX_FRAME_LENGTH = 1024 * 1024; |
|
|
|
|
|
|
|
|
@ -94,15 +96,25 @@ public class ProxyClientContainer implements Container, ChannelStatusListener { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void start() { |
|
|
|
|
|
|
|
|
|
//连接代理服务器
|
|
|
|
|
connectProxyServer(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建SSL处理 |
|
|
|
|
* @param sslContext |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private ChannelHandler createSslHandler(SSLContext sslContext) { |
|
|
|
|
SSLEngine sslEngine = sslContext.createSSLEngine(); |
|
|
|
|
sslEngine.setUseClientMode(true); |
|
|
|
|
return new SslHandler(sslEngine); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 连接代理服务器 |
|
|
|
|
*/ |
|
|
|
|
private void connectProxyServer() { |
|
|
|
|
bootstrap.connect(config.getStringValue("server.host"), config.getIntValue("server.port")).addListener((ChannelFuture future)->{ |
|
|
|
|
if (future.isSuccess()){ |
|
|
|
@ -113,9 +125,9 @@ public class ProxyClientContainer implements Container, ChannelStatusListener { |
|
|
|
|
proxyMessage.setUri(config.getStringValue("client.key")); |
|
|
|
|
future.channel().writeAndFlush(proxyMessage); |
|
|
|
|
sleepTimeMill = 1000; |
|
|
|
|
logger.info("connect proxy server success, {}", future.channel()); |
|
|
|
|
LOGGER.info("connect proxy server success, {}", future.channel()); |
|
|
|
|
}else { |
|
|
|
|
logger.warn("connect proxy server failed", future.cause()); |
|
|
|
|
LOGGER.warn("connect proxy server failed", future.cause()); |
|
|
|
|
|
|
|
|
|
// 连接失败,发起重连
|
|
|
|
|
reconnectWait(); |
|
|
|
@ -131,10 +143,15 @@ public class ProxyClientContainer implements Container, ChannelStatusListener { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) { |
|
|
|
|
//重新连接等待
|
|
|
|
|
reconnectWait(); |
|
|
|
|
//连接代理服务器
|
|
|
|
|
connectProxyServer(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 重新连接等待 |
|
|
|
|
*/ |
|
|
|
|
private void reconnectWait() { |
|
|
|
|
try { |
|
|
|
|
if (sleepTimeMill > 60000) { |
|
|
|
@ -150,7 +167,7 @@ public class ProxyClientContainer implements Container, ChannelStatusListener { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
|
|
//通过容器助手启动容器
|
|
|
|
|
ContainerHelper.start(Arrays.asList(new Container[] { new ProxyClientContainer() })); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|