| 1 package socks.server; 2 import socks.ProxyMessage; 3 import socks.UDPEncapsulation; 4 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.DataInputStream ; 8 import java.io.OutputStream ; 9 import java.io.PushbackInputStream ; 10 import java.net.Socket ; 11 12 37 public class ServerAuthenticatorNone implements ServerAuthenticator{ 38 39 static final byte[] socks5response = {5,0}; 40 41 InputStream in; 42 OutputStream out; 43 44 47 public ServerAuthenticatorNone(){ 48 this.in = null; 49 this.out = null; 50 } 51 57 public ServerAuthenticatorNone(InputStream in, OutputStream out){ 58 this.in = in; 59 this.out = out; 60 } 61 66 public ServerAuthenticator startSession(Socket s) 67 throws IOException { 68 69 PushbackInputStream in = new PushbackInputStream (s.getInputStream()); 70 OutputStream out = s.getOutputStream(); 71 72 int version = in.read(); 73 if(version == 5){ 74 if(!selectSocks5Authentication(in,out,0)) 75 return null; 76 }else if(version == 4){ 77 in.unread(version); 79 }else 80 return null; 81 82 83 return new ServerAuthenticatorNone(in,out); 84 } 85 86 90 public InputStream getInputStream(){ 91 return in; 92 } 93 97 public OutputStream getOutputStream(){ 98 return out; 99 } 100 104 public UDPEncapsulation getUdpEncapsulation(){ 105 return null; 106 } 107 108 111 public boolean checkRequest(ProxyMessage msg){ 112 return true; 113 } 114 115 118 public boolean checkRequest(java.net.DatagramPacket dp, boolean out){ 119 return true; 120 } 121 122 125 public void endSession(){ 126 } 127 128 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; 152 153 response[0] = (byte) 5; response[1] = (byte) 0xFF; 156 int bread = 0; while(bread < num_methods) 158 bread += in.read(method_ids,bread,num_methods-bread); 159 160 for(int i=0;i<num_methods;++i) 161 if(method_ids[i] == methodId){ 162 found = true; 163 response[1] = (byte) methodId; 164 break; 165 } 166 167 out.write(response); 168 return found; 169 } 170 } 171 | Popular Tags |