-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_server.c
166 lines (133 loc) · 3.11 KB
/
proxy_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netdb.h>
#include<errno.h>
void error(char* msg)
{
perror(msg);
exit(0);
}
int main(int argc,char* argv[])
{
pid_t pid;
struct sockaddr_in addr_in,cli_addr,serv_addr;
struct hostent* host;
int sockfd,newsockfd;
if(argc<2)
error("./proxy <port_no>");
printf("\n*****WELCOME TO PROXY SERVER*****\n");
printf("\nCopyright (c) 2014 GODLY T.ALIAS\n\n");
bzero((char*)&serv_addr,sizeof(serv_addr));
bzero((char*)&cli_addr, sizeof(cli_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr=INADDR_ANY;
sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sockfd<0)
error("Problem in initializing socket");
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
error("Error on binding");
listen(sockfd,50);
int clilen=sizeof(cli_addr);
accepting:
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);
if(newsockfd<0)
error("Problem in accepting connection");
pid=fork();
if(pid==0)
{
struct sockaddr_in host_addr;
int flag=0,newsockfd1,n,port=0,i,sockfd1;
char buffer[510],t1[300],t2[300],t3[10];
char* temp=NULL;
bzero((char*)buffer,500);
recv(newsockfd,buffer,500,0);
sscanf(buffer,"%s %s %s",t1,t2,t3);
if(((strncmp(t1,"GET",3)==0))&&((strncmp(t3,"HTTP/1.1",8)==0)||(strncmp(t3,"HTTP/1.0",8)==0))&&(strncmp(t2,"http://",7)==0))
{
strcpy(t1,t2);
flag=0;
for(i=7;i<strlen(t2);i++)
{
if(t2[i]==':')
{
flag=1;
break;
}
}
temp=strtok(t2,"//");
if(flag==0)
{
port=80;
temp=strtok(NULL,"/");
}
else
{
temp=strtok(NULL,":");
}
sprintf(t2,"%s",temp);
printf("host = %s",t2);
host=gethostbyname(t2);
if(flag==1)
{
temp=strtok(NULL,"/");
port=atoi(temp);
}
strcat(t1,"^]");
temp=strtok(t1,"//");
temp=strtok(NULL,"/");
if(temp!=NULL)
temp=strtok(NULL,"^]");
printf("\npath = %s\nPort = %d\n",temp,port);
bzero((char*)&host_addr,sizeof(host_addr));
host_addr.sin_port=htons(port);
host_addr.sin_family=AF_INET;
bcopy((char*)host->h_addr,(char*)&host_addr.sin_addr.s_addr,host->h_length);
sockfd1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
newsockfd1=connect(sockfd1,(struct sockaddr*)&host_addr,sizeof(struct sockaddr));
sprintf(buffer,"\nConnected to %s IP - %s\n",t2,inet_ntoa(host_addr.sin_addr));
if(newsockfd1<0)
error("Error in connecting to remote server");
printf("\n%s\n",buffer);
//send(newsockfd,buffer,strlen(buffer),0);
bzero((char*)buffer,sizeof(buffer));
if(temp!=NULL)
sprintf(buffer,"GET /%s %s\r\nHost: %s\r\nConnection: close\r\n\r\n",temp,t3,t2);
else
sprintf(buffer,"GET / %s\r\nHost: %s\r\nConnection: close\r\n\r\n",t3,t2);
n=send(sockfd1,buffer,strlen(buffer),0);
printf("\n%s\n",buffer);
if(n<0)
error("Error writing to socket");
else{
do
{
bzero((char*)buffer,500);
n=recv(sockfd1,buffer,500,0);
if(!(n<=0))
send(newsockfd,buffer,n,0);
}while(n>0);
}
}
else
{
send(newsockfd,"400 : BAD REQUEST\nONLY HTTP REQUESTS ALLOWED",18,0);
}
close(sockfd1);
close(newsockfd);
close(sockfd);
_exit(0);
}
else
{
close(newsockfd);
goto accepting;
}
return 0;
}