KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > socks > server > ServerAuthenticatorNone


1 package socks.server;
2 import socks.ProxyMessage;
3 import socks.UDPEncapsulation;
4
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.DataInputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.io.PushbackInputStream JavaDoc;
10 import java.net.Socket JavaDoc;
11
12 /**
13  An implementation of ServerAuthenticator, which does <b>not</b> do
14  any authentication.
15 <P>
16 <FONT size="+3" color ="FF0000"> Warning!!</font><br> Should not be
17 used on machines which are not behind the firewall.
18 <p>
19 It is only provided to make implementing other authentication schemes
20 easier.<br>
21 For Example: <tt><pre>
22    class MyAuth extends socks.server.ServerAuthenticator{
23     ...
24     public ServerAuthenticator startSession(java.net.Socket s){
25       if(!checkHost(s.getInetAddress()) return null;
26       return super.startSession(s);
27     }
28
29     boolean checkHost(java.net.Inetaddress addr){
30       boolean allow;
31       //Do it somehow
32       return allow;
33     }
34    }
35 </pre></tt>
36 */

37 public class ServerAuthenticatorNone implements ServerAuthenticator{
38
39    static final byte[] socks5response = {5,0};
40
41    InputStream JavaDoc in;
42    OutputStream JavaDoc out;
43
44    /**
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 JavaDoc in, OutputStream JavaDoc 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.
65     */

66    public ServerAuthenticator startSession(Socket JavaDoc s)
67                                   throws IOException JavaDoc{
68
69      PushbackInputStream JavaDoc in = new PushbackInputStream JavaDoc(s.getInputStream());
70      OutputStream JavaDoc 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        //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 JavaDoc getInputStream(){
91       return in;
92    }
93    /**
94      Get output stream.
95      @return Output stream speciefied in the constructor.
96    */

97    public OutputStream JavaDoc 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 JavaDoc dp, boolean out){
119      return true;
120    }
121
122    /**
123     Does nothing.
124     */

125    public void endSession(){
126    }
127
128    /**
129      Convinience routine for selecting SOCKSv5 authentication.
130      <p>
131      This method reads in authentication methods that client supports,
132      checks wether it supports given method. If it does, the notification
133      method is written back to client, that this method have been chosen
134      for authentication. If given method was not found, authentication
135      failure message is send to client ([5,FF]).
136      @param in Input stream, version byte should be removed from the stream
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 JavaDoc in,
143                                                     OutputStream JavaDoc out,
144                                                     int methodId)
145                                                     throws IOException JavaDoc{
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; //SOCKS version
154
response[1] = (byte) 0xFF; //Not found, we are pessimistic
155

156       int bread = 0; //bytes read so far
157
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