I am working on a C program to connect to a proxy and then through the proxy pull data / send data to the website. I am confused however on what to do after I get the “CONNECT” statement to work.
My current Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define maxlen 2048
int main(int argc, char *argv[])
{
int mysocket;
int len;
char buffer[2000];
char msg[] = "CONNECT http://example.com:80/ HTPP/1.0\r\n\r\n";
mysocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in dest;
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("101.255.60.162");
dest.sin_port = htons(3128);
connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
send(mysocket, msg, strlen(msg), 0);
len = recv(mysocket, buffer, maxlen, 0);
buffer[len] = '\0';
printf("%s \n", buffer);
close(mysocket);
return 0;
I tried sending another command after that but when I send a second command it goes straight to the site and not through the proxy.
Thanks in advance!!
First of all forget about the code and learn the packet flow. Use any packet capturing tool like wireshark for seeing the traffic. Setup a proxy in the browser and open some http site and observe first connect packet to proxy in wireshark. Select the connect packet, right click on it and select “Follow tcp stream”. That will open a new window and you can see the complete flow of packets there for that http request.
Once you know the packet flow then rest of packets can be sent and received in the same way you have made the first CONNECT packet.