cleanup code

This commit is contained in:
lxsang 2020-08-19 12:26:17 +02:00
parent 5765df3697
commit 2041ee2ba0
12 changed files with 22 additions and 482 deletions

View File

@ -6,7 +6,7 @@ After=network.target
Type=simple
User=root
WorkingDirectory=/opt/www
ExecStart=ant-d
ExecStart=/usr/bin/ant-d
Restart=always
LimitNOFILE=65536

Binary file not shown.

View File

@ -51,13 +51,16 @@ void destroy_config()
for_each_assoc(it, server_config.ports)
{
cnf = (port_config_t*)it->value;
if(cnf && cnf->htdocs)
free(cnf->htdocs);
if(cnf->sock > 0)
if(cnf != NULL)
{
close(cnf->sock);
if(cnf->htdocs != NULL)
free(cnf->htdocs);
if(cnf->sock > 0)
{
close(cnf->sock);
}
freedict(cnf->rules);
}
freedict(cnf->rules);
}
freedict(server_config.ports);
}

12
httpd.c
View File

@ -13,18 +13,14 @@ static antd_scheduler_t scheduler;
static int ssl_session_ctx_id = 1;
SSL_CTX *ctx;
void init_openssl()
static void init_openssl()
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_context()
static SSL_CTX *create_context()
{
const SSL_METHOD *method;
SSL_CTX *ctx;
@ -68,7 +64,7 @@ static int alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *ou
}
}
#endif
void configure_context(SSL_CTX *ctx)
static void configure_context(SSL_CTX *ctx)
{
#if defined(SSL_CTX_set_ecdh_auto)
SSL_CTX_set_ecdh_auto(ctx, 1);
@ -121,7 +117,7 @@ void configure_context(SSL_CTX *ctx)
#endif
void stop_serve(int dummy) {
static void stop_serve(int dummy) {
UNUSED(dummy);
// close log server
closelog ();

View File

@ -280,7 +280,11 @@ int antd_send(void *src, const void* data_in, int len_in)
uint8_t* data = (uint8_t*)data_in;
int len = len_in;
antd_client_t * source = (antd_client_t *) src;
if(!src || !data)
{
return -1;
}
#ifdef USE_ZLIB
if(source->zstream && source->z_level != ANTD_CNONE)
{
@ -314,11 +318,6 @@ int antd_send(void *src, const void* data_in, int len_in)
}
#endif
if(!src || !data)
{
return -1;
}
int written;
char* ptr;
int writelen = 0;

View File

@ -1,198 +0,0 @@
//#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
//#define __USE_BSD #define _BSD_SOURCE
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <string.h>
//#include <strings.h>
int read_buf(int fd, char*buf,int size)
{
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = read(fd, &c, 1);
if (n > 0)
{
buf[i] = c;
i++;
}
else if(n == -1) return n;
else
c = '\n';
}
buf[i] = '\0';
return i;
}
int main(int ac, char *av[])
{
int fdm, fds;
int rc;
char input[150];
// Check arguments
fdm = posix_openpt(O_RDWR);
if (fdm < 0)
{
fprintf(stderr, "Error %d on posix_openpt()\n", errno);
return 1;
}
rc = grantpt(fdm);
if (rc != 0)
{
fprintf(stderr, "Error %d on grantpt()\n", errno);
return 1;
}
rc = unlockpt(fdm);
if (rc != 0)
{
fprintf(stderr, "Error %d on unlockpt()\n", errno);
return 1;
}
// Open the slave side ot the PTY
fds = open(ptsname(fdm), O_RDWR);
// Create the child process
if (fork())
{
fd_set fd_in;
// FATHER
// Close the slave side of the PTY
close(fds);
while (1)
{
// Wait for data from standard input and master side of PTY
FD_ZERO(&fd_in);
FD_SET(0, &fd_in);
FD_SET(fdm, &fd_in);
rc = select(fdm + 1, &fd_in, NULL, NULL, NULL);
switch(rc)
{
case -1 : fprintf(stderr, "Error %d on select()\n", errno);
exit(1);
default :
{
// If data on standard input
if (FD_ISSET(0, &fd_in))
{
rc = read(0, input, sizeof(input));
if (rc > 0)
{
// Send data on the master side of PTY
//printf("DATA sent\n");
write(fdm, input, rc);
}
else
{
if (rc < 0)
{
fprintf(stderr, "Error %d on read standard input\n", errno);
exit(1);
}
}
}
// If data on master side of PTY
if (FD_ISSET(fdm, &fd_in))
{
rc = read(fdm, input, sizeof(input));
if (rc > 0)
{
// Send data on standard output
write(1, input, rc);
}
else
{
if (rc <= 0)
{
fprintf(stderr, "Error %d on read master PTY\n", errno);
exit(1);
}
}
}
}
} // End switch
} // End while
}
else
{
struct termios slave_orig_term_settings; // Saved terminal settings
struct termios new_term_settings; // Current terminal settings
// CHILD
// Close the master side of the PTY
close(fdm);
// Save the defaults parameters of the slave side of the PTY
rc = tcgetattr(fds, &slave_orig_term_settings);
// Set RAW mode on slave side of PTY
new_term_settings = slave_orig_term_settings;
cfmakeraw (&new_term_settings);
tcsetattr (fds, TCSANOW, &new_term_settings);
// The slave side of the PTY becomes the standard input and outputs of the child process
close(0); // Close standard input (current terminal)
close(1); // Close standard output (current terminal)
close(2); // Close standard error (current terminal)
dup(fds); // PTY becomes standard input (0)
dup(fds); // PTY becomes standard output (1)
dup(fds); // PTY becomes standard error (2)
// Now the original file descriptor is useless
close(fds);
// Make the current process a new session leader
setsid();
// As the child is a session leader, set the controlling terminal to be the slave side of the PTY
// (Mandatory for programs like the shell to make them manage correctly their outputs)
ioctl(0, TIOCSCTTY, 1);
system("/bin/bash");
/*fd_set child_fds;
char cinput[150];
while(1){
//printf("Wait for data\n");
FD_ZERO(&child_fds);
FD_SET(0, &child_fds);
rc = select(1, &child_fds,NULL, NULL, NULL);
if(rc == -1)
{
break;
}
else
{
if (FD_ISSET(0, &child_fds))
{
rc = read_buf(0, cinput, sizeof(cinput));
if (rc > 0)
{
system(cinput);
} else if(rc < 0) break;
}
}
}*/
// if Error...
_exit(1);
}
return 0;
}

View File

@ -1,108 +0,0 @@
// network support
#include <netinet/in.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
#include <sys/socket.h>
#include <time.h>
#include <resolv.h>
#include "../plugin.h"
#define RQ
#define REQUEST_PATTERN "POST /node_register HTTP/1.0\r\nHost: antd\r\nUser-Agent: antd\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s"
#define JSON_MSG "{\"ip\":\"%s\",\"port\":\"%d\"}"
void init();
struct master_conf_t{
int port;
char* ip;
} ;
struct master_conf_t mconfig;
call __init__ = init;
/*char* get_ip_address()
{
struct ifaddrs* addrs;
getifaddrs(&addrs);
struct ifaddrs* tmp = addrs;
char* ip;
while (tmp)
{
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
ip = inet_ntoa(pAddr->sin_addr);
if(strcmp(ip,"127.0.0.1") != 0)
return ip;
}
tmp = tmp->ifa_next;
}
freeifaddrs(addrs);
return "127.0.0.1";
}*/
int inform_master()
{
int sockfd;
//rpc_response_t* rdata = NULL;
char* request;
char* data = __s(JSON_MSG,get_ip_address(),__plugin__.sport);
while((sockfd = request_socket(mconfig.ip, mconfig.port)) == -1)
{
// wait for 3s and then request to server
usleep(3000000);
}
request = __s(REQUEST_PATTERN, strlen(data), data);
send(sockfd,request, strlen(request),0);
//rdata = parse_response(sockfd);
close(sockfd);
LOG("%s","OK, master registered \n");
free(request);
return 0;
}
static int config_handler(void* conf, const char* section, const char* name,
const char* value)
{
struct master_conf_t* pconfig = (struct master_conf_t*)conf;
char * ppath = NULL;
if (strcmp(name, "port") == 0) {
pconfig->port = atoi(value);
} else if (strcmp(name, "ip") == 0) {
pconfig->ip = strdup(value);
} else {
return 0;
}
return 1;
}
void read_config()
{
mconfig.ip = "127.0.0.1";
mconfig.port = 8080;
char* file = __s("%s%s%s.ini",config_dir(), DIR_SEP, __plugin__.name);
if (ini_parse(file, config_handler, &mconfig) < 0) {
LOG("Can't load '%s'\n. Used defaut configuration", file);
}
printf("%s %d\n",mconfig.ip, mconfig.port );
}
void init()
{
read_config();
pthread_t newthread;
if (pthread_create(&newthread , NULL,(void(*)()) inform_master, NULL) != 0)
perror("pthread_create: cannot create daemon for finding master");
else
{
//reclaim the stack data when thread finish
pthread_detach(newthread) ;
}
}
void pexit()
{
LOG("%s","EXIT daemon");
}
void handler(void* c, const char* m, const char* rqp, dictionary d)
{
text(c);
__t(c,"This is a system plugin. It cant be acessed from the web");
}

View File

@ -118,7 +118,7 @@ static void destroy_queue(antd_task_queue_t q)
free(curr);
}
}
static void work(antd_worker_t* worker)
static void* work(antd_worker_t* worker)
{
antd_scheduler_t* scheduler = (antd_scheduler_t*) worker->manager;
while(scheduler->status)
@ -142,6 +142,7 @@ static void work(antd_worker_t* worker)
}
}
return NULL;
}
/*

View File

@ -1,10 +0,0 @@
#include "utils.h"
int main(int argc, char const *argv[])
{
char* v = url_decode("code=3%2B4");
if(match_float(argv[1]))
printf("It is a float\n");
printf("Result is %s\n",v);
return 0;
}

View File

@ -485,8 +485,7 @@ char* __s(const char* fstring,...)
int dlen;
va_start( arguments, fstring);
dlen = vsnprintf(0,0,fstring,arguments) + 1;
va_end(arguments);
va_end(arguments);
va_end(arguments);
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
{
va_start(arguments, fstring);

View File

@ -374,8 +374,7 @@ void ws_client_close(ws_client_t* wsclient)
#ifdef USE_OPENSSL
if(wsclient->ssl_ctx)
{
if(wsclient->ssl_ctx)
SSL_CTX_free(wsclient->ssl_ctx);
SSL_CTX_free(wsclient->ssl_ctx);
FIPS_mode_set(0);
// DEPRECATED: CONF_modules_unload(1);
EVP_cleanup();

141
relay.c
View File

@ -1,141 +0,0 @@
#include "http_server.h"
#include "libs/scheduler.h"
#include <fcntl.h>
static antd_scheduler_t scheduler;
/*
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);
antd_scheduler_destroy(&scheduler);
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];
set_status(client, 301,"Moved Permanently");
__t(client, "Location: https://%s", host);
__t(client, "%s", "Content-Type: text/html");
response(client, "");
__t(client, "This page has been moved to https://%s", host);
free(host);
free(user_data);
return antd_create_task(NULL,client, NULL, ((antd_client_t*)client)->last_io);
}
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);
//break;
}
}
if(!host) host = strdup("lxsang.me");
void** data = (void**)malloc(2*(sizeof *data));
data[0] = client;
data[1] = (void*)host;
LOG("[%s] Request for: %s --> https://%s\n", ((antd_client_t*)client)->ip, host, host);
return antd_create_task(antd_redirect,data, NULL, time(NULL));
}
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);
//struct timeval timeout;
//timeout.tv_sec = 0;
//timeout.tv_usec = 500;
// 0 worker
antd_scheduler_init(&scheduler, 0);
scheduler.validate_data = 1;
scheduler.destroy_data = antd_free_client;
// set server socket to non blocking
set_nonblock(server_sock);
LOG("relayd running on port %d\n", port);
struct timespec ts_sleep;
while (scheduler.status)
{
// execute task
int stat = antd_task_schedule(&scheduler);
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
if (client_sock == -1)
{
// sleep for 500usec if
// there is nothing todo
if(!stat)
{
ts_sleep.tv_sec = 0;
ts_sleep.tv_nsec = 5000000;
nanosleep(&ts_sleep, NULL);
}
continue;
}
set_nonblock(client_sock);
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
// set timeout to socket
//if (setsockopt (client_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
// perror("setsockopt failed\n");
//if (setsockopt (client_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(timeout)) < 0)
// perror("setsockopt failed\n");
/*
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;
time(&client->last_io);
//accept_request(&client);
antd_add_task(&scheduler, antd_create_task(antd_get_host,(void*)client, antd_free_client, client->last_io));
}
return(0);
}