comparison src/net/sourceforge/jsocks/server/ServerAuthenticatorNone.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 72de889ecfe7
comparison
equal deleted inserted replaced
348:29076621bab0 349:205ee2873330
1 package net.sourceforge.jsocks.server; 1 package socks.server;
2 import net.sourceforge.jsocks.ProxyMessage;
3 import net.sourceforge.jsocks.UDPEncapsulation;
4
2 import java.io.IOException; 5 import java.io.IOException;
3 import java.io.InputStream; 6 import java.io.InputStream;
7 import java.io.DataInputStream;
4 import java.io.OutputStream; 8 import java.io.OutputStream;
5 import java.io.PushbackInputStream; 9 import java.io.PushbackInputStream;
6 import java.net.Socket; 10 import java.net.Socket;
7
8 import net.sourceforge.jsocks.ProxyMessage;
9 import net.sourceforge.jsocks.UDPEncapsulation;
10 11
11 /** 12 /**
12 An implementation of ServerAuthenticator, which does <b>not</b> do 13 An implementation of ServerAuthenticator, which does <b>not</b> do
13 any authentication. 14 any authentication.
14 <P> 15 <P>
31 return allow; 32 return allow;
32 } 33 }
33 } 34 }
34 </pre></tt> 35 </pre></tt>
35 */ 36 */
36 public class ServerAuthenticatorNone implements ServerAuthenticator { 37 public class ServerAuthenticatorNone implements ServerAuthenticator{
37 38
38 static final byte[] socks5response = {5, 0}; 39 static final byte[] socks5response = {5,0};
39 40
40 InputStream in; 41 InputStream in;
41 OutputStream out; 42 OutputStream out;
42 43
43 /** 44 /**
44 Creates new instance of the ServerAuthenticatorNone. 45 Creates new instance of the ServerAuthenticatorNone.
46 */
47 public ServerAuthenticatorNone(){
48 this.in = null;
49 this.out = null;
50 }
51 /**
52 Constructs new ServerAuthenticatorNone object suitable for returning
53 from the startSession function.
54 @param in Input stream to return from getInputStream method.
55 @param out Output stream to return from getOutputStream method.
56 */
57 public ServerAuthenticatorNone(InputStream in, OutputStream out){
58 this.in = in;
59 this.out = out;
60 }
61 /**
62 Grants access to everyone.Removes authentication related bytes from
63 the stream, when a SOCKS5 connection is being made, selects an
64 authentication NONE.
45 */ 65 */
46 public ServerAuthenticatorNone() { 66 public ServerAuthenticator startSession(Socket s)
47 this.in = null; 67 throws IOException{
48 this.out = null; 68
49 } 69 PushbackInputStream in = new PushbackInputStream(s.getInputStream());
50 /** 70 OutputStream out = s.getOutputStream();
51 Constructs new ServerAuthenticatorNone object suitable for returning 71
52 from the startSession function. 72 int version = in.read();
53 @param in Input stream to return from getInputStream method. 73 if(version == 5){
54 @param out Output stream to return from getOutputStream method. 74 if(!selectSocks5Authentication(in,out,0))
75 return null;
76 }else if(version == 4){
77 //Else it is the request message allready, version 4
78 in.unread(version);
79 }else
80 return null;
81
82
83 return new ServerAuthenticatorNone(in,out);
84 }
85
86 /**
87 Get input stream.
88 @return Input stream speciefied in the constructor.
89 */
90 public InputStream getInputStream(){
91 return in;
92 }
93 /**
94 Get output stream.
95 @return Output stream speciefied in the constructor.
96 */
97 public OutputStream getOutputStream(){
98 return out;
99 }
100 /**
101 Allways returns null.
102 @return null
103 */
104 public UDPEncapsulation getUdpEncapsulation(){
105 return null;
106 }
107
108 /**
109 Allways returns true.
110 */
111 public boolean checkRequest(ProxyMessage msg){
112 return true;
113 }
114
115 /**
116 Allways returns true.
117 */
118 public boolean checkRequest(java.net.DatagramPacket dp, boolean out){
119 return true;
120 }
121
122 /**
123 Does nothing.
55 */ 124 */
56 public ServerAuthenticatorNone(InputStream in, OutputStream out) { 125 public void endSession(){
57 this.in = in; 126 }
58 this.out = out;
59 }
60 /**
61 Grants access to everyone.Removes authentication related bytes from
62 the stream, when a SOCKS5 connection is being made, selects an
63 authentication NONE.
64 */
65 public ServerAuthenticator startSession(Socket s)
66 throws IOException {
67 PushbackInputStream in = new PushbackInputStream(s.getInputStream());
68 OutputStream out = s.getOutputStream();
69 int version = in.read();
70 127
71 if (version == 5) { 128 /**
72 if (!selectSocks5Authentication(in, out, 0)) 129 Convinience routine for selecting SOCKSv5 authentication.
73 return null; 130 <p>
74 } 131 This method reads in authentication methods that client supports,
75 else if (version == 4) { 132 checks wether it supports given method. If it does, the notification
76 //Else it is the request message allready, version 4 133 method is written back to client, that this method have been chosen
77 in.unread(version); 134 for authentication. If given method was not found, authentication
78 } 135 failure message is send to client ([5,FF]).
79 else 136 @param in Input stream, version byte should be removed from the stream
80 return null; 137 before calling this method.
138 @param out Output stream.
139 @param methodId Method which should be selected.
140 @return true if methodId was found, false otherwise.
141 */
142 static public boolean selectSocks5Authentication(InputStream in,
143 OutputStream out,
144 int methodId)
145 throws IOException{
146
147 int num_methods = in.read();
148 if (num_methods <= 0) return false;
149 byte method_ids[] = new byte[num_methods];
150 byte response[] = new byte[2];
151 boolean found = false;
81 152
82 return new ServerAuthenticatorNone(in, out); 153 response[0] = (byte) 5; //SOCKS version
83 } 154 response[1] = (byte) 0xFF; //Not found, we are pessimistic
84 155
85 /** 156 int bread = 0; //bytes read so far
86 Get input stream. 157 while(bread < num_methods)
87 @return Input stream speciefied in the constructor. 158 bread += in.read(method_ids,bread,num_methods-bread);
88 */
89 public InputStream getInputStream() {
90 return in;
91 }
92 /**
93 Get output stream.
94 @return Output stream speciefied in the constructor.
95 */
96 public OutputStream getOutputStream() {
97 return out;
98 }
99 /**
100 Allways returns null.
101 @return null
102 */
103 public UDPEncapsulation getUdpEncapsulation() {
104 return null;
105 }
106 159
107 /** 160 for(int i=0;i<num_methods;++i)
108 Allways returns true. 161 if(method_ids[i] == methodId){
109 */ 162 found = true;
110 public boolean checkRequest(ProxyMessage msg) { 163 response[1] = (byte) methodId;
111 return true; 164 break;
112 } 165 }
113 166
114 /** 167 out.write(response);
115 Allways returns true. 168 return found;
116 */ 169 }
117 public boolean checkRequest(java.net.DatagramPacket dp, boolean out) {
118 return true;
119 }
120
121 /**
122 Does nothing.
123 */
124 public void endSession() {
125 }
126
127 /**
128 Convinience routine for selecting SOCKSv5 authentication.
129 <p>
130 This method reads in authentication methods that client supports,
131 checks wether it supports given method. If it does, the notification
132 method is written back to client, that this method have been chosen
133 for authentication. If given method was not found, authentication
134 failure message is send to client ([5,FF]).
135 @param in Input stream, version byte should be removed from the stream
136 before calling this method.
137 @param out Output stream.
138 @param methodId Method which should be selected.
139 @return true if methodId was found, false otherwise.
140 */
141 static public boolean selectSocks5Authentication(InputStream in,
142 OutputStream out,
143 int methodId)
144 throws IOException {
145 int num_methods = in.read();
146
147 if (num_methods <= 0) return false;
148
149 byte method_ids[] = new byte[num_methods];
150 byte response[] = new byte[2];
151 boolean found = false;
152 response[0] = (byte) 5; //SOCKS version
153 response[1] = (byte) 0xFF; //Not found, we are pessimistic
154 int bread = 0; //bytes read so far
155
156 while (bread < num_methods)
157 bread += in.read(method_ids, bread, num_methods - bread);
158
159 for (int i = 0; i < num_methods; ++i)
160 if (method_ids[i] == methodId) {
161 found = true;
162 response[1] = (byte) methodId;
163 break;
164 }
165
166 out.write(response);
167 return found;
168 }
169 } 170 }