I am attempting to make a proxy server, currently want to know why the iPhone I am testing it with sends a CONNECT request, gets the response of "HTTP 200 Connection Established" and usually never responds after that. (sometimes it will respond instantly with "0" or NULL)
I am not actually connecting to where the iPhone wants to go, but I am sending back a response that indicates the proxy has connected
Terminal Output:
./proxy 9000
At this point I enter my local ip in the iPhone proxy settings
Example output:
REC:
CONNECT clientmetrics.kik.com:443 HTTP/1.1
Host: clientmetrics.kik.com
User-Agent: Kik/14.4.0.11622 CFNetwork/897.15 Darwin/17.5.0
Connection: keep-alive
Proxy-Connection: keep-alive
Sent:
HTTP/1.1 200 Connection Established
REC:
// Typically \0 or a 0, or no recv takes place
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
if(argc == 2) {
int main(int argc, char* argv[]) {
system("clear");
// Set up socket, family, port, ip address
int serversocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverinfo;
serverinfo.sin_family = AF_INET;
serverinfo.sin_addr.s_addr = INADDR_ANY;
serverinfo.sin_port = htons(atoi(argv[1]));
// bind server info to socket
if(bind(serversocket, (struct sockaddr*)&serverinfo, sizeof(serverinfo)) == -1) {
printf("failed to bind port to socket, change port\n");
return -1;
}
// accept a connection
listen(serversocket, 1);
int clientsocket = accept(serversocket, NULL, NULL);
// recv and print client message
char buffer[2048];
memset(buffer, 0, sizeof(buffer));
if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
printf("Failed in recv function\n");
} else {
printf("REC\n%s\n", buffer);
}
// send Connection Established
char* sendbuffer = "HTTP/1.1 200 Connection Established";
if(send(clientsocket, sendbuffer, sizeof(sendbuffer), 0) == -1) {
printf("Failed in send function\n");
} else {
printf("Sent:\n%s\n", sendbuffer);
// Recv response from iPhone
memset(buffer, 0, sizeof(buffer));
if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
printf("Failed in recv function\n");
} else {
printf("REC:\n%s\n", buffer);
}
}
} else {
printf("Usage: ./proxy <port>\n");
return 0;
}
}
Was just messing around some more and found that the iPhone will never respond beyond my CONNECT request because the connection must be closed after the proxy responds with HTTP 200 - then the connection can be reestablished and the iphone will continue to send data