Created
June 15, 2020 17:38
-
-
Save zillwc/aa7cc9002720995e2cdc7430b88b10ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <unistd.h> | |
| #include <arpa/inet.h> | |
| int get_proxy_socket(void) { | |
| int fd = socket(AF_INET, SOCK_STREAM, 0); | |
| struct sockaddr_in bind_address; | |
| bind_address.sin_family = AF_INET; | |
| bind_address.sin_port = htons(8080); | |
| bind_address.sin_addr.s_addr = INADDR_ANY; | |
| bind(fd, & bind_address, sizeof(bind_address)); | |
| listen(fd, 1024); | |
| return fd; | |
| } | |
| int fix_crlf_endings(char * buf, int len) { | |
| int ret = len; | |
| for (int i = 0; i < len; i++) { | |
| if (buf[i] == '\r' && buf[i + 1] == '\n') { | |
| buf[i] = '\n'; | |
| /* SUPER PEDANTIC! */ | |
| for (int j = i + 1; j < len; j++) | |
| buf[j] = buf[j + 1]; | |
| ret--; | |
| } | |
| } | |
| return ret; | |
| } | |
| int main(int args, char ** argv) { | |
| int in_len, out_len, real_out, out_fd, in_fd, listen_fd = get_proxy_socket(); | |
| struct sockaddr_in to_address; | |
| char buf[32768]; | |
| to_address.sin_family = AF_INET; | |
| to_address.sin_port = htons(80); | |
| to_address.sin_addr.s_addr = inet_addr("10.0.0.1"); /* This worked for me - YMMV */ | |
| while (1) { | |
| in_fd = accept4(listen_fd, NULL, 0, 0); | |
| if (fork()) | |
| continue; | |
| out_fd = socket(AF_INET, SOCK_STREAM, 0); | |
| connect(out_fd, & to_address, sizeof(to_address)); | |
| if (fork()) { | |
| while (1) { | |
| in_len = read(in_fd, & buf, 1460); | |
| if (in_len <= 0) return 0; | |
| out_len = fix_crlf_endings( & buf, in_len); | |
| if (write(out_fd, & buf, out_len) <= 0) return 0; | |
| } | |
| } else { | |
| while (1) { | |
| if (read(out_fd, & buf, 1460) <= 0) return 0; | |
| if (write(in_fd, & buf, 1460) <= 0) return 0; | |
| } | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment