annotate src/net/sourceforge/jsocks/server/UserPasswordAuthenticator.java @ 391:bcbd579253a1

bump version number
author Carl Byington <carl@five-ten-sg.com>
date Tue, 16 Sep 2014 11:12:45 -0700
parents 72de889ecfe7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
350
72de889ecfe7 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 349
diff changeset
1 package net.sourceforge.jsocks.server;
349
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 import net.sourceforge.jsocks.ProxyMessage;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 import java.io.IOException;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 import java.io.InputStream;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 import java.io.OutputStream;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 import java.net.Socket;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 This class implements SOCKS5 User/Password authentication scheme as
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 defined in rfc1929,the server side of it.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 public class UserPasswordAuthenticator extends ServerAuthenticatorNone{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 static final int METHOD_ID = 2;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 UserValidation validator;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 Construct a new UserPasswordAuthentication object, with given
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 UserVlaidation scheme.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 @param v UserValidation to use for validating users.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 public UserPasswordAuthenticator(UserValidation validator){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 this.validator = validator;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 public ServerAuthenticator startSession(Socket s) throws IOException{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 InputStream in = s.getInputStream();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 OutputStream out = s.getOutputStream();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 if(in.read() != 5) return null; //Drop non version 5 messages.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 if(!selectSocks5Authentication(in,out,METHOD_ID))
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 return null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 if(!doUserPasswordAuthentication(s,in,out))
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 return null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 return new ServerAuthenticatorNone(in,out);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 //Private Methods
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 //////////////////
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 private boolean doUserPasswordAuthentication(Socket s,
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 InputStream in,
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 OutputStream out)
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 throws IOException{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 int version = in.read();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 if(version != 1) return false;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 int ulen = in.read();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 if(ulen < 0) return false;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 byte[] user = new byte[ulen];
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 in.read(user);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 int plen = in.read();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 if(plen < 0) return false;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 byte[] password = new byte[plen];
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 in.read(password);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 if(validator.isUserValid(new String(user), new String(password),s)){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 //System.out.println("user valid");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 out.write(new byte[]{1,0});
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 }else{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 //System.out.println("user invalid");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 out.write(new byte[]{1,1});
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 return false;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 return true;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 }