📄 libproxyfish.c~
字号:
/*************************************************************************** * libproxyfish.c * * Sat Aug 25 10:10:25 2007 * Copyright 2007 hunbuso * hunbuso@gmail.com ****************************************************************************/#include "main.h"#include "gui.h"#include "libproxyfish.h"static GMutex* treeview_window_main_proxy_list_mutex = NULL; /* 信号量 */static gchar* get_content_symbol (gchar* content, const gchar* symbol);static GList* find_proxy_in_content (gchar* content);static void proxy_fish_thread_run (gchar* data, gpointer user_data);/** * 查找符号 */static gchar* get_content_symbol (gchar* content, const gchar* symbol){ gchar* invector = NULL; invector = g_strstr_len(content, strlen(content), symbol); if (!invector) return NULL; else return invector;}/** * 描述:检验字符串是否为纯数字 * 返回:如果字符串是纯数字,则返回真,否则返回假 * 参数: * data: 检验的字符串 */static gboolean check_digital (gchar* data){ if ((!data) || (0 == strlen(data))) return FALSE; guint length = strlen(data); gint i; gchar *p = data; for (i = 0; i < length; i++) { if (!g_ascii_isdigit(*p)) return FALSE; } return TRUE;}/** * 检索 xxx.xxx.xxx.xxx:xxxx格式 */static gboolean find_ip_port_in_content (gchar* content){ const gchar* single_point = "."; const gchar* colon = ":"; gchar* invector = NULL; gchar* invector_1 = NULL; gint i; gchar *j = NULL; if (!content) return FALSE; invector = content; ///找点号 for (i = 0; i < 3; i++) { invector_1 = get_content_symbol(invector, single_point); if (!invector_1) return FALSE; ///三位数 if (invector_1 - invector > 3) return FALSE; ///检验纯数 for (j = invector; j < invector_1; j++) {// DBG("%c\n", *j); if (!g_ascii_isdigit(*j)) return FALSE; } invector = invector_1 + 1; } ///找冒号 invector_1 = get_content_symbol(invector, colon); if (!invector_1) return FALSE; ///三位数 if (invector_1 - invector > 3) return FALSE; ///检验纯数 for (j = invector; j < invector_1; j++) {// DBG("%c\n", *j); if (!g_ascii_isdigit(*j)) return FALSE; } return TRUE;}/** * 检索出格式合适的代理 */static GList* find_proxy_in_content (gchar* content){ const gchar* single_quotes = "'"; gchar* invector = NULL; gchar* invector_1 = NULL; gchar* buffer = NULL;// GList* proxy_list = NULL; if (!content) return NULL; invector = content; while (invector) { invector = get_content_symbol(invector, single_quotes); if (!invector) break; invector ++; invector_1 = get_content_symbol(invector, single_quotes); if (!invector_1) break; ///此处申请的内存在线程池运行函数proxy_fish_thread_run中释放 buffer = g_malloc0(invector_1 - invector + 1); memcpy(buffer, invector, invector_1 - invector); if (find_ip_port_in_content(buffer)) { g_assert(app->env.thread_pool != NULL); g_thread_pool_push(app->env.thread_pool, buffer, NULL); DBG("%s\n", buffer); } invector = invector_1; } return NULL;}/** * 描述: 检索具备一定前缀名的文件 * 返回:具备则返回真,否则返回假 * 参数: * filename: 文件名 * prefix: 前缀,用分号隔开, 例如:"http://;ftp://;" */static gboolean filter_file_has_prefix (const gchar* filename, const gchar* prefix){ gchar **prefix_array = g_strsplit(prefix, ";", 0); gboolean result = FALSE; gint i = 0; while (prefix_array[i]) { if (strlen(prefix_array[i]) > 0) { if (g_str_has_prefix(filename, prefix_array[i])) { result = TRUE; break; } } i++; } g_strfreev(prefix_array); return result;}/** * 描述: 检索具备一定后缀名的文件 * 返回: 具备则返回真,否则返回假 * 参数: * filename: 文件名 * suffix: 后缀,用冒号隔开,例如:"php:html:htm" */static gboolean filter_file_type (const gchar* filename, const gchar* suffix){ gchar **suffix_array = g_strsplit(suffix, ":", 0); gboolean result = FALSE; gint i = 0; while (suffix_array[i]) { if (strlen(suffix_array[i]) > 0) { if (g_str_has_suffix(filename, suffix_array[i])) { result = TRUE; break; } } i++; } g_strfreev(suffix_array); return result;}/** * 获取内容的子链接 */void get_content_child (gchar* content){// GList* url_list = NULL; ///结果 gchar* url_keyword = "href="; ///超链接关键字 gchar* invector = NULL; gchar* invector_1 = NULL; gchar* buffer = NULL; gchar* symbol = "\""; invector = content; while (invector) { invector = g_strstr_len(invector, strlen(content), url_keyword); if (!invector) break; invector = get_content_symbol(invector, symbol) + 1; invector_1 = get_content_symbol(invector, symbol); if (!invector_1) break; buffer = g_malloc0(invector_1 - invector + 1); memcpy(buffer, invector, invector_1 - invector);// url_list = g_list_append(url_list, buffer); if (filter_file_type(buffer, "php:html")){ DBG("%s\n", buffer); g_assert(app->env.thread_pool != NULL); g_thread_pool_push(app->env.thread_pool, buffer, NULL); } invector = invector_1; }// return url_list;}/** * 获取并分析网页的内容 */gchar* parse_url_content (const gchar* url, enum method mt){ gint fd; gchar* init = "GET HTTP/1.1\r\n" "Host: www.proxycn.com\r\n" "User-Agent: ProxyFish\r\n" "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*\r\n" "Keep-Alive: 300\r\n" "Connection: keep-alive\r\n" "Proxy-Connection: keep-alive\r\n" "\r\n"; gchar* host_name = "www.proxycn.com"; GString *request = NULL; guint delay = 6; ///超时 struct sockaddr_in server_addr; struct hostent *host; ///网站返回的缓冲 gchar buf[10*BUFFER_8192]; memset(buf, 0, sizeof(buf)); request = g_string_new(init); request = g_string_insert(request, strlen("GET "), url); DBG("%s\n", request->str); fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } if (!(host = gethostbyname(host_name))) { perror("gethostbyname"); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_addr=*((struct in_addr *)host->h_addr); server_addr.sin_port = htons(80); bzero(&(server_addr.sin_zero), 8); if (connect(fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect error"); close (fd); ///线程中会用到此函数,这里使用的是返回而不是退出 return NULL; } struct timeval timeo_test = {delay,0}; gint savefl = fcntl(fd,F_GETFL); fcntl(fd, F_SETFL, savefl); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_test, sizeof(timeo_test)); if (send(fd, request->str, strlen(request->str), 0) < 0) { perror("send"); close (fd); ///线程中会用到此函数,这里使用的是返回而不是退出 return NULL; } while (recv(fd, buf, sizeof(buf),0) > 0) { if (get_child_url == mt) { // printf("%s\n",buf); get_content_child(buf); } else if (get_proxy == mt) {// printf("%s\n",buf); find_proxy_in_content(buf); } } close (fd); g_string_free(request, TRUE); return NULL;}/* * 检验远程服务器的返回值是否含关键字,含有返回真 */gboolean proxy_fish_check_key_string (const char* buffer, const char* keystr){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -