aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/sourceforge/jsocks/Socks5Message.java
blob: ea2a3214b66f205ca69caeb3f9eacdae4b12b5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package net.sourceforge.jsocks;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 SOCKS5 request/response message.
*/

public class Socks5Message extends ProxyMessage{
   /** Address type of given message*/
   public int addrType;

   byte[] data;

   /**
    Server error response.
    @param cmd Error code.
   */
   public Socks5Message(int cmd){
      super(cmd,null,0);
      data = new byte[3];
      data[0] = SOCKS_VERSION; //Version.
      data[1] = (byte)cmd;     //Reply code for some kind of failure.
      data[2] = 0;             //Reserved byte.
   }

   /**
    Construct client request or server response.
    @param cmd - Request/Response code.
    @param ip  - IP field.
    @paarm port - port field.
   */
   public Socks5Message(int cmd,InetAddress ip,int port){
      super(cmd,ip,port);
      this.host = ip==null?"0.0.0.0":ip.getHostName();
      this.version = SOCKS_VERSION;

      byte[] addr;

      if(ip == null){
         addr = new byte[4];
         addr[0]=addr[1]=addr[2]=addr[3]=0;
      }else
         addr = ip.getAddress();

      addrType = addr.length == 4 ? SOCKS_ATYP_IPV4
                                  : SOCKS_ATYP_IPV6;
 
      data = new byte[6+addr.length];
      data[0] = (byte) SOCKS_VERSION;		//Version
      data[1] = (byte) command;			//Command
      data[2] = (byte) 0;			//Reserved byte
      data[3] = (byte) addrType;		//Address type
 
      //Put Address
      System.arraycopy(addr,0,data,4,addr.length);
      //Put port
      data[data.length-2] = (byte)(port>>8);
      data[data.length-1] = (byte)(port);
   }


   /**
    Construct client request or server response.
    @param cmd - Request/Response code.
    @param hostName  - IP field as hostName, uses ADDR_TYPE of HOSTNAME.
    @paarm port - port field.
   */
   public Socks5Message(int cmd,String hostName,int port){
      super(cmd,null,port);
      this.host = hostName;
      this.version = SOCKS_VERSION;

      //System.out.println("Doing ATYP_DOMAINNAME");

      addrType = SOCKS_ATYP_DOMAINNAME;
      byte addr[] = hostName.getBytes();
 
      data =new byte[7+addr.length];
      data[0] = (byte) SOCKS_VERSION;		//Version
      data[1] = (byte) command;			//Command
      data[2] = (byte) 0;			//Reserved byte
      data[3] = (byte) SOCKS_ATYP_DOMAINNAME;	//Address type
      data[4] = (byte) addr.length;		//Length of the address

      //Put Address
      System.arraycopy(addr,0,data,5,addr.length);
      //Put port
      data[data.length-2] = (byte)(port >>8);
      data[data.length-1] = (byte)(port);
   }

   /**
     Initialises Message from the stream. Reads server response from
     given stream.
     @param in Input stream to read response from.
     @throws SocksException If server response code is not SOCKS_SUCCESS(0), or
     if any error with protocol occurs.
     @throws IOException If any error happens with I/O.
   */
   public Socks5Message(InputStream in) throws SocksException,
                                              IOException{
      this(in,true);
   }

   /**
     Initialises Message from the stream. Reads server response or client 
     request from given stream.
     
     @param in Input stream to read response from.
     @param clinetMode If true read server response, else read client request.
     @throws SocksException If server response code is not SOCKS_SUCCESS(0) and
     reading in client mode, or if any error with protocol occurs.
     @throws IOException If any error happens with I/O.
   */
   public Socks5Message(InputStream in,boolean clientMode)throws SocksException,
                                              IOException{
      read(in,clientMode);
   }


   /**
     Initialises Message from the stream. Reads server response from
     given stream.
     @param in Input stream to read response from.
     @throws SocksException If server response code is not SOCKS_SUCCESS(0), or
     if any error with protocol occurs.
     @throws IOException If any error happens with I/O.
   */
   public void read(InputStream in) throws SocksException,
                                           IOException{
       read(in,true);
   }


   /**
     Initialises Message from the stream. Reads server response or client 
     request from given stream.
     
     @param in Input stream to read response from.
     @param clinetMode If true read server response, else read client request.
     @throws SocksException If server response code is not SOCKS_SUCCESS(0) and
     reading in client mode, or if any error with protocol occurs.
     @throws IOException If any error happens with I/O.
   */
   public void read(InputStream in,boolean clientMode) throws SocksException,
                                           IOException{
      data = null;
      ip = null;

      DataInputStream di = new DataInputStream(in);

      version = di.readUnsignedByte();
      command = di.readUnsignedByte();
      if(clientMode && command != 0)
        throw new SocksException(command);

      @SuppressWarnings("unused")
      int reserved = di.readUnsignedByte();
      addrType = di.readUnsignedByte();

      byte addr[];

      switch(addrType){
         case SOCKS_ATYP_IPV4:
            addr = new byte[4];
            di.readFully(addr);
            host = bytes2IPV4(addr,0);
         break;
         case SOCKS_ATYP_IPV6:
           addr = new byte[SOCKS_IPV6_LENGTH];//I believe it is 16 bytes,huge!
           di.readFully(addr);
           host = bytes2IPV6(addr,0);
         break;
         case SOCKS_ATYP_DOMAINNAME:
           //System.out.println("Reading ATYP_DOMAINNAME");
           addr = new byte[di.readUnsignedByte()];//Next byte shows the length
           di.readFully(addr);
           host = new String(addr);
         break;
         default:
            throw(new SocksException(Proxy.SOCKS_JUST_ERROR));
      }

      port = di.readUnsignedShort();

      if(addrType != SOCKS_ATYP_DOMAINNAME && doResolveIP){
         try{
            ip = InetAddress.getByName(host);
         }catch(UnknownHostException uh_ex){
         }
      }
   }

   /**
    Writes the message to the stream.
    @param out Output stream to which message should be written.
   */
   public void write(OutputStream out)throws SocksException,
                                             IOException{
     if(data == null){
       Socks5Message msg;

       if(addrType == SOCKS_ATYP_DOMAINNAME)
          msg = new Socks5Message(command,host,port);
       else{
          if(ip == null){
             try{
               ip = InetAddress.getByName(host);
             }catch(UnknownHostException uh_ex){
               throw new SocksException(Proxy.SOCKS_JUST_ERROR);
             }
          }
          msg = new Socks5Message(command,ip,port);
       }
       data = msg.data;
     }
     out.write(data);
   }

   /**
    Returns IP field of the message as IP, if the message was created
    with ATYP of HOSTNAME, it will attempt to resolve the hostname,
    which might fail.
    @throws UnknownHostException if host can't be resolved.
   */
   public InetAddress getInetAddress() throws UnknownHostException{
     if(ip!=null) return ip;

     return (ip=InetAddress.getByName(host));
   }

   /**
     Returns string representation of the message.
   */
   public String toString(){
      String s=
        "Socks5Message:"+"\n"+
        "VN   "+version+"\n"+
        "CMD  "+command+"\n"+
        "ATYP "+addrType+"\n"+
        "ADDR "+host+"\n"+
        "PORT "+port+"\n";
      return s;
   }
            

   /**
    *Wether to resolve hostIP returned from SOCKS server
    *that is wether to create InetAddress object from the
    *hostName string
    */
   static public boolean resolveIP(){ return doResolveIP;}

   /**
    *Wether to resolve hostIP returned from SOCKS server
    *that is wether to create InetAddress object from the
    *hostName string
    *@param doResolve Wether to resolve hostIP from SOCKS server.
    *@return Previous value.
    */
   static public boolean resolveIP(boolean doResolve){
      boolean old = doResolveIP;
      doResolveIP = doResolve;
      return old;
   }

   /*
   private static final void debug(String s){
      if(DEBUG)
         System.out.print(s);
   }
   private static final boolean DEBUG = false;
   */

   //SOCKS5 constants
   public static final int SOCKS_VERSION		=5;

   public static final int SOCKS_ATYP_IPV4		=0x1; //Where is 2??
   public static final int SOCKS_ATYP_DOMAINNAME	=0x3; //!!!!rfc1928
   public static final int SOCKS_ATYP_IPV6		=0x4;

   public static final int SOCKS_IPV6_LENGTH		=16;

   static boolean doResolveIP = true;

}