1 package socks; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.io.DataInputStream ; 7 import java.net.InetAddress ; 8 import java.net.UnknownHostException ; 9 10 13 14 class Socks5Message extends ProxyMessage{ 15 16 public int addrType; 17 18 byte[] data; 19 20 24 public Socks5Message(int cmd){ 25 super(cmd,null,0); 26 data = new byte[3]; 27 data[0] = SOCKS_VERSION; data[1] = (byte)cmd; data[2] = 0; } 31 32 38 public Socks5Message(int cmd,InetAddress ip,int port){ 39 super(cmd,ip,port); 40 this.host = ip==null?"0.0.0.0":ip.getHostName(); 41 this.version = SOCKS_VERSION; 42 43 byte[] addr; 44 45 if(ip == null){ 46 addr = new byte[4]; 47 addr[0]=addr[1]=addr[2]=addr[3]=0; 48 }else 49 addr = ip.getAddress(); 50 51 addrType = addr.length == 4 ? SOCKS_ATYP_IPV4 52 : SOCKS_ATYP_IPV6; 53 54 data = new byte[6+addr.length]; 55 data[0] = (byte) SOCKS_VERSION; data[1] = (byte) command; data[2] = (byte) 0; data[3] = (byte) addrType; 60 System.arraycopy(addr,0,data,4,addr.length); 62 data[data.length-2] = (byte)(port>>8); 64 data[data.length-1] = (byte)(port); 65 } 66 67 68 74 public Socks5Message(int cmd,String hostName,int port){ 75 super(cmd,null,port); 76 this.host = hostName; 77 this.version = SOCKS_VERSION; 78 79 81 addrType = SOCKS_ATYP_DOMAINNAME; 82 byte addr[] = hostName.getBytes(); 83 84 data =new byte[7+addr.length]; 85 data[0] = (byte) SOCKS_VERSION; data[1] = (byte) command; data[2] = (byte) 0; data[3] = (byte) SOCKS_ATYP_DOMAINNAME; data[4] = (byte) addr.length; 91 System.arraycopy(addr,0,data,5,addr.length); 93 data[data.length-2] = (byte)(port >>8); 95 data[data.length-1] = (byte)(port); 96 } 97 98 106 public Socks5Message(InputStream in) throws SocksException, 107 IOException { 108 this(in,true); 109 } 110 111 121 public Socks5Message(InputStream in,boolean clientMode)throws SocksException, 122 IOException { 123 read(in,clientMode); 124 } 125 126 127 135 public void read(InputStream in) throws SocksException, 136 IOException { 137 read(in,true); 138 } 139 140 141 151 public void read(InputStream in,boolean clientMode) throws SocksException, 152 IOException { 153 data = null; 154 ip = null; 155 156 DataInputStream di = new DataInputStream (in); 157 158 version = di.readUnsignedByte(); 159 command = di.readUnsignedByte(); 160 if(clientMode && command != 0) 161 throw new SocksException(command); 162 163 int reserved = di.readUnsignedByte(); 164 addrType = di.readUnsignedByte(); 165 166 byte addr[]; 167 168 switch(addrType){ 169 case SOCKS_ATYP_IPV4: 170 addr = new byte[4]; 171 di.readFully(addr); 172 host = bytes2IPV4(addr,0); 173 break; 174 case SOCKS_ATYP_IPV6: 175 addr = new byte[SOCKS_IPV6_LENGTH]; di.readFully(addr); 177 host = bytes2IPV6(addr,0); 178 break; 179 case SOCKS_ATYP_DOMAINNAME: 180 addr = new byte[di.readUnsignedByte()]; di.readFully(addr); 183 host = new String (addr); 184 break; 185 default: 186 throw(new SocksException(Proxy.SOCKS_JUST_ERROR)); 187 } 188 189 port = di.readUnsignedShort(); 190 191 if(addrType != SOCKS_ATYP_DOMAINNAME && doResolveIP){ 192 try{ 193 ip = InetAddress.getByName(host); 194 }catch(UnknownHostException uh_ex){ 195 } 196 } 197 } 198 199 203 public void write(OutputStream out)throws SocksException, 204 IOException { 205 if(data == null){ 206 Socks5Message msg; 207 208 if(addrType == SOCKS_ATYP_DOMAINNAME) 209 msg = new Socks5Message(command,host,port); 210 else{ 211 if(ip == null){ 212 try{ 213 ip = InetAddress.getByName(host); 214 }catch(UnknownHostException uh_ex){ 215 throw new SocksException(Proxy.SOCKS_JUST_ERROR); 216 } 217 } 218 msg = new Socks5Message(command,ip,port); 219 } 220 data = msg.data; 221 } 222 out.write(data); 223 } 224 225 231 public InetAddress getInetAddress() throws UnknownHostException { 232 if(ip!=null) return ip; 233 234 return (ip=InetAddress.getByName(host)); 235 } 236 237 240 public String toString(){ 241 String s= 242 "Socks5Message:"+"\n"+ 243 "VN "+version+"\n"+ 244 "CMD "+command+"\n"+ 245 "ATYP "+addrType+"\n"+ 246 "ADDR "+host+"\n"+ 247 "PORT "+port+"\n"; 248 return s; 249 } 250 251 252 257 static public boolean resolveIP(){ return doResolveIP;} 258 259 266 static public boolean resolveIP(boolean doResolve){ 267 boolean old = doResolveIP; 268 doResolveIP = doResolve; 269 return old; 270 } 271 272 279 280 public static final int SOCKS_VERSION =5; 282 283 public static final int SOCKS_ATYP_IPV4 =0x1; public static final int SOCKS_ATYP_DOMAINNAME =0x3; public static final int SOCKS_ATYP_IPV6 =0x4; 286 287 public static final int SOCKS_IPV6_LENGTH =16; 288 289 static boolean doResolveIP = true; 290 291 } 292 | Popular Tags |