From d8e49ec3f297f9ba0991762eb2cddbc00ed634d2 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Thu, 11 Apr 2013 21:01:32 -0700 Subject: Fix line endings --- .../jsocks/server/ServerAuthenticator.java | 240 +++++++-------- .../jsocks/server/ServerAuthenticatorNone.java | 338 ++++++++++----------- 2 files changed, 289 insertions(+), 289 deletions(-) (limited to 'src/net/sourceforge/jsocks/server') diff --git a/src/net/sourceforge/jsocks/server/ServerAuthenticator.java b/src/net/sourceforge/jsocks/server/ServerAuthenticator.java index 6fb99e0..cb7f0af 100644 --- a/src/net/sourceforge/jsocks/server/ServerAuthenticator.java +++ b/src/net/sourceforge/jsocks/server/ServerAuthenticator.java @@ -1,120 +1,120 @@ -package net.sourceforge.jsocks.server; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.DatagramPacket; -import java.net.Socket; - -import net.sourceforge.jsocks.ProxyMessage; -import net.sourceforge.jsocks.UDPEncapsulation; - -/** - Classes implementing this interface should provide socks server with - authentication and authorization of users. -**/ -public interface ServerAuthenticator{ - - /** - This method is called when a new connection accepted by the server. -

- At this point no data have been extracted from the connection. It is - responsibility of this method to ensure that the next byte in the - stream after this method have been called is the first byte of the - socks request message. For SOCKSv4 there is no authentication data and - the first byte in the stream is part of the request. With SOCKSv5 however - there is an authentication data first. It is expected that implementaions - will process this authentication data. -

- If authentication was successful an instance of ServerAuthentication - should be returned, it later will be used by the server to perform - authorization and some other things. If authentication fails null should - be returned, or an exception may be thrown. - - @param s Accepted Socket. - @return An instance of ServerAuthenticator to be used for this connection - or null - */ - ServerAuthenticator startSession(Socket s) throws IOException; - - /** - This method should return input stream which should be used on the - accepted socket. -

- SOCKSv5 allows to have multiple authentication methods, and these methods - might require some kind of transformations being made on the data. -

- This method is called on the object returned from the startSession - function. - */ - InputStream getInputStream(); - /** - This method should return output stream to use to write to the accepted - socket. -

- SOCKSv5 allows to have multiple authentication methods, and these methods - might require some kind of transformations being made on the data. -

- This method is called on the object returned from the startSession - function. - */ - OutputStream getOutputStream(); - - /** - This method should return UDPEncapsulation, which should be used - on the datagrams being send in/out. -

- If no transformation should be done on the datagrams, this method - should return null. -

- This method is called on the object returned from the startSession - function. - */ - - UDPEncapsulation getUdpEncapsulation(); - - /** - This method is called when a request have been read. -

- Implementation should decide wether to grant request or not. Returning - true implies granting the request, false means request should be rejected. -

- This method is called on the object returned from the startSession - function. - @param msg Request message. - @return true to grant request, false to reject it. - */ - boolean checkRequest(ProxyMessage msg); - - /** - This method is called when datagram is received by the server. -

- Implementaions should decide wether it should be forwarded or dropped. - It is expecteed that implementation will use datagram address and port - information to make a decision, as well as anything else. Address and - port of the datagram are always correspond to remote machine. It is - either destination or source address. If out is true address is destination - address, else it is a source address, address of the machine from which - datagram have been received for the client. -

- Implementaions should return true if the datagram is to be forwarded, and - false if the datagram should be dropped. -

- This method is called on the object returned from the startSession - function. - - @param out If true the datagram is being send out(from the client), - otherwise it is an incoming datagram. - @return True to forward datagram false drop it silently. - */ - boolean checkRequest(DatagramPacket dp, boolean out); - - /** - This method is called when session is completed. Either due to normal - termination or due to any error condition. -

- This method is called on the object returned from the startSession - function. - */ - void endSession(); -} +package net.sourceforge.jsocks.server; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.DatagramPacket; +import java.net.Socket; + +import net.sourceforge.jsocks.ProxyMessage; +import net.sourceforge.jsocks.UDPEncapsulation; + +/** + Classes implementing this interface should provide socks server with + authentication and authorization of users. +**/ +public interface ServerAuthenticator{ + + /** + This method is called when a new connection accepted by the server. +

+ At this point no data have been extracted from the connection. It is + responsibility of this method to ensure that the next byte in the + stream after this method have been called is the first byte of the + socks request message. For SOCKSv4 there is no authentication data and + the first byte in the stream is part of the request. With SOCKSv5 however + there is an authentication data first. It is expected that implementaions + will process this authentication data. +

+ If authentication was successful an instance of ServerAuthentication + should be returned, it later will be used by the server to perform + authorization and some other things. If authentication fails null should + be returned, or an exception may be thrown. + + @param s Accepted Socket. + @return An instance of ServerAuthenticator to be used for this connection + or null + */ + ServerAuthenticator startSession(Socket s) throws IOException; + + /** + This method should return input stream which should be used on the + accepted socket. +

+ SOCKSv5 allows to have multiple authentication methods, and these methods + might require some kind of transformations being made on the data. +

+ This method is called on the object returned from the startSession + function. + */ + InputStream getInputStream(); + /** + This method should return output stream to use to write to the accepted + socket. +

+ SOCKSv5 allows to have multiple authentication methods, and these methods + might require some kind of transformations being made on the data. +

+ This method is called on the object returned from the startSession + function. + */ + OutputStream getOutputStream(); + + /** + This method should return UDPEncapsulation, which should be used + on the datagrams being send in/out. +

+ If no transformation should be done on the datagrams, this method + should return null. +

+ This method is called on the object returned from the startSession + function. + */ + + UDPEncapsulation getUdpEncapsulation(); + + /** + This method is called when a request have been read. +

+ Implementation should decide wether to grant request or not. Returning + true implies granting the request, false means request should be rejected. +

+ This method is called on the object returned from the startSession + function. + @param msg Request message. + @return true to grant request, false to reject it. + */ + boolean checkRequest(ProxyMessage msg); + + /** + This method is called when datagram is received by the server. +

+ Implementaions should decide wether it should be forwarded or dropped. + It is expecteed that implementation will use datagram address and port + information to make a decision, as well as anything else. Address and + port of the datagram are always correspond to remote machine. It is + either destination or source address. If out is true address is destination + address, else it is a source address, address of the machine from which + datagram have been received for the client. +

+ Implementaions should return true if the datagram is to be forwarded, and + false if the datagram should be dropped. +

+ This method is called on the object returned from the startSession + function. + + @param out If true the datagram is being send out(from the client), + otherwise it is an incoming datagram. + @return True to forward datagram false drop it silently. + */ + boolean checkRequest(DatagramPacket dp, boolean out); + + /** + This method is called when session is completed. Either due to normal + termination or due to any error condition. +

+ This method is called on the object returned from the startSession + function. + */ + void endSession(); +} diff --git a/src/net/sourceforge/jsocks/server/ServerAuthenticatorNone.java b/src/net/sourceforge/jsocks/server/ServerAuthenticatorNone.java index 30eb265..e4edbe7 100644 --- a/src/net/sourceforge/jsocks/server/ServerAuthenticatorNone.java +++ b/src/net/sourceforge/jsocks/server/ServerAuthenticatorNone.java @@ -1,169 +1,169 @@ -package net.sourceforge.jsocks.server; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PushbackInputStream; -import java.net.Socket; - -import net.sourceforge.jsocks.ProxyMessage; -import net.sourceforge.jsocks.UDPEncapsulation; - -/** - An implementation of ServerAuthenticator, which does not do - any authentication. -

- Warning!!
Should not be -used on machines which are not behind the firewall. -

-It is only provided to make implementing other authentication schemes -easier.
-For Example:

-   class MyAuth extends socks.server.ServerAuthenticator{
-    ...
-    public ServerAuthenticator startSession(java.net.Socket s){
-      if(!checkHost(s.getInetAddress()) return null;
-      return super.startSession(s);
-    }
-
-    boolean checkHost(java.net.Inetaddress addr){
-      boolean allow;
-      //Do it somehow
-      return allow;
-    }
-   }
-
-*/ -public class ServerAuthenticatorNone implements ServerAuthenticator{ - - static final byte[] socks5response = {5,0}; - - InputStream in; - OutputStream out; - - /** - Creates new instance of the ServerAuthenticatorNone. - */ - public ServerAuthenticatorNone(){ - this.in = null; - this.out = null; - } - /** - Constructs new ServerAuthenticatorNone object suitable for returning - from the startSession function. - @param in Input stream to return from getInputStream method. - @param out Output stream to return from getOutputStream method. - */ - public ServerAuthenticatorNone(InputStream in, OutputStream out){ - this.in = in; - this.out = out; - } - /** - Grants access to everyone.Removes authentication related bytes from - the stream, when a SOCKS5 connection is being made, selects an - authentication NONE. - */ - public ServerAuthenticator startSession(Socket s) - throws IOException{ - - PushbackInputStream in = new PushbackInputStream(s.getInputStream()); - OutputStream out = s.getOutputStream(); - - int version = in.read(); - if(version == 5){ - if(!selectSocks5Authentication(in,out,0)) - return null; - }else if(version == 4){ - //Else it is the request message allready, version 4 - in.unread(version); - }else - return null; - - - return new ServerAuthenticatorNone(in,out); - } - - /** - Get input stream. - @return Input stream speciefied in the constructor. - */ - public InputStream getInputStream(){ - return in; - } - /** - Get output stream. - @return Output stream speciefied in the constructor. - */ - public OutputStream getOutputStream(){ - return out; - } - /** - Allways returns null. - @return null - */ - public UDPEncapsulation getUdpEncapsulation(){ - return null; - } - - /** - Allways returns true. - */ - public boolean checkRequest(ProxyMessage msg){ - return true; - } - - /** - Allways returns true. - */ - public boolean checkRequest(java.net.DatagramPacket dp, boolean out){ - return true; - } - - /** - Does nothing. - */ - public void endSession(){ - } - - /** - Convinience routine for selecting SOCKSv5 authentication. -

- This method reads in authentication methods that client supports, - checks wether it supports given method. If it does, the notification - method is written back to client, that this method have been chosen - for authentication. If given method was not found, authentication - failure message is send to client ([5,FF]). - @param in Input stream, version byte should be removed from the stream - before calling this method. - @param out Output stream. - @param methodId Method which should be selected. - @return true if methodId was found, false otherwise. - */ - static public boolean selectSocks5Authentication(InputStream in, - OutputStream out, - int methodId) - throws IOException{ - - int num_methods = in.read(); - if (num_methods <= 0) return false; - byte method_ids[] = new byte[num_methods]; - byte response[] = new byte[2]; - boolean found = false; - - response[0] = (byte) 5; //SOCKS version - response[1] = (byte) 0xFF; //Not found, we are pessimistic - - int bread = 0; //bytes read so far - while(bread < num_methods) - bread += in.read(method_ids,bread,num_methods-bread); - - for(int i=0;inot do + any authentication. +

+ Warning!!
Should not be +used on machines which are not behind the firewall. +

+It is only provided to make implementing other authentication schemes +easier.
+For Example:

+   class MyAuth extends socks.server.ServerAuthenticator{
+    ...
+    public ServerAuthenticator startSession(java.net.Socket s){
+      if(!checkHost(s.getInetAddress()) return null;
+      return super.startSession(s);
+    }
+
+    boolean checkHost(java.net.Inetaddress addr){
+      boolean allow;
+      //Do it somehow
+      return allow;
+    }
+   }
+
+*/ +public class ServerAuthenticatorNone implements ServerAuthenticator{ + + static final byte[] socks5response = {5,0}; + + InputStream in; + OutputStream out; + + /** + Creates new instance of the ServerAuthenticatorNone. + */ + public ServerAuthenticatorNone(){ + this.in = null; + this.out = null; + } + /** + Constructs new ServerAuthenticatorNone object suitable for returning + from the startSession function. + @param in Input stream to return from getInputStream method. + @param out Output stream to return from getOutputStream method. + */ + public ServerAuthenticatorNone(InputStream in, OutputStream out){ + this.in = in; + this.out = out; + } + /** + Grants access to everyone.Removes authentication related bytes from + the stream, when a SOCKS5 connection is being made, selects an + authentication NONE. + */ + public ServerAuthenticator startSession(Socket s) + throws IOException{ + + PushbackInputStream in = new PushbackInputStream(s.getInputStream()); + OutputStream out = s.getOutputStream(); + + int version = in.read(); + if(version == 5){ + if(!selectSocks5Authentication(in,out,0)) + return null; + }else if(version == 4){ + //Else it is the request message allready, version 4 + in.unread(version); + }else + return null; + + + return new ServerAuthenticatorNone(in,out); + } + + /** + Get input stream. + @return Input stream speciefied in the constructor. + */ + public InputStream getInputStream(){ + return in; + } + /** + Get output stream. + @return Output stream speciefied in the constructor. + */ + public OutputStream getOutputStream(){ + return out; + } + /** + Allways returns null. + @return null + */ + public UDPEncapsulation getUdpEncapsulation(){ + return null; + } + + /** + Allways returns true. + */ + public boolean checkRequest(ProxyMessage msg){ + return true; + } + + /** + Allways returns true. + */ + public boolean checkRequest(java.net.DatagramPacket dp, boolean out){ + return true; + } + + /** + Does nothing. + */ + public void endSession(){ + } + + /** + Convinience routine for selecting SOCKSv5 authentication. +

+ This method reads in authentication methods that client supports, + checks wether it supports given method. If it does, the notification + method is written back to client, that this method have been chosen + for authentication. If given method was not found, authentication + failure message is send to client ([5,FF]). + @param in Input stream, version byte should be removed from the stream + before calling this method. + @param out Output stream. + @param methodId Method which should be selected. + @return true if methodId was found, false otherwise. + */ + static public boolean selectSocks5Authentication(InputStream in, + OutputStream out, + int methodId) + throws IOException{ + + int num_methods = in.read(); + if (num_methods <= 0) return false; + byte method_ids[] = new byte[num_methods]; + byte response[] = new byte[2]; + boolean found = false; + + response[0] = (byte) 5; //SOCKS version + response[1] = (byte) 0xFF; //Not found, we are pessimistic + + int bread = 0; //bytes read so far + while(bread < num_methods) + bread += in.read(method_ids,bread,num_methods-bread); + + for(int i=0;i