Linux 如何通过代理将套接字连接到 http 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9191860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect a socket to an http server through proxy?
提问by Akhil Thayyil
Recently I wrote a program using sockets in C, to connect to an HTTP server running locally and thereby to do requests to that.
最近我用 C 语言编写了一个使用套接字的程序,以连接到本地运行的 HTTP 服务器,从而对其进行请求。
That worked fine for me. After that I tried the same code to connect to another server on the web (e.g. www.google.com), but I was not able to connect and was getting another html response from the proxy server in my network.
这对我来说很好。之后,我尝试使用相同的代码连接到网络上的另一台服务器(例如 www.google.com),但我无法连接并且从网络中的代理服务器获得另一个 html 响应。
- My local IP is: 10.0.2.58
- The proxy IP is: 10.0.0.1
- 我的本地IP是:10.0.2.58
- 代理IP为:10.0.0.1
This is the response I got :
这是我得到的回应:
HTTP/1.1 302 Found
Expires: Fri, 10 Feb 2012 12:47:35 GMT
Expires: 0
Cache-Control: max-age=180000
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Connection: close
Location: http://10.0.0.1:8000/index.php?redirurl=http%3A%2F%2F10.0.2.58%2F
Content-type: text/html
Content-Length: 0
Date: Wed, 08 Feb 2012 10:47:35 GMT
Server: lighttpd/1.4.29
How can I bypass this proxy to connect to external servers?
如何绕过此代理连接到外部服务器?
Response got when tried with CONNECT
尝试使用 CONNECT 时得到响应
HTTP/1.1 302 Found
Expires: Fri, 10 Feb 2012 13:37:58 GMT
Expires: 0
Cache-Control: max-age=180000
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Connection: close
Location: http://10.0.0.1:8000/index.php?redirurl=http%3A%2F%2F10.0.2.58http%3A%2F%2Fwww.google.com%2F
Content-type: text/html
Content-Length: 0
Date: Wed, 08 Feb 2012 11:37:58 GMT
Server: lighttpd/1.4.29
Working code which connect's to my local apache
连接到我本地 apache 的工作代码
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>
#define MAX_BUFFER_SIZE 1024
int main(int argc,char *argv[])
{
int clsd,ssd,status;
char buffer[1024];
char request[]="GET / HTTP/1.1\r\nHost:10.0.2.58\r\n\r\n";
struct sockaddr_in srvr_addr;
struct addrinfo hints,*res;
srvr_addr.sin_family=AF_INET;
srvr_addr.sin_port=htons(80);
srvr_addr.sin_addr.s_addr=inet_addr("10.0.2.58");//Local server
clsd =socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(clsd<=0)
{
perror("Socket init failed..\n");return 1;
}
ssd=connect(clsd,(struct sockaddr *)&srvr_addr,(socklen_t)(sizeof srvr_addr));
if(clsd<=0)
{
perror("Socket connect failed..\n");return 1;
}
write(clsd,request,strlen(request));
memset((void *)&request,0x00,strlen(request));
memset(&buffer,0x00,MAX_BUFFER_SIZE);
do
{
status=read(clsd,&buffer,MAX_BUFFER_SIZE);
write(1,&buffer,status);
memset((void *)&request,0x00,strlen(request));
memset(&buffer,0x00,MAX_BUFFER_SIZE);
do
{
status=read(clsd,&buffer,MAX_BUFFER_SIZE);
write(1,&buffer,status);
}while(status>0);
close(clsd);
return 0;
}
采纳答案by dennis
To use connections via proxy (or if they are implicitly proxy-fied), first you should connect to proxy, send a 'CONNECT' message with target host; proxy will establish connection and return you data.
要通过代理使用连接(或者如果它们是隐式代理),首先您应该连接到代理,向目标主机发送“CONNECT”消息;代理将建立连接并返回您的数据。
Here is in steps:
下面是步骤:
- open socket to proxy host
- send 'CONNECT http://www.google.com:80HTTP/1.0\r\n\r\n' string
- wait for recv
- 打开套接字到代理主机
- 发送 ' CONNECT http://www.google.com:80HTTP/1.0\r\n\r\n' 字符串
- 等待接收
You must specify protocol (in our case is HTTP 1.0, non-chunked) with ending newline characters, so proxy knows how to communicate with end point.
您必须指定带有结束换行符的协议(在我们的例子中是 HTTP 1.0,非分块),因此代理知道如何与端点通信。
You can find details about CONNECT method at http://www.ietf.org/rfc/rfc2817.txt
您可以在http://www.ietf.org/rfc/rfc2817.txt找到有关 CONNECT 方法的详细信息
回答by nickgrim
If you're specifically trying to bypassthe proxy, you should talk to whoever administers your network to find out if that's even possible. If your first block of output is an attempt to connect to Google then it appears to me that there's some kind of transparent proxy on your network that you'll have to take special (and network-specific) steps to bypass.
如果您特别想绕过代理,您应该与管理您的网络的人交谈,以了解这是否可能。如果您的第一个输出块是尝试连接到 Google,那么在我看来,您的网络上存在某种透明代理,您必须采取特殊的(和特定于网络的)步骤来绕过。
Of course, if you're just interested in getting data, you could try following the redirect...
当然,如果您只是对获取数据感兴趣,可以尝试按照重定向...