@ -1,9 +1,9 @@
// This file is included in nginx-ssl-util.cpp, which defines NGINX_OPENSSL.
# ifndef __NGINX_UTIL_C
# define __NGINX_UTIL_C
# include "nginx-util.hpp"
# ifndef NO_SSL
# include "nginx-ssl-util.hpp"
# endif
void create_lan_listen ( )
{
@ -13,9 +13,8 @@ void create_lan_listen()
std : : string ssl_listen = listen ;
std : : string ssl_listen_default = listen ;
# ifndef NO_UBUS
auto add_listen = [ & listen , & listen_default
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
, & ssl_listen , & ssl_listen_default
# endif
]
@ -26,36 +25,49 @@ void create_lan_listen()
const std : : string val = pre + ip + suf ;
listen + = " \t listen " + val + " :80; \n " ;
listen_default + = " \t listen " + val + " :80 default_server; \n " ;
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
ssl_listen + = " \t listen " + val + " :443 ssl; \n " ;
ssl_listen_default + = " \t listen " + val + " :443 ssl default_server; \n " ;
# endif
} ;
auto loopback_status = ubus : : call ( " network.interface.loopback " , " status " ) ;
# ifndef NO_UBUS
try {
auto loopback_status = ubus : : call ( " network.interface.loopback " , " status " ) ;
for ( auto ip : loopback_status . filter ( " ipv4-address " , " " , " address " ) ) {
add_listen ( " " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " " ) ;
}
for ( auto ip : loopback_status . filter ( " ipv4-address " , " " , " address " ) ) {
add_listen ( " " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " " ) ;
}
for ( auto ip : loopback_status . filter ( " ipv6-address " , " " , " address " ) ) {
add_listen ( " [ " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " ] " ) ;
}
for ( auto ip : loopback_status . filter ( " ipv6-address " , " " , " address " ) ) {
add_listen ( " [ " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " ] " ) ;
}
} catch ( const std : : runtime_error & ) { /* do nothing about it */ }
auto lan_status = ubus : : call ( " network.interface.lan " , " status " ) ;
try {
auto lan_status = ubus : : call ( " network.interface.lan " , " status " ) ;
for ( auto ip : lan_status . filter ( " ipv4-address " , " " , " address " ) ) {
add_listen ( " " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " " ) ;
}
for ( auto ip : lan_status . filter ( " ipv4-address " , " " , " address " ) ) {
add_listen ( " " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " " ) ;
}
for ( auto ip : lan_status . filter ( " ipv6-address " , " " , " address " ) ) {
add_listen ( " [ " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " ] " ) ;
}
for ( auto ip : lan_status . filter ( " ipv6-address " , " " , " address " ) ) {
add_listen ( " [ " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " ] " ) ;
}
for ( auto ip : lan_status . filter ( " ipv6-prefix-assignment " , " " ,
" local-address " , " address " ) )
{
add_listen ( " [ " , static_cast < const char * > ( blobmsg_data ( ip ) ) , " ] " ) ;
}
} catch ( const std : : runtime_error & ) { /* do nothing about it */ }
# else
add_listen ( " " , " 127.0.0.1 " , " " ) ;
# endif
write_file ( LAN_LISTEN , listen ) ;
write_file ( LAN_LISTEN_DEFAULT , listen_default ) ;
# ifdef NGINX_OPENSSL
# ifndef NO_ SSL
write_file ( LAN_SSL_LISTEN , ssl_listen ) ;
write_file ( LAN_SSL_LISTEN_DEFAULT , ssl_listen_default ) ;
# endif
@ -66,23 +78,23 @@ void init_lan()
{
std : : exception_ptr ex ;
# ifdef NGINX_OPEN SSL
auto thrd = std : : thread ( [ & ex ] {
try { add_ssl_if_needed ( std : : string { LAN_NAME } ) ; }
# ifndef NO_ SSL
auto thrd = std : : thread ( [ ] { //&ex
try { add_ssl_if_needed ( std : : string { LAN_NAME } ) ; }
catch ( . . . ) {
std : : cerr < < " init_lan error: cannot add SSL for " < < LAN_NAME < < std : : endl ;
ex = std : : current_exception ( ) ;
std : : cerr < < " init_lan notice: no server named " < < LAN_NAME < < std : : endl ;
// not: ex = std::current_exception();
}
} ) ;
# endif
try { create_lan_listen ( ) ; }
catch ( . . . ) {
std : : cerr < < " init_lan error: cannot create LAN listen directiv es " < < std : : endl ;
std : : cerr < < " init_lan error: cannot create LAN listen fil es " < < std : : endl ;
ex = std : : current_exception ( ) ;
}
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
thrd . join ( ) ;
# endif
@ -96,7 +108,7 @@ void get_env()
std : : cout < < " CONF_DIR= " < < " ' " < < CONF_DIR < < " ' " < < std : : endl ;
std : : cout < < " LAN_NAME= " < < " ' " < < LAN_NAME < < " ' " < < std : : endl ;
std : : cout < < " LAN_LISTEN= " < < " ' " < < LAN_LISTEN < < " ' " < < std : : endl ;
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
std : : cout < < " LAN_SSL_LISTEN= " < < " ' " < < LAN_SSL_LISTEN < < " ' " < < std : : endl ;
std : : cout < < " SSL_SESSION_CACHE_ARG= " < < " ' " < < SSL_SESSION_CACHE_ARG ( LAN_NAME ) < <
" ' " < < std : : endl ;
@ -114,7 +126,7 @@ auto main(int argc, char * argv[]) -> int
auto cmds = std : : array {
std : : array < std : : string_view , 2 > { " init_lan " , " " } ,
std : : array < std : : string_view , 2 > { " get_env " , " " } ,
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
std : : array < std : : string_view , 2 > { ADD_SSL_FCT , " server_name " } ,
std : : array < std : : string_view , 2 > { " del_ssl " , " server_name " } ,
# endif
@ -126,7 +138,7 @@ auto main(int argc, char * argv[]) -> int
else if ( argc = = 2 & & args [ 1 ] = = cmds [ 1 ] [ 0 ] ) { get_env ( ) ; }
# ifdef NGINX_OPEN SSL
# ifndef NO_ SSL
else if ( argc = = 3 & & args [ 1 ] = = cmds [ 2 ] [ 0 ] )
{ add_ssl_if_needed ( std : : string { args [ 2 ] } ) ; }
@ -138,13 +150,30 @@ auto main(int argc, char * argv[]) -> int
# endif
else {
std : : cerr < < " Tool for creating Nginx configuration files ( " ;
# ifdef VERSION
std : : cerr < < " version " < < VERSION < < " " ;
# endif
std : : cerr < < " with " ;
# ifndef NO_UBUS
std : : cerr < < " ubus, " ;
# endif
# ifndef NO_SSL
std : : cerr < < " libopenssl, " ;
# ifdef NO_PCRE
std : : cerr < < " std::regex, " ;
# else
std : : cerr < < " PCRE, " ;
# endif
# endif
std : : cerr < < " pthread and libstdcpp). " < < std : : endl ;
auto usage = std : : string { " usage: " } + * argv + " [ " ;
for ( auto cmd : cmds ) {
usage + = std : : string { cmd [ 0 ] } ;
usage + = std : : string { cmd [ 1 ] } + " | " ;
}
usage [ usage . size ( ) - 1 ] = ' ] ' ;
std : : cerr < < usage < < std : : endl ;
throw std : : runtime_error ( " main error: argument not recognized " ) ;
@ -161,5 +190,3 @@ auto main(int argc, char * argv[]) -> int
return 1 ;
}
# endif