【问题标题】:proxy tunneling with c使用 c 的代理隧道
【发布时间】:2013-01-08 05:53:22
【问题描述】:

我正在做一个小项目(C 编程)以通过代理进行隧道传输,当我尝试这样做时出现错误:

HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE9
Date: Mon, 07 Jan 2013 22:20:56 GMT
Content-Type: text/html 
Content-Length: 1456
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from xt03
Via: 1.0 xt03:80 (squid/2.7.STABLE9)
Connection: close

我发送到代理服务器的数据是:

 char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";

任何帮助都会很棒!

----- 更新 ------

我已经尝试了你们建议的方法,但我仍然遇到错误。我将发布我的整个代码集,看看它是否是套接字中的某些东西导致它发送错误信息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define maxlen 2048

int main(int argc, char *argv[])
{
int mysocket;
int len;
char buffer[2000];
char msg[] = "CONNECT example.org:80 HTTP/1.0 \r\n HOST example.org:80 \r\n\r\n";


mysocket = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in dest;

memset(&dest, 0, sizeof(dest));

dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("125.39.66.150");
dest.sin_port = htons(80);

connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));

send(mysocket, msg, strlen(msg), 0);
len = recv(mysocket, buffer, maxlen, 0);

buffer[len] = '\0';

printf("%s \n", buffer);
close(mysocket);
return 0;

}

----- 更新 ------

新错误:

HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE5
Date: Tue, 08 Jan 2013 22:40:24 GMT
Content-Type: text/html
Content-Length: 1158
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from DXT-BJ-219
X-Cache-Lookup: NONE from DXT-BJ-219:80
Connection: close

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-    serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P> 
While trying to process the request:
<PRE>
CONNECT example.com:80 HTTP/1.1


</PRE>
<P>
The following error was encountered:
<UL>
<LI>
<STRONG>
Invalid Request
</STRONG>
</UL>

<P>
Some aspect of the HTTP Request is invalid.  Possible problems:
<UL>
<LI>Missing or unknown request method
<LI>Missing URL
<LI>Missing HTTP Identifier (HTTP/1.0)
<LI>Request is too large
<LI>Content-Length missing for POST or PUT requests
<LI>Illegal character in hostname; underscores are not allowed
</UL>
<P>Your cache administrator is <A HREF="mailto:noc_admin@163.com">noc_admin@163.com</A>. 

<BR clear="all">
<HR noshade size="1px">
<ADDRESS>
by DXT-BJ-219
</ADDRESS>
</BODY></HTML>

【问题讨论】:

    标签: c http networking proxy proxytunnel


    【解决方案1】:

    “错误请求”表示您发送到服务器的请求格式不正确,无法理解。我认为这样做的原因是您发送 8 个字符 &lt;CR&gt;&lt;LF&gt; (例如,'\r\n。

    试试

    char msg[] = "CONNECT example.com:80 HTTP/1.0\r\nHOST example:80\r\n\r\n";
    

    【讨论】:

    • 我尝试了上面的建议,但仍然遇到同样的错误。我在主要部分发布了我的其余代码。也许它与套接字有关。
    • @user1626342:不要在\r\n 周围放置任何空格。我有一种感觉,每一行都必须以空格开头。
    • 我在没有空格的情况下试了一下,得到了完全相同的错误。我在上面附加了我的其余代码?还有什么我做错了吗?顺便说一句,感谢您的所有帮助!
    • @user1626342:尝试仅使用 CONNECT 行,例如"CONNECT example.com:80 HTTP/1.0\r\n\r\b"。也许它不喜欢请求中的HOST 命令。
    • 当我尝试新方法时,我得到了不同类型的错误。它或多或少地生成了一个表明错误的 html 页面。我将它作为更新发布在主要部分。
    【解决方案2】:

    请参考:rfc2616

    10.4.1 400 错误请求

    由于语法错误,服务器无法理解请求。客户端不应该在没有修改的情况下重复请求。

    当你经过时:

    char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";
    

    那么,&lt;CR&gt;&lt;LF&gt; 部分可能会按原样作为字符串发送,而不是 ASCII 0xA 和 0xD,它们代表您打算发送的控制字符。 最简单的解决方法是使用 C 等效项:\r 用于 &lt;CR&gt;\n 用于 &lt;LF&gt;

    char msg[] = "CONNECT example.com:80 HTTP/1.0 \r\n HOST example.com:80 \r\n\r\n";
    

    如果您打算扩展 msg[] 数组的使用以包含更多请求,您可能需要编写一个包装函数来将 msg[] 中的 CR、LF 和其他控制字符串转换为适当的 C 字符等效项形成正确的信息。

    希望这会有所帮助。

    【讨论】:

    • 标头末尾需要空行:\r\n\r\n。
    • 这与我提供的答案有何不同?
    • 对错误代码和描述性的可靠引用。解决有关 msg[] 未来使用问题的固有模糊性。
    猜你喜欢
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2011-05-22
    • 2011-11-15
    • 1970-01-01
    • 2017-04-15
    • 2012-08-02
    相关资源
    最近更新 更多