1 2 29 30 35 36 package com.jcraft.jsch; 37 38 import java.io.*; 39 import java.net.*; 40 41 public class ProxySOCKS5 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 ProxySOCKS5(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 ProxySOCKS5(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 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 114 115 buf[index++]=5; 116 117 buf[index++]=2; 118 buf[index++]=0; buf[index++]=2; 121 out.write(buf, 0, index); 122 123 133 fill(in, buf, 2); 135 136 boolean check=false; 137 switch((buf[1])&0xff){ 138 case 0: check=true; 140 break; 141 case 2: if(user==null || passwd==null)break; 143 144 163 index=0; 164 buf[index++]=1; 165 buf[index++]=(byte)(user.length()); 166 System.arraycopy(user.getBytes(), 0, buf, index, user.length()); 167 index+=user.length(); 168 buf[index++]=(byte)(passwd.length()); 169 System.arraycopy(passwd.getBytes(), 0, buf, index, passwd.length()); 170 index+=passwd.length(); 171 172 out.write(buf, 0, index); 173 174 188 fill(in, buf, 2); 190 if(buf[1]==0) 191 check=true; 192 break; 193 default: 194 } 195 196 if(!check){ 197 try{ socket.close(); } 198 catch(Exception eee){ 199 } 200 throw new JSchException("fail in SOCKS5 proxy"); 201 } 202 203 228 229 index=0; 230 buf[index++]=5; 231 buf[index++]=1; buf[index++]=0; 233 234 byte[] hostb=host.getBytes(); 235 int len=hostb.length; 236 buf[index++]=3; buf[index++]=(byte)(len); 238 System.arraycopy(hostb, 0, buf, index, len); 239 index+=len; 240 buf[index++]=(byte)(port>>>8); 241 buf[index++]=(byte)(port&0xff); 242 243 out.write(buf, 0, index); 244 245 279 280 fill(in, buf, 4); 282 283 if(buf[1]!=0){ 284 try{ socket.close(); } 285 catch(Exception eee){ 286 } 287 throw new JSchException("ProxySOCKS5: server returns "+buf[1]); 288 } 289 290 switch(buf[3]&0xff){ 291 case 1: 292 fill(in, buf, 6); 294 break; 295 case 3: 296 fill(in, buf, 1); 298 fill(in, buf, (buf[0]&0xff)+2); 300 break; 301 case 4: 302 fill(in, buf, 18); 304 break; 305 default: 306 } 307 } 308 catch(RuntimeException e){ 309 throw e; 310 } 311 catch(Exception e){ 312 try{ if(socket!=null)socket.close(); } 313 catch(Exception eee){ 314 } 315 String message="ProxySOCKS5: "+e.toString(); 316 if(e instanceof Throwable ) 317 throw new JSchException(message, (Throwable )e); 318 throw new JSchException(message); 319 } 320 } 321 public InputStream getInputStream(){ return in; } 322 public OutputStream getOutputStream(){ return out; } 323 public Socket getSocket(){ return socket; } 324 public void close(){ 325 try{ 326 if(in!=null)in.close(); 327 if(out!=null)out.close(); 328 if(socket!=null)socket.close(); 329 } 330 catch(Exception e){ 331 } 332 in=null; 333 out=null; 334 socket=null; 335 } 336 public static int getDefaultPort(){ 337 return DEFAULTPORT; 338 } 339 private void fill(InputStream in, byte[] buf, int len) throws JSchException, IOException{ 340 int s=0; 341 while(s<len){ 342 int i=in.read(buf, s, len-s); 343 if(i<=0){ 344 throw new JSchException("ProxySOCKS5: stream is closed"); 345 } 346 s+=i; 347 } 348 } 349 } 350 | Popular Tags |