1 2 29 30 import com.jcraft.jhttptunnel.*; 31 32 import java.net.*; 33 import java.io.*; 34 35 public class JHTC{ 36 public static void main(String [] arg){ 37 try{ 38 39 if(arg.length==0){ 40 System.err.println("Enter hostname[:port]"); 41 System.exit(1); 42 } 43 44 String host=arg[0]; 45 int hport=8888; 46 if(host.indexOf(':')!=-1){ 47 hport=Integer.parseInt(host.substring(host.lastIndexOf(':') + 1)); 48 host=host.substring(0, host.lastIndexOf(':')); 49 } 50 51 int port=2323; 52 String _port=System.getProperty("F"); 53 if(_port!=null){ 54 port=Integer.parseInt(_port); 55 } 56 57 String proxy_host=System.getProperty("P"); 58 int proxy_port=8080; 59 if(proxy_host!=null && proxy_host.indexOf(':')!=-1){ 60 proxy_port=Integer.parseInt(proxy_host.substring(proxy_host.lastIndexOf(':') + 1)); 61 proxy_host=proxy_host.substring(0, proxy_host.lastIndexOf(':')); 62 } 63 64 ServerSocket ss=new ServerSocket(port); 65 while(true){ 66 Socket socket=ss.accept(); 67 socket.setTcpNoDelay(true); 68 69 System.out.println("accept: "+socket); 70 71 final InputStream sin=socket.getInputStream(); 72 final OutputStream sout=socket.getOutputStream(); 73 74 final JHttpTunnelClient jhtc=new JHttpTunnelClient(host, hport); 75 if(proxy_host!=null){ 76 jhtc.setProxy(proxy_host, proxy_port); 77 } 78 79 jhtc.setInBound(new InBoundSocket()); 82 jhtc.setOutBound(new OutBoundSocket()); 83 84 jhtc.connect(); 85 final InputStream jin=jhtc.getInputStream(); 86 final OutputStream jout=jhtc.getOutputStream(); 87 88 Runnable runnable=new Runnable (){ 89 public void run(){ 90 byte[] tmp=new byte[1024]; 91 try{ 92 while(true){ 93 int i=jin.read(tmp); 94 if(i>0){ 95 sout.write(tmp, 0, i); 96 continue; 97 } 98 break; 99 } 100 } 101 catch(Exception e){ 102 } 103 try{ 104 sin.close(); 105 jin.close(); 106 jhtc.close(); 107 } 108 catch(Exception e){ 109 } 110 } 111 }; 112 (new Thread (runnable)).start(); 113 114 byte[] tmp=new byte[1024]; 115 try{ 116 while(true){ 117 int i=sin.read(tmp); 118 if(i>0){ 120 jout.write(tmp, 0, i); 121 continue; 122 } 123 break; 124 } 125 } 126 catch(Exception e){ 127 } 129 try{ 130 socket.close(); 131 jin.close(); 132 jhtc.close(); 133 } 134 catch(Exception e){ 135 } 136 } 137 } 138 catch(JHttpTunnelException e){ 139 System.err.println(e); 140 } 141 catch(IOException e){ 142 System.err.println(e); 143 } 144 } 145 } 146 | Popular Tags |