gpt4 book ai didi

How can I create a forward proxy that runs on a server with no exposed ports? [closed](如何创建在没有公开端口的服务器上运行的转发代理?[已关闭])

转载 作者:bug小助手 更新时间:2023-10-25 21:12:09 24 4
gpt4 key购买 nike




I am trying to build a web scraping utility that allows anyone to run a program and have their computer act as a forward proxy, mainly to get around IP blocking. The high level overview of this would work as follows:

我正在尝试建立一个网络抓取实用程序,允许任何人运行一个程序,并让他们的计算机充当转发代理,主要是为了绕过IP阻止。这方面的高级别概览的工作方式如下:


Requester --sends http request--> Centralized server --distributes request to random client--> 
Client --sends request to target server--> Target server --responds back down to requester-->
Client --> Centralized server -> Requester

The tricky part of this is allowing the client to act as a forward proxy, without actually exposing any ports (as I want this to have no tricky networking setup, e.g. port forwarding). Additionally, I believe it makes sense for the Centralized server to look like a typical forward proxy to to the Requester, so it could work with the following code:

其中棘手的部分是允许客户端充当转发代理,而不实际暴露任何端口(因为我希望这没有棘手的网络设置,例如端口转发)。此外,我认为集中式服务器看起来像一个典型的转发代理到转发器是有意义的,所以它可以使用以下代码:


func main() {
proxyURL, err := url.Parse("http://localhost:8080")
if err != nil {
panic(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
response, err := httpClient.Get("https://example.com/")
if err != nil {
panic(err)
}
log.Println("Response status:", response.Status)
}

My original design involved having each client open a websocket connection to the server, thus removing the need for them to have exposed ports. Then, raw HTTP requests would be sent to the client, who would relay the request/response to/from the target server. However, I've been having alot of trouble getting it working properly. Here is my attempt:

我最初的设计包括让每个客户端打开到服务器的WebSocket连接,从而消除了它们具有公开端口的需要。然后,原始的HTTP请求将被发送到客户端,客户端将向目标服务器转发请求/响应,或从目标服务器转发请求/响应。然而,我在让它正常工作方面遇到了很多麻烦。以下是我的尝试:


// I don't know what the implementation of this would be.
type WSClient interface {
io.Reader
io.Writer
io.Closer
}

func main() {
var wsConn WSClient
proxy := func(w http.ResponseWriter, req *http.Request) {
client, _, err := w.(http.Hijacker).Hijack()
if err != nil {
w.WriteHeader(502)
fmt.Fprint(w, err)
return
}
client.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))

go func() {
_, err = io.Copy(wsConn, client)
if err != nil {
fmt.Println(err)
return
}
client.Close()
}()
go func() {
_, err := io.Copy(client, wsConn)
if err != nil {
fmt.Println(err)
return
}
wsConn.Close()
}()
}
http.ListenAndServe(":8080", http.HandlerFunc(proxy))
}

I don't really know where to go from here, nor does this feel like the right way to accomplish my goal of a super easy-to-setup forward proxy. Would anyone be able to point me in the right direction, or give me some code samples to help me get closer to my desired functionality?

我真的不知道从哪里开始,这也不是实现我的目标--一个超级易于设置的转发代理--的正确方式。有没有人能为我指明正确的方向,或者给我一些代码样例来帮助我更接近我想要的功能?


Edit: Some people have mentioned that without opening ports, I cannot receive any requests to forward. I am hoping to send the requests via a web socket opened by the client, thus bypassing this restriction.

编辑:有人提到,如果不开放端口,我就无法收到任何转发请求。我希望通过客户端打开的Web套接字发送请求,从而绕过这一限制。


更多回答

If the client can't expose any ports, it can't receive any requests to forward. Your question embodies a contradiction in terms.

如果客户端不能公开任何端口,则它不能接收任何转发请求。你的问题体现了术语上的矛盾。

优秀答案推荐

Unless the proxies request originate locally (and I mean internally vs. over any network connection, even from the loopback), you are going to have listeners, which by your definition, I think would be "exposed".

除非代理请求源自本地(我指的是内部与通过任何网络连接,甚至来自环回),否则您将拥有监听程序,根据您的定义,我认为它将被“暴露”。


I think you mean "exposed" ports as in it listens for inbound connections, the classic approach is to have have the proxy server setup on as a bastion, with one network interface on the internet and one on the intranet.

我想你的意思是“暴露的”端口,因为它监听入站连接,经典的方法是将代理服务器设置为堡垒,一个网络接口在互联网上,一个在内部网上。


Your proxy listens on the intranet, and only makes outbound connections on the internet.

您的代理监听内部网,并且只在Internet上建立出站连接。


By definition, a web proxy (forward or reverse) has to listen for a request.

根据定义,Web代理(正向或反向)必须侦听请求。


更多回答

So is there no way I can open a connection, thus ‘exposing’ a process without exposing any ports? I was hoping I could solve this by using a web socket or something

那么,有没有办法打开一个连接,从而在不公开任何端口的情况下“公开”一个进程呢?我希望我能通过使用网络插座或别的什么来解决这个问题。

Exposed could mean different things, so this comes down to what you are comfortable with (or did someone else give you this criteria?). For example, in a corporate network, if you had DHCP "fixed" assignment (IT gave your desktop the same IP address all the time, and nobody else...) then you could have the proxy server also have a firewall that allowed only connections from your desktop. To me, that wouldn't be "exposed", because the system wouldn't let anyone else connect to the port listener.

暴露可能意味着不同的事情,所以这归结为你对什么感到舒服(或者是其他人给了你这个标准?)例如,在公司网络中,如果您有固定的DHCP分配(它始终为您的桌面分配相同的IP地址,而不是其他任何人...)然后,您可以让代理服务器也有一个防火墙,只允许来自您的桌面的连接。对我来说,这不会“暴露”,因为系统不允许其他任何人连接到端口侦听器。

If by web socket, you mean you could also control the desktop's opening port, then you could even have the firewall be: only from this port-from this ip address. That would mean even if someone was casually messing around on your desktop browser, they would still be rejected (unless they got lucky).

如果你所说的网络插座,你的意思是你也可以控制桌面的开放端口,那么你甚至可以让防火墙:只能从这个端口-从这个IP地址。这将意味着,即使有人在你的桌面浏览器上随意捣乱,他们仍然会被拒绝(除非他们很幸运)。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com