@ -199,7 +199,7 @@ namespace llarp
void
void
Router : : Freeze ( )
Router : : Freeze ( )
{
{
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
return ;
return ;
for_each_connection (
for_each_connection (
@ -209,7 +209,7 @@ namespace llarp
void
void
Router : : Thaw ( )
Router : : Thaw ( )
{
{
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
return ;
return ;
std : : unordered_set < RouterID > peer_pubkeys ;
std : : unordered_set < RouterID > peer_pubkeys ;
@ -231,10 +231,10 @@ namespace llarp
}
}
void
void
Router : : GossipRCIfNeeded ( const RouterContact rc )
Router : : GossipRCIfNeeded ( const LocalRC rc )
{
{
/// if we are not a service node forget about gossip
/// if we are not a service node forget about gossip
if ( not IsServiceN ode( ) )
if ( not is_service_n ode( ) )
return ;
return ;
/// wait for random uptime
/// wait for random uptime
if ( std : : chrono : : milliseconds { Uptime ( ) } < _randomStartDelay )
if ( std : : chrono : : milliseconds { Uptime ( ) } < _randomStartDelay )
@ -245,7 +245,7 @@ namespace llarp
bool
bool
Router : : GetRandomGoodRouter ( RouterID & router )
Router : : GetRandomGoodRouter ( RouterID & router )
{
{
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
{
{
return _rc_lookup_handler . get_random_whitelist_router ( router ) ;
return _rc_lookup_handler . get_random_whitelist_router ( router ) ;
}
}
@ -271,7 +271,7 @@ namespace llarp
}
}
void
void
Router : : connect_to ( const R outerContact & rc )
Router : : connect_to ( const R emoteRC & rc )
{
{
_link_manager . connect_to ( rc ) ;
_link_manager . connect_to ( rc ) ;
}
}
@ -314,7 +314,7 @@ namespace llarp
{
{
_encryption = _key_manager - > encryptionKey ;
_encryption = _key_manager - > encryptionKey ;
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
{
{
# if defined(ANDROID) || defined(IOS)
# if defined(ANDROID) || defined(IOS)
LogError ( " running a service node on mobile device is not possible. " ) ;
LogError ( " running a service node on mobile device is not possible. " ) ;
@ -396,9 +396,9 @@ namespace llarp
log : : debug ( logcat , " Configuring router " ) ;
log : : debug ( logcat , " Configuring router " ) ;
is_service_node = conf . router . m_isRelay ;
_ is_service_node = conf . router . m_isRelay ;
if ( is_service_node)
if ( _ is_service_node)
{
{
rpc_addr = oxenmq : : address ( conf . lokid . lokidRPCAddr ) ;
rpc_addr = oxenmq : : address ( conf . lokid . lokidRPCAddr ) ;
_rpc_client = std : : make_shared < rpc : : LokidRpcClient > ( _lmq , weak_from_this ( ) ) ;
_rpc_client = std : : make_shared < rpc : : LokidRpcClient > ( _lmq , weak_from_this ( ) ) ;
@ -417,9 +417,9 @@ namespace llarp
_node_db = std : : move ( nodedb ) ;
_node_db = std : : move ( nodedb ) ;
log : : debug (
log : : debug (
logcat , is_service_node ? " Running as a relay (service node) " : " Running as a client " ) ;
logcat , _ is_service_node ? " Running as a relay (service node) " : " Running as a client " ) ;
if ( is_service_node)
if ( _ is_service_node)
{
{
_rpc_client - > ConnectAsync ( rpc_addr ) ;
_rpc_client - > ConnectAsync ( rpc_addr ) ;
}
}
@ -438,34 +438,10 @@ namespace llarp
return true ;
return true ;
}
}
/// called in disk worker thread
void
Router : : HandleSaveRC ( ) const
{
std : : string fname = our_rc_file . string ( ) ;
router_contact . write ( fname . c_str ( ) ) ;
}
bool
Router : : SaveRC ( )
{
LogDebug ( " verify RC signature " ) ;
if ( ! router_contact . verify ( now ( ) ) ) // TODO: RouterContact -> RemoteRC
{
Dump < RouterContact : : MAX_RC_SIZE > ( rc ( ) ) ;
LogError ( " RC is invalid, not saving " ) ;
return false ;
}
if ( is_service_node )
_node_db - > put_rc ( router_contact ) ;
queue_disk_io ( [ & ] ( ) { HandleSaveRC ( ) ; } ) ;
return true ;
}
bool
bool
Router : : IsServiceN ode( ) const
Router : : is_service_node ( ) const
{
{
return is_service_node;
return _is_service_node ;
}
}
bool
bool
@ -507,7 +483,7 @@ namespace llarp
bool
bool
Router : : have_snode_whitelist ( ) const
Router : : have_snode_whitelist ( ) const
{
{
return IsServiceN ode( ) and _rc_lookup_handler . has_received_whitelist ( ) ;
return is_service_n ode( ) and _rc_lookup_handler . has_received_whitelist ( ) ;
}
}
bool
bool
@ -566,13 +542,13 @@ namespace llarp
bool
bool
Router : : update_rc ( )
Router : : update_rc ( )
{
{
SecretKey nextOnionKey ;
router_contact. resign ( ) ;
RouterContact nextRC = router_contact ;
if ( is_service_node ( ) )
if ( ! nextRC . sign ( identity ( ) ) ) // TODO: RouterContact -> LocalRC
{
return false ;
_node_db - > put_rc ( router_contact . view ( ) ) ;
router_contact = std : : move ( nextRC ) ;
queue_disk_io ( [ & ] ( ) { router_contact . write ( our_rc_file ) ; } ) ;
if ( IsServiceNode ( ) )
}
return SaveRC ( ) ;
return true ;
return true ;
}
}
@ -626,12 +602,13 @@ namespace llarp
auto & networkConfig = conf . network ;
auto & networkConfig = conf . network ;
/// build a set of strictConnectPubkeys (
/// build a set of strictConnectPubkeys
std : : unordered_set < RouterID > strictConnectPubkeys ;
std : : unordered_set < RouterID > strictConnectPubkeys ;
if ( not networkConfig . m_strictConnect . empty ( ) )
if ( not networkConfig . m_strictConnect . empty ( ) )
{
{
const auto & val = networkConfig . m_strictConnect ;
const auto & val = networkConfig . m_strictConnect ;
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
throw std : : runtime_error ( " cannot use strict-connect option as service node " ) ;
throw std : : runtime_error ( " cannot use strict-connect option as service node " ) ;
if ( val . size ( ) < 2 )
if ( val . size ( ) < 2 )
throw std : : runtime_error (
throw std : : runtime_error (
@ -660,7 +637,7 @@ namespace llarp
for ( const auto & router : configRouters )
for ( const auto & router : configRouters )
{
{
log : : debug ( logcat , " Loading bootstrap router list from {} " , defaultBootstrapFile ) ;
log : : debug ( logcat , " Loading bootstrap router list from {} " , defaultBootstrapFile ) ;
bootstrap_rc_list . AddFromF ile( router ) ;
bootstrap_rc_list . read_from_f ile( router ) ;
}
}
for ( const auto & rc : conf . bootstrap . routers )
for ( const auto & rc : conf . bootstrap . routers )
@ -668,37 +645,10 @@ namespace llarp
bootstrap_rc_list . emplace ( rc ) ;
bootstrap_rc_list . emplace ( rc ) ;
}
}
// in case someone has an old bootstrap file and is trying to use a bootstrap
// that no longer exists
auto clearBadRCs = [ this ] ( ) {
for ( auto it = bootstrap_rc_list . begin ( ) ; it ! = bootstrap_rc_list . end ( ) ; )
{
if ( it - > is_obsolete_bootstrap ( ) )
log : : warning ( logcat , " ignoring obsolete boostrap RC: {} " , RouterID { it - > router_id ( ) } ) ;
else if ( not it - > verify ( now ( ) ) ) // TODO: RouterContact -> RemoteRC
log : : warning ( logcat , " ignoring invalid bootstrap RC: {} " , RouterID { it - > router_id ( ) } ) ;
else
{
+ + it ;
continue ;
}
// we are in one of the above error cases that we warned about:
it = bootstrap_rc_list . erase ( it ) ;
}
} ;
clearBadRCs ( ) ;
if ( bootstrap_rc_list . empty ( ) and not conf . bootstrap . seednode )
if ( bootstrap_rc_list . empty ( ) and not conf . bootstrap . seednode )
{
{
auto fallbacks = llarp : : load_bootstrap_fallbacks ( ) ;
auto fallbacks = llarp : : load_bootstrap_fallbacks ( ) ;
if ( auto itr = fallbacks . find ( router_contact . netID . ToString ( ) ) ; itr ! = fallbacks . end ( ) )
{
bootstrap_rc_list = itr - > second ;
log : : debug (
logcat , " loaded {} default fallback bootstrap routers " , bootstrap_rc_list . size ( ) ) ;
clearBadRCs ( ) ;
}
if ( bootstrap_rc_list . empty ( ) and not conf . bootstrap . seednode )
if ( bootstrap_rc_list . empty ( ) and not conf . bootstrap . seednode )
{
{
// empty after trying fallback, if set
// empty after trying fallback, if set
@ -711,6 +661,24 @@ namespace llarp
}
}
}
}
// in case someone has an old bootstrap file and is trying to use a bootstrap
// that no longer exists
for ( auto it = bootstrap_rc_list . begin ( ) ; it ! = bootstrap_rc_list . end ( ) ; )
{
if ( it - > is_obsolete_bootstrap ( ) )
log : : warning ( logcat , " ignoring obsolete boostrap RC: {} " , it - > router_id ( ) ) ;
else if ( not it - > verify ( ) )
log : : warning ( logcat , " ignoring invalid bootstrap RC: {} " , it - > router_id ( ) ) ;
else
{
+ + it ;
continue ;
}
// we are in one of the above error cases that we warned about:
it = bootstrap_rc_list . erase ( it ) ;
}
if ( conf . bootstrap . seednode )
if ( conf . bootstrap . seednode )
LogInfo ( " we are a seed node " ) ;
LogInfo ( " we are a seed node " ) ;
else
else
@ -727,10 +695,10 @@ namespace llarp
& _hidden_service_context ,
& _hidden_service_context ,
strictConnectPubkeys ,
strictConnectPubkeys ,
bootstrap_rc_list ,
bootstrap_rc_list ,
is_service_node) ;
_ is_service_node) ;
// FIXME: kludge for now, will be part of larger cleanup effort.
// FIXME: kludge for now, will be part of larger cleanup effort.
if ( is_service_node)
if ( _ is_service_node)
InitInboundLinks ( ) ;
InitInboundLinks ( ) ;
else
else
InitOutboundLinks ( ) ;
InitOutboundLinks ( ) ;
@ -759,7 +727,7 @@ namespace llarp
}
}
// API config
// API config
if ( not IsServiceN ode( ) )
if ( not is_service_n ode( ) )
{
{
hidden_service_context ( ) . AddEndpoint ( conf ) ;
hidden_service_context ( ) . AddEndpoint ( conf ) ;
}
}
@ -767,19 +735,13 @@ namespace llarp
return true ;
return true ;
}
}
bool
Router : : CheckRenegotiateValid ( RouterContact newrc , RouterContact oldrc )
{
return _rc_lookup_handler . check_renegotiate_valid ( newrc , oldrc ) ;
}
bool
bool
Router : : IsBootstrapNode ( const RouterID r ) const
Router : : IsBootstrapNode ( const RouterID r ) const
{
{
return std : : count_if (
return std : : count_if (
bootstrap_rc_list . begin ( ) ,
bootstrap_rc_list . begin ( ) ,
bootstrap_rc_list . end ( ) ,
bootstrap_rc_list . end ( ) ,
[ r ] ( const R outerContact & rc ) - > bool { return rc . router_id ( ) = = r ; } )
[ r ] ( const R emoteRC & rc ) - > bool { return rc . router_id ( ) = = r ; } )
> 0 ;
> 0 ;
}
}
@ -797,7 +759,7 @@ namespace llarp
LogInfo ( node_db ( ) - > num_loaded ( ) , " RCs loaded " ) ;
LogInfo ( node_db ( ) - > num_loaded ( ) , " RCs loaded " ) ;
LogInfo ( bootstrap_rc_list . size ( ) , " bootstrap peers " ) ;
LogInfo ( bootstrap_rc_list . size ( ) , " bootstrap peers " ) ;
LogInfo ( NumberOfConnectedRouters ( ) , " router connections " ) ;
LogInfo ( NumberOfConnectedRouters ( ) , " router connections " ) ;
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
{
{
LogInfo ( NumberOfConnectedClients ( ) , " client connections " ) ;
LogInfo ( NumberOfConnectedClients ( ) , " client connections " ) ;
LogInfo ( ToString ( router_contact . age ( now ) ) , " since we last updated our RC " ) ;
LogInfo ( ToString ( router_contact . age ( now ) ) , " since we last updated our RC " ) ;
@ -814,7 +776,7 @@ namespace llarp
std : : string status ;
std : : string status ;
auto out = std : : back_inserter ( status ) ;
auto out = std : : back_inserter ( status ) ;
fmt : : format_to ( out , " v{} " , fmt : : join ( llarp : : LOKINET_VERSION , " . " ) ) ;
fmt : : format_to ( out , " v{} " , fmt : : join ( llarp : : LOKINET_VERSION , " . " ) ) ;
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
{
{
fmt : : format_to (
fmt : : format_to (
out ,
out ,
@ -890,7 +852,7 @@ namespace llarp
_rc_lookup_handler . periodic_update ( now ) ;
_rc_lookup_handler . periodic_update ( now ) ;
const bool has_whitelist = _rc_lookup_handler . has_received_whitelist ( ) ;
const bool has_whitelist = _rc_lookup_handler . has_received_whitelist ( ) ;
const bool is_snode = IsServiceN ode( ) ;
const bool is_snode = is_service_n ode( ) ;
const bool is_decommed = appears_decommed ( ) ;
const bool is_decommed = appears_decommed ( ) ;
bool should_gossip = appears_funded ( ) ;
bool should_gossip = appears_funded ( ) ;
@ -916,7 +878,7 @@ namespace llarp
GossipRCIfNeeded ( router_contact ) ;
GossipRCIfNeeded ( router_contact ) ;
}
}
// remove RCs for nodes that are no longer allowed by network policy
// remove RCs for nodes that are no longer allowed by network policy
node_db ( ) - > RemoveIf ( [ & ] ( const R outerContact & rc ) - > bool {
node_db ( ) - > RemoveIf ( [ & ] ( const R emoteRC & rc ) - > bool {
// don't purge bootstrap nodes from nodedb
// don't purge bootstrap nodes from nodedb
if ( IsBootstrapNode ( rc . router_id ( ) ) )
if ( IsBootstrapNode ( rc . router_id ( ) ) )
{
{
@ -1057,32 +1019,12 @@ namespace llarp
_last_tick = llarp : : time_now_ms ( ) ;
_last_tick = llarp : : time_now_ms ( ) ;
}
}
void
Router : : modify_rc ( std : : function < std : : optional < RouterContact > ( RouterContact ) > modify )
{
if ( auto maybe = modify ( rc ( ) ) )
{
router_contact = * maybe ;
update_rc ( ) ;
_rcGossiper . GossipRC ( rc ( ) ) ;
}
}
bool
bool
Router : : GetRandomConnectedRouter ( R outerContact & result ) const
Router : : GetRandomConnectedRouter ( RemoteRC & result ) const
{
{
return _link_manager . get_random_connected ( result ) ;
return _link_manager . get_random_connected ( result ) ;
}
}
void
Router : : HandleDHTLookupForExplore ( RouterID /*remote*/ , const std : : vector < RouterContact > & results )
{
for ( const auto & rc : results )
{
_rc_lookup_handler . check_rc ( rc ) ;
}
}
void
void
Router : : set_router_whitelist (
Router : : set_router_whitelist (
const std : : vector < RouterID > & whitelist ,
const std : : vector < RouterID > & whitelist ,
@ -1200,7 +1142,7 @@ namespace llarp
_route_poker - > start ( ) ;
_route_poker - > start ( ) ;
is_running . store ( true ) ;
is_running . store ( true ) ;
_started_at = now ( ) ;
_started_at = now ( ) ;
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
{
{
// do service node testing if we are in service node whitelist mode
// do service node testing if we are in service node whitelist mode
_loop - > call_every ( consensus : : REACHABILITY_TESTING_TIMER_INTERVAL , weak_from_this ( ) , [ this ] {
_loop - > call_every ( consensus : : REACHABILITY_TESTING_TIMER_INTERVAL , weak_from_this ( ) , [ this ] {
@ -1428,7 +1370,7 @@ namespace llarp
bool
bool
Router : : HasClientExit ( ) const
Router : : HasClientExit ( ) const
{
{
if ( IsServiceN ode( ) )
if ( is_service_n ode( ) )
return false ;
return false ;
const auto & ep = hidden_service_context ( ) . GetDefault ( ) ;
const auto & ep = hidden_service_context ( ) . GetDefault ( ) ;
return ep and ep - > HasExit ( ) ;
return ep and ep - > HasExit ( ) ;