使用 Java 编程代理服务器 - 如何?

发布于 2024-11-02 18:36:33 字数 1225 浏览 0 评论 0原文

我有一个任务是用java编写一个代理服务器。我必须能够读取和修改 http 标头并阻止提供的黑名单上的某些网站。

我有使用java的经验,但从未使用过http、套接字、连接、端口等。

已经有几天尝试谷歌一些示例或教程,但我发现要么非常简单,没有完整的功能,要么非常非常复杂或不起作用。

你能帮我提供一些相关的示例、链接、教程等吗?

我还必须注意到,我发现了一个带有开源代理服务器的网页,但它们并没有真正工作或非常复杂。

谢谢!

编辑:

嗨,我发现一些代码可以侦听来自浏览器的连接并为每个连接启动一个新线程。

public class Main {
public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
boolean listening = true;

int port = 10000;    //default
try {
    port = Integer.parseInt(args[0]);
} catch (Exception e) {
    //ignore me
}

try {
    serverSocket = new ServerSocket(port);
    System.out.println("Started on: " + port);
} catch (IOException e) {
    System.err.println("Could not listen on port: " + args[0]);
    System.exit(-1);
}

while (listening) {
    new ProxyThreadServer(serverSocket.accept()).start();
}
serverSocket.close();
}

}

我真正困惑的部分是如何将请求传输到服务器(URL),从中获取响应并将响应发送到浏览器。

所以基本上我需要 4 个步骤:

Listen and get the request from a browser.
Forward the request to the web-server.
Get the response from the web server.
Send the response to the browser.

额外的功能是处理标头并阻止一些连接。但对于开始来说,这 4 个步骤就足够了。

I have an assignment to write a proxy server with java. I must be able to read and modify http headers and block some sites on a provided black-list.

I have an experience with java, but never worked with http, sockets, connections, ports etc..

Already couple of days trying Google some examples or tutorials, but what I find is either very simple and don't have full capabilities or very very complicated or not working.

Can you help me with some relevant examples, links, tutorials etc...?

I have also to notice, that I found a webpage with an open source proxy servers, but they don't really work or very complicated.

thanks!

Edit:

Hi, I found some code that listens to connections coming from a browser and starts a new thread for every connection.

public class Main {
public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
boolean listening = true;

int port = 10000;    //default
try {
    port = Integer.parseInt(args[0]);
} catch (Exception e) {
    //ignore me
}

try {
    serverSocket = new ServerSocket(port);
    System.out.println("Started on: " + port);
} catch (IOException e) {
    System.err.println("Could not listen on port: " + args[0]);
    System.exit(-1);
}

while (listening) {
    new ProxyThreadServer(serverSocket.accept()).start();
}
serverSocket.close();
}

}

The part that I'm really confused with is how to transfer the request to a server(URL) , get a response from it and send the response to the browser.

So basically I need a 4 steps:

Listen and get the request from a browser.
Forward the request to the web-server.
Get the response from the web server.
Send the response to the browser.

The extra features is working with the headers and block some connections. But for the start these 4 steps will do fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-11-09 18:36:33

最简单的方法是让线程在 ServerSocket 上接受新连接。对于它获得的每个连接(通过accept调用),启动一个新线程来处理该连接。将通过 accept 调用收到的新 Socket 传递给新的会话处理线程。这就是代理服务器的核心。

对于每个会话处理线程,您需要读取来自客户端的 HTTP 请求并决定如何处理它。您需要确定要联系哪个 HTTP 服务器,然后使用您创建的新 Socket 对象连接到它。此时您可以在客户端和服务器之间进行双向转发,因此您的代理对任一端都是透明的。

HTTP 是一个复杂的野兽,所以希望您的作业范围相当有限,在这种情况下,这个大纲应该可以帮助您入门。

希望有帮助!

The easiest way is to have a thread accepting new connections on a ServerSocket. For each connection that it gets (via the accept call), start off a new thread to handle that connection. Pass the new Socket that you receive via the accept call to the new session handling thread. That is the core of the proxy server.

For each of these session handling threads, you need to read the HTTP request from the client and decide what to do with it. You need to determine which HTTP server you are going to contact, then connect to it with a new Socket object that you create. You can do two-way forwarding between the client and server at that point so your proxy is transparent to either end.

HTTP is a complicated beast so hopefully your assignment is pretty limited in scope, in which case this outline should help you get started.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文