1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.proxy;
17
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.Executors;
21
22 import org.jboss.netty.bootstrap.ServerBootstrap;
23 import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
24 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
25 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
26
27 public class HexDumpProxy {
28
29 private final int localPort;
30 private final String remoteHost;
31 private final int remotePort;
32
33 public HexDumpProxy(int localPort, String remoteHost, int remotePort) {
34 this.localPort = localPort;
35 this.remoteHost = remoteHost;
36 this.remotePort = remotePort;
37 }
38
39 public void run() {
40 System.err.println(
41 "Proxying *:" + localPort + " to " +
42 remoteHost + ':' + remotePort + " ...");
43
44
45 Executor executor = Executors.newCachedThreadPool();
46 ServerBootstrap sb = new ServerBootstrap(
47 new NioServerSocketChannelFactory(executor, executor));
48
49
50 ClientSocketChannelFactory cf =
51 new NioClientSocketChannelFactory(executor, executor);
52
53 sb.setPipelineFactory(
54 new HexDumpProxyPipelineFactory(cf, remoteHost, remotePort));
55
56
57 sb.bind(new InetSocketAddress(localPort));
58 }
59
60 public static void main(String[] args) throws Exception {
61
62 if (args.length != 3) {
63 System.err.println(
64 "Usage: " + HexDumpProxy.class.getSimpleName() +
65 " <local port> <remote host> <remote port>");
66 return;
67 }
68
69
70 int localPort = Integer.parseInt(args[0]);
71 String remoteHost = args[1];
72 int remotePort = Integer.parseInt(args[2]);
73
74 new HexDumpProxy(localPort, remoteHost, remotePort).run();
75 }
76 }