1 package socks; 2 3 import java.net.*; 4 import java.io.*; 5 6 10 public class SocksServerSocket extends ServerSocket{ 11 protected Proxy proxy; 13 protected String localHost; 14 protected InetAddress localIP; 15 protected int localPort; 16 17 boolean doing_direct = false; 18 InetAddress remoteAddr; 19 20 26 public SocksServerSocket(String host,int port) 27 throws SocksException,UnknownHostException,IOException{ 28 this(Proxy.defaultProxy,host,port); 29 } 30 37 public SocksServerSocket(Proxy p,String host,int port) 38 throws SocksException,UnknownHostException,IOException{ 39 40 41 super(0); 42 if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY); 43 proxy = p.copy(); 45 if(proxy.isDirect(host)){ 46 remoteAddr = InetAddress.getByName(host); 47 proxy = null; 48 doDirect(); 49 }else{ 50 processReply(proxy.bind(host,port)); 51 } 52 } 53 54 60 public SocksServerSocket(InetAddress ip, int port) throws SocksException, 61 IOException{ 62 this(Proxy.defaultProxy,ip,port); 63 } 64 65 72 public SocksServerSocket(Proxy p,InetAddress ip, int port) 73 throws SocksException,IOException{ 74 super(0); 75 76 if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY); 77 this.proxy = p.copy(); 78 79 if(proxy.isDirect(ip)){ 80 remoteAddr = ip; 81 doDirect(); 82 }else{ 83 processReply(proxy.bind(ip,port)); 84 } 85 } 86 87 88 91 public Socket accept() throws IOException{ 92 Socket s; 93 94 if(!doing_direct){ 95 if(proxy == null) return null; 96 97 ProxyMessage msg = proxy.accept(); 98 s = msg.ip == null? new SocksSocket(msg.host,msg.port,proxy) 99 : new SocksSocket(msg.ip,msg.port,proxy); 100 proxy.proxySocket.setSoTimeout(0); 102 }else{ 104 while(true){ 107 s = super.accept(); 108 if(s.getInetAddress().equals(remoteAddr)){ 109 break; 112 }else 113 s.close(); } 115 116 } 117 proxy = null; 118 return s; 120 } 121 122 127 public void close() throws IOException{ 128 super.close(); 129 if(proxy != null) proxy.endSession(); 130 proxy = null; 131 } 132 133 141 public String getHost(){ 142 return localHost; 143 } 144 145 150 public InetAddress getInetAddress(){ 151 if(localIP == null){ 152 try{ 153 localIP = InetAddress.getByName(localHost); 154 }catch(UnknownHostException e){ 155 return null; 156 } 157 } 158 return localIP; 159 } 160 161 165 public int getLocalPort(){ 166 return localPort; 167 } 168 169 176 public void setSoTimeout(int timeout) throws SocketException{ 177 super.setSoTimeout(timeout); 178 if(!doing_direct) proxy.proxySocket.setSoTimeout(timeout); 179 } 180 181 182 185 private void processReply(ProxyMessage reply)throws SocksException{ 186 localPort = reply.port; 187 191 if(reply.host.equals("0.0.0.0")){ 192 localIP = proxy.proxyIP; 193 localHost = localIP.getHostName(); 194 }else{ 195 localHost = reply.host; 196 localIP = reply.ip; 197 } 198 } 199 200 private void doDirect(){ 201 doing_direct = true; 202 localPort = super.getLocalPort(); 203 localIP = super.getInetAddress(); 204 localHost = localIP.getHostName(); 205 } 206 207 } 208
| Popular Tags
|