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