简体   繁体   English

用c代理隧道

[英]proxy tunneling with c

I am working on a small project (C programming) to tunnel through a proxy and when I try to do it I get a error: 我正在一个小项目(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

The data I am sending to the proxy server is: 我发送到代理服务器的数据是:

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

Any help would be great! 任何帮助将是巨大的!

------ UPDATE ------ ------更新------

I have tried the methods you guys suggested and I am still getting an error. 我尝试了你们建议的方法,但仍然出现错误。 I will post my entire set of code and see if it is something in the socket causing it to send the wrong information. 我将发布我的整个代码集,看看套接字中是否有东西导致它发送错误的信息。

#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;

} }

----- UPDATE ------ -----更新------

New error: 新错误:

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>

“Bad request” meant that the request you sent to the server was malformed and couldn't be understood. “错误的请求”表示您发送到服务器的请求格式错误,无法理解。 I think the reason for this is that you are sending the 8 characters <CR><LF> (as in, '<', 'C', 'R' etc.) rather than the 2 characters that they should represent, ie \\r\\n . 我认为这样做的原因是您发送的是8个字符<CR><LF> (如'<','C','R'等),而不是它们应表示的2个字符\\r\\n

Try 尝试

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

Refer this: rfc2616 引用此: rfc2616

10.4.1 400 Bad Request 10.4.1 400错误的请求

The request could not be understood by the server due to malformed syntax. 由于语法格式错误,服务器无法理解该请求。 The client SHOULD NOT repeat the request without modifications. 客户不应在没有修改的情况下重复请求。

When you are passing: 通过时:

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

Then perhaps the <CR> and <LF> parts are sent as it is as strings, instead of the ASCII 0xA and 0xD, which represent the control characters you intend to send. 然后,也许<CR><LF>部分将以字符串形式发送,而不是ASCII 0xA和0xD,它们代表要发送的控制字符。 The simplest fix would be using C equivalents: \\r for <CR> and \\n for <LF> : 最简单的解决方法是使用C等效项: \\r表示<CR>\\n表示<LF>

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

If you intend to expand the use of the msg[] array to contain many more requests you might have to write a wrapper function to translate the CR,LF and other control strings in the msg[] into appropriate C char equivalents to form proper message. 如果打算扩展使用msg[]数组来包含更多请求,则可能必须编写包装函数,将msg[]的CR,LF和其他控制字符串转换为适当的C字符等效形式,以形成正确的消息。 。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM