Java HTTP / HTTPS代理服务器

时间:2019-11-27 15:08:03

标签: java sockets http proxy proxy-server

我正在编写自己的Java代理服务器。当我尝试使用它时,我得到了正确的输出(我看到通过代理服务器的所有命令),但是我要访问的任何网站都根本无法工作,并且我的浏览器中出现以下错误:ERR_TUNNEL_CONNECTION_FAILED 。我实际上不擅长网络问题,有些事情我不理解。因此,下面是我的代码,如果有人告诉我问题出在哪里,我将非常高兴:

import java.io.*;
import java.net.*;

public class ProxyServer
{
    private String host;
    private int localPort;
    private int remotePort;
    private ProxyServer(String host, int localPort, int remotePort)
    {
        this.host=host;
        this.localPort=localPort;
        this.remotePort=remotePort;
    }
    public static void main(String[] args) throws IOException
    {
        new ProxyServer("localhost", 8080, 9001).start();
    }
    public void start() throws IOException
    {
        System.out.println("Starting the proxy server for "+this.host+":"+this.localPort+"...");
        ServerSocket ss=new ServerSocket(this.localPort);
        final byte[] request=new byte[4096];
        byte[] response=new byte[4096];
        while(true)
        {
            Socket client=null;
            Socket server=null;
            try
            {
                client=ss.accept();
                final InputStream streamFromClient=client.getInputStream();
                OutputStream streamToClient=client.getOutputStream();
                System.out.println(new BufferedReader(new InputStreamReader(streamFromClient)).readLine());
                try
                {
                    server=new Socket(host, this.remotePort);
                }
                catch(IOException exc)
                {
                    PrintWriter out=new PrintWriter(streamToClient);
                    out.println("Proxy server cannot connect to "+host+":"+this.remotePort+"\n"+exc);
                    out.flush();
                    client.close();
                    continue;
                }
                InputStream streamFromServer=server.getInputStream();
                final OutputStream streamToServer=server.getOutputStream();
                System.out.println(new BufferedReader(new InputStreamReader(streamFromServer)).readLine());
                new Thread
                (
                    ()->
                    {
                        int bytesRead;
                        try
                        {
                            while((bytesRead=streamFromClient.read(request))!=-1)
                            {
                                streamToServer.write(request, 0, bytesRead);
                                streamToServer.flush();
                            }
                        }
                        catch(IOException exc){}
                        try
                        {
                            streamToServer.close();
                        }
                        catch(IOException exc){}
                    }
                ).start();
                int bytesRead;
                try
                {
                    while((bytesRead=streamFromServer.read(response))!=-1)
                    {
                        streamToClient.write(response, 0, bytesRead);
                        streamToClient.flush();
                    }
                }
                catch(IOException exc){}
                streamToClient.close();
            }
            catch(IOException exc)
            {
                System.out.println(exc.getMessage());
            }
            finally
            {
                try
                {
                    if(server!=null) server.close();
                    if(client!=null) client.close();
                }
                catch(IOException exc){}
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您使用预定义的硬编码目标制作了简单的tcp代理:host:remote_port

它不是HTTP代理。

因此,您的解决方案应该可以在以下情况下起作用:

  1. 您正在运行squid或某些适当的HTTP代理 本地主机:9001。
  2. 并将其仅用作HTTP代理(我的意思是当 您配置浏览器时,您不应该告诉它您的应用程序是https 代理)。

如果您打算制作独立代理,则需要分析HTTP请求并从请求中获取主机,端口和协议,以了解您应该连接哪个服务器(包括端口)以及应该使用哪种协议(即http或https)。 当浏览器直接请求服务(不使用代理)时,它仅发送URI;当浏览器通过代理发送请求时,它将在请求中使用完整URL。

因此请求中的第一行将是这样的:

GET http://google.com[:80]/blah-blah-blah HTTP/1.1
...headers here...

您的代理需要分析URL并从中获取主机,端口和URI。 因此,您将需要解析域名并使用修改后的第一行将主机连接到所需的端口。看起来像:

GET /blah-blah-blah HTTP/1.1
...headers here...

如果您使用额外的HTTP代理服务器,该服务器在9001端口上进行监听,请检查其是否处于运行状态。