Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use resolveAddrLocally in a chained ProxyServer #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.ravn.jsocks</groupId>
<artifactId>jsocks</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<name>SOCKS proxy</name>
<description>Cloned from http://sourceforge.net/projects/jsocks/</description>

Expand Down Expand Up @@ -50,4 +50,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
15 changes: 12 additions & 3 deletions src/com/runjva/sourceforge/jsocks/protocol/ProxyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ private void handleRequest(final ProxyMessage msg) throws IOException {

if (msg.ip == null) {
if (msg instanceof Socks5Message) {
msg.ip = InetAddress.getByName(msg.host);

if (getProxy() == null || (getProxy() != null && getProxy().resolveAddrLocally()))
msg.ip = InetAddress.getByName(msg.host);
} else {
throw new SocksException(SocksProxyBase.SOCKS_FAILURE);
}
Expand Down Expand Up @@ -345,7 +347,10 @@ private void onConnect(final ProxyMessage msg) throws IOException {
if (proxy == null) {
s = new Socket(msg.ip, msg.port);
} else {
s = new SocksSocket(proxy, msg.ip, msg.port);
if (proxy.resolveAddrLocally())
s = new SocksSocket(proxy, msg.ip, msg.port);
else
s = new SocksSocket(proxy, msg.host, msg.port);
}

log.info("Connected to " + s.getInetAddress() + ":" + s.getPort());
Expand All @@ -372,7 +377,11 @@ private void onBind(final ProxyMessage msg) throws IOException {
if (proxy == null) {
ss = new ServerSocket(0);
} else {
ss = new SocksServerSocket(proxy, msg.ip, msg.port);
if (proxy.resolveAddrLocally())
ss = new SocksServerSocket(proxy, msg.ip, msg.port);
else
ss = new SocksServerSocket(proxy, msg.host, msg.port);

}

ss.setSoTimeout(acceptTimeout);
Expand Down
27 changes: 0 additions & 27 deletions src/com/runjva/sourceforge/jsocks/protocol/Socks5Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class Socks5Proxy extends SocksProxyBase implements Cloneable {
private Hashtable<Integer, Authentication> authMethods = new Hashtable<Integer, Authentication>();
private int selectedMethod;

boolean resolveAddrLocally = true;
UDPEncapsulation udp_encapsulation = null;

// Public Constructors
Expand Down Expand Up @@ -92,32 +91,6 @@ public Socks5Proxy(InetAddress proxyIP, int proxyPort) {
// Public instance methods
// ========================

/**
* Wether to resolve address locally or to let proxy do so.
* <p>
* SOCKS5 protocol allows to send host names rather then IPs in the
* requests, this option controls wether the hostnames should be send to the
* proxy server as names, or should they be resolved locally.
*
* @param doResolve
* Wether to perform resolution locally.
* @return Previous settings.
*/
public boolean resolveAddrLocally(boolean doResolve) {
final boolean old = resolveAddrLocally;
resolveAddrLocally = doResolve;
return old;
}

/**
* Get current setting on how the addresses should be handled.
*
* @return Current setting for address resolution.
* @see Socks5Proxy#resolveAddrLocally(boolean doResolve)
*/
public boolean resolveAddrLocally() {
return resolveAddrLocally;
}

/**
* Adds another authentication method.
Expand Down
28 changes: 28 additions & 0 deletions src/com/runjva/sourceforge/jsocks/protocol/SocksProxyBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class SocksProxyBase {
// Protected static/class variables
protected static SocksProxyBase defaultProxy = null;

boolean resolveAddrLocally = true;
// Constructors
// ====================
SocksProxyBase(SocksProxyBase chainProxy, String proxyHost, int proxyPort)
Expand Down Expand Up @@ -159,6 +160,33 @@ public boolean isDirect(InetAddress host) {
return directHosts.contains(host);
}

/**
* Wether to resolve address locally or to let proxy do so.
* <p>
* SOCKS5 protocol allows to send host names rather then IPs in the
* requests, this option controls wether the hostnames should be send to the
* proxy server as names, or should they be resolved locally.
*
* @param doResolve
* Wether to perform resolution locally.
* @return Previous settings.
*/
public boolean resolveAddrLocally(boolean doResolve) {
final boolean old = resolveAddrLocally;
resolveAddrLocally = doResolve;
return old;
}

/**
* Get current setting on how the addresses should be handled.
*
* @return Current setting for address resolution.
* @see Socks5Proxy#resolveAddrLocally(boolean doResolve)
*/
public boolean resolveAddrLocally() {
return resolveAddrLocally;
}

/**
* Set the proxy which should be used to connect to given proxy.
*
Expand Down