socks
Class Proxy

java.lang.Object
  extended by socks.Proxy
Direct Known Subclasses:
Socks4Proxy, Socks5Proxy

public abstract class Proxy
extends java.lang.Object

Abstract class Proxy, base for classes Socks4Proxy and Socks5Proxy. Defines methods for specifying default proxy, to be used by all classes of this package.


Field Summary
protected  Proxy chainProxy
           
protected static Proxy defaultProxy
           
protected  InetRange directHosts
           
protected  java.io.InputStream in
           
protected  java.io.OutputStream out
           
protected  java.lang.String proxyHost
           
protected  java.net.InetAddress proxyIP
           
protected  int proxyPort
           
protected  java.net.Socket proxySocket
           
static int SOCKS_ADDR_NOT_SUPPORTED
           
static int SOCKS_AUTH_FAILURE
           
static int SOCKS_AUTH_NOT_SUPPORTED
           
static int SOCKS_BADCONNECT
           
static int SOCKS_BADNETWORK
           
static int SOCKS_CMD_NOT_SUPPORTED
           
static int SOCKS_CONNECTION_REFUSED
           
static int SOCKS_DIRECT_FAILED
           
static int SOCKS_FAILURE
           
static int SOCKS_HOST_UNREACHABLE
           
static int SOCKS_JUST_ERROR
           
static int SOCKS_METHOD_NOTSUPPORTED
           
static int SOCKS_NO_PROXY
           
static int SOCKS_PROXY_IO_ERROR
           
static int SOCKS_PROXY_NO_CONNECT
           
static int SOCKS_SUCCESS
           
static int SOCKS_TTL_EXPIRE
           
protected  int version
           
 
Method Summary
protected  ProxyMessage accept()
           
 void addDirect(java.net.InetAddress ip)
          Adds given ip to the list of direct addresses.
 void addDirect(java.net.InetAddress from, java.net.InetAddress to)
          Adds given range of addresses to the lsit of direct addresses, machines within this range will be accessed without using proxy.
 boolean addDirect(java.lang.String host)
          Adds host to the list of direct addresses.
protected  ProxyMessage bind(java.net.InetAddress ip, int port)
           
protected  ProxyMessage bind(java.lang.String host, int port)
           
protected  ProxyMessage connect(java.net.InetAddress ip, int port)
           
protected  ProxyMessage connect(java.lang.String host, int port)
           
protected abstract  Proxy copy()
           
protected  void endSession()
           
protected  ProxyMessage exchange(ProxyMessage request)
          Sends the request reads reply and returns it throws exception if something wrong with IO or the reply code is not zero
protected abstract  ProxyMessage formMessage(java.io.InputStream in)
           
protected abstract  ProxyMessage formMessage(int cmd, java.net.InetAddress ip, int port)
           
protected abstract  ProxyMessage formMessage(int cmd, java.lang.String host, int port)
           
 Proxy getChainProxy()
          Get proxy which is used to connect to this proxy.
static Proxy getDefaultProxy()
          Get current default proxy.
 InetRange getDirect()
          Get the list of direct hosts.
 java.net.InetAddress getInetAddress()
          Get the ip address of the proxy server host.
 int getPort()
          Get the port on which proxy server is running.
 boolean isDirect(java.net.InetAddress host)
          Check wether the given host is on the list of direct addresses.
 boolean isDirect(java.lang.String host)
          Check wether the given host is on the list of direct address.
static Proxy parseProxy(java.lang.String proxy_entry)
          Parses strings in the form: host[:port:user:password], and creates proxy from information obtained from parsing.
protected  ProxyMessage readMsg()
          Reads the reply from the SOCKS server
protected  void sendMsg(ProxyMessage msg)
          Sends the request to SOCKS server
 void setChainProxy(Proxy chainProxy)
          Set the proxy which should be used to connect to given proxy.
static void setDefaultProxy(java.net.InetAddress ipAddress, int port)
          Sets SOCKS5 proxy as default.
static void setDefaultProxy(java.net.InetAddress ipAddress, int port, java.lang.String user)
          Sets SOCKS4 proxy as default.
static void setDefaultProxy(Proxy p)
          Sets default proxy.
static void setDefaultProxy(java.lang.String hostName, int port)
          Sets SOCKS5 proxy as default.
static void setDefaultProxy(java.lang.String hostName, int port, java.lang.String user)
          Sets SOCKS4 proxy as default.
 void setDirect(InetRange ir)
          Sets given InetRange as the list of direct address, previous list will be discarded, any changes done previously with addDirect(Inetaddress) will be lost.
protected  void startSession()
           
 java.lang.String toString()
          Get string representation of this proxy.
protected  ProxyMessage udpAssociate(java.net.InetAddress ip, int port)
           
protected  ProxyMessage udpAssociate(java.lang.String host, int port)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

directHosts

protected InetRange directHosts

proxyIP

protected java.net.InetAddress proxyIP

proxyHost

protected java.lang.String proxyHost

proxyPort

protected int proxyPort

proxySocket

protected java.net.Socket proxySocket

in

protected java.io.InputStream in

out

protected java.io.OutputStream out

version

protected int version

chainProxy

protected Proxy chainProxy

defaultProxy

protected static Proxy defaultProxy

SOCKS_SUCCESS

public static final int SOCKS_SUCCESS
See Also:
Constant Field Values

SOCKS_FAILURE

public static final int SOCKS_FAILURE
See Also:
Constant Field Values

SOCKS_BADCONNECT

public static final int SOCKS_BADCONNECT
See Also:
Constant Field Values

SOCKS_BADNETWORK

public static final int SOCKS_BADNETWORK
See Also:
Constant Field Values

SOCKS_HOST_UNREACHABLE

public static final int SOCKS_HOST_UNREACHABLE
See Also:
Constant Field Values

SOCKS_CONNECTION_REFUSED

public static final int SOCKS_CONNECTION_REFUSED
See Also:
Constant Field Values

SOCKS_TTL_EXPIRE

public static final int SOCKS_TTL_EXPIRE
See Also:
Constant Field Values

SOCKS_CMD_NOT_SUPPORTED

public static final int SOCKS_CMD_NOT_SUPPORTED
See Also:
Constant Field Values

SOCKS_ADDR_NOT_SUPPORTED

public static final int SOCKS_ADDR_NOT_SUPPORTED
See Also:
Constant Field Values

SOCKS_NO_PROXY

public static final int SOCKS_NO_PROXY
See Also:
Constant Field Values

SOCKS_PROXY_NO_CONNECT

public static final int SOCKS_PROXY_NO_CONNECT
See Also:
Constant Field Values

SOCKS_PROXY_IO_ERROR

public static final int SOCKS_PROXY_IO_ERROR
See Also:
Constant Field Values

SOCKS_AUTH_NOT_SUPPORTED

public static final int SOCKS_AUTH_NOT_SUPPORTED
See Also:
Constant Field Values

SOCKS_AUTH_FAILURE

public static final int SOCKS_AUTH_FAILURE
See Also:
Constant Field Values

SOCKS_JUST_ERROR

public static final int SOCKS_JUST_ERROR
See Also:
Constant Field Values

SOCKS_DIRECT_FAILED

public static final int SOCKS_DIRECT_FAILED
See Also:
Constant Field Values

SOCKS_METHOD_NOTSUPPORTED

public static final int SOCKS_METHOD_NOTSUPPORTED
See Also:
Constant Field Values
Method Detail

getPort

public int getPort()
Get the port on which proxy server is running.

Returns:
Proxy port.

getInetAddress

public java.net.InetAddress getInetAddress()
Get the ip address of the proxy server host.

Returns:
Proxy InetAddress.

addDirect

public void addDirect(java.net.InetAddress ip)
Adds given ip to the list of direct addresses. This machine will be accessed without using proxy.


addDirect

public boolean addDirect(java.lang.String host)
Adds host to the list of direct addresses. This machine will be accessed without using proxy.


addDirect

public void addDirect(java.net.InetAddress from,
                      java.net.InetAddress to)
Adds given range of addresses to the lsit of direct addresses, machines within this range will be accessed without using proxy.


setDirect

public void setDirect(InetRange ir)
Sets given InetRange as the list of direct address, previous list will be discarded, any changes done previously with addDirect(Inetaddress) will be lost. The machines in this range will be accessed without using proxy.

Parameters:
ir - InetRange which should be used to look up direct addresses.
See Also:
InetRange

getDirect

public InetRange getDirect()
Get the list of direct hosts.

Returns:
Current range of direct address as InetRange object.
See Also:
InetRange

isDirect

public boolean isDirect(java.lang.String host)
Check wether the given host is on the list of direct address.

Parameters:
host - Host name to check.
Returns:
true if the given host is specified as the direct addresses.

isDirect

public boolean isDirect(java.net.InetAddress host)
Check wether the given host is on the list of direct addresses.

Parameters:
host - Host address to check.
Returns:
true if the given host is specified as the direct address.

setChainProxy

public void setChainProxy(Proxy chainProxy)
Set the proxy which should be used to connect to given proxy.

Parameters:
chainProxy - Proxy to use to connect to this proxy.

getChainProxy

public Proxy getChainProxy()
Get proxy which is used to connect to this proxy.

Returns:
Proxy which is used to connect to this proxy, or null if proxy is to be contacted directly.

toString

public java.lang.String toString()
Get string representation of this proxy.

Overrides:
toString in class java.lang.Object

setDefaultProxy

public static void setDefaultProxy(java.lang.String hostName,
                                   int port,
                                   java.lang.String user)
                            throws java.net.UnknownHostException
Sets SOCKS4 proxy as default.

Parameters:
hostName - Host name on which SOCKS4 server is running.
port - Port on which SOCKS4 server is running.
user - Username to use for communications with proxy.
Throws:
java.net.UnknownHostException

setDefaultProxy

public static void setDefaultProxy(java.net.InetAddress ipAddress,
                                   int port,
                                   java.lang.String user)
Sets SOCKS4 proxy as default.

Parameters:
ipAddress - Host address on which SOCKS4 server is running.
port - Port on which SOCKS4 server is running.
user - Username to use for communications with proxy.

setDefaultProxy

public static void setDefaultProxy(java.lang.String hostName,
                                   int port)
                            throws java.net.UnknownHostException
Sets SOCKS5 proxy as default. Default proxy only supports no-authentication.

Parameters:
hostName - Host name on which SOCKS5 server is running.
port - Port on which SOCKS5 server is running.
Throws:
java.net.UnknownHostException

setDefaultProxy

public static void setDefaultProxy(java.net.InetAddress ipAddress,
                                   int port)
Sets SOCKS5 proxy as default. Default proxy only supports no-authentication.

Parameters:
ipAddress - Host address on which SOCKS5 server is running.
port - Port on which SOCKS5 server is running.

setDefaultProxy

public static void setDefaultProxy(Proxy p)
Sets default proxy.

Parameters:
p - Proxy to use as default proxy.

getDefaultProxy

public static Proxy getDefaultProxy()
Get current default proxy.

Returns:
Current default proxy, or null if none is set.

parseProxy

public static Proxy parseProxy(java.lang.String proxy_entry)
Parses strings in the form: host[:port:user:password], and creates proxy from information obtained from parsing.

Defaults: port = 1080.
If user specified but not password, creates Socks4Proxy, if user not specified creates Socks5Proxy, if both user and password are speciefied creates Socks5Proxy with user/password authentication.

Parameters:
proxy_entry - String in the form host[:port:user:password]
Returns:
Proxy created from the string, null if entry was somehow invalid(host unknown for example, or empty string)

startSession

protected void startSession()
                     throws SocksException
Throws:
SocksException

copy

protected abstract Proxy copy()

formMessage

protected abstract ProxyMessage formMessage(int cmd,
                                            java.net.InetAddress ip,
                                            int port)

formMessage

protected abstract ProxyMessage formMessage(int cmd,
                                            java.lang.String host,
                                            int port)
                                     throws java.net.UnknownHostException
Throws:
java.net.UnknownHostException

formMessage

protected abstract ProxyMessage formMessage(java.io.InputStream in)
                                     throws SocksException,
                                            java.io.IOException
Throws:
SocksException
java.io.IOException

connect

protected ProxyMessage connect(java.net.InetAddress ip,
                               int port)
                        throws SocksException
Throws:
SocksException

connect

protected ProxyMessage connect(java.lang.String host,
                               int port)
                        throws java.net.UnknownHostException,
                               SocksException
Throws:
java.net.UnknownHostException
SocksException

bind

protected ProxyMessage bind(java.net.InetAddress ip,
                            int port)
                     throws SocksException
Throws:
SocksException

bind

protected ProxyMessage bind(java.lang.String host,
                            int port)
                     throws java.net.UnknownHostException,
                            SocksException
Throws:
java.net.UnknownHostException
SocksException

accept

protected ProxyMessage accept()
                       throws java.io.IOException,
                              SocksException
Throws:
java.io.IOException
SocksException

udpAssociate

protected ProxyMessage udpAssociate(java.net.InetAddress ip,
                                    int port)
                             throws SocksException
Throws:
SocksException

udpAssociate

protected ProxyMessage udpAssociate(java.lang.String host,
                                    int port)
                             throws java.net.UnknownHostException,
                                    SocksException
Throws:
java.net.UnknownHostException
SocksException

endSession

protected void endSession()

sendMsg

protected void sendMsg(ProxyMessage msg)
                throws SocksException,
                       java.io.IOException
Sends the request to SOCKS server

Throws:
SocksException
java.io.IOException

readMsg

protected ProxyMessage readMsg()
                        throws SocksException,
                               java.io.IOException
Reads the reply from the SOCKS server

Throws:
SocksException
java.io.IOException

exchange

protected ProxyMessage exchange(ProxyMessage request)
                         throws SocksException
Sends the request reads reply and returns it throws exception if something wrong with IO or the reply code is not zero

Throws:
SocksException


Copyright © 2012. All Rights Reserved.