Skip to content

Commit

Permalink
authenticated proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Nov 1, 2024
1 parent 197f2fe commit fb0d864
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 114 deletions.
144 changes: 31 additions & 113 deletions unirest-bdd-tests/src/test/java/BehaviorTests/JankyProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

package BehaviorTests;

import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.UnauthorizedResponse;
import kong.unirest.core.Unirest;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.net.*;
/**
Expand All @@ -33,127 +39,39 @@
*
*/
public class JankyProxy {
public static ThreadProxy thread;
private static int remoteport;
private static boolean wasused;

public static void runServer(String host, int remoteport, int localport) {
try {
// and the local port that we listen for connections on
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" + remoteport
+ " on port " + localport);
ServerSocket server = new ServerSocket(localport);
thread = new ThreadProxy(server, host, remoteport);
} catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java ProxyMultiThread "
+ "<host> <remoteport> <localport>");
}
}
JankyProxy.remoteport = remoteport;
var server = Javalin.create(c -> {

}).start(host, localport);

server.before("/*", JankyProxy::before);
server.get("/*", JankyProxy::execute);

public static boolean wasUsed(){
return thread.wasUsedForClientToServer;
}

public static void shutdown(){
if(thread != null){
try {
thread.shutdown();
}catch (Exception e){
e.printStackTrace();
}
private static void before(@NotNull Context it) {
if (it.basicAuthCredentials() == null) {
it.header("WWW-Authenticate", "Basic realm=\"User Visible Realm\", charset=\"UTF-8\"");
it.status(401);
it.result("Login required");
throw new UnauthorizedResponse();
}
}
}
/**
* Handles a socket connection to the proxy server from the client and uses 2
* threads to proxy between server and client
*
* @author jcgonzalez.com
*
*/
class ThreadProxy extends Thread {
private Socket sClient;
private ServerSocket socket;
private final String SERVER_URL;
private final int SERVER_PORT;
public boolean wasUsedForClientToServer;

ThreadProxy(ServerSocket socket, String ServerUrl, int ServerPort) {
this.SERVER_URL = ServerUrl;
this.SERVER_PORT = ServerPort;
this.socket = socket;
this.start();
private static void execute(@NotNull Context context) {
wasused = true;
context.status(200);
context.result("Im a proxy");
}
@Override
public void run() {
try {
sClient = socket.accept();
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
final InputStream inFromClient = sClient.getInputStream();
final OutputStream outToClient = sClient.getOutputStream();
Socket client = null, server = null;
// connects a socket to the server
try {
server = new Socket(SERVER_URL, SERVER_PORT);
} catch (IOException e) {
PrintWriter out = new PrintWriter(new OutputStreamWriter(outToClient));
out.flush();
System.out.println("SERVER_URL = " + SERVER_URL + " : " + SERVER_PORT);
throw new RuntimeException(e);
}
// a new thread to manage streams from server to client (DOWNLOAD)
final InputStream inFromServer = server.getInputStream();
final OutputStream outToServer = server.getOutputStream();
// a new thread for uploading to the server
new Thread(() -> {
int bytes_read;
try {
while ((bytes_read = inFromClient.read(request)) != -1) {
outToServer.write(request, 0, bytes_read);
outToServer.flush();
wasUsedForClientToServer = true;
}
} catch (IOException ignored) {
}
try {
outToServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// current thread manages streams from server to client (DOWNLOAD)
int bytes_read;
try {
while ((bytes_read = inFromServer.read(reply)) != -1) {
outToClient.write(reply, 0, bytes_read);
outToClient.flush();
//TODO CREATE YOUR LOGIC HERE
}
} catch (IOException e) {
// e.printStackTrace();
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
outToClient.close();
sClient.close();
} catch (IOException e) {
e.printStackTrace();
}

public static boolean wasUsed(){
return wasused;
}

public void shutdown() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
interrupt();
public static void shutdown(){
}
}
}
24 changes: 23 additions & 1 deletion unirest-bdd-tests/src/test/java/BehaviorTests/ProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled // The Janky Proxy is pretty janky and isn't entirely stable in CI
//@Disabled // The Janky Proxy is pretty janky and isn't entirely stable in CI
class ProxyTest extends BddTest {

@AfterEach
Expand All @@ -58,6 +58,28 @@ void canUseNonAuthProxy() {
assertTrue(JankyProxy.wasUsed());
}

@Test
void canUseAuthProxy() {
JankyProxy.runServer("localhost", 4567, 7777);

// System.setProperty("http.proxyUser", "ftproxy");
// System.setProperty("http.proxyPassword", "**************");
// System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

Unirest.config().proxy(new Proxy("localhost", 7777, "ftproxy", "**************"));




Unirest.get(MockServer.GET)
.header("Proxy-Authentication", "fuck")
.asObject(RequestCapture.class)
.getBody()
.assertStatus(200);

assertTrue(JankyProxy.wasUsed());
}

@Test
void canUseNonAuthProxyWithEasyMethod() {
JankyProxy.runServer("localhost", 4567, 7777);
Expand Down

0 comments on commit fb0d864

Please sign in to comment.