| 1 package socks; 2 import java.net.*; 3 import java.io.*; 4 import java.util.Hashtable ; 5 import java.util.Enumeration ; 6 7 12 13 public abstract class Proxy{ 14 15 protected InetRange directHosts = new InetRange(); 17 18 protected InetAddress proxyIP = null; 19 protected String proxyHost = null; 20 protected int proxyPort; 21 protected Socket proxySocket = null; 22 23 protected InputStream in; 24 protected OutputStream out; 25 26 protected int version; 27 28 protected Proxy chainProxy = null; 29 30 31 protected static Proxy defaultProxy = null; 33 34 Proxy(Proxy chainProxy, 37 String proxyHost,int proxyPort)throws UnknownHostException{ 38 this.chainProxy = chainProxy; 39 this.proxyHost = proxyHost; 40 41 if(chainProxy == null) 42 this.proxyIP = InetAddress.getByName(proxyHost); 43 44 this.proxyPort = proxyPort; 45 } 46 47 48 Proxy(String proxyHost,int proxyPort)throws UnknownHostException{ 49 this(null,proxyHost,proxyPort); 50 } 51 52 Proxy(Proxy chainProxy,InetAddress proxyIP,int proxyPort){ 53 this.chainProxy = chainProxy; 54 this.proxyIP = proxyIP; 55 this.proxyPort = proxyPort; 56 } 57 58 Proxy(InetAddress proxyIP,int proxyPort){ 59 this(null,proxyIP,proxyPort); 60 } 61 62 Proxy(Proxy p){ 63 this.proxyIP = p.proxyIP; 64 this.proxyPort = p.proxyPort; 65 this.version = p.version; 66 this.directHosts = p.directHosts; 67 } 68 69 72 76 public int getPort(){ 77 return proxyPort; 78 } 79 83 public InetAddress getInetAddress(){ 84 return proxyIP; 85 } 86 90 public void addDirect(InetAddress ip){ 91 directHosts.add(ip); 92 } 93 97 public boolean addDirect(String host){ 98 return directHosts.add(host); 99 } 100 104 public void addDirect(InetAddress from,InetAddress to){ 105 directHosts.add(from,to); 106 } 107 115 public void setDirect(InetRange ir){ 116 directHosts = ir; 117 } 118 119 124 public InetRange getDirect(){ 125 return directHosts; 126 } 127 132 public boolean isDirect(String host){ 133 return directHosts.contains(host); 134 } 135 140 public boolean isDirect(InetAddress host){ 141 return directHosts.contains(host); 142 } 143 147 public void setChainProxy(Proxy chainProxy){ 148 this.chainProxy = chainProxy; 149 } 150 151 156 public Proxy getChainProxy(){ 157 return chainProxy; 158 } 159 160 164 public String toString(){ 165 return (""+proxyIP.getHostName()+":"+proxyPort+"\tVersion "+version); 166 } 167 168 169 172 178 public static void setDefaultProxy(String hostName,int port,String user) 179 throws UnknownHostException{ 180 defaultProxy = new Socks4Proxy(hostName,port,user); 181 } 182 183 189 public static void setDefaultProxy(InetAddress ipAddress,int port, 190 String user){ 191 defaultProxy = new Socks4Proxy(ipAddress,port,user); 192 } 193 199 public static void setDefaultProxy(String hostName,int port) 200 throws UnknownHostException{ 201 defaultProxy = new Socks5Proxy(hostName,port); 202 } 203 209 public static void setDefaultProxy(InetAddress ipAddress,int port){ 210 defaultProxy = new Socks5Proxy(ipAddress,port); 211 } 212 216 public static void setDefaultProxy(Proxy p){ 217 defaultProxy = p; 218 } 219 220 224 public static Proxy getDefaultProxy(){ 225 return defaultProxy; 226 } 227 228 240 public static Proxy parseProxy(String proxy_entry){ 241 242 String proxy_host; 243 int proxy_port = 1080; 244 String proxy_user = null; 245 String proxy_password = null; 246 Proxy proxy; 247 248 java.util.StringTokenizer st = new java.util.StringTokenizer ( 249 proxy_entry,":"); 250 if(st.countTokens() < 1) return null; 251 252 proxy_host = st.nextToken(); 253 if(st.hasMoreTokens()) 254 try{ 255 proxy_port = Integer.parseInt(st.nextToken().trim()); 256 }catch(NumberFormatException nfe){} 257 258 if(st.hasMoreTokens()) 259 proxy_user = st.nextToken(); 260 261 if(st.hasMoreTokens()) 262 proxy_password = st.nextToken(); 263 264 try{ 265 if(proxy_user == null) 266 proxy = new Socks5Proxy(proxy_host,proxy_port); 267 else if(proxy_password == null) 268 proxy = new Socks4Proxy(proxy_host,proxy_port,proxy_user); 269 else{ 270 proxy = new Socks5Proxy(proxy_host,proxy_port); 271 UserPasswordAuthentication upa = new UserPasswordAuthentication( 272 proxy_user, proxy_password); 273 274 ((Socks5Proxy)proxy).setAuthenticationMethod(upa.METHOD_ID,upa); 275 } 276 }catch(UnknownHostException uhe){ 277 return null; 278 } 279 280 return proxy; 281 } 282 283 284 287 protected void startSession()throws SocksException{ 288 try{ 289 if(chainProxy == null) 290 proxySocket = new Socket(proxyIP,proxyPort); 291 else if(proxyIP != null) 292 proxySocket = new SocksSocket(chainProxy,proxyIP,proxyPort); 293 else 294 proxySocket = new SocksSocket(chainProxy,proxyHost,proxyPort); 295 296 in = proxySocket.getInputStream(); 297 out = proxySocket.getOutputStream(); 298 }catch(SocksException se){ 299 throw se; 300 }catch(IOException io_ex){ 301 throw new SocksException(SOCKS_PROXY_IO_ERROR,""+io_ex); 302 } 303 } 304 305 protected abstract Proxy copy(); 306 protected abstract ProxyMessage formMessage(int cmd,InetAddress ip,int port); 307 protected abstract ProxyMessage formMessage(int cmd,String host,int port) 308 throws UnknownHostException; 309 protected abstract ProxyMessage formMessage(InputStream in) 310 throws SocksException, 311 IOException; 312 313 314 protected ProxyMessage connect(InetAddress ip,int port) 315 throws SocksException{ 316 try{ 317 startSession(); 318 ProxyMessage request = formMessage(SOCKS_CMD_CONNECT, 319 ip,port); 320 return exchange(request); 321 }catch(SocksException se){ 322 endSession(); 323 throw se; 324 } 325 } 326 protected ProxyMessage connect(String host,int port) 327 throws UnknownHostException,SocksException{ 328 try{ 329 startSession(); 330 ProxyMessage request = formMessage(SOCKS_CMD_CONNECT, 331 host,port); 332 return exchange(request); 333 }catch(SocksException se){ 334 endSession(); 335 throw se; 336 } 337 } 338 339 protected ProxyMessage bind(InetAddress ip,int port) 340 throws SocksException{ 341 try{ 342 startSession(); 343 ProxyMessage request = formMessage(SOCKS_CMD_BIND, 344 ip,port); 345 return exchange(request); 346 }catch(SocksException se){ 347 endSession(); 348 throw se; 349 } 350 } 351 protected ProxyMessage bind(String host,int port) 352 throws UnknownHostException,SocksException{ 353 try{ 354 startSession(); 355 ProxyMessage request = formMessage(SOCKS_CMD_BIND, 356 host,port); 357 return exchange(request); 358 }catch(SocksException se){ 359 endSession(); 360 throw se; 361 } 362 } 363 364 protected ProxyMessage accept() 365 throws IOException,SocksException{ 366 ProxyMessage msg; 367 try{ 368 msg = formMessage(in); 369 }catch(InterruptedIOException iioe){ 370 throw iioe; 371 }catch(IOException io_ex){ 372 endSession(); 373 throw new SocksException(SOCKS_PROXY_IO_ERROR,"While Trying accept:" 374 +io_ex); 375 } 376 return msg; 377 } 378 379 protected ProxyMessage udpAssociate(InetAddress ip,int port) 380 throws SocksException{ 381 try{ 382 startSession(); 383 ProxyMessage request = formMessage(SOCKS_CMD_UDP_ASSOCIATE, 384 ip,port); 385 if(request != null) 386 return exchange(request); 387 }catch(SocksException se){ 388 endSession(); 389 throw se; 390 } 391 endSession(); 393 throw new SocksException(SOCKS_METHOD_NOTSUPPORTED, 394 "This version of proxy does not support UDP associate, use version 5"); 395 } 396 protected ProxyMessage udpAssociate(String host,int port) 397 throws UnknownHostException,SocksException{ 398 try{ 399 startSession(); 400 ProxyMessage request = formMessage(SOCKS_CMD_UDP_ASSOCIATE, 401 host,port); 402 if(request != null) return exchange(request); 403 }catch(SocksException se){ 404 endSession(); 405 throw se; 406 } 407 endSession(); 409 throw new SocksException(SOCKS_METHOD_NOTSUPPORTED, 410 "This version of proxy does not support UDP associate, use version 5"); 411 } 412 413 414 protected void endSession(){ 415 try{ 416 if(proxySocket!=null) proxySocket.close(); 417 proxySocket = null; 418 }catch(IOException io_ex){ 419 } 420 } 421 422 425 protected void sendMsg(ProxyMessage msg)throws SocksException, 426 IOException{ 427 msg.write(out); 428 } 429 430 433 protected ProxyMessage readMsg()throws SocksException, 434 IOException{ 435 return formMessage(in); 436 } 437 442 protected ProxyMessage exchange(ProxyMessage request) 443 throws SocksException{ 444 ProxyMessage reply; 445 try{ 446 request.write(out); 447 reply = formMessage(in); 448 }catch(SocksException s_ex){ 449 throw s_ex; 450 }catch(IOException ioe){ 451 throw(new SocksException(SOCKS_PROXY_IO_ERROR,""+ioe)); 452 } 453 return reply; 454 } 455 456 457 460 461 463 public static final int SOCKS_SUCCESS =0; 464 public static final int SOCKS_FAILURE =1; 465 public static final int SOCKS_BADCONNECT =2; 466 public static final int SOCKS_BADNETWORK =3; 467 public static final int SOCKS_HOST_UNREACHABLE =4; 468 public static final int SOCKS_CONNECTION_REFUSED =5; 469 public static final int SOCKS_TTL_EXPIRE =6; 470 public static final int SOCKS_CMD_NOT_SUPPORTED =7; 471 public static final int SOCKS_ADDR_NOT_SUPPORTED =8; 472 473 public static final int SOCKS_NO_PROXY =1<<16; 474 public static final int SOCKS_PROXY_NO_CONNECT =2<<16; 475 public static final int SOCKS_PROXY_IO_ERROR =3<<16; 476 public static final int SOCKS_AUTH_NOT_SUPPORTED =4<<16; 477 public static final int SOCKS_AUTH_FAILURE =5<<16; 478 public static final int SOCKS_JUST_ERROR =6<<16; 479 480 public static final int SOCKS_DIRECT_FAILED =7<<16; 481 public static final int SOCKS_METHOD_NOTSUPPORTED =8<<16; 482 483 484 static final int SOCKS_CMD_CONNECT =0x1; 485 static final int SOCKS_CMD_BIND =0x2; 486 static final int SOCKS_CMD_UDP_ASSOCIATE =0x3; 487 488 } 489 | Popular Tags |