/* pr0n clicker 1.0 by HoGs HeaD
 * -----------------------------
 * Howdy. Whoah, it's been a while since I released a program! Well, su1d
 * gave me the idea for this program and he is supposedly going to make a 
 * Windows version soon.
 * 
 * What this program does: You know those banners that give you ten cents
 * or something cheap when they're clicked? Well, this program will click 
 * a banner with a list of wingates you have, so you can just have a large 
 * list of wingates, turn this on, and start gaining some money.
 * 
 * This also has functions for downloading http files through wingates or just
 * plain downloading the http file. You may use these functions provided they
 * are in a different program than this, and that you don't sell the program
 * you use that code for. When you download this code you agree to not claim
 * this code as your own.
 * 
 * Special thanks to: SIN, duke(for being a massah debugger), su1d(for the
 * idea)
 * ------- hawgshead@yahoo.com ------------------------------------- */

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>

/* Cute function to download an HTTP file, compliments of me. */

void downloadHttpFile(char *server, char *file)
{
   FILE *fp;
   struct hostent *getFrom;
   struct sockaddr_in from;
   char inBuff[100], *oldBuff, outBuff[100];
   int sckFd, isConnected, numSent;
   
   if(isdigit(*server)){
      from.sin_addr.s_addr = inet_addr(server);
   }else{
      getFrom = gethostbyname(server);
      strncpy((char *)&from.sin_addr, (char *)getFrom->h_addr, sizeof(from.sin_addr));
   }
   from.sin_family = AF_INET;
   from.sin_port = htons(80);
   sckFd = socket(AF_INET, SOCK_STREAM, 0);
   
   if(sckFd < 0){
      printf("Socket could not be established.\n");
      exit(0);
   };
   isConnected = connect(sckFd, (struct sockaddr *)&from, sizeof(from));
   if(isConnected < 0){
      printf("Connection failed.\n");
      exit(0);      
   }else{
      printf("Connection success.\n");
       
   snprintf(outBuff, sizeof(outBuff), "GET %s\n", file);

   numSent=send(sckFd, outBuff, sizeof(outBuff), 0);
   fflush(0);   
   if((fp=fopen("temp.html", "w"))==NULL){
      printf("Error opening temporary file for writing!\n");
      exit(0);
   }
   
      get:
   numSent=recv(sckFd, inBuff, sizeof(inBuff), 0);
   oldBuff=inBuff;
   fprintf(fp, "%s", inBuff);
   if((strstr(oldBuff, "</HTML>"))==NULL){
     bzero(inBuff, sizeof(inBuff));
     goto get;
   }else{   
      fclose(fp);
   }
         
   printf("HTTP download complete.\n");   
   };
   close(sckFd);
}

/* Download an HTTP file through a WinGate. Woop! */

void throughWingate(char *wserver, char *httpServer, char *wfile)
{
   FILE *wfp;
   struct hostent *wgetFrom;
   struct sockaddr_in wfrom;
   char winBuff[100], *woldBuff, woutBuff[100];
   int wsckFd, wisConnected, wnumSent;
   
   if(isdigit(*wserver)){
      wfrom.sin_addr.s_addr = inet_addr(wserver);
   }else{
      wgetFrom = gethostbyname(wserver);
      strncpy((char *)&wfrom.sin_addr, (char *)wgetFrom->h_addr, sizeof(wfrom.sin_addr));   
   }
   wfrom.sin_family = AF_INET;
   wfrom.sin_port   = htons(23);
   wsckFd = socket(AF_INET, SOCK_STREAM, 0);
   
   if(wsckFd < 0){
      printf("Socket could not be established.\n");
      exit(0);
   };
   wisConnected = connect(wsckFd, (struct sockaddr *)&wfrom, sizeof(wfrom));
   if(wisConnected < 0){
      printf("Connection could not be established.\n");
   }else{
      printf("Connection success.\n");
      snprintf(woutBuff, sizeof(woutBuff), "%s 80\n", httpServer);
      
      do{
	  bzero(winBuff, sizeof(winBuff));
	  read(wsckFd, winBuff, sizeof(winBuff));
      }while((strstr(winBuff, "WinGate>"))==NULL);
      printf("Connection established, sending commands.\n");
      write(wsckFd, woutBuff, strlen(woutBuff));
      do{
	 bzero(winBuff, sizeof(winBuff));
	 read(wsckFd, winBuff, sizeof(winBuff));
      }while((strstr(winBuff, "Connected"))==NULL);
      printf("Connection established through WinGate->Server.\n");
 
      snprintf(woutBuff, sizeof(woutBuff), "GET %s\n", wfile);
      write(wsckFd, woutBuff, strlen(woutBuff));
      fflush(0);
      if((wfp=fopen("temp.html", "w"))==NULL){
	 printf("Unable to open temporary file for output!\n");
	 exit(0);
      }
      getit:
	 read(wsckFd, winBuff, sizeof(winBuff));
         woldBuff=winBuff;
         fprintf(wfp, "%s", winBuff);
      if((strstr(winBuff, "</HTML>"))==NULL){
	 bzero(winBuff, sizeof(winBuff));
	 goto getit;
      }else{
	 fclose(wfp);
      }
      printf("HTTP download complete.\n");
     
   }
   close(wsckFd);
}

/* Parse the WinGate file and try each entry in the file. */

void parseGates(char *gateFile, char *httpServ, char *fileName)
{
   FILE *gate;
   char currGate[100];

   int i=0;

   
   if((gate=fopen(gateFile, "r"))==NULL){
      printf("Error opening WinGate list.\n");
      exit(0);
   }
   while(fgets(currGate, 80, gate) != NULL){
   *(strchr(currGate, '\n')) = '\0';   
   throughWingate(currGate, httpServ, fileName);
   }
   
   fclose(gate);
}

/* Main function. */

void main(int argc, char **argv){
   printf("HoGs HeaD's Linux pr0n clicker 1.0.\nhawgshead@yahoo.com\n");
   printf("-----------------------------------\n");
   if(argc < 3){
      printf("Usage: pron (server) (file) (wingate list)\n EX. pron www.yahoo.com /index.html wingate.list\n");
      exit(0);
   };
   
   printf("Connecting from localhost\n");
   printf("-------------------------\n");
   downloadHttpFile(argv[1], argv[2]);
   printf("------------------------\n");
   printf("Connecting from WinGates\n");
   printf("------------------------\n");
   parseGates(argv[3], argv[1], argv[2]);
   unlink("temp.html");
}

 

