LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Web proxy in linux (https://www.linuxquestions.org/questions/programming-9/web-proxy-in-linux-4175424079/)

joyce092130 08-26-2012 12:47 PM

Web proxy in linux
 
Help me pls!

I need to build a basic web proxy capable of accepting HTTP requests, making requests from remote servers, and returning data to a client.

Our project should create a binary name proxy that will compile and run on the x86 Linux machines. The first command line argument should be the port that the proxy will listen from.
what shall i do first?
i am currently using Win XP OS.

Please. help badly needed.

dugan 08-26-2012 12:56 PM

What you should do first is obvious:

Quote:

Our project should... compile and run on the x86 Linux machines.
Quote:

i am currently using Win XP OS.
Set up a local Linux installation that's as similar as possible to the machine that your work will be built and tested on. Develop and test on that.

If you're really asking about how to write the proxy, well, here's a good overview of the principles you'll need to know:

http://beej.us/guide/bgnet/

If you're really asking for any code at all to be written for you, then I'm sorry, but we can't help with that.

bigearsbilly 08-30-2012 03:15 AM

homework?

joyce092130 09-01-2012 11:11 AM

proxy server codes
 
anybody please help me fix errors on my proxy server code or better if u can help me improve it:
here is my code:
Code:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <signal.h>

long int thout = 0;
int setTime = 0;
int requestNum = 0;

void overRun_handler(int signo)
{
if(signo == SIGALRM)
{
printf("\n<<--Response %d terminate by order-->>n",requestNum);
printf(">>%ld bytes has been transmitted\n>>",thout);
exit(1);
}
else printf("Catch a wrong signal.\n");

void proxyProcess(int,int,int);

int main(int argc,char **argv)
{
if(signal(SIGALRM,overRun_handler)==SIG_ERR)
{
perror("Can't regist signal handler");
exit(1);
}
if(argc!=3)
{
printf("Arguments are needed: %s <port number><connection time>\n",argv[0]);
exit(0);
}
if(atoi(argv[1])<=0)
{
printf("Invalid port number.\n");
exit(0);
}
if(atoi(argv[2])<0)
{
printf("Invalid connection time.\nNo time set.\n");
}
else setTime = atoi(argv[2]);

struct sockaddr_in sin;
struct sockaddr_in cin;
short port = atoi(argv[1]);
int sd, csd, mode;
int len = sizeof(sin);
int fd =open("static.txt", O_WRONLY|O_CREAT|O_TRUNC);

if(fd < 0)
{
perror("Fail to initial static file.");
exit(1);
}
else
{
write(fd, "0",1);
close(fd);
}

if(port>8000) mode=1;
else mode =0;

memset(&sin,0, sizeof(sin));
memset(&cin,0, sizeof(cin));
sin.sin_family =AF_INET;
sin.sin_addr.s_addr =INADDR_ANY;
sin.sin_port =htons(port);

sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("Fail to socket(sd)");
exit(1);
}
if((bind(sd, (struct sockaddr *)(&sin), sizeof(sin)))<0)
{
perror("Fail to bind");
exit(1);
}
if((bind(sd, (struct sockaddr *)(&sin), sizeof(sin)))<0)
{
perror("Fail to bind");
exit(1);
}
if ((listen(sd, 10))<0)
{
perror("Fail to listen");
exit(1);
}
while(1)
{
csd = accept(sd, (struct sockaddr *)(&cin), &len);
requestNum++;
if ((fork() == 0) && (csd>0))
{
if((close(sd))<0)
{
perror("Fail to close (sd)");
}
printf("\n<--Begin of %d -->\n", requestNum);
printf("--%d-->Client IP:%s\n--%d-->Port: %d\n", requestNum,
inet_ntoa(cin.sin_addr), requestNum, ntohs(cin.sin_port));
proxyProcess(csd,requestNum, mode);
if((close(csd))<0)
{
perror("Fail to close (csd)");
}
exit(0);
}
else if(csd < 0)
{
perror("Fail to accept");
exit(0);
}
else close(csd);
}
close(sd);
return 0;
}

void proxyProcess(int csd, int requestNum, int mode)
{
struct sockaddr_in web;
char * request, *begin, *end, *c, *ip;
char url1[512];
char url2[512];
char buf[2048];
int n, i, wsd;
int iport = 80;
int count = 0;
struct timeb time1, time2;

memset(buf, 0, sizeof(buf));
memset(url1, 0, sizeof(url1));
memset(url2, 0, sizeof(url2));

n = read (csd, buf, 2048);

if((strstr(buf,"Proxy-Authorization: Basic U2NodW1hY2hlcjpkYXZpZDg40TI5"))==0)
{
printf("There is no authorization\n");
char *msg = "HTTP/1.1 407 Proxy Authentication Required\r\nContent-Type: text/html\r\nProxy-Authenticate: Basic realm=\n";
write(csd, msg, strlen(msg));
close(csd);
exit(0);
}

begin = strstr(buf, "Accept Encoding: ");
if(begin!=NULL)
{
end = strstr(begin, "\r\n");
end +=2;
request = calloc(sizeof(char), strlen(buf)-(end-begin));
memcopy(request, buf, begin-buf);
strcat(request, end);
}

c = strstr(buf, "Host:\n ");
for (c+=5, i=0; *c!='\r'; c++, i++)
{
if(*c == ':')
{
c++;
int j = 0;
char port[5];
memset(port, 0, sizeof(port));
for(j = 0; *c!= ' '; c++, j++)
{
port[j] = *c;

if(j>5)
break;
}
iport = atoi(port);
break;
}
else
url1[i] = *c;
}

printf("--%d-->To:%s(port:%d)\n", requestNum, url1, iport);

FILE * fp = fopen("list.txt", "r");
while(feof(fp) == 0)
{
fgets(url2, 512, fp);
url2[strlen(url2)-1] = '\0';

if(strcmp(url2, url1) == 0)
{
printf("\n<--!Websites in blacklist!-->\n");
memset(url1, 0, sizeof(url1));
i = open("html.html", O_RDONLY);
while(1)
{
n = read(i, url1, 512);
write(csd, url1, 512);
memset(url1, 0, sizeof(url1));

if(n!=512)
break;
}
close(i);
printf("\n<--End of Process %d-->\n", requestNum);
break;
exit(0);
}
else
memset(url2, 0, sizeof(url2));
}
fclose(fp);

struct hostent * host;
host = gethostbyname(url1);
ip = inet_ntoa(*((struct in_addr *)host -> h_addr));
printf("--%d-->To:%s\n", requestNum, ip);
web.sin_family = AF_INET;
web.sin_port = htons(iport);
web.sin_addr.s_addr = inet_addr(ip);
if((wsd = socket(PF_INET, SOCK_STREAM, 0))<0)
{
perror("Fail to connect (wsd)");
exit(1);
}
printf("\n<--Request %d-->\n%s\n", requestNum, request);
write(wsd, request, strlen(request));
free(request);
memset(buf, 0, sizeof(buf));
count = 0;
ftime(&time1);
while((n = read(wsd, buf, 2048))>0)
{
char * response = calloc(sizeof(char), n);

if(count == 0)
{
ftime(&time2);
printf("<<Response time of request %d --<%lmds\n", requestNum,(time2.time-time1.time)*1000+(time2.millitm-time1.millitm));
if(setTime != 0)
alarm(setTime);
count ++;
}
if(strstr(buf, "<html>") !=0)
printf("A website has been opened\n");

fp = fopen("static.txt","r");
fscanf(fp, "%ld", &thout);
fclose(fp);
thout +=n;
fp = fopen("static.txt", "w");
fprintf(fp, "%ld", thout);
fclose(fp);

if(mode == 1)
{
if((begin = strstr(buf, "<titlt>"))|| (begin=
strstr(buf, "<title>")))
{
char warn[] = "Proxy:";
begin +=7;
if((c = strstr(buf, "Content-Length"))!=0)
{
char * str = strstr(c, "\r\n");
str +=2;
memcpy(response, buf, c-buf);
strncat(response, str, begin - str);
}
else
memcpy(response, buf, begin-buf);

strncat(response, warn, strlen(warn));
strncat(response, begin, strlen(begin));
write(csd, response, strlen(response));
}
else
write(csd, buf, n);
}
else write(csd, buf, n);

if((c = strstr(buf, "\r\n\r\n"))&&(count == 1))
{
count++;
printf("\n<--Response %d-->\n", requestNum);
for(i=0; i<=c-buf; i++)
printf("%c", buf[1]);
printf("\n");
}
memset(buf, 0, sizeof(buf));
}
printf(">>%ld Bytes have been transmitted.\n", thout);
printf("\n<--End of Process %d-->\n", requestNum);
}

and here are the errors:
Code:

jocelyn@jocelyn-VirtualBox:~$ gcc -Wall -o proxy proxy.c
proxy.c: In function ‘overRun_handler’:
proxy.c:28:5: warning: ‘main’ is normally a non-static function [-Wmain]
proxy.c: In function ‘main’:
proxy.c:101:28: warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness [-Wpointer-sign]
/usr/include/i386-linux-gnu/sys/socket.h:214:12: note: expected ‘socklen_t * __restrict__’ but argument is of type ‘int *’
proxy.c: In function ‘overRun_handler’:
proxy.c:130:6: error: static declaration of ‘proxyProcess’ follows non-static declaration
proxy.c:26:6: note: previous declaration of ‘proxyProcess’ was here
proxy.c: In function ‘proxyProcess’:
proxy.c:163:2: warning: implicit declaration of function ‘memcopy’ [-Wimplicit-function-declaration]
proxy.c:247:2: warning: use of ‘l’ length modifier with ‘m’ type character [-Wformat]
proxy.c:132:21: warning: variable ‘web’ set but not used [-Wunused-but-set-variable]
proxy.c: In function ‘overRun_handler’:
proxy.c:301:1: error: expected declaration or statement at end of input

thank u so much.

Ser Olmy 09-01-2012 12:12 PM

I'm way out of my league here, but as for the first error, I believe the compiler is saying that this (line 26):
Code:

void proxyProcess(int,int,int);
...conflicts with this (line 130):
Code:

void proxyProcess(int csd, int requestNum, int mode)
(Tip: Some indentation in the code would do wonders for readability, IMHO.)

Edit: Reformatting with proper indentation revealed a missing } at the end of overRun_handler.

NevemTeve 09-01-2012 12:13 PM

Start with something much easier. After a few years of intensive learning you might really think of this projects.

joyce092130 09-01-2012 12:35 PM

thank u ser Olmy, will check on that.

joyce092130 09-01-2012 12:37 PM

@nevemteve: i need to finish this by friday :(. this assignment is due on saturday.
please help. thanks

dugan 09-01-2012 12:41 PM

Quote:

Originally Posted by joyce092130 (Post 4770041)
@nevemteve: i need to finish this by friday :(. this assignment is due on saturday.
please help. thanks

It was Saturday for me when you posted this.

As has already been recommended, the first step in fixing your code is to properly indent it. That's where you should start.

NevemTeve 09-01-2012 01:21 PM

You project, if properly done, would consist of quite a few thousands lines of C-source. You cannot do that by yesterday, or even tomorrow.


All times are GMT -5. The time now is 06:21 PM.