📄 get_proxy_address.c
字号:
#include <stdio.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <netdb.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#define HTTP_REQ "GET /proxy1.html HTTP/1.1\r\n"\"Host: www.cnproxy.com:80\r\n"\"User-Agent: Mozilla/5.0\r\n"\"Accept: text/xml,text/html;*/*\r\n\r\n"int main(int argc, char *argv[]){ int sockfd; struct sockaddr_in servaddr; struct hostent *host = NULL; char **ptr = NULL, *p = NULL, *q = NULL; char buffer[65535]; //char http_answer[4096]; char html[65535]; int recv_len, i, recved_len, table_num = 0; int code; char ip_port[30]; unsigned long ip; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); return 0; } host = gethostbyname("www.cnproxy.com"); if (host == NULL) { perror("gethostbyname"); return 0; } if (host->h_addrtype != AF_INET) { printf("Address type error!\n"); return 0; } bzero(&servaddr, sizeof(servaddr)); for (ptr = host->h_addr_list; *ptr != NULL; ptr++) { //printf("address %s\n", inet_ntop(host->h_addrtype, *ptr, buffer, sizeof(buffer))); memmove(&ip, *ptr, host->h_length); //ip = ntohl(ip); memmove(&servaddr.sin_addr, &ip, sizeof(ip)); break; } servaddr.sin_family = AF_INET; servaddr.sin_port = htons(80); if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("connect"); return 0; } send(sockfd, HTTP_REQ, sizeof(HTTP_REQ), 0); recved_len = 0; html[0] = '\0'; code = 0; while (1) {rerecv: recv_len = recv(sockfd, buffer, sizeof(buffer), 0); //printf("%s\n", buffer); p = buffer; //strncpy(http_answer, buffer, sizeof(http_answer)); if (code == 0) { p = strchr(buffer, ' '); p++; sscanf(p, "%d", &code); if (code != 200) { printf("Be rejected!\n"); return 0; } p = strstr(buffer, "Content-Length"); p += strlen("Content-Length"); p++; sscanf(p, "%d", &recv_len); p = strstr(buffer, "\r\n\r\n"); p += strlen("\r\n\r\n"); } recved_len += strlen(p); while (1) { if (table_num == 3) break; p = strstr(p, "<table>"); if (p == NULL) goto rerecv; p += strlen("<table>"); table_num++; if (table_num == 3) { p += strlen("\r\n"); p = strstr(p, "\r\n"); if (p == NULL) goto rerecv; p += strlen("\r\n"); } } q = strstr(p, "</table>"); if (q == NULL) { strcat(html, p); goto rerecv; } else { strcat(html, p); break; } } //p = strstr(html, "\r\n"); //p += strlen("\r\n"); p = html; q = strstr(html, "</table>"); //printf("%s\n*********************\n", p); while (p < q) { p = strstr(p, "<tr><td>"); p += strlen("<tr><td>"); for (i = 0; *p != '&'; p++, i++) { if (i >= 30) break; ip_port[i] = *p; } if (i >= 30) continue; ip_port[i] = ':'; i++; p = strchr(p, ':'); p++; for (; *p != '<'; p++, i++) { if (i >= 30) break; ip_port[i] = *p; } if (i >= 30) continue; ip_port[i] = '\0'; if (judge_ip_port(ip_port) == 0) printf("%s\n", ip_port); p = strstr(p, "\r\n"); p += strlen("\r\n"); } close(sockfd); return 0;}int judge_ip_port(const char *ip_port){ int ret = -1; const char *p = NULL; int i = 0; int tmp; p = ip_port; while (1) { p = strchr(ip_port, '.'); if (p != NULL) { i++; if (sscanf(ip_port, "%d", &tmp) != 1) { goto end; } if (tmp >= 255 || tmp < 0) goto end; if (i >= 4) goto end; ip_port = p + 1; } else { break; } } if (i != 3) goto end; p = strchr(ip_port, ':'); if (p == NULL) goto end; if (sscanf(++p, "%d", &tmp) != 1) goto end; if (tmp < 0 || tmp > 65535) goto end; ret = 0;end: return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -