2018-09-25 17:51:56 +02:00
|
|
|
#include "http_server.h"
|
|
|
|
#include "libs/scheduler.h"
|
2018-09-26 10:30:04 +02:00
|
|
|
#include <fcntl.h>
|
2018-10-01 22:49:20 +02:00
|
|
|
static antd_scheduler_t scheduler;
|
2018-09-25 17:51:56 +02:00
|
|
|
/*
|
|
|
|
this node is a relay from the http
|
|
|
|
to https
|
|
|
|
*/
|
|
|
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
|
|
|
int server_sock = -1;
|
|
|
|
void stop_serve(int dummy) {
|
|
|
|
UNUSED(dummy);
|
2018-10-01 22:49:20 +02:00
|
|
|
antd_scheduler_destroy(&scheduler);
|
2018-09-25 17:51:56 +02:00
|
|
|
close(server_sock);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
HTTP/1.1 301 Moved Permanently
|
|
|
|
Location: http://www.example.org/
|
|
|
|
Content-Type: text/html
|
|
|
|
Content-Length: 174
|
|
|
|
*/
|
|
|
|
void* antd_redirect(void* user_data)
|
|
|
|
{
|
|
|
|
void** data = (void**)user_data;
|
|
|
|
void* client = data[0];
|
|
|
|
char* host = (char*)data[1];
|
2018-10-09 17:24:00 +02:00
|
|
|
set_status(client, 301,"Moved Permanently");
|
2018-09-25 17:51:56 +02:00
|
|
|
__t(client, "Location: https://%s", host);
|
|
|
|
__t(client, "%s", "Content-Type: text/html");
|
2018-10-09 17:24:00 +02:00
|
|
|
response(client, "");
|
|
|
|
__t(client, "This page has been moved to https://%s", host);
|
2018-09-25 17:51:56 +02:00
|
|
|
free(host);
|
|
|
|
free(user_data);
|
2019-08-22 14:12:47 +02:00
|
|
|
return antd_create_task(NULL,client, NULL, ((antd_client_t*)client)->last_io);
|
2018-09-25 17:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* antd_free_client(void* client)
|
|
|
|
{
|
|
|
|
antd_client_t * source = (antd_client_t *) client;
|
|
|
|
if(source->ip) free(source->ip);
|
|
|
|
close(source->sock);
|
|
|
|
free(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* antd_get_host(void * client)
|
|
|
|
{
|
|
|
|
char buff[1024];
|
|
|
|
char* line, *token;
|
|
|
|
char* host = NULL;
|
|
|
|
while((read_buf(client,buff,sizeof(buff))) && strcmp("\r\n",buff))
|
|
|
|
{
|
|
|
|
line = buff;
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
token = strsep(&line, ":");
|
|
|
|
trim(token,' ');
|
|
|
|
trim(line,' ');
|
|
|
|
if(token && strcasecmp(token,"Host")==0)
|
|
|
|
if(line)
|
|
|
|
{
|
|
|
|
host = strdup(line);
|
2018-10-09 17:24:00 +02:00
|
|
|
//break;
|
2018-09-25 17:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!host) host = strdup("lxsang.me");
|
|
|
|
void** data = (void**)malloc(2*(sizeof *data));
|
|
|
|
data[0] = client;
|
|
|
|
data[1] = (void*)host;
|
2018-09-26 10:30:04 +02:00
|
|
|
LOG("[%s] Request for: %s --> https://%s\n", ((antd_client_t*)client)->ip, host, host);
|
2019-08-22 14:12:47 +02:00
|
|
|
return antd_create_task(antd_redirect,data, NULL, time(NULL));
|
2018-09-25 17:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
// load the config first
|
|
|
|
unsigned port = 80;
|
|
|
|
int client_sock = -1;
|
|
|
|
struct sockaddr_in client_name;
|
|
|
|
socklen_t client_name_len = sizeof(client_name);
|
|
|
|
// ignore the broken PIPE error when writing
|
|
|
|
//or reading to/from a closed socked connection
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGABRT, SIG_IGN);
|
|
|
|
signal(SIGINT, stop_serve);
|
|
|
|
server_sock = startup(&port);
|
2018-09-27 17:18:31 +02:00
|
|
|
//struct timeval timeout;
|
|
|
|
//timeout.tv_sec = 0;
|
|
|
|
//timeout.tv_usec = 500;
|
2018-09-26 10:30:04 +02:00
|
|
|
// 0 worker
|
2018-10-01 22:49:20 +02:00
|
|
|
antd_scheduler_init(&scheduler, 0);
|
2019-08-22 14:12:47 +02:00
|
|
|
scheduler.validate_data = 1;
|
|
|
|
scheduler.destroy_data = antd_free_client;
|
2018-09-26 10:30:04 +02:00
|
|
|
// set server socket to non blocking
|
2018-10-09 17:24:00 +02:00
|
|
|
set_nonblock(server_sock);
|
2018-09-26 10:30:04 +02:00
|
|
|
LOG("relayd running on port %d\n", port);
|
2018-10-09 17:24:00 +02:00
|
|
|
struct timespec ts_sleep;
|
2018-10-01 22:49:20 +02:00
|
|
|
while (scheduler.status)
|
2018-09-25 17:51:56 +02:00
|
|
|
{
|
2018-09-26 10:30:04 +02:00
|
|
|
// execute task
|
2018-10-09 17:24:00 +02:00
|
|
|
int stat = antd_task_schedule(&scheduler);
|
2018-09-25 17:51:56 +02:00
|
|
|
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
|
|
|
if (client_sock == -1)
|
|
|
|
{
|
2018-10-09 17:24:00 +02:00
|
|
|
// sleep for 500usec if
|
|
|
|
// there is nothing todo
|
|
|
|
if(!stat)
|
|
|
|
{
|
|
|
|
ts_sleep.tv_sec = 0;
|
|
|
|
ts_sleep.tv_nsec = 5000000;
|
|
|
|
nanosleep(&ts_sleep, NULL);
|
|
|
|
}
|
|
|
|
|
2018-09-25 17:51:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-09 17:24:00 +02:00
|
|
|
set_nonblock(client_sock);
|
2018-09-25 17:51:56 +02:00
|
|
|
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
|
|
|
|
// set timeout to socket
|
|
|
|
|
2018-09-27 15:00:19 +02:00
|
|
|
//if (setsockopt (client_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
|
|
|
|
// perror("setsockopt failed\n");
|
2018-09-25 17:51:56 +02:00
|
|
|
|
2018-09-27 15:00:19 +02:00
|
|
|
//if (setsockopt (client_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
|
|
|
|
// perror("setsockopt failed\n");
|
2018-09-25 17:51:56 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
get the remote IP
|
|
|
|
*/
|
|
|
|
client->ip = NULL;
|
|
|
|
if (client_name.sin_family == AF_INET)
|
|
|
|
client->ip = strdup(inet_ntoa(client_name.sin_addr));
|
|
|
|
client->sock = client_sock;
|
|
|
|
//accept_request(&client);
|
2019-08-22 14:12:47 +02:00
|
|
|
antd_add_task(&scheduler, antd_create_task(antd_get_host,(void*)client, antd_free_client, client->last_io));
|
2018-09-25 17:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|