gpt4 book ai didi

C++ HTTP Winsock : "Banned URL" at school, 即使是允许的网站

转载 作者:太空狗 更新时间:2023-10-29 23:05:30 26 4
gpt4 key购买 nike

我对使用套接字还很陌生,并且正在从事我的第一个项目;我实际上完全打算在没有任何库的情况下完成其中的第一个。我在 Windows 7 上,仅使用 WinAPI。

我在学校部分地研究它,在我的学校他们有一个网络过滤器,我相信是 FortiGuard。即使当我尝试通过网络浏览器打开允许的域获取页面时,例如 google.com,我也会收到以下消息:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">html,body{height:100%;padding:0;margin:0;}.oc{display:table;width:100%;height:100%;}.ic{display:table-cell;vertical-align:middle;height:100%;}div.msg{display:block;border:1px solid#30c;padding:0;width:500px;font-family:helvetica,sans-serif;margin:10px auto;}h1{font-weight:bold;color:#fff;font-size:14px;margin:0;padding:2px;text-align:center;background: #30c;}p{font-size:12px;margin:15px auto;width:75%;font-family:helvetica,sans-serif;text-align:left;}</style>
<title>The URL you requested has been blocked</title>
</head>
<body><div class="oc"><div class="ic"><div class="msg"><h1>The URL you requested has been blocked</h1><p>The page you have requested has been blocked, because the URL is banned.<br /><br />URL = invalid<br /></p></div></div></div></body>
</html>

(我试着把它分解一下)。

如您所见,它说该 URL 被禁止,我认为这是网络过滤器,因为这只发生在我在学校的时候

至少我相信我正在对一个在网络浏览器上正常工作的网站使用正确的标准 HTTP GET 请求,但我收到了这条消息。我对套接字请求做错了吗?

这是我的套接字代码:

我有一个基本的“socket_t”结构来传递给我的所有函数。这是它的定义:

//custom socket structure
typedef struct
{
//windows-specific
struct sockaddr win_addr;
u_long mode;
SOCKET socket;//acutal SOCKET structure

// General
bool listening;//set to true if actively listening
bool thread_terminate;//when boolean is set to true, listening thread terminates
void (*error_callback) (int);
http_response_t * response;
} socket_t;

对于连接:

//see socket_t definition in socket.h
//returns 0 on success, SOCKET_ERROR on WinSock failure, positive error code on DNS failure
int socket_connect(socket_t * sock, char * addr, int port)
{
//bear in mind sock is the custom socket_t structure. sock->socket is the actual SOCKET structure.
//pardon the nomenclature. rookie code.
//TODO: IPv6 support?

//DNS lookup structures
struct addrinfo * res = NULL;// Result of the getaddrinfo() call
struct sockaddr * sockaddr_v4 = NULL;// IPv4 sockaddr structure

// So-called "hints" structure detailed in the getaddrinfo() MSDN page.
// I guess it contains information for the DNS lookup.
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

//Perform DNS lookup
DWORD getaddrinfo_res = getaddrinfo(addr, "80\0", &hints, &res);//hardcode port number, for now
if(getaddrinfo_res != 0) return getaddrinfo_res;

//for each
for(struct addrinfo * ptr = res; ptr != NULL; ptr = ptr->ai_next)
{
switch(ptr->ai_family)
{
case AF_INET:
sockaddr_v4 = (struct sockaddr *) ptr->ai_addr;//set current address
sock->win_addr = * sockaddr_v4;//set socket address
}
}

//initialize actual SOCKET
sock->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);// TCP/IP, stream-oriented, and TCP rather than UDP; respectively
if(sock->socket == INVALID_SOCKET) return SOCKET_ERROR;

//actual connection
int connect_res = connect(sock->socket, &(sock->win_addr), sizeof(sock->win_addr));
if(connect_res == SOCKET_ERROR) return SOCKET_ERROR;
return 0;
}

和 HTTP 请求位:

// Execute an HTTP request.
// Param 1: socket_t object
// Param 2: resource locator not including site name.
int socket_httprequest(socket_t * sock, char * rl)
{
std::string str_req = std::string("GET /");
str_req.append(rl);
str_req.append(" HTTP/1.0\r\n");

/*
//user agent header
str_req.append("User-Agent: conley-client/");
str_req.append(VERSION);
str_req.append("\r\n");
*/

//final newline
str_req.append("\r\n");

const char * z_req = str_req.c_str();
return send(sock->socket, z_req, (int) strlen(z_req), 0);
}

它们都在主函数中调用,如下所示:

// Initialization
conley::init_sockets();
conley::init_http();

conley::socket_t * sock = conley::socket_alloc(err_callback);
int connect_res = conley::socket_connect(sock, "google.com\0", 80);
if(connect_res > 0) std::cout << "DNS ERR " << connect_res << std::endl;
if(connect_res < 0) std::cout << "CONNECT ERR " << WSAGetLastError() << std::endl;
if(connect_res == 0) std::cout << "Connected" << std::endl;

int httpreq_res = conley::socket_httprequest(sock, "\0");
if(httpreq_res != -1) std::cout << "HTTP request success: " << httpreq_res << " bytes sent" << std::endl;
else std::cout << "HTTP request failure: Error " << WSAGetLastError() << std::endl;

conley::socket_listen(sock);

感谢您的宝贵时间!

最佳答案

可能错误不是你的程序引起的,而是因为过滤器检测到请求不是浏览器发送的。浏览器发送诸如用户代理之类的 header ,以向将请求发送到的服务器标识自己。我的建议是使用像 Wireshark 这样的工具捕获浏览器生成的流量并尝试从您的应用程序中模仿它。

关于C++ HTTP Winsock : "Banned URL" at school, 即使是允许的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145104/

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