1 package socks; 2 import java.net.*; 3 import java.io.*; 4 import java.util.Hashtable ; 5 import java.util.Enumeration ; 6 7 10 11 public class Socks5Proxy extends Proxy implements Cloneable { 12 13 private Hashtable authMethods = new Hashtable (); 15 private int selectedMethod; 16 17 boolean resolveAddrLocally = true; 18 UDPEncapsulation udp_encapsulation=null; 19 20 21 24 31 public Socks5Proxy(Proxy p,String proxyHost,int proxyPort) 32 throws UnknownHostException{ 33 super(p,proxyHost,proxyPort); 34 version = 5; 35 setAuthenticationMethod(0,new AuthenticationNone()); 36 } 37 38 44 public Socks5Proxy(String proxyHost,int proxyPort) 45 throws UnknownHostException{ 46 this(null,proxyHost,proxyPort); 47 } 48 49 50 56 public Socks5Proxy(Proxy p,InetAddress proxyIP,int proxyPort){ 57 super(p,proxyIP,proxyPort); 58 version = 5; 59 setAuthenticationMethod(0,new AuthenticationNone()); 60 } 61 62 67 public Socks5Proxy(InetAddress proxyIP,int proxyPort){ 68 this(null,proxyIP,proxyPort); 69 } 70 71 74 75 84 public boolean resolveAddrLocally(boolean doResolve){ 85 boolean old = resolveAddrLocally; 86 resolveAddrLocally = doResolve; 87 return old; 88 } 89 94 public boolean resolveAddrLocally(){ 95 return resolveAddrLocally; 96 } 97 98 104 public boolean setAuthenticationMethod(int methodId, 105 Authentication method){ 106 if(methodId<0 || methodId > 255) 107 return false; 108 if(method == null){ 109 return (authMethods.remove(new Integer (methodId)) != null); 111 }else{ authMethods.put(new Integer (methodId),method); 113 } 114 return true; 115 } 116 117 122 public Authentication getAuthenticationMethod(int methodId){ 123 Object method = authMethods.get(new Integer (methodId)); 124 if(method == null) return null; 125 return (Authentication)method; 126 } 127 128 131 public Object clone(){ 132 Socks5Proxy newProxy = new Socks5Proxy(proxyIP,proxyPort); 133 newProxy.authMethods = (Hashtable ) this.authMethods.clone(); 134 newProxy.directHosts = (InetRange)directHosts.clone(); 135 newProxy.resolveAddrLocally = resolveAddrLocally; 136 newProxy.chainProxy = chainProxy; 137 return newProxy; 138 } 139 140 143 144 147 protected Proxy copy(){ 148 Socks5Proxy copy = new Socks5Proxy(proxyIP,proxyPort); 149 copy.authMethods = this.authMethods; copy.directHosts = this.directHosts; 151 copy.chainProxy = this.chainProxy; 152 copy.resolveAddrLocally = this.resolveAddrLocally; 153 return copy; 154 } 155 159 protected void startSession()throws SocksException{ 160 super.startSession(); 161 Authentication auth; 162 Socket ps = proxySocket; 164 try{ 165 166 byte nMethods = (byte) authMethods.size(); 168 byte[] buf = new byte[2+nMethods]; buf[0] = (byte) version; 170 buf[1] = nMethods; int i=2; 172 173 Enumeration ids = authMethods.keys(); 174 while(ids.hasMoreElements()) 175 buf[i++] = (byte)((Integer )ids.nextElement()).intValue(); 176 177 out.write(buf); 178 out.flush(); 179 180 int versionNumber = in.read(); 181 selectedMethod = in.read(); 182 183 if(versionNumber < 0 || selectedMethod < 0){ 184 endSession(); 186 throw(new SocksException(SOCKS_PROXY_IO_ERROR, 187 "Connection to proxy lost.")); 188 } 189 if(versionNumber < version){ 190 } 192 if(selectedMethod == 0xFF){ ps.close(); 194 throw ( new SocksException(SOCKS_AUTH_NOT_SUPPORTED)); 195 } 196 197 auth = getAuthenticationMethod(selectedMethod); 198 if(auth == null){ 199 throw(new SocksException(SOCKS_JUST_ERROR, 202 "Speciefied Authentication not found!")); 203 } 204 Object [] in_out = auth.doSocksAuthentication(selectedMethod,ps); 205 if(in_out == null){ 206 throw(new SocksException(SOCKS_AUTH_FAILURE)); 208 } 209 215 in = (InputStream) in_out[0]; 216 out = (OutputStream) in_out[1]; 217 if(in_out.length > 2) 218 udp_encapsulation = (UDPEncapsulation) in_out[2]; 219 220 }catch(SocksException s_ex){ 221 throw s_ex; 222 }catch(UnknownHostException uh_ex){ 223 throw(new SocksException(SOCKS_PROXY_NO_CONNECT)); 224 }catch(SocketException so_ex){ 225 throw(new SocksException(SOCKS_PROXY_NO_CONNECT)); 226 }catch(IOException io_ex){ 227 throw(new SocksException(SOCKS_PROXY_IO_ERROR,""+io_ex)); 229 } 230 } 231 232 protected ProxyMessage formMessage(int cmd,InetAddress ip,int port){ 233 return new Socks5Message(cmd,ip,port); 234 } 235 protected ProxyMessage formMessage(int cmd,String host,int port) 236 throws UnknownHostException{ 237 if(resolveAddrLocally) 238 return formMessage(cmd,InetAddress.getByName(host),port); 239 else 240 return new Socks5Message(cmd,host,port); 241 } 242 protected ProxyMessage formMessage(InputStream in) 243 throws SocksException, 244 IOException{ 245 return new Socks5Message(in); 246 } 247 248 } 249 | Popular Tags |