-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #833 from the-thing/proxy_testing
Mina proxy handshake fix and proxy integration tests
- Loading branch information
Showing
8 changed files
with
377 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
quickfixj-core/src/main/java/quickfix/mina/CustomSslFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package quickfix.mina; | ||
|
||
import org.apache.mina.core.filterchain.IoFilterChain; | ||
import org.apache.mina.core.session.IoSession; | ||
import org.apache.mina.filter.ssl.SslFilter; | ||
|
||
import javax.net.ssl.SSLContext; | ||
|
||
/** | ||
* Temporary {@link SslFilter} wrapper that prevents auto connect for initiators. | ||
*/ | ||
public class CustomSslFilter extends SslFilter { | ||
|
||
private static final boolean DEFAULT_AUTO_START = true; | ||
|
||
private final boolean autoStart; | ||
|
||
public CustomSslFilter(SSLContext sslContext) { | ||
this(sslContext, DEFAULT_AUTO_START); | ||
} | ||
|
||
public CustomSslFilter(SSLContext sslContext, boolean autoStart) { | ||
super(sslContext); | ||
this.autoStart = autoStart; | ||
} | ||
|
||
@Override | ||
public void onPostAdd(IoFilterChain parent, String name, NextFilter next) throws Exception { | ||
IoSession session = parent.getSession(); | ||
|
||
if (session.isConnected() && autoStart) { | ||
onConnected(next, session); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
quickfixj-core/src/test/java/quickfix/mina/SocksProxyServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package quickfix.mina; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.example.socksproxy.SocksServerInitializer; | ||
import io.netty.handler.logging.LogLevel; | ||
import io.netty.handler.logging.LoggingHandler; | ||
import org.apache.mina.util.DaemonThreadFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* Simple SOCKS proxy server based on Netty examples. Only SOCKS protocols are currently supported. | ||
* The implementation performs the proxy handshake, but it doesn't perform any user authentication. | ||
*/ | ||
public class SocksProxyServer { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SocksProxyServer.class); | ||
private static final ThreadFactory THREAD_FACTORY = new DaemonThreadFactory(); | ||
|
||
private final ServerBootstrap bootstrap; | ||
private final int port; | ||
private Channel channel; | ||
|
||
public SocksProxyServer(int port) { | ||
this.bootstrap = new ServerBootstrap(); | ||
this.bootstrap.group(new NioEventLoopGroup(THREAD_FACTORY), new NioEventLoopGroup(THREAD_FACTORY)) | ||
.channel(NioServerSocketChannel.class) | ||
.handler(new LoggingHandler(LogLevel.DEBUG)) | ||
.childHandler(new SocksServerInitializer()); | ||
this.port = port; | ||
} | ||
|
||
public synchronized void start() { | ||
if (channel != null) { | ||
throw new IllegalStateException("SOCKS proxy server is running already"); | ||
} | ||
|
||
try { | ||
channel = bootstrap.bind(port) | ||
.sync() | ||
.channel(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
LOGGER.info("SOCKS proxy server started at port: {}", port); | ||
} | ||
|
||
public synchronized void stop() { | ||
if (channel == null) { | ||
throw new IllegalStateException("SOCKS proxy server is not running"); | ||
} | ||
|
||
try { | ||
channel.close().sync(); | ||
channel = null; | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException("Failed to close SOCKS proxy server"); | ||
} | ||
|
||
LOGGER.info("SOCKS proxy server stopped at port {}", port); | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
} |
Oops, something went wrong.