comparison src/net/sourceforge/jsocks/Socks4Message.java @ 349:205ee2873330

update jsocks to 2011-03-19
author Carl Byington <carl@five-ten-sg.com>
date Fri, 01 Aug 2014 11:23:10 -0700
parents 0ce5cc452d02
children 2bc6805cbc91
comparison
equal deleted inserted replaced
348:29076621bab0 349:205ee2873330
1 package net.sourceforge.jsocks; 1 package net.sourceforge.jsocks;
2 import java.io.DataInputStream; 2 import java.io.*;
3 import java.io.IOException; 3 import java.net.*;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.UnknownHostException;
8 4
9 /** 5 /**
10 SOCKS4 Reply/Request message. 6 SOCKS4 Reply/Request message.
11 */ 7 */
12 8
13 public class Socks4Message extends ProxyMessage { 9 class Socks4Message extends ProxyMessage{
14 10
15 private byte[] msgBytes; 11 private byte[] msgBytes;
16 private int msgLength; 12 private int msgLength;
17 13
18 /** 14 /**
19 * Server failed reply, cmd command for failed request 15 * Server failed reply, cmd command for failed request
20 */ 16 */
21 public Socks4Message(int cmd) { 17 public Socks4Message(int cmd){
22 super(cmd, null, 0); 18 super(cmd,null,0);
23 this.user = null; 19 this.user = null;
24 msgLength = 2;
25 msgBytes = new byte[2];
26 msgBytes[0] = (byte) 0;
27 msgBytes[1] = (byte) command;
28 }
29 20
30 /** 21 msgLength = 2;
31 * Server successfull reply 22 msgBytes = new byte[2];
32 */
33 public Socks4Message(int cmd, InetAddress ip, int port) {
34 this(0, cmd, ip, port, null);
35 }
36 23
37 /** 24 msgBytes[0] = (byte) 0;
38 * Client request 25 msgBytes[1] = (byte) command;
39 */ 26 }
40 public Socks4Message(int cmd, InetAddress ip, int port, String user) {
41 this(SOCKS_VERSION, cmd, ip, port, user);
42 }
43 27
44 /** 28 /**
45 * Most general constructor 29 * Server successfull reply
46 */ 30 */
47 public Socks4Message(int version, int cmd, 31 public Socks4Message(int cmd,InetAddress ip,int port){
48 InetAddress ip, int port, String user) { 32 this(0,cmd,ip,port,null);
49 super(cmd, ip, port); 33 }
50 this.user = user;
51 this.version = version;
52 msgLength = user == null ? 8 : 9 + user.length();
53 msgBytes = new byte[msgLength];
54 msgBytes[0] = (byte) version;
55 msgBytes[1] = (byte) command;
56 msgBytes[2] = (byte)(port >> 8);
57 msgBytes[3] = (byte) port;
58 byte[] addr;
59 34
60 if (ip != null) 35 /**
61 addr = ip.getAddress(); 36 * Client request
62 else { 37 */
63 addr = new byte[4]; 38 public Socks4Message(int cmd,InetAddress ip,int port,String user){
64 addr[0] = addr[1] = addr[2] = addr[3] = 0; 39 this(SOCKS_VERSION,cmd,ip,port,user);
65 } 40 }
66 41
67 System.arraycopy(addr, 0, msgBytes, 4, 4); 42 /**
43 * Most general constructor
44 */
45 public Socks4Message(int version, int cmd,
46 InetAddress ip,int port,String user){
47 super(cmd,ip,port);
48 this.user = user;
49 this.version = version;
68 50
69 if (user != null) { 51 msgLength = user == null?8:9+user.length();
70 byte[] buf = user.getBytes(); 52 msgBytes = new byte[msgLength];
71 System.arraycopy(buf, 0, msgBytes, 8, buf.length);
72 msgBytes[msgBytes.length - 1 ] = 0;
73 }
74 }
75 53
76 /** 54 msgBytes[0] = (byte) version;
77 *Initialise from the stream 55 msgBytes[1] = (byte) command;
78 *If clientMode is true attempts to read a server response 56 msgBytes[2] = (byte) (port >> 8);
79 *otherwise reads a client request 57 msgBytes[3] = (byte) port;
80 *see read for more detail
81 */
82 public Socks4Message(InputStream in, boolean clientMode) throws IOException {
83 msgBytes = null;
84 read(in, clientMode);
85 }
86 58
87 @Override 59 byte[] addr;
88 public void read(InputStream in) throws IOException {
89 read(in, true);
90 }
91 60
92 @Override 61 if(ip != null)
93 public void read(InputStream in, boolean clientMode) throws IOException { 62 addr = ip.getAddress();
94 boolean mode4a = false; 63 else{
95 DataInputStream d_in = new DataInputStream(in); 64 addr = new byte[4];
96 version = d_in.readUnsignedByte(); 65 addr[0]=addr[1]=addr[2]=addr[3]=0;
97 command = d_in.readUnsignedByte(); 66 }
67 System.arraycopy(addr,0,msgBytes,4,4);
98 68
99 if (clientMode && command != REPLY_OK) { 69 if(user != null){
100 String errMsg; 70 byte[] buf = user.getBytes();
71 System.arraycopy(buf,0,msgBytes,8,buf.length);
72 msgBytes[msgBytes.length -1 ] = 0;
73 }
74 }
101 75
102 if (command > REPLY_OK && command < REPLY_BAD_IDENTD) 76 /**
103 errMsg = replyMessage[command - REPLY_OK]; 77 *Initialise from the stream
104 else 78 *If clientMode is true attempts to read a server response
105 errMsg = "Unknown Reply Code"; 79 *otherwise reads a client request
80 *see read for more detail
81 */
82 public Socks4Message(InputStream in, boolean clientMode) throws IOException{
83 msgBytes = null;
84 read(in,clientMode);
85 }
106 86
107 throw new SocksException(command, errMsg); 87 public void read(InputStream in) throws IOException{
108 } 88 read(in,true);
89 }
109 90
110 port = d_in.readUnsignedShort(); 91 public void read(InputStream in, boolean clientMode) throws IOException{
111 byte[] addr = new byte[4]; 92 DataInputStream d_in = new DataInputStream(in);
112 d_in.readFully(addr); 93 version= d_in.readUnsignedByte();
94 command = d_in.readUnsignedByte();
95 if(clientMode && command != REPLY_OK){
96 String errMsg;
97 if(command >REPLY_OK && command < REPLY_BAD_IDENTD)
98 errMsg = replyMessage[command-REPLY_OK];
99 else
100 errMsg = "Unknown Reply Code";
101 throw new SocksException(command,errMsg);
102 }
103 port = d_in.readUnsignedShort();
104 byte[] addr = new byte[4];
105 d_in.readFully(addr);
106 ip=bytes2IP(addr);
107 host = ip.getHostName();
108 if(!clientMode){
109 int b = in.read();
110 //Hope there are no idiots with user name bigger than this
111 byte[] userBytes = new byte[256];
112 int i = 0;
113 for(i =0;i<userBytes.length && b>0;++i){
114 userBytes[i] = (byte) b;
115 b = in.read();
116 }
117 user = new String(userBytes,0,i);
118 }
119 }
120 public void write(OutputStream out) throws IOException{
121 if(msgBytes == null){
122 Socks4Message msg = new Socks4Message(version,command,ip,port,user);
123 msgBytes = msg.msgBytes;
124 msgLength = msg.msgLength;
125 }
126 out.write(msgBytes);
127 }
113 128
114 if (addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0) 129 //Class methods
115 mode4a = true; 130 static InetAddress bytes2IP(byte[] addr){
116 else { 131 String s = bytes2IPV4(addr,0);
117 ip = bytes2IP(addr); 132 try{
118 host = ip.getHostName(); 133 return InetAddress.getByName(s);
119 } 134 }catch(UnknownHostException uh_ex){
135 return null;
136 }
137 }
120 138
121 if (!clientMode) { 139 //Constants
122 StringBuilder sb = new StringBuilder();
123 int b;
124 140
125 while ((b = in.read()) != 0) 141 static final String[] replyMessage ={
126 sb.append((char) b); 142 "Request Granted",
143 "Request Rejected or Failed",
144 "Failed request, can't connect to Identd",
145 "Failed request, bad user name"};
127 146
128 user = sb.toString(); 147 static final int SOCKS_VERSION = 4;
129 148
130 if (mode4a) { 149 public final static int REQUEST_CONNECT = 1;
131 sb.setLength(0); 150 public final static int REQUEST_BIND = 2;
132 151
133 while ((b = in.read()) != 0) 152 public final static int REPLY_OK = 90;
134 sb.append((char) b); 153 public final static int REPLY_REJECTED = 91;
135 154 public final static int REPLY_NO_CONNECT = 92;
136 host = sb.toString(); 155 public final static int REPLY_BAD_IDENTD = 93;
137 }
138 }
139 }
140 @Override
141 public void write(OutputStream out) throws IOException {
142 if (msgBytes == null) {
143 Socks4Message msg = new Socks4Message(version, command, ip, port, user);
144 msgBytes = msg.msgBytes;
145 msgLength = msg.msgLength;
146 }
147
148 out.write(msgBytes);
149 }
150
151 //Class methods
152 static InetAddress bytes2IP(byte[] addr) {
153 String s = bytes2IPV4(addr, 0);
154
155 try {
156 return InetAddress.getByName(s);
157 }
158 catch (UnknownHostException uh_ex) {
159 return null;
160 }
161 }
162
163 //Constants
164
165 static final String[] replyMessage = {
166 "Request Granted",
167 "Request Rejected or Failed",
168 "Failed request, can't connect to Identd",
169 "Failed request, bad user name"
170 };
171
172 static final int SOCKS_VERSION = 4;
173
174 public final static int REQUEST_CONNECT = 1;
175 public final static int REQUEST_BIND = 2;
176
177 public final static int REPLY_OK = 90;
178 public final static int REPLY_REJECTED = 91;
179 public final static int REPLY_NO_CONNECT = 92;
180 public final static int REPLY_BAD_IDENTD = 93;
181 156
182 } 157 }