This commit is contained in:
Casey 2024-01-08 16:54:53 +03:00
parent 7533a3bae1
commit d543f68947
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 27 additions and 24 deletions

51
wsvpn.c
View File

@ -106,7 +106,6 @@ static void handle_client(struct mg_connection *connection, int event_type, void
if (connection->rem.port == 0) return; if (connection->rem.port == 0) return;
memset(connection->data, 0, 32); memset(connection->data, 0, 32);
} else if (event_type == MG_EV_HTTP_MSG) { } else if (event_type == MG_EV_HTTP_MSG) {
printf("http: %p\n", connection);
struct mg_http_message *http_message = (struct mg_http_message *) event_data; struct mg_http_message *http_message = (struct mg_http_message *) event_data;
if (mg_http_match_uri(http_message, "/open")) { if (mg_http_match_uri(http_message, "/open")) {
mg_ws_upgrade(connection, http_message, NULL); mg_ws_upgrade(connection, http_message, NULL);
@ -148,21 +147,18 @@ static void handle_client(struct mg_connection *connection, int event_type, void
} }
} else if (event_type == MG_EV_WS_OPEN) { } else if (event_type == MG_EV_WS_OPEN) {
struct mg_http_message *http_message = (struct mg_http_message *) event_data; struct mg_http_message *http_message = (struct mg_http_message *) event_data;
printf("ws open: %p\n", connection);
on_ws_connect(connection, http_message, fn_data); on_ws_connect(connection, http_message, fn_data);
} else if (event_type == MG_EV_WS_MSG) { } else if (event_type == MG_EV_WS_MSG) {
printf("websocket: %p\n", connection);
struct mg_ws_message *ws_message = (struct mg_ws_message *)event_data; struct mg_ws_message *ws_message = (struct mg_ws_message *)event_data;
on_ws_message(connection, ws_message, fn_data); on_ws_message(connection, ws_message, fn_data);
} else if (event_type == MG_EV_CLOSE) { } else if (event_type == MG_EV_CLOSE) {
printf("closed: %p\n", connection);
if (connection->is_websocket) { if (connection->is_websocket) {
on_ws_disconnect(connection, fn_data); on_ws_disconnect(connection, fn_data);
} }
} }
} }
void ws_send_error(struct mg_connection *connection, uint16_t request_id, const char *fmt, ...) { void ws_send_error(struct client *client, uint16_t request_id, const char *fmt, ...) {
static char buffer[1024]; static char buffer[1024];
memset(buffer, 0, 1024); memset(buffer, 0, 1024);
@ -181,10 +177,10 @@ void ws_send_error(struct mg_connection *connection, uint16_t request_id, const
metrics.sent_bytes += 5 + text_size; metrics.sent_bytes += 5 + text_size;
metrics.sent_messages++; metrics.sent_messages++;
metrics.errors++; metrics.errors++;
mg_ws_send(connection, buffer, 5 + text_size, WEBSOCKET_OP_BINARY); mg_ws_send(client->connection, buffer, 5 + text_size, WEBSOCKET_OP_BINARY);
} }
void ws_send_info(struct mg_connection *connection, const char *fmt, ...) { void ws_send_info(struct client *client, const char *fmt, ...) {
static char buffer[1024]; static char buffer[1024];
memset(buffer, 0, 1024); memset(buffer, 0, 1024);
@ -200,10 +196,10 @@ void ws_send_info(struct mg_connection *connection, const char *fmt, ...) {
metrics.sent_bytes += 3 + text_size; metrics.sent_bytes += 3 + text_size;
metrics.sent_messages++; metrics.sent_messages++;
mg_ws_send(connection, buffer, 3 + text_size, WEBSOCKET_OP_BINARY); mg_ws_send(client->connection, buffer, 3 + text_size, WEBSOCKET_OP_BINARY);
} }
void ws_respond(struct mg_connection *connection, uint16_t request_id, void *data, uint32_t size) { void ws_respond(struct client *client, uint16_t request_id, void *data, uint32_t size) {
static char buffer[MAX_PACKET_SIZE]; static char buffer[MAX_PACKET_SIZE];
assert(size < MAX_PACKET_SIZE); assert(size < MAX_PACKET_SIZE);
buffer[0] = 'R'; buffer[0] = 'R';
@ -212,10 +208,12 @@ void ws_respond(struct mg_connection *connection, uint16_t request_id, void *dat
if (size != 0) memcpy(&buffer[3], data, size); if (size != 0) memcpy(&buffer[3], data, size);
metrics.sent_bytes += 3 + size; metrics.sent_bytes += 3 + size;
metrics.sent_messages++; metrics.sent_messages++;
mg_ws_send(connection, buffer, size + 3, WEBSOCKET_OP_BINARY); mg_ws_send(client->connection, buffer, size + 3, WEBSOCKET_OP_BINARY);
} }
static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message, void *data) { static void on_ws_connect(struct mg_connection *connection, struct mg_http_message *message, void *data) {
(void)message;
(void)data;
struct client *client = malloc(sizeof(struct client)); struct client *client = malloc(sizeof(struct client));
memcpy(&connection->data[0], &client, sizeof(struct client *)); memcpy(&connection->data[0], &client, sizeof(struct client *));
client->connection = connection; client->connection = connection;
@ -229,8 +227,11 @@ static void on_ws_connect(struct mg_connection *connection, struct mg_http_messa
} }
static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message, void *data) { static void on_ws_message(struct mg_connection *connection, struct mg_ws_message *message, void *data) {
(void)data;
if ((message->flags & 15) != WEBSOCKET_OP_BINARY) { if ((message->flags & 15) != WEBSOCKET_OP_BINARY) {
ws_send_error(connection, -1, "This text could've been a binary."); const char *err_str = "This server only works in binary mode. Sorry!";
mg_ws_send(connection, err_str, strlen(err_str), WEBSOCKET_OP_TEXT);
connection->is_draining = 1; connection->is_draining = 1;
return; return;
} }
@ -252,7 +253,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
{ {
metrics.method_calls[0]++; metrics.method_calls[0]++;
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
printf("%p[%04x] modem.open(%d)\n", client, request_id, channel); printf("%p[%04x] modem.open(%d)\n", (void*)client, request_id, channel);
modem_open(client, request_id, channel); modem_open(client, request_id, channel);
} }
return; return;
@ -260,7 +261,7 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
{ {
metrics.method_calls[1]++; metrics.method_calls[1]++;
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
printf("%p[%04x] modem.isOpen(%d)\n", client, request_id, channel); printf("%p[%04x] modem.isOpen(%d)\n", (void*)client, request_id, channel);
modem_isOpen(client, request_id, channel); modem_isOpen(client, request_id, channel);
} }
return; return;
@ -268,14 +269,14 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
{ {
metrics.method_calls[2]++; metrics.method_calls[2]++;
uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]); uint16_t channel = ntohs(*(uint16_t*)&message->data.ptr[3]);
printf("%p[%04x] modem.close(%d)\n", client, request_id, channel); printf("%p[%04x] modem.close(%d)\n", (void*)client, request_id, channel);
modem_close(client, request_id, channel); modem_close(client, request_id, channel);
} }
return; return;
case 'C': // closeAll case 'C': // closeAll
{ {
metrics.method_calls[3]++; metrics.method_calls[3]++;
printf("%p[%04x] modem.closeAll()\n", client, request_id); printf("%p[%04x] modem.closeAll()\n", (void*)client, request_id);
modem_closeAll(client, request_id); modem_closeAll(client, request_id);
} }
return; return;
@ -289,13 +290,15 @@ static void on_ws_message(struct mg_connection *connection, struct mg_ws_message
} }
return; return;
default: default:
ws_send_error(connection, request_id, "Unknown opcode: 0x%02x", message->data.ptr[0]); ws_send_error(client, request_id, "Unknown opcode: 0x%02x", message->data.ptr[0]);
connection->is_draining = 1; connection->is_draining = 1;
return; return;
} }
} }
static void on_ws_disconnect(struct mg_connection *connection, void *data) { static void on_ws_disconnect(struct mg_connection *connection, void *data) {
(void)data;
struct client *client = *(struct client **)&connection->data[0]; struct client *client = *(struct client **)&connection->data[0];
if (client->connection == connection) { if (client->connection == connection) {
free(client); free(client);
@ -305,22 +308,22 @@ static void on_ws_disconnect(struct mg_connection *connection, void *data) {
static void modem_open(struct client *client, uint16_t request_id, uint16_t channel) { static void modem_open(struct client *client, uint16_t request_id, uint16_t channel) {
if (client_is_open(client, channel)) { if (client_is_open(client, channel)) {
ws_respond(client->connection, request_id, NULL, 0); ws_respond(client, request_id, NULL, 0);
} }
if (client->next_open_channel_index == MAX_OPEN_CHANNELS) { if (client->next_open_channel_index == MAX_OPEN_CHANNELS) {
ws_send_error(client->connection, request_id, "Too many open channels"); ws_send_error(client, request_id, "Too many open channels");
return; return;
} }
client->open_channels[client->next_open_channel_index] = channel; client->open_channels[client->next_open_channel_index] = channel;
client->next_open_channel_index++; client->next_open_channel_index++;
ws_respond(client->connection, request_id, NULL, 0); ws_respond(client, request_id, NULL, 0);
} }
static void modem_isOpen(struct client *client, uint16_t request_id, uint16_t channel) { static void modem_isOpen(struct client *client, uint16_t request_id, uint16_t channel) {
unsigned char is_open = client_is_open(client, channel) ? 42 : 0; unsigned char is_open = client_is_open(client, channel) ? 42 : 0;
ws_respond(client->connection, request_id, &is_open, 1); ws_respond(client, request_id, &is_open, 1);
} }
static void modem_close(struct client *client, uint16_t request_id, uint16_t channel) { static void modem_close(struct client *client, uint16_t request_id, uint16_t channel) {
@ -331,20 +334,20 @@ static void modem_close(struct client *client, uint16_t request_id, uint16_t cha
break; break;
} }
} }
ws_respond(client->connection, request_id, NULL, 0); ws_respond(client, request_id, NULL, 0);
} }
static void modem_closeAll(struct client *client, uint16_t request_id) { static void modem_closeAll(struct client *client, uint16_t request_id) {
client->next_open_channel_index = 0; client->next_open_channel_index = 0;
memset(client->open_channels, 0, sizeof(uint16_t) * MAX_OPEN_CHANNELS); memset(client->open_channels, 0, sizeof(uint16_t) * MAX_OPEN_CHANNELS);
ws_respond(client->connection, request_id, NULL, 0); ws_respond(client, request_id, NULL, 0);
} }
static void modem_transmit(struct client *client, uint16_t request_id, uint16_t channel, uint16_t reply_channel, void *data, uint16_t size) { static void modem_transmit(struct client *client, uint16_t request_id, uint16_t channel, uint16_t reply_channel, void *data, uint16_t size) {
static uint8_t buffer[MAX_PACKET_SIZE + 7]; static uint8_t buffer[MAX_PACKET_SIZE + 7];
if (size > MAX_PACKET_SIZE) { if (size > MAX_PACKET_SIZE) {
ws_send_error(client->connection, request_id, "Packet too big: %d > %d", size, MAX_PACKET_SIZE); ws_send_error(client, request_id, "Packet too big: %d > %d", size, MAX_PACKET_SIZE);
return; return;
} }
@ -369,7 +372,7 @@ static void modem_transmit(struct client *client, uint16_t request_id, uint16_t
} }
} }
} }
ws_respond(client->connection, request_id, NULL, 0); ws_respond(client, request_id, NULL, 0);
} }
bool client_is_open(struct client *client, uint16_t channel) { bool client_is_open(struct client *client, uint16_t channel) {