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

Static username and password authentication added as a launcher option #286

Open
wants to merge 1 commit 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
38 changes: 30 additions & 8 deletions src/main/java/org/littleshoot/proxy/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.littleshoot.proxy;

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

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
Expand All @@ -12,20 +16,18 @@
import org.littleshoot.proxy.extras.SelfSignedMitmManager;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.impl.ProxyUtils;
import org.littleshoot.proxy.impl.StaticProxyAuthenticator;
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.
*/
public class Launcher {

private static final Logger LOG = LoggerFactory.getLogger(Launcher.class);

private static final String DEFAULT_REALM = "LittleProxy";

private static final String OPTION_DNSSEC = "dnssec";

private static final String OPTION_PORT = "port";
Expand All @@ -36,9 +38,15 @@ public class Launcher {

private static final String OPTION_NIC = "nic";

private static final String OPTION_REALM = "realm";

private static final String OPTION_USERNAME = "username";

private static final String OPTION_PASSWORD = "password";

/**
* Starts the proxy from the command line.
*
*
* @param args
* Any command line arguments.
*/
Expand All @@ -53,7 +61,13 @@ public static void main(final String... args) {
options.addOption(null, OPTION_HELP, false,
"Display command line help.");
options.addOption(null, OPTION_MITM, false, "Run as man in the middle.");

options.addOption(null, OPTION_USERNAME, true,
"Use a static authenticator with the speficied username.");
options.addOption(null, OPTION_PASSWORD, true,
"If " + OPTION_USERNAME + " is set then use this password, otherwise use a blank password.");
options.addOption(null, OPTION_REALM, true,
"If " + OPTION_USERNAME + " is set then use this realm, otherwise use '" + DEFAULT_REALM + "'.");

final CommandLineParser parser = new PosixParser();
final CommandLine cmd;
try {
Expand Down Expand Up @@ -98,11 +112,19 @@ public static void main(final String... args) {
bootstrap.withNetworkInterface(new InetSocketAddress(val, 0));
}

if (cmd.hasOption(OPTION_USERNAME)) {
LOG.info("Running with static authenticator");
final String userName = cmd.getOptionValue(OPTION_USERNAME);
final String password = cmd.hasOption(OPTION_PASSWORD) ? cmd.getOptionValue(OPTION_PASSWORD) : "";
final String realm = cmd.hasOption(OPTION_REALM) ? cmd.getOptionValue(OPTION_REALM) : DEFAULT_REALM;
bootstrap.withProxyAuthenticator(new StaticProxyAuthenticator(userName, password, realm));
}

if (cmd.hasOption(OPTION_MITM)) {
LOG.info("Running as Man in the Middle");
bootstrap.withManInTheMiddle(new SelfSignedMitmManager());
}

if (cmd.hasOption(OPTION_DNSSEC)) {
final String val = cmd.getOptionValue(OPTION_DNSSEC);
if (ProxyUtils.isTrue(val)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.littleshoot.proxy.impl;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this package.


import org.littleshoot.proxy.ProxyAuthenticator;

/**
* Basic authenticator with static username and password
*/
public class StaticProxyAuthenticator implements ProxyAuthenticator {
private final String userName;

private final String password;

private final String realm;

public StaticProxyAuthenticator(String userName, String password, String realm) {
super();
this.userName = userName;
this.password = password;
this.realm = realm;
}

@Override
public boolean authenticate(String userName, String password) {
return this.userName.equals(userName) && this.password.equals(password);
}

@Override
public String getRealm() {
return realm;
}
}