1 package socks.server; 2 3 import socks.ProxyMessage; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 import java.net.Socket ; 8 9 13 public class UserPasswordAuthenticator extends ServerAuthenticatorNone{ 14 15 static final int METHOD_ID = 2; 16 17 UserValidation validator; 18 19 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; 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 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 out.write(new byte[]{1,0}); 65 }else{ 66 out.write(new byte[]{1,1}); 68 return false; 69 } 70 71 return true; 72 } 73 } 74 | Popular Tags |