注:本文包含大量可执行的JAVA代码以解释代理相关的原理
4jpublic class SimpleTransProxy {
public static void main(String[] args) throws IOException { int port = 8006; ServerSocketChannel localServer = ServerSocketChannel.open(); localServer.bind(new InetSocketAddress(port)); Reactor reactor = new Reactor(); // REACTOR线程 GlobalThreadPool.REACTOR_EXECUTOR.submit(reactor::run);
// WORKER单线程调试 while (localServer.isOpen()) { // 此处阻塞等待连接 SocketChannel remoteClient = localServer.accept();
// 工作线程 GlobalThreadPool.WORK_EXECUTOR.submit(new Runnable() { public void run() { // 代理到远程 SocketChannel remoteServer = new ProxyHandler().proxy(remoteClient);
// 透明传输 reactor.pipe(remoteClient, remoteServer) .pipe(remoteServer, remoteClient); } }); } }}
class ProxyHandler { private String method; private String host; private int port; private SocketChannel remoteServer; private SocketChannel remoteClient;
/** * 原始信息 */ private List<ByteBuffer> buffers = new ArrayList<>(); private StringBuilder stringBuilder = new StringBuilder();
/** * 连接到远程 * @param remoteClient * @return * @throws IOException */ public SocketChannel proxy(SocketChannel remoteClient) throws IOException { this.remoteClient = remoteClient; connect(); return this.remoteServer; }
public void connect() throws IOException { // 解析METHOD, HOST和PORT beforeConnected();
// 链接REMOTE SERVER createRemoteServer();
// CONNECT请求回应,其他请求WRITE THROUGH afterConnected(); }
protected void beforeConnected() throws IOException { // 读取HEADER readAllHeader();
// 解析HOST和PORT parseRemoteHostAndPort(); }
/** * 创建远程连接 * @throws IOException */ protected void createRemoteServer() throws IOException { remoteServer = SocketChannel.open(new InetSocketAddress(host, port)); }
/** * 连接建立后预处理 * @throws IOException */ protected void afterConnected() throws IOException { // 当CONNECT请求时,默认写入200到CLIENT if ("CONNECT".equalsIgnoreCase(method)) { // CONNECT默认为443端口,根据HOST再解析 remoteClient.write(ByteBuffer.wrap("HTTP/1.0 200 Connection Established\r\nProxy-agent: nginx\r\n\r\n".getBytes())); } else { writeThrouth(); } }
protected void writeThrouth() { buffers.forEach(byteBuffer -> { try { remoteServer.write(byteBuffer); } catch (IOException e) { e.printStackTrace(); } }); }
/** * 读取请求内容 * @throws IOException */ protected void readAllHeader() throws IOException { while (true) { ByteBuffer clientBuffer = newByteBuffer(); int read = remoteClient.read(clientBuffer); clientBuffer.flip(); appendClientBuffer(clientBuffer); if (read < clientBuffer.capacity()) { break; } } }
/** * 解析出HOST和PROT * @throws IOException */ protected void parseRemoteHostAndPort() throws IOException { // 读取第一批,获取到METHOD method = parseRequestMethod(stringBuilder.toString());
// 默认为80端口,根据HOST再解析 port = 80; if ("CONNECT".equalsIgnoreCase(method)) { port = 443; }
this.host = parseHost(stringBuilder.toString());
URI remoteServerURI = URI.create(host); host = remoteServerURI.getHost();
if (remoteServerURI.getPort() > 0) { port = remoteServerURI.getPort(); } }
protected void appendClientBuffer(ByteBuffer clientBuffer) { buffers.add(clientBuffer); stringBuilder.append(new String(clientBuffer.array(), clientBuffer.position(), clientBuffer.limit())); }
protected static ByteBuffer newByteBuffer() { // buffer必须大于7,保证能读到method return ByteBuffer.allocate(128); }
private static String parseRequestMethod(String rawContent) { // create uri return rawContent.split("\r\n")[0].split(" ")[0]; }
private static String parseHost(String rawContent) { String[] headers = rawContent.split("\r\n"); String host = "host:"; for (String header : headers) { if (header.length() > host.length()) { String key = header.substring(0, host.length()); String value = header.substring(host.length()).trim(); if (host.equalsIgnoreCase(key)) { if (!value.startsWith("http://") && !value.startsWith("https://")) { value = "http://" + value; } return value; } } } return ""; }
}
4jclass Reactor {
private Selector selector;
private volatile boolean finish = false;
public Reactor() { selector = Selector.open(); }
public Reactor pipe(SocketChannel from, SocketChannel to) { from.configureBlocking(false); from.register(selector, SelectionKey.OP_READ, new SocketPipe(this, from, to)); return this; }
public void run() { try { while (!finish) { if (selector.selectNow() > 0) { Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey selectionKey = it.next(); if (selectionKey.isValid() && selectionKey.isReadable()) { ((SocketPipe) selectionKey.attachment()).pipe(); } it.remove(); } } } } finally { close(); } }
public synchronized void close() { if (finish) { return; } finish = true; if (!selector.isOpen()) { return; } for (SelectionKey key : selector.keys()) { closeChannel(key.channel()); key.cancel(); } if (selector != null) { selector.close(); } }
public void cancel(SelectableChannel channel) { SelectionKey key = channel.keyFor(selector); if (Objects.isNull(key)) { return; } key.cancel(); }
public void closeChannel(Channel channel) { SocketChannel socketChannel = (SocketChannel)channel; if (socketChannel.isConnected() && socketChannel.isOpen()) { socketChannel.shutdownOutput(); socketChannel.shutdownInput(); } socketChannel.close(); }}
class SocketPipe {
private Reactor reactor;
private SocketChannel from;
private SocketChannel to;
public void pipe() { // 取消监听 clearInterestOps();
GlobalThreadPool.PIPE_EXECUTOR.submit(new Runnable() { public void run() { int totalBytesRead = 0; ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while (valid(from) && valid(to)) { byteBuffer.clear(); int bytesRead = from.read(byteBuffer); totalBytesRead = totalBytesRead + bytesRead; byteBuffer.flip(); to.write(byteBuffer); if (bytesRead < byteBuffer.capacity()) { break; } } if (totalBytesRead < 0) { reactor.closeChannel(from); reactor.cancel(from); } else { // 重置监听 resetInterestOps(); } } }); }
protected void clearInterestOps() { from.keyFor(reactor.getSelector()).interestOps(0); to.keyFor(reactor.getSelector()).interestOps(0); }
protected void resetInterestOps() { from.keyFor(reactor.getSelector()).interestOps(SelectionKey.OP_READ); to.keyFor(reactor.getSelector()).interestOps(SelectionKey.OP_READ); }
private boolean valid(SocketChannel channel) { return channel.isConnected() && channel.isRegistered() && channel.isOpen(); }}
public class SslSocketChannel {
/** * 握手加解密需要的四个存储 */ protected ByteBuffer myAppData; // 明文 protected ByteBuffer myNetData; // 密文 protected ByteBuffer peerAppData; // 明文 protected ByteBuffer peerNetData; // 密文
/** * 握手加解密过程中用到的异步执行器 */ protected ExecutorService executor = Executors.newSingleThreadExecutor();
/** * 原NIO 的 CHANNEL */ protected SocketChannel socketChannel;
/** * SSL 引擎 */ protected SSLEngine engine;
public SslSocketChannel(SSLContext context, SocketChannel socketChannel, boolean clientMode) throws Exception { // 原始的NIO SOCKET this.socketChannel = socketChannel;
// 初始化BUFFER SSLSession dummySession = context.createSSLEngine().getSession(); myAppData = ByteBuffer.allocate(dummySession.getApplicationBufferSize()); myNetData = ByteBuffer.allocate(dummySession.getPacketBufferSize()); peerAppData = ByteBuffer.allocate(dummySession.getApplicationBufferSize()); peerNetData = ByteBuffer.allocate(dummySession.getPacketBufferSize()); dummySession.invalidate();
engine = context.createSSLEngine(); engine.setUseClientMode(clientMode); engine.beginHandshake(); }
/** * 参考 https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html * 实现的 SSL 的握手协议 * @return * @throws IOException */ protected boolean doHandshake() throws IOException { SSLEngineResult result; HandshakeStatus handshakeStatus;
int appBufferSize = engine.getSession().getApplicationBufferSize(); ByteBuffer myAppData = ByteBuffer.allocate(appBufferSize); ByteBuffer peerAppData = ByteBuffer.allocate(appBufferSize); myNetData.clear(); peerNetData.clear();
handshakeStatus = engine.getHandshakeStatus(); while (handshakeStatus != HandshakeStatus.FINISHED && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) { switch (handshakeStatus) { case NEED_UNWRAP: if (socketChannel.read(peerNetData) < 0) { if (engine.isInboundDone() && engine.isOutboundDone()) { return false; } try { engine.closeInbound(); } catch (SSLException e) { log.debug("收到END OF STREAM,关闭连接.", e); } engine.closeOutbound(); handshakeStatus = engine.getHandshakeStatus(); break; } peerNetData.flip(); try { result = engine.unwrap(peerNetData, peerAppData); peerNetData.compact(); handshakeStatus = result.getHandshakeStatus(); } catch (SSLException sslException) { engine.closeOutbound(); handshakeStatus = engine.getHandshakeStatus(); break; } switch (result.getStatus()) { case OK: break; case BUFFER_OVERFLOW: peerAppData = enlargeApplicationBuffer(engine, peerAppData); break; case BUFFER_UNDERFLOW: peerNetData = handleBufferUnderflow(engine, peerNetData); break; case CLOSED: if (engine.isOutboundDone()) { return false; } else { engine.closeOutbound(); handshakeStatus = engine.getHandshakeStatus(); break; } default: throw new IllegalStateException("无效的握手状态: " + result.getStatus()); } break; case NEED_WRAP: myNetData.clear(); try { result = engine.wrap(myAppData, myNetData); handshakeStatus = result.getHandshakeStatus(); } catch (SSLException sslException) { engine.closeOutbound(); handshakeStatus = engine.getHandshakeStatus(); break; } switch (result.getStatus()) { case OK : myNetData.flip(); while (myNetData.hasRemaining()) { socketChannel.write(myNetData); } break; case BUFFER_OVERFLOW: myNetData = enlargePacketBuffer(engine, myNetData); break; case BUFFER_UNDERFLOW: throw new SSLException("加密后消息内容为空,报错"); case CLOSED: try { myNetData.flip(); while (myNetData.hasRemaining()) { socketChannel.write(myNetData); } peerNetData.clear(); } catch (Exception e) { handshakeStatus = engine.getHandshakeStatus(); } break; default: throw new IllegalStateException("无效的握手状态: " + result.getStatus()); } break; case NEED_TASK: Runnable task; while ((task = engine.getDelegatedTask()) != null) { executor.execute(task); } handshakeStatus = engine.getHandshakeStatus(); break; case FINISHED: break; case NOT_HANDSHAKING: break; default: throw new IllegalStateException("无效的握手状态: " + handshakeStatus); } }
return true; }
/** * 参考 https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html * 实现的 SSL 的传输读取协议 * @param consumer * @throws IOException */ public void read(Consumer<ByteBuffer> consumer) throws IOException { // BUFFER初始化 peerNetData.clear(); int bytesRead = socketChannel.read(peerNetData); if (bytesRead > 0) { peerNetData.flip(); while (peerNetData.hasRemaining()) { peerAppData.clear(); SSLEngineResult result = engine.unwrap(peerNetData, peerAppData); switch (result.getStatus()) { case OK: log.debug("收到远程的返回结果消息为:" + new String(peerAppData.array(), 0, peerAppData.position())); consumer.accept(peerAppData); peerAppData.flip(); break; case BUFFER_OVERFLOW: peerAppData = enlargeApplicationBuffer(engine, peerAppData); break; case BUFFER_UNDERFLOW: peerNetData = handleBufferUnderflow(engine, peerNetData); break; case CLOSED: log.debug("收到远程连接关闭消息."); closeConnection(); return; default: throw new IllegalStateException("无效的握手状态: " + result.getStatus()); } } } else if (bytesRead < 0) { log.debug("收到END OF STREAM,关闭连接."); handleEndOfStream(); } }
public void write(String message) throws IOException { write(ByteBuffer.wrap(message.getBytes())); }
/** * 参考 https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html * 实现的 SSL 的传输写入协议 * @param message * @throws IOException */ public void write(ByteBuffer message) throws IOException { myAppData.clear(); myAppData.put(message); myAppData.flip(); while (myAppData.hasRemaining()) { myNetData.clear(); SSLEngineResult result = engine.wrap(myAppData, myNetData); switch (result.getStatus()) { case OK: myNetData.flip(); while (myNetData.hasRemaining()) { socketChannel.write(myNetData); } log.debug("写入远程的消息为: {}", message); break; case BUFFER_OVERFLOW: myNetData = enlargePacketBuffer(engine, myNetData); break; case BUFFER_UNDERFLOW: throw new SSLException("加密后消息内容为空."); case CLOSED: closeConnection(); return; default: throw new IllegalStateException("无效的握手状态: " + result.getStatus()); } } }
/** * 关闭连接 * @throws IOException */ public void closeConnection() throws IOException { engine.closeOutbound(); doHandshake(); socketChannel.close(); executor.shutdown(); }
/** * END OF STREAM(-1)默认是关闭连接 * @throws IOException */ protected void handleEndOfStream() throws IOException { try { engine.closeInbound(); } catch (Exception e) { log.error("END OF STREAM 关闭失败.", e); } closeConnection(); }
}
@Slf4jpublic class NioSslServer {
public static void main(String[] args) throws Exception { NioSslServer sslServer = new NioSslServer("127.0.0.1", 8006); sslServer.start(); // 使用 curl -vv -k 'https://localhost:8006' 连接 }
private SSLContext context;
private Selector selector;
public NioSslServer(String hostAddress, int port) throws Exception { // 初始化SSL Context context = serverSSLContext();
// 注册监听器 selector = SelectorProvider.provider().openSelector(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(new InetSocketAddress(hostAddress, port)); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); }
public void start() throws Exception {
log.debug("等待连接中.");
while (true) { selector.select(); Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator(); while (selectedKeys.hasNext()) { SelectionKey key = selectedKeys.next(); selectedKeys.remove(); if (!key.isValid()) { continue; } if (key.isAcceptable()) { accept(key); } else if (key.isReadable()) { ((SslSocketChannel)key.attachment()).read(buf->{}); // 直接回应一个OK ((SslSocketChannel)key.attachment()).write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nOK\r\n\r\n"); ((SslSocketChannel)key.attachment()).closeConnection(); } } } }
private void accept(SelectionKey key) throws Exception { log.debug("接收新的请求.");
SocketChannel socketChannel = ((ServerSocketChannel)key.channel()).accept(); socketChannel.configureBlocking(false);
SslSocketChannel sslSocketChannel = new SslSocketChannel(context, socketChannel, false); if (sslSocketChannel.doHandshake()) { socketChannel.register(selector, SelectionKey.OP_READ, sslSocketChannel); } else { socketChannel.close(); log.debug("握手失败,关闭连接."); } }}
4jpublic class NioSslClient {
public static void main(String[] args) throws Exception { NioSslClient sslClient = new NioSslClient("httpbin.org", 443); sslClient.connect(); // 请求 'https://httpbin.org/get' }
private String remoteAddress;
private int port;
private SSLEngine engine;
private SocketChannel socketChannel;
private SSLContext context;
/** * 需要远程的HOST和PORT * @param remoteAddress * @param port * @throws Exception */ public NioSslClient(String remoteAddress, int port) throws Exception { this.remoteAddress = remoteAddress; this.port = port;
context = clientSSLContext(); engine = context.createSSLEngine(remoteAddress, port); engine.setUseClientMode(true); }
public boolean connect() throws Exception { socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress(remoteAddress, port)); while (!socketChannel.finishConnect()) { // 通过REACTOR,不会出现等待情况 //log.debug("连接中.."); }
SslSocketChannel sslSocketChannel = new SslSocketChannel(context, socketChannel, true); sslSocketChannel.doHandshake();
// 握手完成后,开启SELECTOR Selector selector = SelectorProvider.provider().openSelector(); socketChannel.register(selector, SelectionKey.OP_READ, sslSocketChannel);
// 写入请求 sslSocketChannel.write("GET /get HTTP/1.1\r\n" + "Host: httpbin.org:443\r\n" + "User-Agent: curl/7.62.0\r\n" + "Accept: */*\r\n" + "\r\n");
// 读取结果 while (true) { selector.select(); Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator(); while (selectedKeys.hasNext()) { SelectionKey key = selectedKeys.next(); selectedKeys.remove(); if (key.isValid() && key.isReadable()) { ((SslSocketChannel)key.attachment()).read(buf->{ log.info("{}", new String(buf.array(), 0, buf.position())); }); ((SslSocketChannel)key.attachment()).closeConnection(); return true; } } } }}
public void start() { HookedExecutors.newSingleThreadExecutor().submit(() ->{ log.info("开始启动代理服务器,监听端口:{}", auditProxyConfig.getProxyServerPort()); EventLoopGroup bossGroup = new NioEventLoopGroup(auditProxyConfig.getBossThreadCount()); EventLoopGroup workerGroup = new NioEventLoopGroup(auditProxyConfig.getWorkThreadCount()); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.DEBUG)) .childHandler(new ServerChannelInitializer(auditProxyConfig)) .bind(auditProxyConfig.getProxyServerPort()).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { log.error("代理服务器被中断.", e); Thread.currentThread().interrupt(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }); }
protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline() .addLast(new HttpRequestDecoder()) .addLast(new HttpObjectAggregator(auditProxyConfig.getMaxRequestSize())) .addLast(new ServerChannelHandler(auditProxyConfig)); }
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest)msg;
try { if (isConnectRequest(request)) { // CONNECT 请求,存储待处理 saveConnectRequest(ctx, request);
// 禁止读取 ctx.channel().config().setAutoRead(false);
// 发送回应 connectionEstablished(ctx, ctx.newPromise().addListener(future -> { if (future.isSuccess()) { // 升级 if (isSslRequest(request) && !isUpgraded(ctx)) { upgrade(ctx); }
// 开放消息读取 ctx.channel().config().setAutoRead(true); ctx.read(); } }));
} else { // 其他请求,判定是否已升级 if (!isUpgraded(ctx)) {
// 升级引擎 upgrade(ctx); }
// 连接远程 connectRemote(ctx, request); } } finally { ctx.fireChannelRead(msg); } }
/** * 初始化远程连接 * @param ctx * @param httpRequest */ protected void connectRemote(ChannelHandlerContext ctx, FullHttpRequest httpRequest) { Bootstrap b = new Bootstrap(); b.group(ctx.channel().eventLoop()) // use the same EventLoop .channel(ctx.channel().getClass()) .handler(new ClientChannelInitializer(auditProxyConfig, ctx, safeCopy(httpRequest)));
// 动态连接代理 FullHttpRequest originRequest = ctx.channel().attr(CONNECT_REQUEST).get(); if (originRequest == null) { originRequest = httpRequest; } ChannelFuture cf = b.connect(new InetSocketAddress(calculateHost(originRequest), calculatePort(originRequest))); Channel cch = cf.channel(); ctx.channel().attr(CLIENT_CHANNEL).set(cch); }
protected void initChannel(SocketChannel ch) throws Exception { SocketAddress socketAddress = calculateProxy(); if (!Objects.isNull(socketAddress)) { ch.pipeline().addLast(new HttpProxyHandler(calculateProxy(), auditProxyConfig.getUserName(), auditProxyConfig .getPassword())); } if (isSslRequest()) { String host = host(); int port = port(); if (StringUtils.isNoneBlank(host) && port > 0) { ch.pipeline().addLast(new SslHandler(sslEngine(host, port))); } } ch.pipeline().addLast(new ClientChannelHandler(clientContext, httpRequest)); }
注:本文使用Netty的零拷贝;存储请求以解析处理;但并未实现对RESPONSE的处理;也就是RESPONSE是直接通过网关,此方面避免了常见的代理实现,内存泄漏OOM相关问题;
点击阅读原文查看详情!