Skip to content

Commit

Permalink
none
Browse files Browse the repository at this point in the history
  • Loading branch information
sdk250 committed Jul 12, 2023
1 parent 2d901ef commit aebb415
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 82 deletions.
Binary file modified sock
Binary file not shown.
293 changes: 214 additions & 79 deletions sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,231 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define SIZE 0x4F0
#define SIZE 0x2000

long int process(int, const char*, char*);
int setNonBlocking(int);

int main(int argc, char** argv) {
int fd = 0;
int server_fd = 0;
unsigned short int port = 80;
struct sockaddr_in addr = {0}, server_addr = {0};
char* url = NULL;
char* data = NULL;
char* buffer = NULL;
int fd = 0;
int server_fd = 0;
unsigned short int port = 80;
struct sockaddr_in addr = {0}, server_addr = {0};
char* url = NULL;
char* data = NULL;
char* buffer = NULL;

if (argc >= 4) {
url = argv[1];
port = atoi(argv[2]);
} else
exit(EXIT_FAILURE);
buffer = (char*) malloc(sizeof(char) * SIZE);
data = (char*) malloc(sizeof(char) * SIZE);
snprintf(data, SIZE, "CONNECT %s:%uHTTP/1.1\r\nHost: 153.3.236.22\r\nUser-Agent: baiduboxapp\r\nX-T5-Auth: 683556443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n", url, port);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("Create socket file object: %d\n", fd);
addr.sin_family = AF_INET;
addr.sin_port = htons(443);
printf("%d\n", inet_pton(AF_INET, "153.3.236.22", &(addr.sin_addr.s_addr)));
// server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// printf("Create socket file object of server: %d\n", server_fd);
// server_addr.sin_family = AF_INET;
// server_addr.sin_port = htons(8080);
// printf("Server address return: %d\n", inet_pton(AF_INET, ""
// if (argc >= 4) {
// url = argv[1];
// port = atoi(argv[2]);
// } else
// exit(EXIT_FAILURE);
if ((server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Create server socket file descriptor fail");
close(server_fd);
exit(EXIT_FAILURE);
}
buffer = (char*) malloc(sizeof(char) * SIZE);
data = (char*) malloc(sizeof(char) * SIZE);
memset(&server_addr, '\0', sizeof(struct sockaddr));
memset(buffer, '\0', SIZE);
memset(data, '\0', SIZE);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[1]));
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_fd, (const struct sockaddr*)&server_addr, sizeof(struct sockaddr)) < 0) {
perror("Bind 0.0.0.0:8080 error");
free(buffer);
free(data);
exit(EXIT_FAILURE);
close(server_fd);
}
if (listen(server_fd, 1) < 0) {
perror("listen");
free(buffer);
free(data);
exit(EXIT_FAILURE);
close(server_fd);
}
for (int i = 0; i < 6; i++) {
struct sockaddr_in client_addr = {0};
socklen_t len = sizeof(struct sockaddr);
int client_fd = 0;
char* url = (char*) malloc(sizeof(char) * SIZE);
char* _buffer = (char*) malloc(sizeof(char) * SIZE);

memset(&client_addr, '\0', sizeof(struct sockaddr));
memset(data, '\0', SIZE);
memset(url, '\0', SIZE);

if ((client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &len)) < 0) {
perror("Accept user");
continue;
}
printf("Accept %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
for (int i = 0, total = 0; (i = read(client_fd, buffer + total, SIZE - total)); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
memcpy(_buffer, buffer, SIZE);
char* s = strchr(_buffer, ' ') + 1;
*(s + (strchr(s + 1, ' ') - s)) = '\0';
puts(s);
if (strstr(s, "http://")) {
s += 7;
char* p = NULL;
if ((p = strchr(s, '/'))) {
strncpy(url, s, p - s);
strcat(url, ":80");
strcat(url, p);
} else {
strncpy(url, s, strlen(s));
strcat(url, ":80");
}
} else {
strcpy(url, s);
// s += 9;
// puts(s);
// char* p = NULL;
// if ((p = strchr(s, '/'))) {
// strncpy(url, s, p - s);
// strcat(url, ":443");
// strcat(url, p);
// } else {
// strncpy(url, s, strlen(s));
// strcat(url, ":443");
// }
}
printf("S1: %s\n\n\n", url);
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Create socket for zl");
close(client_fd);
free(url);
free(_buffer);
close(fd);
break;
};
addr.sin_family = AF_INET;
addr.sin_port = htons(443);
inet_pton(AF_INET, "153.3.236.22", &(addr.sin_addr.s_addr));
if ((connect(fd, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
perror("Connect zl");
close(client_fd);
free(url);
free(_buffer);
close(fd);
break;
}
// printf("Return: %d\n", setNonBlocking(fd));
memset(data, '\0', SIZE);
memset(_buffer, '\0', SIZE);
snprintf(data, SIZE, "CONNECT %sHTTP/1.1\r\nHost: 153.3.236.22\r\nUser-Agent: baiduboxapp\r\nX-T5-Auth: 683556443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n", url);
for (int i = 0, total = 0; (i = write(fd, data, strlen(data))); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
for (int i = 0, total = 0; (i = read(fd, _buffer, SIZE)); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
puts(_buffer);
memset(_buffer, '\0', SIZE);
for (int i = 0, total = 0; (i = write(fd, buffer, strlen(buffer))); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
puts(buffer);
for (int i = 0, total = 0; (i = read(fd, _buffer, SIZE)); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
puts(_buffer);
for (int i = 0, total = 0; (i = write(client_fd, _buffer, strlen(_buffer))); total += i > 0 ? i : 0) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
close(client_fd);
free(url);
free(_buffer);
close(fd);
}

// snprintf(data, SIZE, "CONNECT %s:%uHTTP/1.1\r\nHost: 153.3.236.22\r\nUser-Agent: baiduboxapp\r\nX-T5-Auth: 683556443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n", url, port);
// fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// printf("Create socket file object: %d\n", fd);
// addr.sin_family = AF_INET;
// addr.sin_port = htons(443);
// printf("%d\n", inet_pton(AF_INET, "153.3.236.22", &(addr.sin_addr.s_addr)));

printf("Connect return: %d\n", connect(fd, (struct sockaddr *)&addr, sizeof(addr)));
printf("Return: %d\n", setNonBlocking(fd));
printf("Received: %ld\n", process(fd, data, buffer));
puts(buffer);
memset(data, '\0', SIZE);
memset(buffer, '\0', SIZE);
for (int i = 3; i < argc; i++) {
strcat(data, argv[i]);
strcat(data, (i == argc - 1) ? "\r\n\r\n" : "\r\n");
}
printf("Received: %ld\n", process(fd, data, buffer));
puts(buffer);
// memset(data, '\0', SIZE);
// memset(buffer, '\0', SIZE);
// strcpy(data, "GET /get?a=0 HTTP/1.1\r\nHost: 54.165.93.75\r\nConnection: Keep-Alive\r\n\r\n");
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
// memset(data, '\0', SIZE);
// memset(buffer, '\0', SIZE);
// strcpy(data, "POST /post HTTP/1.1\r\nHost: 54.165.93.75\r\nAccept: application/json\r\nConnection: Close\r\n\r\n{\"a\":0}");
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
close(fd);
free(buffer);
free(data);
puts("Done.");
// printf("Connect return: %d\n", connect(fd, (struct sockaddr *)&addr, sizeof(addr)));
// printf("Return: %d\n", setNonBlocking(fd));
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
// memset(data, '\0', SIZE);
// memset(buffer, '\0', SIZE);
// for (int i = 3; i < argc; i++) {
// strcat(data, argv[i]);
// strcat(data, (i == argc - 1) ? "\r\n\r\n" : "\r\n");
// }
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
// memset(data, '\0', SIZE);
// memset(buffer, '\0', SIZE);
// strcpy(data, "GET /get?a=0 HTTP/1.1\r\nHost: 54.165.93.75\r\nConnection: Keep-Alive\r\n\r\n");
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
// memset(data, '\0', SIZE);
// memset(buffer, '\0', SIZE);
// strcpy(data, "POST /post HTTP/1.1\r\nHost: 54.165.93.75\r\nAccept: application/json\r\nConnection: Close\r\n\r\n{\"a\":0}");
// printf("Received: %ld\n", process(fd, data, buffer));
// puts(buffer);
// close(fd);
free(buffer);
free(data);
puts("Done.");

return 0;
return 0;
}

long int process(int fd, const char* msg, char* buffer) {
puts(msg);
long int total_recv = 0;
long int total_send = 0;
for (
int i = 0;
(i = write(fd, msg, strlen(msg)));
total_send += (i > 0) ? i : 0
) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
printf("Sended: %ld\n", total_send);
for (
int i = 0;
(i = read(fd, buffer + total_recv, SIZE - total_recv));
total_recv += (i > 0) ? i : 0
) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
sleep(1);
} else
break;
}
*(buffer + total_recv - 1) = '\0';
return total_recv;
// puts(msg);
long int total_recv = 0;
long int total_send = 0;
for (
int i = 0;
(i = write(fd, msg, strlen(msg)));
total_send += i > 0 ? i : 0
) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
sleep(1);
else
break;
}
for (
int i = 0;
(i = read(fd, buffer + total_recv, SIZE - total_recv));
total_recv += i > 0 ? i : 0
) {
if (i == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
sleep(1);
} else
break;
}
*(buffer + total_recv - 1) = '\0';
printf("Received: %ld\n", total_recv);
return total_recv;
}

int setNonBlocking(int sockfd) {
Expand Down
6 changes: 3 additions & 3 deletions sock.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(("127.0.0.1", 8080))
sock.send(b"GET / HTTP/1.1\r\nUser-Agent: C/Socket\r\n\r\n")
sock.connect(("153.3.236.22", 443))
sock.send(b"CONNECT httpbin.org:443/getHTTP/1.1\r\nHost: 153.3.236.22\r\nUser-Agent: baiduboxapp\r\nX-T5-Auth: 683556443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n")
print(sock.recv(100))
sock.send(b"GET /get HTTP/1.1\r\nHost: 54.204.94.184\r\nConnection: Keep-Alive\r\n\r\n")
sock.send(b"GET /get HTTP/1.1\r\nHost: httpbin.org\r\nConnection: Keep-Alive\r\n\r\n")
print(sock.recv(0x1FF).decode())

0 comments on commit aebb415

Please sign in to comment.