1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
31 This file depends on following documents,
32 - SOCKS: A protocol for TCP proxy across firewalls, Ying-Da Lee
33 http://www.socks.nec.com/protocol/socks4.protocol
36 package com.jcraft.jsch;
41 public class ProxySOCKS4 implements Proxy{
42 private static int DEFAULTPORT=1080;
43 private String proxy_host;
44 private int proxy_port;
45 private InputStream in;
46 private OutputStream out;
47 private Socket socket;
49 private String passwd;
51 public ProxySOCKS4(String proxy_host){
53 String host=proxy_host;
54 if(proxy_host.indexOf(':')!=-1){
56 host=proxy_host.substring(0, proxy_host.indexOf(':'));
57 port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
65 public ProxySOCKS4(String proxy_host, int proxy_port){
66 this.proxy_host=proxy_host;
67 this.proxy_port=proxy_port;
69 public void setUserPasswd(String user, String passwd){
73 public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
75 if(socket_factory==null){
76 socket=Util.createSocket(proxy_host, proxy_port, timeout);
77 //socket=new Socket(proxy_host, proxy_port);
78 in=socket.getInputStream();
79 out=socket.getOutputStream();
82 socket=socket_factory.createSocket(proxy_host, proxy_port);
83 in=socket_factory.getInputStream(socket);
84 out=socket_factory.getOutputStream(socket);
87 socket.setSoTimeout(timeout);
89 socket.setTcpNoDelay(true);
91 byte[] buf=new byte[1024];
97 The client connects to the SOCKS server and sends a CONNECT request when
98 it wants to establish a connection to an application server. The client
99 includes in the request packet the IP address and the port number of the
100 destination host, and userid, in the following format.
102 +----+----+----+----+----+----+----+----+----+----+....+----+
103 | VN | CD | DSTPORT | DSTIP | USERID |NULL|
104 +----+----+----+----+----+----+----+----+----+----+....+----+
105 # of bytes: 1 1 2 4 variable 1
107 VN is the SOCKS protocol version number and should be 4. CD is the
108 SOCKS command code and should be 1 for CONNECT request. NULL is a byte
116 buf[index++]=(byte)(port>>>8);
117 buf[index++]=(byte)(port&0xff);
120 InetAddress addr=InetAddress.getByName(host);
121 byte[] byteAddress = addr.getAddress();
122 for (int i = 0; i < byteAddress.length; i++) {
123 buf[index++]=byteAddress[i];
126 catch(UnknownHostException uhe){
127 throw new JSchException("ProxySOCKS4: "+uhe.toString(), uhe);
131 System.arraycopy(Util.str2byte(user), 0, buf, index, user.length());
132 index+=user.length();
135 out.write(buf, 0, index);
138 The SOCKS server checks to see whether such a request should be granted
139 based on any combination of source IP address, destination IP address,
140 destination port number, the userid, and information it may obtain by
141 consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
142 server makes a connection to the specified port of the destination host.
143 A reply packet is sent to the client when this connection is established,
144 or when the request is rejected or the operation fails.
146 +----+----+----+----+----+----+----+----+
147 | VN | CD | DSTPORT | DSTIP |
148 +----+----+----+----+----+----+----+----+
151 VN is the version of the reply code and should be 0. CD is the result
152 code with one of the following values:
155 91: request rejected or failed
156 92: request rejected becasue SOCKS server cannot connect to
158 93: request rejected because the client program and identd
159 report different user-ids
161 The remaining fields are ignored.
167 int i=in.read(buf, s, len-s);
169 throw new JSchException("ProxySOCKS4: stream is closed");
174 throw new JSchException("ProxySOCKS4: server returns VN "+buf[0]);
177 try{ socket.close(); }
178 catch(Exception eee){
180 String message="ProxySOCKS4: server returns CD "+buf[1];
181 throw new JSchException(message);
184 catch(RuntimeException e){
188 try{ if(socket!=null)socket.close(); }
189 catch(Exception eee){
191 throw new JSchException("ProxySOCKS4: "+e.toString());
194 public InputStream getInputStream(){ return in; }
195 public OutputStream getOutputStream(){ return out; }
196 public Socket getSocket(){ return socket; }
199 if(in!=null)in.close();
200 if(out!=null)out.close();
201 if(socket!=null)socket.close();
209 public static int getDefaultPort(){