1 package socks; 2 3 import java.net.*; 4 import java.io.*; 5 6 42 43 public class SocksSocket extends Socket{ 44 protected Proxy proxy; 46 protected String localHost, remoteHost; 47 protected InetAddress localIP, remoteIP; 48 protected int localPort,remotePort; 49 50 private Socket directSock = null; 51 52 61 public SocksSocket(String host,int port) 62 throws SocksException,UnknownHostException{ 63 this(Proxy.defaultProxy,host,port); 64 } 65 95 public SocksSocket(Proxy p,String host,int port) 96 throws SocksException,UnknownHostException{ 97 98 99 if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY); 100 proxy = p.copy(); 102 remoteHost = host; 103 remotePort = port; 104 if(proxy.isDirect(host)){ 105 remoteIP = InetAddress.getByName(host); 106 doDirect(); 107 } 108 else 109 processReply(proxy.connect(host,port)); 110 } 111 112 113 121 public SocksSocket(InetAddress ip, int port) throws SocksException{ 122 this(Proxy.defaultProxy,ip,port); 123 } 124 125 132 public SocksSocket(Proxy p,InetAddress ip, int port) throws SocksException{ 133 if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY); 134 this.proxy = p.copy(); 135 this.remoteIP = ip; 136 this.remotePort = port; 137 this.remoteHost = ip.getHostName(); 138 if(proxy.isDirect(remoteIP)) 139 doDirect(); 140 else 141 processReply(proxy.connect(ip,port)); 142 } 143 144 145 149 protected SocksSocket(String host,int port,Proxy proxy){ 150 this.remotePort = port; 151 this.proxy = proxy; 152 this.localIP = proxy.proxySocket.getLocalAddress(); 153 this.localPort = proxy.proxySocket.getLocalPort(); 154 this.remoteHost = host; 155 } 156 protected SocksSocket(InetAddress ip,int port,Proxy proxy){ 157 remoteIP = ip; 158 remotePort = port; 159 this.proxy = proxy; 160 this.localIP = proxy.proxySocket.getLocalAddress(); 161 this.localPort = proxy.proxySocket.getLocalPort(); 162 remoteHost = remoteIP.getHostName(); 163 } 164 165 168 public void close() throws IOException{ 169 if(proxy!= null)proxy.endSession(); 170 proxy = null; 171 } 172 175 public InputStream getInputStream(){ 176 return proxy.in; 177 } 178 181 public OutputStream getOutputStream(){ 182 return proxy.out; 183 } 184 187 public int getPort(){ 188 return remotePort; 189 } 190 195 public String getHost(){ 196 return remoteHost; 197 } 198 206 public InetAddress getInetAddress(){ 207 if(remoteIP == null){ 208 try{ 209 remoteIP = InetAddress.getByName(remoteHost); 210 }catch(UnknownHostException e){ 211 return null; 212 } 213 } 214 return remoteIP; 215 } 216 217 222 public int getLocalPort(){ 223 return localPort; 224 } 225 226 233 public InetAddress getLocalAddress(){ 234 if(localIP == null){ 235 try{ 236 localIP = InetAddress.getByName(localHost); 237 }catch(UnknownHostException e){ 238 return null; 239 } 240 } 241 return localIP; 242 } 243 249 public String getLocalHost(){ 250 return localHost; 251 } 252 253 256 public void setSoLinger(boolean on,int val) throws SocketException{ 257 proxy.proxySocket.setSoLinger(on,val); 258 } 259 262 public int getSoLinger(int timeout) throws SocketException{ 263 return proxy.proxySocket.getSoLinger(); 264 } 265 268 public void setSoTimeout(int timeout) throws SocketException{ 269 proxy.proxySocket.setSoTimeout(timeout); 270 } 271 274 public int getSoTimeout(int timeout) throws SocketException{ 275 return proxy.proxySocket.getSoTimeout(); 276 } 277 280 public void setTcpNoDelay(boolean on) throws SocketException{ 281 proxy.proxySocket.setTcpNoDelay(on); 282 } 283 286 public boolean getTcpNoDelay() throws SocketException{ 287 return proxy.proxySocket.getTcpNoDelay(); 288 } 289 290 293 public String toString(){ 294 if(directSock!=null) return "Direct connection:"+directSock; 295 return ("Proxy:"+proxy+";"+"addr:"+remoteHost+",port:"+remotePort 296 +",localport:"+localPort); 297 298 } 299 300 303 private void processReply(ProxyMessage reply)throws SocksException{ 304 localPort = reply.port; 305 309 if(reply.host.equals("0.0.0.0")){ 310 localIP = proxy.proxyIP; 311 localHost = localIP.getHostName(); 312 }else{ 313 localHost = reply.host; 314 localIP = reply.ip; 315 } 316 } 317 private void doDirect()throws SocksException{ 318 try{ 319 directSock = new Socket(remoteIP,remotePort); 321 proxy.out = directSock.getOutputStream(); 322 proxy.in = directSock.getInputStream(); 323 proxy.proxySocket = directSock; 324 localIP = directSock.getLocalAddress(); 325 localPort = directSock.getLocalPort(); 326 }catch(IOException io_ex){ 327 throw new SocksException(Proxy.SOCKS_DIRECT_FAILED, 328 "Direct connect failed:"+io_ex); 329 } 330 } 331 332 } 333 | Popular Tags |