|
|
|
@ -1,17 +1,20 @@
|
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
#include "json/layout.h"
|
|
|
|
|
|
|
|
|
|
#include "controller/cli.h"
|
|
|
|
|
#include "process.h"
|
|
|
|
|
|
|
|
|
|
// Submodules
|
|
|
|
|
#include "json/layout.h"
|
|
|
|
|
#include "form.h"
|
|
|
|
|
|
|
|
|
|
// Platform libraries
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#define OBJ_TYPE struct app_args
|
|
|
|
|
static struct json_object_field app_args_layout[] = {
|
|
|
|
|
JSON_FIELD_STRING( domain, true ),
|
|
|
|
|
JSON_FIELD_STRING( icann_domain, false ),
|
|
|
|
|
JSON_FIELD_STRING( user_agent, false ),
|
|
|
|
|
{
|
|
|
|
|
.key = "addr",
|
|
|
|
@ -29,8 +32,10 @@ static struct json_object_field app_args_layout[] = {
|
|
|
|
|
JSON_FIELD_INTEGER( outbox_discard_limit, false ),
|
|
|
|
|
|
|
|
|
|
JSON_FIELD_BOOL( develop, false ),
|
|
|
|
|
JSON_FIELD_BOOL( enabled, false ),
|
|
|
|
|
JSON_FIELD_BOOL( disabled, false ),
|
|
|
|
|
JSON_FIELD_BOOL( configured, false ),
|
|
|
|
|
|
|
|
|
|
JSON_FIELD_INTEGER( setup_wizard_step, false ),
|
|
|
|
|
JSON_FIELD_END,
|
|
|
|
|
};
|
|
|
|
|
#undef OBJ_TYPE
|
|
|
|
@ -55,28 +60,40 @@ void app_args_refresh_tor_hidden_service()
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
struct app_args* app_args_new( int argc, char** argv )
|
|
|
|
|
void app_args_load()
|
|
|
|
|
{
|
|
|
|
|
struct app_args* args = (struct app_args*)malloc( sizeof(struct app_args) );
|
|
|
|
|
memset(args,0,sizeof(*args));
|
|
|
|
|
if( !args ) { return NULL; }
|
|
|
|
|
g_server->http_settings.bind_port = 9000 + ( rand() % 41000 );
|
|
|
|
|
g_server->http_settings.bind_address = strdup("0.0.0.0");
|
|
|
|
|
g_server->tor_socks_port = ( rand() % 41000 ) + 9000;
|
|
|
|
|
g_server->section = -1;
|
|
|
|
|
g_server->outbox_discard_limit = 5;
|
|
|
|
|
g_server->user_agent = strdup( "Apogee/0.1" );
|
|
|
|
|
g_server->disabled = true;
|
|
|
|
|
|
|
|
|
|
json_read_object_layout_from_file( "data/server.json", app_args_layout, g_server );
|
|
|
|
|
|
|
|
|
|
g_server = args;
|
|
|
|
|
app_args_refresh_tor_hidden_service();
|
|
|
|
|
|
|
|
|
|
args->http_settings.bind_port = 9000 + ( rand() % 41000 );
|
|
|
|
|
args->http_settings.bind_address = strdup("0.0.0.0");
|
|
|
|
|
args->tor_socks_port = ( rand() % 41000 ) + 9000;
|
|
|
|
|
args->section = -1;
|
|
|
|
|
args->outbox_discard_limit = 5;
|
|
|
|
|
args->user_agent = strdup( "Apogee/0.1" );
|
|
|
|
|
if( g_server->icann_domain ) {
|
|
|
|
|
g_server->domain = strdup(g_server->icann_domain);
|
|
|
|
|
} else if( g_server->tor_hidden_service ) {
|
|
|
|
|
g_server->domain = strdup(g_server->tor_hidden_service);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
struct app_args* app_args_new( int argc, char** argv )
|
|
|
|
|
{
|
|
|
|
|
if( !g_server ) {
|
|
|
|
|
struct app_args* args = (struct app_args*)malloc( sizeof(struct app_args) );
|
|
|
|
|
memset(args,0,sizeof(*args));
|
|
|
|
|
if( !args ) { return NULL; }
|
|
|
|
|
|
|
|
|
|
json_read_object_layout_from_file( "data/server.json", app_args_layout, args );
|
|
|
|
|
g_server = args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app_args_refresh_tor_hidden_service();
|
|
|
|
|
app_args_load();
|
|
|
|
|
|
|
|
|
|
if( ( argc > 1 ) && ( 0 != strncmp(argv[1],"--",2) ) ) {
|
|
|
|
|
handle_command( argv, argc );
|
|
|
|
|
free(args);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -84,14 +101,14 @@ struct app_args* app_args_new( int argc, char** argv )
|
|
|
|
|
const char* arg = argv[i];
|
|
|
|
|
|
|
|
|
|
// Debug flag
|
|
|
|
|
if( 0 == strcmp(argv[i],"--debug") ) { args->debug = true; goto next_arg; }
|
|
|
|
|
if( 0 == strcmp(argv[i],"--debug") ) { g_server->debug = true; goto next_arg; }
|
|
|
|
|
|
|
|
|
|
// Sections by number
|
|
|
|
|
if( sscanf(arg,"--section=%d",&args->section) ) { goto next_arg; }
|
|
|
|
|
if( sscanf(arg,"--section=%d",&g_server->section) ) { goto next_arg; }
|
|
|
|
|
|
|
|
|
|
// Override server port
|
|
|
|
|
if( sscanf(arg,"--listen=%d",&args->http_settings.bind_port) ) {
|
|
|
|
|
printf( "Listening on port %d\n", args->http_settings.bind_port );
|
|
|
|
|
if( sscanf(arg,"--listen=%d",&g_server->http_settings.bind_port) ) {
|
|
|
|
|
printf( "Listening on port %d\n", g_server->http_settings.bind_port );
|
|
|
|
|
goto next_arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -99,7 +116,7 @@ struct app_args* app_args_new( int argc, char** argv )
|
|
|
|
|
if( ( argv[i][0] == '-' ) && ( argv[i][1] == '-' ) ) {
|
|
|
|
|
for( int i = 0; i <= process_get_max_section(); ++i ) {
|
|
|
|
|
if( strcmp( &argv[i][2], process_get_section_name(i) ) ) {
|
|
|
|
|
args->section = i;
|
|
|
|
|
g_server->section = i;
|
|
|
|
|
goto next_arg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -107,32 +124,74 @@ struct app_args* app_args_new( int argc, char** argv )
|
|
|
|
|
|
|
|
|
|
// Development
|
|
|
|
|
if( 0 == strcmp(argv[i],"--devel") ) {
|
|
|
|
|
args->section = 100;
|
|
|
|
|
g_server->section = 100;
|
|
|
|
|
goto next_arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unknown argument
|
|
|
|
|
printf( "Unknown argument: %s\n", argv[i] );
|
|
|
|
|
free(args);
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
next_arg:;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
return g_server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void app_args_release( struct app_args* args )
|
|
|
|
|
{
|
|
|
|
|
free(args->http_settings.bind_address);
|
|
|
|
|
free(args->domain);
|
|
|
|
|
free(args->icann_domain);
|
|
|
|
|
free(args->tor_hidden_service);
|
|
|
|
|
free(args->user_agent);
|
|
|
|
|
free(args);
|
|
|
|
|
}
|
|
|
|
|
void app_args_save()
|
|
|
|
|
{
|
|
|
|
|
// TODO: save server settings to disk
|
|
|
|
|
// Save to disk
|
|
|
|
|
json_write_object_layout_to_file( "data/server.json", "\t", app_args_layout, g_server );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void app_args_load_from_form( struct app_args* args, struct form_parser* fp )
|
|
|
|
|
{
|
|
|
|
|
char* key = NULL;
|
|
|
|
|
while( (key=form_pull_parser_read_key(fp)) != NULL ) {
|
|
|
|
|
for( struct json_object_field* jof = app_args_layout; jof->key; jof += 1 ) {
|
|
|
|
|
intptr_t field_offset = (intptr_t)args + jof->offset;
|
|
|
|
|
|
|
|
|
|
if( strcmp(jof->key, key) == 0 ) {
|
|
|
|
|
if( jof->type == &json_field_string ) {
|
|
|
|
|
const char* value = form_pull_parser_read_value(fp);
|
|
|
|
|
char** field = (char**)field_offset;
|
|
|
|
|
|
|
|
|
|
printf( "%s: %s\n", jof->key, value );
|
|
|
|
|
free( *field );
|
|
|
|
|
*field = NULL;
|
|
|
|
|
|
|
|
|
|
if( value ) {
|
|
|
|
|
*field = strdup(value);
|
|
|
|
|
}
|
|
|
|
|
} else if( jof->type == &json_field_integer ) {
|
|
|
|
|
const char* value = form_pull_parser_read_value(fp);
|
|
|
|
|
printf( "%s: %s\n", jof->key, value );
|
|
|
|
|
|
|
|
|
|
int* field = (int*)field_offset;
|
|
|
|
|
sscanf( value, "%d", field );
|
|
|
|
|
} else if( jof->type == &json_field_bool ) {
|
|
|
|
|
const char* value = form_pull_parser_read_value(fp);
|
|
|
|
|
printf( "%s: %s\n", jof->key, value );
|
|
|
|
|
|
|
|
|
|
bool* field = (bool*)field_offset;
|
|
|
|
|
if( strcmp(value,"on") == 0 ) {
|
|
|
|
|
*field = true;
|
|
|
|
|
} else {
|
|
|
|
|
*field = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf( "Unknown type for field %s\n", jof->key );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|