LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2012, 12:47 PM   #1
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Rep: Reputation: Disabled
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.
 
Old 08-26-2012, 12:56 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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.

Last edited by dugan; 08-26-2012 at 01:11 PM.
 
Old 08-30-2012, 03:15 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
homework?
 
Old 09-01-2012, 11:11 AM   #4
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
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.

Last edited by joyce092130; 09-01-2012 at 11:13 AM.
 
Old 09-01-2012, 12:12 PM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
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.

Last edited by Ser Olmy; 09-01-2012 at 12:30 PM.
 
Old 09-01-2012, 12:13 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Start with something much easier. After a few years of intensive learning you might really think of this projects.
 
Old 09-01-2012, 12:35 PM   #7
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
thank u ser Olmy, will check on that.
 
Old 09-01-2012, 12:37 PM   #8
joyce092130
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
@nevemteve: i need to finish this by friday . this assignment is due on saturday.
please help. thanks
 
Old 09-01-2012, 12:41 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by joyce092130 View Post
@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.

Last edited by dugan; 09-01-2012 at 12:46 PM.
 
Old 09-01-2012, 01:21 PM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NiX Linux Web Proxy Script tniemela Linux - Software 2 08-17-2011 05:05 PM
squid as Web proxy server in linux world salimshahzad Linux - Newbie 2 02-01-2010 05:57 AM
LXer: Sun To Open Source Java System Web Server and Web Proxy Technologies LXer Syndicated Linux News 0 08-03-2008 07:50 PM
Linux web proxy for Windows 2003 domain rajthampi Linux - Security 2 06-04-2008 12:03 AM
How to setup home linux box as web proxy for use from work? Anubis Linux - Networking 10 03-11-2008 09:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration