This commit is contained in:
Claudio Maggioni 2020-04-04 21:13:25 +02:00
parent 09c95f472b
commit c6a7ebe8a3
4 changed files with 182 additions and 253 deletions

BIN
a.out

Binary file not shown.

13
hw1/Makefile Normal file
View File

@ -0,0 +1,13 @@
CXXFLAGS := -std=c++17 -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 \
-lGraphicsMagick++
.PHONY: clean all
all:
${CXX} -o ./server ${CXXFLAGS} server.cc
macos:
${CXX} -o ./server -I /usr/local/include/ImageMagick-7 ${CXXFLAGS} server.cc
clean:
@rm -r ./server

18
hw1/readme.md Normal file
View File

@ -0,0 +1,18 @@
# Insidious Proxy
### Author: Claudio Maggioni
## Sources and references used to write this program:
- [UniversalPizzoccheri](https://git.maggioni.xyz/maggicl/UniversalPizzoccheri)
(own work from high-school), used for code snippets on sockets
- cppreference.com and man 3, for documentation (as usual)
- [micro_proxy](http://acme.com/software/micro_proxy/), for general inspiration for the high
level implementation and code snippets for DNS queries.
## Dependemcies:
- [GraphicsMagick++](http://www.graphicsmagick.org/project.html) for imagemagick bindings in
order to rotate the image. On macos, install with `brew install
graphicsmagick`.
## Building:
On MacOS, run `make macos`. Otherwise use simply `make`.

View File

@ -1,4 +1,22 @@
// vim: set ts=2 sw=2 et tw=80:
/*
* Insidious Proxy - Claudio Maggioni
*
* Sources and references used to write this program:
* - https://git.maggioni.xyz/maggicl/UniversalPizzoccheri (own work from
* high-school), used for code snippets on sockets
* - Cppreference.com and man 3, for documentation (as usual)
* - http://acme.com/software/micro_proxy/, for general inspiration for the high
* level implementation and code snippets for DNS queries.
*
* Dependemcies:
* - http://www.graphicsmagick.org/project.html for imagemagick bindings in
* order to rotate the image. On macos, install with 'brew install
* graphicsmagick'.
*
* Building:
* On MacOS, run 'make macos'. Otherwise use simply 'make'.
*/
#include <cctype>
#include <arpa/inet.h>
@ -14,7 +32,6 @@
#include <iostream>
#include <map>
#include <vector>
#include <curl/curl.h>
#include <netdb.h>
using namespace std;
@ -239,7 +256,7 @@ bool is_chunk_end(vector<uint8_t>& body) {
* inspecting the HTTP headers), false is returned, otherwise returns true.
*/
bool fetch_body(FILE* in, vector<uint8_t>& body,
const map<string, string> headers) {
const map<string, string> headers, bool strip_chunked) {
bool chunked;
size_t length;
@ -265,7 +282,7 @@ bool fetch_body(FILE* in, vector<uint8_t>& body,
for(size_t w = 0; w < length; w += r) {
r = fread(buf, 1, (length - w) > BUFFER ? BUFFER : (length - w), in);
if (r == -1) return false;
body.insert(end(body), begin(buf), end(buf));
body.insert(end(body), buf, buf + r);
}
} else {
// This was implemented before Prof. Carzaniga said chunked encoding is not
@ -288,7 +305,7 @@ bool fetch_body(FILE* in, vector<uint8_t>& body,
break;
}
if (will_chunk_start && is_chunk_start(buf)) {
if (will_chunk_start && is_chunk_start(buf) && strip_chunked) {
continue;
}
@ -298,9 +315,13 @@ bool fetch_body(FILE* in, vector<uint8_t>& body,
will_chunk_start = is_chunk_end(body);
}
body.insert(end(body), begin(buf), end(buf));
body.push_back(fgetc(in)); // \r
body.push_back(fgetc(in)); // \n
char a = fgetc(in), b = fgetc(in);
if (!strip_chunked) {
body.insert(end(body), begin(buf), end(buf));
body.push_back(a); // \r
body.push_back(b); // \n
}
}
return true;
@ -343,85 +364,27 @@ void send_error(FILE* in, FILE* out, const char* protocol, const int status,
}
/**
* Parses the HTTP method name given as string and sets cURL options
* accordingly. Returns false if method is not supported on unknown, true
* otherwise
*/
bool set_curl_method(CURL* c, const char* method) {
if (!strcmp(method, "GET")) {
curl_easy_setopt(c, CURLOPT_HTTPGET, 1);
} else if (!strcmp(method, "HEAD")) {
curl_easy_setopt(c, CURLOPT_HTTPGET, 1);
curl_easy_setopt(c, CURLOPT_NOBODY, 1);
} else if (!strcmp(method, "POST")) {
curl_easy_setopt(c, CURLOPT_POST, 1);
} else if (!strcmp(method, "PUT")) {
curl_easy_setopt(c, CURLOPT_PUT, 1);
} else if (!strcmp(method, "DELETE")) {
curl_easy_setopt(c, CURLOPT_CUSTOMREQUEST, "DELETE");
} else {
return false;
}
return true;
}
struct buffer {
size_t size;
uint8_t* data;
};
void buffer_init(struct buffer* self) {
self->size = 0;
self->data = (uint8_t*) malloc(8192);
if (self->data) {
self->data[0] = 0;
}
}
size_t write_buffer(void *ptr, size_t size, size_t nmemb,
struct buffer* rbody) {
size_t index = rbody->size;
size_t n = size * nmemb;
rbody->size += n;
uint8_t* newdata = (uint8_t*) realloc(rbody->data, rbody->size + 1);
if (!newdata) {
perror("reallocation of body buffer failed");
if (rbody->data) free(rbody->data);
rbody->data = NULL;
return 0;
} else {
rbody->data = newdata;
}
memcpy((rbody->data + index), ptr, n);
rbody->data[rbody->size] = '\0';
return size * nmemb;
}
/**
* Converts url in a zero-terminated string for the host name. Port is retured
* Converts url in a zero-terminated string for the host name, copied in a
* pre-allocated char array given as copy_in. Port is returned
* as integer, -1 on error
*/
int find_host_port(char* url) {
char* i = url;
int find_host_port(const char* url, char* copy_in) {
const char* i = url;
size_t c = 0;
while (*i != ':' && *i != '\0') {
while (*i != ':' && *i != '\0' && *i != '/') {
if (c >= 256 || (!isalnum(*i) && *i != '-' && *i != '.')) return -1;
*copy_in = *i;
copy_in++;
i++;
c++;
}
if (c == 0) return -1;
*i = '\0';
*copy_in = '\0';
i++;
if (*i == '\0') return 80;
if (*i == '\0' || *i == '/') return 80;
unsigned p;
if (sscanf(i, "%5u", &p) == 1) {
@ -436,7 +399,6 @@ struct forward {
int out;
};
bool total_write(uint8_t* data, size_t n, int out) {
for(size_t w = 0; w < n;) {
ssize_t written = write(out, data + w, n - w);
@ -461,7 +423,9 @@ void* forwarder_thread(void* data) {
}
if (!total_write(buffer, r, f->out)) {
cout << "Closing CONNECT forwaredr" << endl;
#if DEBUG
cerr << "Closing CONNECT forwarder" << endl;
#endif
return NULL;
}
}
@ -472,9 +436,12 @@ void* forwarder_thread(void* data) {
/**
* Returns a socket file descriptof of a newly opened socket to the given host.
*/
int open_client_socket(FILE* in, FILE* out, const char* protocol, char* host) {
int open_client_socket(FILE* in, FILE* out, const char* protocol,
const char* host) {
int port;
if ((port = find_host_port(host)) == -1) {
char hostname[257];
if ((port = find_host_port(host, hostname)) == -1) {
send_error(in, out, protocol, 500, "Hostname parse error");
}
@ -483,7 +450,7 @@ int open_client_socket(FILE* in, FILE* out, const char* protocol, char* host) {
send_error(in, out, protocol, 500, "TCP socket connection error");
}
struct hostent *he = gethostbyname(host);
struct hostent *he = gethostbyname(hostname);
if (!he) {
send_error(in, out, protocol, 404, "Unknown host");
}
@ -530,7 +497,9 @@ void handle_connect(FILE* in, FILE* out, const char* protocol, char* url,
fprintf(out, "%s %d %s\r\n\r\n", protocol, 200, "Connection established");
fflush(out);
cout << "Connection established" << endl;
#if DEBUG
cerr << "CONNECT: Connection established" << endl;
#endif
pthread_t ffrom;
pthread_create(&ffrom, NULL, forwarder_thread, &from);
@ -540,29 +509,6 @@ void handle_connect(FILE* in, FILE* out, const char* protocol, char* url,
pthread_join(ffrom, NULL);
}
struct read_buffer {
const vector<uint8_t>& body;
size_t index;
};
static size_t read_buffer(void *dest, size_t size, size_t nmemb, void *userp)
{
struct read_buffer *wt = (struct read_buffer*) userp;
size_t buffer_size = size * nmemb;
size_t remaining = wt->body.size() - wt->index;
if (buffer_size == wt->body.size()) {
return 0;
}
size_t chunk_len = remaining > buffer_size ? buffer_size : remaining;
auto start = wt->body.begin() + wt->index;
copy(start, start + chunk_len, (uint8_t*) dest);
wt->index += chunk_len;
return chunk_len;
}
void* request_thread(void* data) {
int fd = *((int*) data);
FILE* in = fdopen(dup(*((int*) data)), "r");
@ -583,8 +529,10 @@ void* request_thread(void* data) {
send_error(in, out, protocol, 400, "Bad request line");
}
cout << "METHOD: " << method << " URL: " << url << " PROTOCOL: " << protocol
#if DEBUG
cerr << "METHOD: " << method << " URL: " << url << " PROTOCOL: " << protocol
<< endl;
#endif
if (!parse_headers(in, headers)) {
send_error(in, out, protocol, 400, "Malformed header");
@ -595,198 +543,148 @@ void* request_thread(void* data) {
" URL (this is a proxy)");
}
cout << "BODY: " << endl;
bool has_body = fetch_body(in, body, headers, false);
// check if Content-Length is present and read Content-Length bytes
// check if Transfer-Encoding: chunked is present and wait for \r\n on a
// line
if (has_body(method)) {
if (!fetch_body(in, body, headers)) {
send_error(in, out, protocol, 411,
"No Content-Length or Transfer-Encoding");
}
}
cout << "PARSED" << endl;
#if DEBUG
cerr << "has_body: " << has_body << endl;
cerr << "Request body parsed" << endl;
#endif
if (!strcmp(method, "CONNECT")) {
handle_connect(in, out, protocol, url, fd);
break;
} else {
// Delete Proxy-Connection header
{
auto i = headers.find("Proxy-Connection");
if (i != headers.end()) headers.erase(i);
}
if (strncmp(url, "http://", 7) {
send_error(in. out, protocol, 400, "Protocol in URL not supported");
if (strncmp(url, "http://", 7)) {
send_error(in, out, protocol, 400, "Protocol in URL not supported");
}
char* host = url + 7;
int serverfd = open_client_socket(in, out, protocol, host);
FILE* in = fdopen(dup(serverfd), "r");
FILE* out = fdopen(dup(serverfd), "w");
FILE* s_in = fdopen(dup(serverfd), "r");
FILE* s_out = fdopen(dup(serverfd), "w");
fprintf("%s %s %s\r\n", method, url, protocol);
for (auto i = headers.begin(); i != headers.end(); i++) {
fprintf("%s: %s\r\n", i->first.c_str(), i->second.c_str());
}
/*struct buffer rhead;
struct buffer rbody;
buffer_init(&rhead);
buffer_init(&rbody);
if(!rhead.data || !rbody.data) {
send_error(in, out, protocol, 500, "Failed to allocate response buffer");
}
CURL *curl;
curl = curl_easy_init();
if (!curl) {
send_error(in, out, protocol, 500, "Cannot init CURL");
}
if (!set_curl_method(curl, method)) {
send_error(in, out, protocol, 405, "Method not implemented");
}
curl_easy_setopt(curl, CURLOPT_URL, url);
struct curl_slist *chunk = NULL;
fprintf(s_out, "%s %s %s\r\n", method, url, protocol);
for (auto i = headers.begin(); i != headers.end(); i++) {
string header = i->first + ": " + i->second;
chunk = curl_slist_append(chunk, header.c_str());
fprintf(s_out, "%s: %s\r\n", i->first.c_str(), i->second.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
fprintf(s_out, "\r\n");
if (has_body(method)) {
struct read_buffer r = { .body = body, .index = 0 };
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_buffer);
curl_easy_setopt(curl, CURLOPT_READDATA, &r);
chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
chunk = curl_slist_append(chunk, "Expect:");
}
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_buffer);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &rhead);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_buffer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rbody);
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
send_error(in, out, protocol, 502, "Request to server failed");
} else*/ {
char protocol_r[10];
char message[8000];
unsigned code;
const char* image = NULL;
const char* const PNG = "PNG";
const char* const JPEG = "JPEG";
{
cout << "RESPONSE: " << endl;
FILE* res_head = fmemopen(rhead.data, rhead.size, "r");
fscanf(res_head, "%10s %3u %8000s\r\n", protocol_r, &code, message);
cout << "STATUS: " << code << " PROTOCOL: " << protocol_r << endl;
headers.empty();
parse_headers(res_head, headers);
auto a = headers.find("Content-Type");
if (a != headers.end()) {
if (a->second == "image/jpeg") image = JPEG;
else if (a->second == "image/png") image = PNG;
}
if (has_body) {
for (auto i = body.begin(); i < body.end(); i++) {
fputc(*i, s_out);
}
}
cout << "image: " << (image ? image : "NULL") << endl;
fflush(s_out);
char protocol_r[10];
char message[8000];
unsigned code;
const char* image = NULL;
const char* const PNG = "PNG";
const char* const JPEG = "JPEG";
fscanf(s_in, "%10s %3u ", protocol_r, &code);
fgets(message, 8000, s_in);
message[strlen(message) - 2] = '\0';
#if DEBUG
cerr << "Response: STATUS: " << code << " MESSAGE: " << message << endl;
#endif
headers.clear();
parse_headers(s_in, headers);
{
auto a = headers.find("Content-Type");
if (a != headers.end()) {
if (a->second == "image/jpeg") image = JPEG;
else if (a->second == "image/png") image = PNG;
}
}
body.clear();
has_body = fetch_body(s_in, body, headers, true);
fclose(s_in);
fclose(s_out);
close(serverfd);
Magick::Blob output;
if (has_body && image) {
try {
Magick::Blob my_blob(&body[0], body.size());
Magick::Image to_rotate;
if (image) {
try {
Magick::Blob my_blob(rbody.data, rbody.size);
Magick::Blob output;
to_rotate = Magick::Image(my_blob);
} catch(Magick::Warning& ignored) {}
Magick::Image to_rotate;
try {
to_rotate.magick(image);
} catch(Magick::Warning& ignored) {}
try {
to_rotate = Magick::Image(my_blob);
} catch(Magick::Warning& ignored) {}
try {
to_rotate.magick(image);
} catch(Magick::Warning& ignored) {}
try {
switch (alteration) {
case FLIP: to_rotate.flip(); break;
case LEFT: to_rotate.rotate(-90); break;
case RIGHT: to_rotate.rotate(90); break;
}
} catch(Magick::Warning& ignored) {}
try {
to_rotate.write(&output);
} catch(Magick::Warning& ignored) {}
fprintf(out, "%s %u %s\n", protocol_r, code, message);
headers["Content-Length"] = to_string(output.length());
headers["Connection"] = "keep-alive";
for (auto i = headers.begin(); i != headers.end(); i++) {
string header = i->first + ": " + i->second + "\r\n";
fprintf(out, "%s", header.c_str());
try {
switch (alteration) {
case FLIP: to_rotate.flip(); break;
case LEFT: to_rotate.rotate(-90); break;
case RIGHT: to_rotate.rotate(90); break;
}
fprintf(out, "\r\n");
fflush(out);
} catch(Magick::Warning& ignored) {}
total_write((uint8_t*) output.data(), output.length(), fd);
} catch (Magick::Exception &error) {
cout << "Magick++ image conversion failed: " << error.what()
<< endl;
send_error(in, out, protocol, 500, "Image conversion failed");
}
} else {
total_write(rhead.data, rhead.size, fd);
total_write(rbody.data, rbody.size, fd);
fflush(out);
try {
to_rotate.write(&output);
} catch(Magick::Warning& ignored) {}
fprintf(out, "%s %u %s\n", protocol_r, code, message);
headers["Content-Length"] = to_string(output.length());
} catch (Magick::Exception &error) {
cout << "Magick++ image conversion failed: " << error.what()
<< endl;
send_error(in, out, protocol, 500, "Image conversion failed");
}
} else if (has_body) {
headers["Content-Length"] = to_string(body.size());
} else {
headers["Content-Length"] = "0";
}
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
free(rhead.data);
free(rbody.data);
}
fprintf(out, "%s %u %s\n", protocol_r, code, message);
{
auto i = headers.find("Connection");
if (i != headers.end() && i->second == "close") {
break;
headers["Connection"] = "keep-alive";
for (auto i = headers.begin(); i != headers.end(); i++) {
string header = i->first + ": " + i->second + "\r\n";
fprintf(out, "%s", header.c_str());
}
fprintf(out, "\r\n");
fflush(out);
if (has_body && image) {
total_write((uint8_t*) output.data(), output.length(), fd);
} else if (has_body) {
total_write(&body[0], body.size(), fd);
}
fflush(out);
}
}
cout << "closing data socket...\n";
fclose(in);
fflush(out);
fclose(out);
close(fd);
pthread_exit(NULL);
}