KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JHTC


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

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 JavaDoc[] arg){
37     try{
38
39       if(arg.length==0){
40     System.err.println("Enter hostname[:port]");
41     System.exit(1);
42       }
43
44       String JavaDoc 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 JavaDoc _port=System.getProperty("F");
53       if(_port!=null){
54     port=Integer.parseInt(_port);
55       }
56
57       String JavaDoc 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 InBoundURL());
80
// jhtc.setOutBound(new OutBoundURL());
81
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 JavaDoc runnable=new Runnable JavaDoc(){
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 JavaDoc e){
102           }
103           try{
104         sin.close();
105         jin.close();
106         jhtc.close();
107           }
108           catch(Exception JavaDoc e){
109           }
110         }
111       };
112     (new Thread JavaDoc(runnable)).start();
113
114     byte[] tmp=new byte[1024];
115     try{
116       while(true){
117         int i=sin.read(tmp);
118 //System.out.println("i="+i+" "+jout);
119
if(i>0){
120           jout.write(tmp, 0, i);
121           continue;
122         }
123         break;
124       }
125     }
126     catch(Exception JavaDoc e){
127       //System.out.println(e);
128
}
129     try{
130       socket.close();
131       jin.close();
132       jhtc.close();
133     }
134     catch(Exception JavaDoc 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