#include #include #include #include #include #include #include #include #include #include #include #include #include const int BUFSIZE=1024; int MakeServerSocket(char *port) { const int MAXNAMELEN = 255; //longest a name can be const int BACKLOG = 3; //max number of queued connections char localhostname[MAXNAMELEN]; // local host name int s; //file descriptor struct sockaddr_in sa; //gotta love arcane socket types struct hostent *hp; //host entry struct servent *sp; //service entry int portnum;//stores the port int ret; //error code form bind gethostname(localhostname,MAXNAMELEN); hp = gethostbyname(localhostname); //no need to check for error here because localhost will resolve sscanf(port, "%d", &portnum); if (portnum == 0) { sp=getservbyname(port, "tcp"); if (sp==NULL) { return -1; } portnum = ntohs(sp->s_port); } sa.sin_port = htons(portnum); bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; s = socket(hp->h_addrtype, SOCK_STREAM, 0); if (s==-1) { return -1; } ret = bind(s, (struct sockaddr *)&sa, sizeof(sa)); if (ret==-1) { return -1; } ret=listen(s, BACKLOG); if (ret==-1) { return -1; } cout << "Waiting for connection on port " << port << endl; return s; } int MakeClientSocket(char *host, char *port) { int s; //nice file descriptor struct sockaddr_in sa; //window to the world struct hostent *hp; //host entry struct servent *sp; //service entry int portnum; //pretty self explanatory int ret;//connect error value hp = gethostbyname(host); if (hp==NULL) { return -1; } bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; sscanf(port, "%d", &portnum); if (portnum > 0) { sa.sin_port = htons(portnum); } else { sp=getservbyname(port, "tcp"); if (sp==NULL) { return -1; } portnum = sp->s_port; sa.sin_port = sp->s_port; } s = socket(hp->h_addrtype, SOCK_STREAM, 0); if (s==-1) { return -1; } ret = connect(s, (struct sockaddr *)&sa, sizeof(sa)); if (ret==-1) { return -1; } cout << "Client socket connected to host " << host << " port " << port << endl; return s; } int banned(char *host) { if (strcmp(host,"www.microsoft.com")) return 0; else return 1; } int main(int argc, char *argv[]) { char temp[10]; int s; ofstream log("proxy_log"); if (argc<2) { cout << "Enter a port number:"; cin >> temp; s=MakeServerSocket(temp); } else s = MakeServerSocket(argv[1]); if (s!=-1) while (1) { char ip[BUFSIZE]; // used for turning 32 bit int into a human looking ip address struct sockaddr_in sa;//window to the world int sa_len = sizeof(sa);//how big is our window int fd = accept(s, (struct sockaddr *)&sa, (unsigned int *)&sa_len); //file descriptor we talk out of int addr=sa.sin_addr.s_addr; sprintf(ip,"%i.%i.%i.%i",(addr)&0xFF,(addr>>8)&0xFF,(addr>>16)&0xFF,(addr>>24)&0xFF); cout << "Connection from "<< ip << " family " << sa.sin_family << " port " << ntohs(sa.sin_port) << endl; if(fork()>0) { //do proxy stuff here char buf[BUFSIZE]; int len; len = read(fd, buf, BUFSIZE); buf[len]=0; time_t hmm=time(NULL); struct tm *current=localtime(&hmm); log << ip << " - - " << "[" << current->tm_mday << "/" << current->tm_mon<< "/" << current->tm_year+1900 << " " << current->tm_hour << ":" << current->tm_min << ":" << current->tm_sec <<"] - "<microsoft sucks",41); } else { int remote=MakeClientSocket(host,"80"); if (remote==-1) cout << "no connection to remote server" << endl; else { char tmp[BUFSIZE]; int len2; len2=read(remote,tmp,strlen(tmp)); int l=sprintf(tmp,"%s %s %s",get,path,version); len2=write(remote,tmp,l); len2=write(remote,"\n\n",2); if (len2==-1) cout << "get to remote failed" << endl; else cout << "wrote command =" << tmp <<"=" <0); close(remote); } } close(fd); exit(1); } close(fd); } log.close(); return 0; }