Makefile: build 2 more examples
[rofl0r-rocksock.git] / examples / proxychk.c
blobb23977064253a80a122501a44e6dd080e1e21114
1 /*
3 * author: rofl0r
5 * License: LGPL 2.1+ with static linking exception
8 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <pthread.h>
14 #include <errno.h>
15 #include <time.h>
16 #include <unistd.h>
17 #include "../rocksock.h"
19 static int timeout;
20 static char chk_host[512];
21 static int chk_host_port;
23 static int scanHost(char* baseproxy, char* scanproxy) {
24 rocksock skt;
25 rocksock* soc = &skt;
26 int ret;
28 rs_proxy proxies[2];
30 rocksock_init(soc, proxies);
31 rocksock_set_timeout(soc, timeout);
32 if(baseproxy)
33 if(rocksock_add_proxy_fromstring(soc, baseproxy))
34 return -1;
35 if(rocksock_add_proxy_fromstring(soc, scanproxy))
36 return -1;
38 if (!rocksock_connect(soc, chk_host, chk_host_port, 0)){
39 ret = 0; /* success */
40 } else {
41 //printf("%s\n", soc->lasterror.errormsg);
42 ret = -1;
45 rocksock_disconnect(soc);
46 rocksock_clear(soc);
48 return ret;
51 static int usage(const char *argv0) {
52 dprintf(2,
53 "proxy checker.\n"
54 "%s options\n"
55 "[-b type://[user:pass@]base_proxy_ip:port]\n"
56 "[-t type]\n"
57 "[-c check_ip:port]\n"
58 "[-T timeout_in_millisec]\n"
59 "\n"
60 "read [type://]ips:port from STDIN\n"
61 "if type is given, all ip:port tuples from STDIN won't need a prefix\n"
62 "if base proxy is given, it will be used as first proxy\n"
63 "timeout defaults to 1500.\n"
64 "checkip:port defaults to cnn.com:80\n"
65 "for every proxy that works, its address will be echoed to stdout\n"
66 "exit code is 0 when last proxy worked\n\n"
67 "example: echo 127.0.0.1:9050 | %s -t socks4 -T 2000\n"
68 "example: echo socks5://user:pass@127.0.0.1:1080 | %s\n"
69 "\nyou may want to use JOBFLOW or GNU parallel for speedy parallel checks\n"
70 , argv0, argv0, argv0);
71 return 1;
74 int main(int argc, char** argv) {
75 int c;
76 char *typestring = 0;
77 char *baseproxy = 0;
78 char *checkurl = "cnn.com:80";
79 timeout = 1500;
81 while ((c = getopt(argc, argv, "c:T:b:t:")) != -1) {
82 switch(c) {
83 case 'c':
84 checkurl = optarg;
85 break;
86 case 'T':
87 timeout = atoi(optarg);
88 break;
89 case 't':
90 if(!strcmp(optarg, "socks4")) ;
91 else if(!strcmp(optarg, "socks5")) ;
92 else if(!strcmp(optarg, "http")) ;
93 else {
94 dprintf(2, "invalid proxy type\n");
95 return 1;
97 typestring = optarg;
98 break;
99 case 'b':
100 baseproxy = optarg;
101 break;
102 default:
103 return usage(argv[0]);
108 char *p = strchr(checkurl, ':');
109 if(!p) return usage(argv[0]);
111 size_t l = p-checkurl;
112 strncpy(chk_host, checkurl, l);
113 chk_host[l] = 0;
114 chk_host_port = atoi(++p);
117 char buf[1024];
118 int ret = 0;
119 while(fgets(buf, sizeof buf, stdin)) {
120 char nb[1024+10], *p;
121 if((p = strrchr(buf, '\n')))
122 *p = 0;
123 if(*buf == 0 || *buf == '#') continue;
124 snprintf(nb, sizeof nb, "%s%s%s",
125 typestring ? typestring : "",
126 typestring ? "://" : "",
127 buf);
128 if(0 == (ret = scanHost(baseproxy, nb)))
129 printf("%s\n", nb);
132 return ret;