在Firefox中使用Java HTTP代理 [英] Using a Java HTTP proxy with Firefox

查看:80
本文介绍了在Firefox中使用Java HTTP代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个HTTP代理,该代理接受格式错误的HTTP请求,将其更正,将其转发到服务器,然后将服务器的响应转发回客户端.但是,我在测试它时遇到很多麻烦.

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.

我正在红帽服务器上的虚拟机(我具有root特权)上运行代理,该服务器要挂接到端口12345,并在端口12345上侦听.然后在我的计算机上,在Firefox中,我要转到选项">连接"设置>手动代理配置,然后输入虚拟机的IP地址和端口12345.

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.

但是,当我在虚拟机上运行代理并尝试转到Firefox中的站点时,什么也没发生.这是我的代码:

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();
    }
}

我现在不担心要修复HTTP请求,因此我已将toFromClient()中对requestNormalizer()的调用注释掉了.

I'm not worrying about fixing the HTTP request right now, so I have the call to requestNormalizer() in toFromClient() commented out.

我已经确定(通过打印语句)我的代码永远不会超过"client = welcome.accept();"在toFromClient()方法中处于空闲状态,直到我将其停止之前一直处于空闲状态,因此我猜测当我尝试连接到Firefox中的网页时,我的程序从未收到任何连接.我尝试使用不同于端口12345的端口,并且还能够通过使用Netcat侦听该端口,然后在另一个控制台窗口中连接到该端口,从而成功建立与VM上的端口12345的连接.使用Netcat.另外,我也无法从自己的计算机(我在Firefox上而不是VM上)通过telnet连接到VM.

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.

提前谢谢!

推荐答案

您是否尝试过使用安装在同一台计算机上的链接连接到此代理?它可以 帮助您找出何时有什么东西阻止了您的vm和Firefox之间的通信.

Have you tried to connect to this proxy using links installed on the same machine? It could help you to figure out whenever there is something, which blocks communication between your vm and firefox.

您可以通过执行yum install links来安装链接(或链接)(很抱歉,我现在还没有Red Hat).

You can install links(or elinks) by doing yum install links (sorry I do not have Red Hat near me right now).

如果您能够通过链接进行连接,请查看 http://www. techotopia.com/index.php/Basic_RHEL_6_Firewall_Configuration

In case if you are able to connect via links have a look at http://www.techotopia.com/index.php/Basic_RHEL_6_Firewall_Configuration

这篇关于在Firefox中使用Java HTTP代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆