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