| 1 | //#include <stdlib.h> |
|---|
| 2 | //#include <stdio.h> |
|---|
| 3 | //#include <stdarg.h> |
|---|
| 4 | //#include <string.h> |
|---|
| 5 | //#include <signal.h> |
|---|
| 6 | //#include <sys/socket.h> |
|---|
| 7 | //#include <sys/select.h> |
|---|
| 8 | //#include <sys/types.h> |
|---|
| 9 | //#include <netinet/in.h> |
|---|
| 10 | |
|---|
| 11 | #include "sockslib.h" |
|---|
| 12 | |
|---|
| 13 | // for discarding duplicates |
|---|
| 14 | // cat file | sort | uniq > newfile |
|---|
| 15 | |
|---|
| 16 | #define MAX_BUF_LEN 512 |
|---|
| 17 | |
|---|
| 18 | int dont_actually_check_proxies; |
|---|
| 19 | int verbose_mode; |
|---|
| 20 | FILE *check_proxy_output; |
|---|
| 21 | |
|---|
| 22 | int num_checked; |
|---|
| 23 | int num_passed; |
|---|
| 24 | |
|---|
| 25 | void sig_handler(int nig) {} |
|---|
| 26 | |
|---|
| 27 | void usage() { |
|---|
| 28 | printf("washingmachine v0.7\n"); |
|---|
| 29 | printf("Function:\n"); |
|---|
| 30 | printf(" Takes a proxy list from stdin (format [ip:port\\n]), prints working\n"); |
|---|
| 31 | printf(" proxies to output, prints verbose information to stderr\n"); |
|---|
| 32 | printf("\n"); |
|---|
| 33 | printf("Example usage:\n"); |
|---|
| 34 | printf(" cat unchecked_proxies.txt | ./washingmachine > checked_proxies.txt\n"); |
|---|
| 35 | printf("\n"); |
|---|
| 36 | printf(" Command line options\n"); |
|---|
| 37 | printf(" -v toggle verbose mode (off by default)\n"); |
|---|
| 38 | printf(" -d dont check just scan (off by default)\n"); |
|---|
| 39 | printf(" -t [seconds] set a timeout on possibly blocking function\n"); |
|---|
| 40 | printf(" calls (default 3 seconds)\n"); |
|---|
| 41 | printf(" -o [file] set a filename to output proxies\n"); |
|---|
| 42 | printf(" (default stdout)\n"); |
|---|
| 43 | printf(" -h print this help information\n"); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void report(int nig) { |
|---|
| 47 | if(nig == SIGINT) |
|---|
| 48 | fprintf(stderr, "\n"); |
|---|
| 49 | |
|---|
| 50 | fprintf(stderr, "- Report -------------------\n"); |
|---|
| 51 | fprintf(stderr, " Proxies checked - %d\n", num_checked); |
|---|
| 52 | fprintf(stderr, " Proxies passed - %d\n", num_passed); |
|---|
| 53 | fprintf(stderr, " Ratio - %d%c\n", (num_passed*100)/num_checked, '%'); |
|---|
| 54 | fprintf(stderr, "----------------------------\n"); |
|---|
| 55 | |
|---|
| 56 | if(nig == SIGINT) |
|---|
| 57 | exit(0); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | int main(int argc, char ** argv) { |
|---|
| 61 | int argi; |
|---|
| 62 | char str[MAX_BUF_LEN]; |
|---|
| 63 | |
|---|
| 64 | signal(SIGALRM, sig_handler); |
|---|
| 65 | signal(SIGINT, report); |
|---|
| 66 | |
|---|
| 67 | dont_actually_check_proxies = 0; |
|---|
| 68 | verbose_mode = 0; |
|---|
| 69 | check_proxy_output = stdout; |
|---|
| 70 | |
|---|
| 71 | num_checked = 0; |
|---|
| 72 | num_passed = 0; |
|---|
| 73 | |
|---|
| 74 | for(argi = 1; argi < argc; argi++) { |
|---|
| 75 | if(strncmp("-v", argv[argi], 2) == 0) { |
|---|
| 76 | verbose_mode = !verbose_mode; |
|---|
| 77 | } |
|---|
| 78 | if(strncmp("-d", argv[argi], 2) == 0) { |
|---|
| 79 | dont_actually_check_proxies = !dont_actually_check_proxies; |
|---|
| 80 | } |
|---|
| 81 | else if(strncmp("-t", argv[argi], 2) == 0) { |
|---|
| 82 | if(argc > argi+1) { |
|---|
| 83 | set_signal_timeout(atoi(argv[argi+1])); |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | fprintf(stderr, "Supply a timeout value in seconds\n"); |
|---|
| 87 | return 0; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | else if(strncmp("-o", argv[argi], 2) == 0) { |
|---|
| 91 | if(argc > argi+1) { |
|---|
| 92 | check_proxy_output = fopen(argv[argi+1], "a"); |
|---|
| 93 | if(check_proxy_output == NULL) { |
|---|
| 94 | fprintf(stderr, "Cannot open supplied output file\n"); |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | else { |
|---|
| 99 | fprintf(stderr, "Supply a filename to output proxies too\n"); |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | if(strncmp("-h", argv[argi], 2) == 0) { |
|---|
| 104 | usage(); |
|---|
| 105 | return 0; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | set_verbose_mode(verbose_mode); |
|---|
| 110 | |
|---|
| 111 | while(fgets(str, MAX_BUF_LEN, stdin) != NULL) { |
|---|
| 112 | char *prox; |
|---|
| 113 | int port; |
|---|
| 114 | |
|---|
| 115 | int return_code; |
|---|
| 116 | |
|---|
| 117 | if(split_proxy_port(str, &prox, &port) == 0) { |
|---|
| 118 | num_checked++; |
|---|
| 119 | |
|---|
| 120 | if(dont_actually_check_proxies) { |
|---|
| 121 | fprintf(check_proxy_output, "%s:%d\n", prox, port); |
|---|
| 122 | free(prox); |
|---|
| 123 | continue; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | return_code = proxy_test(prox, port); |
|---|
| 127 | |
|---|
| 128 | if(return_code == TESTPROXY_RETCODE_SUCCESS) { |
|---|
| 129 | num_passed++; |
|---|
| 130 | fprintf(check_proxy_output, "%s:%d\n", prox, port); |
|---|
| 131 | fflush(check_proxy_output); |
|---|
| 132 | fprintf(stderr, "Got proxy: [%s:%d]\n", prox, port); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | free(prox); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | if(check_proxy_output != stdout) { |
|---|
| 140 | fclose(check_proxy_output); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | report(0); |
|---|
| 144 | |
|---|
| 145 | return 0; |
|---|
| 146 | } |
|---|