]> Joshua Wise's Git repositories - dumload.git/blame - src/com/jcraft/jsch/ProxySOCKS4.java
A few icon tweaks
[dumload.git] / src / com / jcraft / jsch / ProxySOCKS4.java
CommitLineData
0763e16d
JW
1/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2/*
3Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
18THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30/*
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
34 */
35
36package com.jcraft.jsch;
37
38import java.io.*;
39import java.net.*;
40
41public 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;
48 private String user;
49 private String passwd;
50
51 public ProxySOCKS4(String proxy_host){
52 int port=DEFAULTPORT;
53 String host=proxy_host;
54 if(proxy_host.indexOf(':')!=-1){
55 try{
56 host=proxy_host.substring(0, proxy_host.indexOf(':'));
57 port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
58 }
59 catch(Exception e){
60 }
61 }
62 this.proxy_host=host;
63 this.proxy_port=port;
64 }
65 public ProxySOCKS4(String proxy_host, int proxy_port){
66 this.proxy_host=proxy_host;
67 this.proxy_port=proxy_port;
68 }
69 public void setUserPasswd(String user, String passwd){
70 this.user=user;
71 this.passwd=passwd;
72 }
73 public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
74 try{
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();
80 }
81 else{
82 socket=socket_factory.createSocket(proxy_host, proxy_port);
83 in=socket_factory.getInputStream(socket);
84 out=socket_factory.getOutputStream(socket);
85 }
86 if(timeout>0){
87 socket.setSoTimeout(timeout);
88 }
89 socket.setTcpNoDelay(true);
90
91 byte[] buf=new byte[1024];
92 int index=0;
93
94/*
95 1) CONNECT
96
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.
101
102 +----+----+----+----+----+----+----+----+----+----+....+----+
103 | VN | CD | DSTPORT | DSTIP | USERID |NULL|
104 +----+----+----+----+----+----+----+----+----+----+....+----+
105 # of bytes: 1 1 2 4 variable 1
106
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
109 of all zero bits.
110*/
111
112 index=0;
113 buf[index++]=4;
114 buf[index++]=1;
115
116 buf[index++]=(byte)(port>>>8);
117 buf[index++]=(byte)(port&0xff);
118
119 try{
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];
124 }
125 }
126 catch(UnknownHostException uhe){
127 throw new JSchException("ProxySOCKS4: "+uhe.toString(), uhe);
128 }
129
130 if(user!=null){
131 System.arraycopy(Util.str2byte(user), 0, buf, index, user.length());
132 index+=user.length();
133 }
134 buf[index++]=0;
135 out.write(buf, 0, index);
136
137/*
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.
145
146 +----+----+----+----+----+----+----+----+
147 | VN | CD | DSTPORT | DSTIP |
148 +----+----+----+----+----+----+----+----+
149 # of bytes: 1 1 2 4
150
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:
153
154 90: request granted
155 91: request rejected or failed
156 92: request rejected becasue SOCKS server cannot connect to
157 identd on the client
158 93: request rejected because the client program and identd
159 report different user-ids
160
161 The remaining fields are ignored.
162*/
163
164 int len=8;
165 int s=0;
166 while(s<len){
167 int i=in.read(buf, s, len-s);
168 if(i<=0){
169 throw new JSchException("ProxySOCKS4: stream is closed");
170 }
171 s+=i;
172 }
173 if(buf[0]!=0){
174 throw new JSchException("ProxySOCKS4: server returns VN "+buf[0]);
175 }
176 if(buf[1]!=90){
177 try{ socket.close(); }
178 catch(Exception eee){
179 }
180 String message="ProxySOCKS4: server returns CD "+buf[1];
181 throw new JSchException(message);
182 }
183 }
184 catch(RuntimeException e){
185 throw e;
186 }
187 catch(Exception e){
188 try{ if(socket!=null)socket.close(); }
189 catch(Exception eee){
190 }
191 throw new JSchException("ProxySOCKS4: "+e.toString());
192 }
193 }
194 public InputStream getInputStream(){ return in; }
195 public OutputStream getOutputStream(){ return out; }
196 public Socket getSocket(){ return socket; }
197 public void close(){
198 try{
199 if(in!=null)in.close();
200 if(out!=null)out.close();
201 if(socket!=null)socket.close();
202 }
203 catch(Exception e){
204 }
205 in=null;
206 out=null;
207 socket=null;
208 }
209 public static int getDefaultPort(){
210 return DEFAULTPORT;
211 }
212}
This page took 0.042626 seconds and 5 git commands to generate.