1 2 29 30 35 36 package com.jcraft.jsch; 37 38 import java.io.*; 39 import java.net.*; 40 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; 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 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 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(user.getBytes(), 0, buf, index, user.length()); 132 index+=user.length(); 133 } 134 buf[index++]=0; 135 out.write(buf, 0, index); 136 137 163 164 int len=6; 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 } 213 | Popular Tags |