Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

prateek.somaiya avatar image
prateek.somaiya asked ·

Connecting to a Cluster via SOCKS Proxy

With the old DSE Java Driver (1.8.x) I was able to connect to a cluster using Custom Netty Options like this

builder.withNettyOptions(new MyNettyOptions("127.0.0.1", 10080));
import java.net.InetSocketAddress;
import com.datastax.driver.core.NettyOptions;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.proxy.Socks5ProxyHandler;

public class MyNettyOptions extends NettyOptions {
	private String socksProxyHost;
	private Integer socksProxyPort;
	
	@Override
	public void afterChannelInitialized(SocketChannel channel) throws Exception {
        channel.pipeline().addFirst(new Socks5ProxyHandler(new InetSocketAddress(socksProxyHost, socksProxyPort)));
	}

	public MyNettyOptions(String socksProxyHost, Integer socksProxyPort) {
		super();
		this.socksProxyHost = socksProxyHost;
		this.socksProxyPort = socksProxyPort;
	}
}
builder.build();

How do I achieve the same in the new 2.2.0 driver (which uses the 4.2 Cassandra driver), reference https://docs.datastax.com/en/developer/java-driver/4.2/upgrade_guide/

Looks like I can only set the SocketOptions via advanced.socket in config, but how do I modify the channel pipeline now? If that's not possible, is there any way to connect via a SOCKS proxy or it is gone now?

java driver
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Any takers?

0 Likes 0 · ·
prateek.somaiya avatar image
prateek.somaiya answered ·

Got an answer from DSE Support, the feature is there, just not documented completely Here is how it can be done

public class MyNettyOptions extends DefaultNettyOptions {
	private String socksProxyHost;
	private Integer socksProxyPort;

	public MyNettyOptions(InternalDriverContext context, String socksProxyHost, Integer socksProxyPort) {
		super(context);
		this.socksProxyHost = socksProxyHost;
		this.socksProxyPort = socksProxyPort;
	}

	@Override
	public void afterChannelInitialized(Channel channel) {
		if (!StringUtils.isEmpty(socksProxyHost) && socksProxyPort != null && socksProxyPort > 0) {
			channel.pipeline().addFirst(new Socks5ProxyHandler(new InetSocketAddress(socksProxyHost, socksProxyPort)));
		}
	}
}
public class MyDseDriverContext extends DefaultDriverContext {
	public MyDseDriverContext(DriverConfigLoader configLoader, ProgrammaticArguments programmaticArguments) {
		super(configLoader, programmaticArguments);
	}

	@Override
	protected NettyOptions buildNettyOptions() {
		return new MyNettyOptions(this, "127.0.0.1", 10001);
	}
}
public class MyDseSessionBuilder extends SessionBuilder {

	@Override
	protected DriverContext buildContext(DriverConfigLoader configLoader, ProgrammaticArguments programmaticArguments) {
		return new MyDseDriverContext(configLoader, programmaticArguments);
	}

	@Override
	protected CqlSession wrap(@NonNull CqlSession defaultSession) {
		return defaultSession;
	}
}

And then build the session using MyDseSessionBuilder

new MyDseSessionBuilder().addContactPoint(new InetSocketAddress("x.x.x.x", 9042))
					.withLocalDatacenter("SearchGraphAnalytics").build()

Remember the netty JARs are shaded so you need to include netty JARs on your own to use the Socks5ProxyHandler, I used the netty-all version 4.1.39.Final

            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>${netty.version}</version>
            </dependency>

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered ·

@prateek.somaiya thanks for question. I'm aware that you logged a Support ticket for this and we worked internally to get you an answer.

For those coming across this post, configuring custom Netty options is still possible but it isn't readily obvious that you need to use the internal API. We are working on documenting this in the next release (internal ID JAVA-2428) but in the meantime, @olivier.michallat_30685 has provided a code sample preview in this Gist. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.