I am writing an HTTP proxy in Java which takes malformed HTTP request, corrects them, forwards them to a server, and then forwards the server's response back to the client. However, I'm having a lot of trouble testing it.
I am running the proxy on a virtual machine (of which I have root privilages) on a Red Hat server that I am sshing into and listening at port 12345. Then on my machine, in Firefox I am going to Options>Connection Settings>Manual Proxy Configuration and entering the IP address for the virtual machine and port 12345.
However, when I run the proxy on my virtual machine and try to go to a site in Firefox nothing happens. Here is my code:
package proxy;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class WebProxy {
public void run() {
try {
toFromClient();
}
catch (IOException e) {
e.printStackTrace();
}
}
public String requestNormalizer(String badRequest) {
return badRequest.replace(badRequest.substring(0, badRequest.indexOf(" ")), badRequest.substring(0, badRequest.indexOf(" ")).toUpperCase());
}
public void toFromClient() throws IOException {
ServerSocket welcome;
Socket client;
String request;
String response;
DataOutputStream outputToClient;
BufferedReader inputFromClient;
boolean listening = true;
welcome = new ServerSocket(12345);
while (listening) {
client = welcome.accept();
inputFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
request = inputFromClient.readLine();
// request = requestNormalizer(request);
response = toFromServer(request);
outputToClient = new DataOutputStream(client.getOutputStream());
outputToClient.writeBytes(response);
}
}
public String toFromServer(String request) throws UnknownHostException, IOException {
Socket server;
String response;
DataOutputStream outputToServer;
BufferedReader inputFromServer;
if (!dnsQuery(request)[1].equals(""))
server = new Socket((InetAddress) dnsQuery(request)[0], (int) dnsQuery(request)[1]);
else
server = new Socket((InetAddress) dnsQuery(request)[0], 80);
outputToServer = new DataOutputStream(server.getOutputStream());
inputFromServer = new BufferedReader(new InputStreamReader(server.getInputStream()));
outputToServer.writeBytes(request);
response = inputFromServer.readLine();
server.close();
return response;
}
public Object[] dnsQuery(String request) throws UnknownHostException {
Object[] addressPort = new Object[2];
String hostname = request.substring(request.indexOf("host") + 6);
hostname = hostname.substring(0, hostname.indexOf("\r"));
if (hostname.contains(":")) {
hostname = hostname.substring(0, hostname.indexOf(":"));
addressPort[1] = hostname.substring(hostname.indexOf(":"));
}
else
addressPort[1] = "";
addressPort[0] = InetAddress.getByName(hostname);
return addressPort;
}
public static void main(String[] args) {
WebProxy wp = new WebProxy();
wp.run();
}
}
I'm not worrying about fixing the HTTP request right now, so I have the call to requestNormalizer() in toFromClient() commented out.
I've determined (through print statments) that my code never gets past "client = welcome.accept();" in the toFromClient() method and idles until I stop it, so I'm guessing that my program is never receiving any connection when I try to connect to a webpage in Firefox. I've tried ports different than port 12345, and I've also been able to successfully set up a connection to port 12345 on my VM by listening on that port using Netcat, and then connecting to that port in a different console window, also using Netcat. Also I am unable to telnet to the VM from my own machine (the one I am using Firefox on, not the VM).
Does anyone have any idea what the issue could be? Is it the code, or is it some other issue like a firewall, etc.? Please let me know I have have failed to sufficiently clarify something or if anyone needs any additional information.
Thanks in advance!