Skip to content

Commit

Permalink
Merge pull request #172 from schoeffm/master
Browse files Browse the repository at this point in the history
Bind the Proxy to a specific network interface
  • Loading branch information
adamfisk committed Mar 9, 2015
2 parents 4c60fa8 + 7f08008 commit bf3fd76
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
Expand Up @@ -293,11 +293,18 @@ HttpProxyServerBootstrap withConnectTimeout(
*/
HttpProxyServerBootstrap withThrottling(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond);

/**
* All outgoing-communication of the proxy-instance is goin' to be routed via the given network-interface
*
* @param inetSocketAddress to be used for outgoing communication
*/
HttpProxyServerBootstrap withNetworkInterface(InetSocketAddress inetSocketAddress);

/**
* <p>
* Build and starts the server.
* </p>
*
*
* @return the newly built and started server
*/
HttpProxyServer start();
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/littleshoot/proxy/Launcher.java
@@ -1,8 +1,5 @@
package org.littleshoot.proxy;

import java.io.File;
import java.util.Arrays;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
Expand All @@ -11,14 +8,17 @@
import org.apache.commons.cli.PosixParser;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.littleshoot.proxy.extras.SelfSignedMitmManager;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.impl.ProxyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.Arrays;

/**
* Launches a new HTTP proxy.
*/
Expand All @@ -31,9 +31,11 @@ public class Launcher {
private static final String OPTION_PORT = "port";

private static final String OPTION_HELP = "help";

private static final String OPTION_MITM = "mitm";

private static final String OPTION_NIC = "nic";

/**
* Starts the proxy from the command line.
*
Expand All @@ -47,6 +49,7 @@ public static void main(final String... args) {
options.addOption(null, OPTION_DNSSEC, true,
"Request and verify DNSSEC signatures.");
options.addOption(null, OPTION_PORT, true, "Run on the specified port.");
options.addOption(null, OPTION_NIC, true, "Run on a specified Nic");
options.addOption(null, OPTION_HELP, false,
"Display command line help.");
options.addOption(null, OPTION_MITM, false, "Run as man in the middle.");
Expand Down Expand Up @@ -83,12 +86,18 @@ public static void main(final String... args) {
port = defaultPort;
}


System.out.println("About to start server on port: " + port);
HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer
.bootstrapFromFile("./littleproxy.properties")
.withPort(port)
.withAllowLocalOnly(false);


if (cmd.hasOption(OPTION_NIC)) {
final String val = cmd.getOptionValue(OPTION_NIC);
bootstrap.withNetworkInterface(new InetSocketAddress(val, 0));
}

if (cmd.hasOption(OPTION_MITM)) {
LOG.info("Running as Man in the Middle");
bootstrap.withManInTheMiddle(new SelfSignedMitmManager());
Expand Down Expand Up @@ -125,8 +134,7 @@ private static void printHelp(final Options options,
}

private static void pollLog4JConfigurationFileIfAvailable() {
File log4jConfigurationFile = new File(
"src/test/resources/log4j.xml");
File log4jConfigurationFile = new File("src/test/resources/log4j.xml");
if (log4jConfigurationFile.exists()) {
DOMConfigurator.configureAndWatch(
log4jConfigurationFile.getAbsolutePath(), 15);
Expand Down
Expand Up @@ -105,6 +105,7 @@ public class DefaultHttpProxyServer implements HttpProxyServer {
* The actual address to which the server is bound. May be different from the requestedAddress in some circumstances,
* for example when the requested port is 0.
*/
private volatile InetSocketAddress localAddress;
private volatile InetSocketAddress boundAddress;
private final SslEngineSource sslEngineSource;
private final boolean authenticateSslClients;
Expand Down Expand Up @@ -217,7 +218,8 @@ private DefaultHttpProxyServer(ServerGroup serverGroup,
int connectTimeout,
HostResolver serverResolver,
long readThrottleBytesPerSecond,
long writeThrottleBytesPerSecond) {
long writeThrottleBytesPerSecond,
InetSocketAddress localAddress) {
this.serverGroup = serverGroup;
this.transportProtocol = transportProtocol;
this.requestedAddress = requestedAddress;
Expand All @@ -240,6 +242,7 @@ private DefaultHttpProxyServer(ServerGroup serverGroup,
} else {
this.globalTrafficShapingHandler = null;
}
this.localAddress = localAddress;
}

/**
Expand Down Expand Up @@ -280,6 +283,10 @@ public HostResolver getServerResolver() {
return serverResolver;
}

public InetSocketAddress getLocalAddress() {
return localAddress;
}

@Override
public InetSocketAddress getListenAddress() {
return boundAddress;
Expand Down Expand Up @@ -322,7 +329,8 @@ public HttpProxyServerBootstrap clone() {
connectTimeout,
serverResolver,
globalTrafficShapingHandler != null ? globalTrafficShapingHandler.getReadLimit() : 0,
globalTrafficShapingHandler != null ? globalTrafficShapingHandler.getWriteLimit() : 0);
globalTrafficShapingHandler != null ? globalTrafficShapingHandler.getWriteLimit() : 0,
localAddress);
}

@Override
Expand Down Expand Up @@ -642,6 +650,7 @@ private static class DefaultHttpProxyServerBootstrap implements
private HostResolver serverResolver = new DefaultHostResolver();
private long readThrottleBytesPerSecond;
private long writeThrottleBytesPerSecond;
private InetSocketAddress localAddress;

private DefaultHttpProxyServerBootstrap() {
}
Expand All @@ -659,7 +668,9 @@ private DefaultHttpProxyServerBootstrap(
boolean transparent, int idleConnectionTimeout,
Collection<ActivityTracker> activityTrackers,
int connectTimeout, HostResolver serverResolver,
long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond) {
long readThrottleBytesPerSecond,
long writeThrottleBytesPerSecond,
InetSocketAddress localAddress) {
this.original = original;
this.transportProtocol = transportProtocol;
this.requestedAddress = requestedAddress;
Expand All @@ -678,6 +689,7 @@ private DefaultHttpProxyServerBootstrap(
this.serverResolver = serverResolver;
this.readThrottleBytesPerSecond = readThrottleBytesPerSecond;
this.writeThrottleBytesPerSecond = writeThrottleBytesPerSecond;
this.localAddress = localAddress;
}

private DefaultHttpProxyServerBootstrap(Properties props) {
Expand Down Expand Up @@ -717,6 +729,12 @@ public HttpProxyServerBootstrap withPort(int port) {
return this;
}

@Override
public HttpProxyServerBootstrap withNetworkInterface(InetSocketAddress inetSocketAddress) {
this.localAddress = inetSocketAddress;
return this;
}

@Override
public HttpProxyServerBootstrap withAllowLocalOnly(
boolean allowLocalOnly) {
Expand Down Expand Up @@ -854,7 +872,8 @@ transportProtocol, determineListenAddress(),
proxyAuthenticator, chainProxyManager, mitmManager,
filtersSource, transparent,
idleConnectionTimeout, activityTrackers, connectTimeout,
serverResolver, readThrottleBytesPerSecond, writeThrottleBytesPerSecond);
serverResolver, readThrottleBytesPerSecond, writeThrottleBytesPerSecond,
localAddress);
}

private InetSocketAddress determineListenAddress() {
Expand Down
Expand Up @@ -3,7 +3,6 @@
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ChannelFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelInitializer;
Expand Down Expand Up @@ -604,6 +603,7 @@ protected void initChannel(Channel ch) throws Exception {
proxyServer.getConnectTimeout());

if (localAddress != null) {
cb.bind(localAddress);
return cb.connect(remoteAddress, localAddress);
} else {
return cb.connect(remoteAddress);
Expand Down Expand Up @@ -753,7 +753,7 @@ private void setupConnectionParameters() throws UnknownHostException {
this.currentFilters.proxyToServerResolutionSucceeded(
serverHostAndPort, this.remoteAddress);

this.localAddress = null;
this.localAddress = proxyServer.getLocalAddress();
}
}

Expand Down

0 comments on commit bf3fd76

Please sign in to comment.