c++ - 如何通过代理将套接字连接到http服务器?

标签 c++ c linux proxy network-programming

最近我用 C 编写了一个使用套接字的程序,以连接到本地运行的 HTTP 服务器,从而向其发出请求。

这对我来说效果很好。之后,我尝试使用相同的代码连接到网络上的另一台服务器(例如 www.google.com),但我无法连接,并且从我网络中的代理服务器获得了另一个 html 响应。

  • 我的本地IP是:10.0.2.58
  • 代理IP为:10.0.0.1

这是我得到的回复:

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

如何绕过此代理连接到外部服务器?


尝试使用 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

连接到我本地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;
}

最佳答案

要通过代理使用连接(或者如果它们是隐式代理),首先您应该连接到代理,向目标主机发送“CONNECT”消息;代理将建立连接并向您返回数据。

步骤如下:

  1. 打开到代理主机的套接字
  2. 发送'CONNECT http://www.google.com:80 HTTP/1.0\r\n\r\n' 字符串
  3. 等待接收

您必须使用结束换行符指定协议(protocol)(在我们的例子中是 HTTP 1.0,非分 block ),以便代理知道如何与端点通信。

您可以在 http://www.ietf.org/rfc/rfc2817.txt 找到有关 CONNECT 方法的详细信息

关于c++ - 如何通过代理将套接字连接到http服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191860/

相关文章:

c++ - 二进制通用 lambda 不匹配 ptr 到函数参数?

c++ - 递归函数可以内联吗?

c - A/C编译器是否在编译时执行移位操作?

linux - SIGSEGV,段错误。仅在 linux 上,它适用于 windows

linux - 无法重启 docker 容器 : OCI runtime create failed: container with id exist

c++ - 汇编如何做参数传递: by value,引用,不同类型/数组的指针?

c - 正则表达式不工作

c - 插入到在 C 中使用 void* 的二叉树

python - 在ubuntu上安装SHOGUN工具箱到python3

具有父节点的二叉树的 C++ 复制构造函数