Updated RpcServer Initialization and Logic

-- Moved all RPCServer initialization logic to rpcserver constructor
    -- Fixed config logic, fxn binding to rpc address, fxn adding rpc cats
    -- router hive failed CI/CD resulting from outdated reference to rpcBindAddr
    -- ipc socket as default hidden from windows (for now)
refactored config endpoint
    - added rpc call script (contrib/omq-rpc.py)
    - added new fxns to .ini config stuff
    - added delete .ini file functionality to config endpoint
    - added edge case control for config endpoint

add commented out line in clang-form for header reorg later
pull/2121/head
dan 1 year ago
parent 0edfe8ff83
commit 13b01c86a6

@ -54,3 +54,8 @@ PointerAlignment: Left
# when wrapping function calls/declarations, force each parameter to have its own line
BinPackParameters: 'false'
BinPackArguments: 'false'
# TODO: uncomment me when we are reading to rearrange the header includes
# IncludeBlocks: Regroup
# IncludeCategories: 'llarp/'

@ -49,8 +49,7 @@ endif()
option(USE_AVX2 "enable avx2 code" OFF)
option(USE_NETNS "enable networking namespace support. Linux only" OFF)
option(NATIVE_BUILD "optimise for host system and FPU" ON)
option(EMBEDDED_CFG "optimise for older hardware or embedded systems" OFF)
option(BUILD_LIBLOKINET "build liblokinet.so" ON)
option(WITH_EMBEDDED_LOKINET "build liblokinet.so for embedded lokinet" OFF)
option(XSAN "use sanitiser, if your system has it (requires -DCMAKE_BUILD_TYPE=Debug)" OFF)
option(USE_JEMALLOC "Link to jemalloc for memory allocations, if found" ON)
option(TESTNET "testnet build" OFF)

@ -33,7 +33,6 @@ for abi in $build_abis; do
-DBUILD_PACKAGE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_TESTING=OFF \
-DBUILD_LIBLOKINET=OFF \
-DWITH_TESTS=OFF \
-DWITH_BOOTSTRAP=OFF \
-DNATIVE_BUILD=OFF \

@ -13,7 +13,6 @@ cd build-mac
cmake \
-G Ninja \
-DBUILD_STATIC_DEPS=ON \
-DBUILD_LIBLOKINET=OFF \
-DWITH_TESTS=OFF \
-DWITH_BOOTSTRAP=OFF \
-DNATIVE_BUILD=OFF \

@ -0,0 +1,99 @@
#!/usr/bin/env python3
import nacl.bindings as sodium
from nacl.public import PrivateKey
from nacl.signing import SigningKey, VerifyKey
import nacl.encoding
import requests
import zmq
import zmq.utils.z85
import sys
import re
import time
import random
import shutil
context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.setsockopt(zmq.CONNECT_TIMEOUT, 5000)
socket.setsockopt(zmq.HANDSHAKE_IVL, 5000)
#socket.setsockopt(zmq.IMMEDIATE, 1)
if len(sys.argv) > 1 and any(sys.argv[1].startswith(x) for x in ("ipc://", "tcp://", "curve://")):
remote = sys.argv[1]
del sys.argv[1]
else:
remote = "ipc://./rpc.sock"
curve_pubkey = b''
my_privkey, my_pubkey = b'', b''
# If given a curve://whatever/pubkey argument then transform it into 'tcp://whatever' and put the
# 'pubkey' back into argv to be handled below.
if remote.startswith("curve://"):
pos = remote.rfind('/')
pkhex = remote[pos+1:]
remote = "tcp://" + remote[8:pos]
if len(pkhex) != 64 or not all(x in "0123456789abcdefABCDEF" for x in pkhex):
print("curve:// addresses must be in the form curve://HOST:PORT/REMOTE_PUBKEY_HEX", file=sys.stderr)
sys.exit(1)
sys.argv[1:0] = [pkhex]
if len(sys.argv) > 1 and len(sys.argv[1]) == 64 and all(x in "0123456789abcdefABCDEF" for x in sys.argv[1]):
curve_pubkey = bytes.fromhex(sys.argv[1])
del sys.argv[1]
socket.curve_serverkey = curve_pubkey
if len(sys.argv) > 1 and len(sys.argv[1]) == 64 and all(x in "0123456789abcdefABCDEF" for x in sys.argv[1]):
my_privkey = bytes.fromhex(sys.argv[1])
del sys.argv[1]
my_pubkey = zmq.utils.z85.decode(zmq.curve_public(zmq.utils.z85.encode(my_privkey)))
else:
my_privkey = PrivateKey.generate()
my_pubkey = my_privkey.public_key.encode()
my_privkey = my_privkey.encode()
print("No curve client privkey given; generated a random one (pubkey: {}, privkey: {})".format(
my_pubkey.hex(), my_privkey.hex()), file=sys.stderr)
socket.curve_secretkey = my_privkey
socket.curve_publickey = my_pubkey
if not 2 <= len(sys.argv) <= 3 or any(x in y for x in ("--help", "-h") for y in sys.argv[1:]):
print("Usage: {} [ipc:///path/to/sock|tcp://1.2.3.4:5678] [SERVER_CURVE_PUBKEY [LOCAL_CURVE_PRIVKEY]] COMMAND ['JSON']".format(
sys.argv[0]), file=sys.stderr)
sys.exit(1)
beginning_of_time = time.clock_gettime(time.CLOCK_MONOTONIC)
print("Connecting to {}".format(remote), file=sys.stderr)
socket.connect(remote)
to_send = [sys.argv[1].encode(), b'tagxyz123']
to_send += (x.encode() for x in sys.argv[2:])
print("Sending {}".format(to_send[0]), file=sys.stderr)
socket.send_multipart(to_send)
if socket.poll(timeout=5000):
m = socket.recv_multipart()
recv_time = time.clock_gettime(time.CLOCK_MONOTONIC)
if len(m) < 3 or m[0:2] != [b'REPLY', b'tagxyz123']:
print("Received unexpected {}-part reply:".format(len(m)), file=sys.stderr)
for x in m:
print("- {}".format(x))
else: # m[2] is numeric value, m[3] is data part, and will become m[2] <- changed
print("Received reply in {:.6f}s:".format(recv_time - beginning_of_time), file=sys.stderr)
if len(m) < 3:
print("(empty reply data)", file=sys.stderr)
else:
for x in m[2:]:
print("{} bytes data part:".format(len(x)), file=sys.stderr)
if any(x.startswith(y) for y in (b'd', b'l', b'i')) and x.endswith(b'e'):
sys.stdout.buffer.write(x)
else:
print(x.decode(), end="\n\n")
else:
print("Request timed out", file=sys.stderr)
socket.close(linger=0)
sys.exit(1)
# ./lmq-rpc.py ipc://$HOME/.oxen/testnet/oxend.sock 'llarp.get_service_nodes' | jq

@ -32,7 +32,6 @@ cmake \
-DBUILD_PACKAGE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_TESTING=OFF \
-DBUILD_LIBLOKINET=OFF \
-DWITH_TESTS=OFF \
-DWITH_BOOTSTRAP=OFF \
-DNATIVE_BUILD=OFF \

@ -20,7 +20,7 @@ add_library(lokinet-cryptography
libntrup/src/ref/rq.c
)
target_include_directories(lokinet-cryptography PUBLIC libntrup/include)
target_include_directories(lokinet-cryptography PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/libntrup/include)
# The avx implementation uses runtime CPU feature detection to enable itself, so we *always* want to
# compile it with avx2/fma support when supported by the compiler even if we aren't compiling with

@ -0,0 +1,97 @@
# High Level Iterative Approach
the desired outcome of this refactor will be splitting the existing code up into a stack of new components.
a layer hides all functionality of the layer below it to reduce the complexity like the OSI stack intends to.
the refactor starts at the top layer, wiring up the old implementation piecewise to the top layer.
once the top layer is wired up to the old implementation we will move down to the next layer.
this will repeat until we reach the bottom layer.
once the old implementation is wired up into these new clearly defined layers, we can fixup or replace different parts of each layer one at a time as needed.
working down from each layer will let us pick apart the old implementation (if needed) that we would wire up to the new base classes for that layer we are defining now without worrying about what is below it (yet).
this refactor is very able to be split up into small work units that (ideally) do not confict with each other.
PDU: https://en.wikipedia.org/wiki/Protocol_data_unit
# The New Layers
from top to bottom the new layers are:
* Platform Layer
* Flow Layer
* Routing Layer
* Onion Layer
* Link Layer
* Wire Layer
## Platform Layer
this is the top layer, it is responsibile ONLY to act as a handler of reading data from the "user" (via tun interface or whatever) to forward to the flow layer as desired, and to take data from the flow layer and send it to the "user".
any kind of IP/dns mapping or traffic isolation details are done here. embedded lokinet would be implemented in this layer as well, as it is without a full tun interface.
Platform layer PDU are what the OS gives us and we internally convert them into flow layer PDU and hand them off to the flow layer.
## Flow Layer
this layer is tl;dr mean to multiplex data from the platform layer across the routing layer and propagating PDU from the routing to the platform layer if needed.
the flow layer is responsible for sending platform layer PDU across path we have already established.
this layer is informed by the routing layer below it of state changes in what paths are available for use.
the flow layer requests from the layer below to make new paths if it wishes to get new ones on demand.
this layer will recieve routing layer PDU from the routing layer and apply any congestion control needed to buffer things to the os if it is needed at all.
flow layer PDU are (data, ethertype, src-pubkey, dst-pubkey, isolation-metric) tuples.
data is the datum we are tunneling over lokinet. ethertype tells us what kind of datum this is, e.g. plainquic/ipv4/ipv6/auth/etc.
src-pubkey and dst-pubkey are public the ed25519 public keys of each end of the flow in use.
the isolation metric is a piece of metadata we use to distinguish unique flows (convotag). in this new seperation convotags explicitly do not hand over across paths.
## Routing Layer
this layer is tl;dr meant for path management but not path building.
the routing layer is responsible for sending/recieving flow layer PDU, DHT requests/responses, latency testing PDU and any other kind of PDU we send/recieve over the onion layer.
this layer will be responsible for managing paths we have already built across lokinet.
the routing layer will periodically measure path status/latency, and do any other kinds of perioidic path related tasks post build.
this layer when asked for a new path from the flow layer will use one that has been prebuilt already and if the number of prebuilt paths is below a threshold we will tell the onion layer to build more paths.
the routing layer will recieve path build results be their success/fail/timeout from the onion layer that were requested and apply any congestion control needed at the pivot router.
routing layer PDU are (data, src-path, dst-path) tuples.
data is the datum we are transferring between paths.
src-path and dst-path are (pathid, router id) tuples, the source being which path this routing layer PDU originated from, destination being which path it is going to.
in the old model, router id is always the router that recieves it as the pivot router, this remains the same unless we explicitly provide router-id.
this lets us propagate hints to DHT related PDU held inside the datum.
## Onion Layer
the onion layer is repsonsible for path builds, path selection logic and low level details of encrypted/decrypting PDU that are onion routed over paths.
this layer is requested by the routing layer to build a path to a pivot router with an optional additional constraints (e.g. unique cidr/operator/geoip/etc, latency constaints, hop length, path lifetime).
the onion layer will encrypt PDU and send them to link layer as (frame/edge router id) tuples, and recieve link layer frames from edge routers, decrypt them and propagate them as needed to the routing layer.
this layer also handles transit onion traffic and transit path build responsibilities as a snode and apply congestion control as needed per transit path.
the onion layer PDU are (data, src-path, dst-path) tuples.
src-path and dst-path are (router-id, path-id) tuples which contain the ed25519 pubkey of the node and the 128 bit path-id it was associated with.
data is some datum we are onion routing that we would apply symettric encryption as needed before propagating to upper or lower layers.
## Link Layer
the link layer is responsbile for transmission of frames between nodes.
this layer will handle queuing and congestion control between wire proto sessions between nodes.
the link layer is will initate and recieve wire session to/from remote nodes.
the link layer PDU is (data, src-router-id, dst-router-id) tuples.
data is a datum of a link layer frame.
src-router-id and dst-router-id are (ed25519-pubkey, net-addr, wire-proto-info) tuples.
the ed25519 pubkey is a .snode address, (clients have these too but they are ephemeral).
net-addr is an (ip, port) tuple the node is reachable via the wire protocol.
wire-proto-info is dialect specific wire protocol specific info.
## Wire Layer
the wire layer is responsible for transmitting link layer frames between nodes.
all details here are specific to each wire proto dialect.

@ -1,4 +1,3 @@
option(SUBMODULE_CHECK "Enables checking that vendored library submodules are up to date" ON)
if(SUBMODULE_CHECK)
find_package(Git)
@ -140,3 +139,27 @@ if(WITH_BOOTSTRAP)
endif()
endif()
# libcrypt defaults, only on with macos and non static linux
set(default_libcrypt OFF)
if(LINUX AND NOT STATIC_LINK)
set(default_libcrypt ON)
endif()
if(MACOS)
set(default_libcrypt ON)
endif()
option(WITH_LIBCRYPT "enable fast password hash with libcrypt" ${default_libcrypt})
add_library(lokinet-libcrypt INTERFACE)
if(WITH_LIBCRYPT)
pkg_check_modules(LIBCRYPT libcrypt IMPORTED_TARGET REQUIRED)
add_definitions(-DHAVE_CRYPT)
target_link_libraries(lokinet-libcrypt INTERFACE PkgConfig::LIBCRYPT)
message(STATUS "using libcrypt ${LIBCRYPT_VERSION}")
else()
# TODO static build lib crypt?
message(STATUS "not building with libcrypt")
endif()

@ -1 +1 @@
Subproject commit 12c17d6eab754908cd88f05d09b9388381e47515
Subproject commit 9f2323a2db5fc54fe8394892769eff859967f735

@ -1,5 +1,12 @@
include(Version)
target_sources(lokinet-cryptography PRIVATE
crypto/crypto_libsodium.cpp
crypto/crypto.cpp
crypto/encrypted_frame.cpp
crypto/types.cpp
)
add_library(lokinet-util
STATIC
${CMAKE_CURRENT_BINARY_DIR}/constants/version.cpp
@ -15,19 +22,9 @@ add_library(lokinet-util
util/thread/threading.cpp
util/time.cpp)
add_dependencies(lokinet-util genversion)
target_include_directories(lokinet-util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR})
target_link_libraries(lokinet-util PUBLIC
lokinet-cryptography
nlohmann_json::nlohmann_json
filesystem
oxenc::oxenc
oxen::logging
)
# lokinet-platform holds all platform specific code
add_library(lokinet-platform
STATIC
# for networking
@ -45,9 +42,6 @@ add_library(lokinet-platform
vpn/platform.cpp
)
target_link_libraries(lokinet-platform PUBLIC lokinet-cryptography lokinet-util Threads::Threads base_libs uvw)
target_link_libraries(lokinet-platform PRIVATE oxenmq::oxenmq)
if (ANDROID)
target_sources(lokinet-platform PRIVATE android/ifaddrs.c util/nop_service_manager.cpp)
endif()
@ -66,36 +60,26 @@ if (WIN32)
net/win32.cpp
vpn/win32.cpp
win32/service_manager.cpp
win32/exec.cpp)
add_library(lokinet-win32 STATIC
win32/exec.cpp
win32/dll.cpp
win32/exception.cpp)
add_library(lokinet-wintun STATIC
win32/wintun.cpp)
add_library(lokinet-windivert STATIC
win32/exception.cpp
win32/wintun.cpp
win32/windivert.cpp)
# wintun and windivert are privated linked by lokinet-platform
# this is so their details do not leak out to deps of lokinet-platform
# wintun and windivert still need things from lokinet-platform
target_compile_options(lokinet-wintun PUBLIC -I${CMAKE_BINARY_DIR}/wintun/include/)
target_compile_options(lokinet-windivert PUBLIC -I${CMAKE_BINARY_DIR}/WinDivert-${WINDIVERT_VERSION}/include/)
target_include_directories(lokinet-windivert PUBLIC ${PROJECT_SOURCE_DIR})
target_link_libraries(lokinet-wintun PUBLIC lokinet-platform lokinet-util lokinet-config)
target_link_libraries(lokinet-win32 PUBLIC lokinet-util)
target_link_libraries(lokinet-windivert PUBLIC oxen-logging)
target_link_libraries(lokinet-windivert PRIVATE lokinet-win32)
target_link_libraries(lokinet-platform PRIVATE lokinet-win32 lokinet-wintun lokinet-windivert)
target_include_directories(lokinet-platform PRIVATE ${CMAKE_BINARY_DIR}/wintun/include/ ${CMAKE_BINARY_DIR}/WinDivert-${WINDIVERT_VERSION}/include/)
else()
target_sources(lokinet-platform PRIVATE
net/posix.cpp)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
target_include_directories(lokinet-platform SYSTEM PUBLIC /usr/local/include)
if(APPLE)
add_subdirectory(apple)
target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp)
endif()
# lokinet-dns is the dns parsing and hooking library that we use to
# parse modify and reconstitute dns wire proto, dns queries and RR
# should have no concept of dns caching, this is left as an implementation
# detail of dns resolvers (LATER: make separate lib for dns resolvers)
add_library(lokinet-dns
STATIC
dns/message.cpp
@ -107,13 +91,50 @@ add_library(lokinet-dns
dns/server.cpp
dns/srv_data.cpp)
# platform specific bits and bobs for setting dns
add_library(lokinet-dns-platform INTERFACE)
if(WITH_SYSTEMD)
target_sources(lokinet-dns PRIVATE dns/nm_platform.cpp dns/sd_platform.cpp)
add_library(lokinet-dns-systemd STATIC dns/nm_platform.cpp dns/sd_platform.cpp)
target_link_libraries(lokinet-dns-platform INTERFACE lokinet-dns-systemd)
endif()
target_link_libraries(lokinet-dns PUBLIC lokinet-platform uvw)
target_link_libraries(lokinet-dns PRIVATE libunbound lokinet-config)
# lokinet-nodedb holds all types and logic for storing parsing and constructing
# nodedb data published to the network and versions of it stored locally
add_library(lokinet-nodedb
STATIC
bootstrap.cpp
net/address_info.cpp
net/exit_info.cpp
net/traffic_policy.cpp
nodedb.cpp
pow.cpp
profiling.cpp
router_contact.cpp
router_id.cpp
router_version.cpp
)
set(BOOTSTRAP_FALLBACKS)
foreach(bs IN ITEMS MAINNET TESTNET)
if(BOOTSTRAP_FALLBACK_${bs})
message(STATUS "Building with ${bs} fallback boostrap path \"${BOOTSTRAP_FALLBACK_${bs}}\"")
file(READ "${BOOTSTRAP_FALLBACK_${bs}}" bs_data HEX)
if(bs STREQUAL TESTNET)
set(network "gamma")
elseif(bs STREQUAL MAINNET)
set(network "lokinet")
else()
string(TOLOWER "${bs}" network)
endif()
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "\\\\x\\1" bs_data "${bs_data}")
set(BOOTSTRAP_FALLBACKS "${BOOTSTRAP_FALLBACKS}{\"${network}\"s, \"${bs_data}\"sv},\n")
endif()
endforeach()
configure_file("bootstrap-fallbacks.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp" @ONLY)
target_sources(lokinet-nodedb PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp")
# lokinet-config is for all configuration types and parsers
add_library(lokinet-config
STATIC
config/config.cpp
@ -121,18 +142,15 @@ add_library(lokinet-config
config/ini.cpp
config/key_manager.cpp)
target_link_libraries(lokinet-config PUBLIC lokinet-dns lokinet-platform oxenmq::oxenmq)
add_library(lokinet-amalgum
# lokinet-consensus is for deriving and tracking network consensus state for both service nodes and clients
add_library(lokinet-consensus
STATIC
consensus/reachability_testing.cpp
)
bootstrap.cpp
context.cpp
crypto/crypto_libsodium.cpp
crypto/crypto.cpp
crypto/encrypted_frame.cpp
crypto/types.cpp
# lokinet-dht holds all logic related to interacting with and participating in the DHT hashring
add_library(lokinet-dht
STATIC
dht/context.cpp
dht/dht.cpp
dht/explorenetworkjob.cpp
@ -151,44 +169,56 @@ add_library(lokinet-amalgum
dht/recursiverouterlookup.cpp
dht/serviceaddresslookup.cpp
dht/taglookup.cpp
)
endpoint_base.cpp
# lokinet-layer-flow is the flow layer which sits atop the routing layer which manages
# flows between lokinet snapp endpoints be they .loki or .snode
add_library(lokinet-layer-flow
STATIC
layers/flow/stub.cpp # todo: remove me
)
exit/context.cpp
exit/endpoint.cpp
exit/exit_messages.cpp
exit/policy.cpp
exit/session.cpp
handlers/exit.cpp
handlers/tun.cpp
# lokinet-layer-onion is the "dumb" onion routing layer with builds manages and does i/o
# with onion paths. onion paths anonymize routing layer pdu.
add_library(lokinet-layer-onion
STATIC
path/ihophandler.cpp
path/path_context.cpp
path/path.cpp
path/pathbuilder.cpp
path/pathset.cpp
path/transit_hop.cpp
messages/relay.cpp
messages/relay_commit.cpp
messages/relay_status.cpp
)
# lokinet-layer-wire is a layer 1 analog which splits up
# layer 2 frames into layer 1 symbols which in the case of iwp are encrypted udp/ip packets
add_library(lokinet-layer-wire
STATIC
iwp/iwp.cpp
iwp/linklayer.cpp
iwp/message_buffer.cpp
iwp/session.cpp
)
# lokinet-layer-link is for our layer 2 analog which splits up layer 2 frames into
# a series of layer 1 symbols which are then transmitted between lokinet instances
add_library(lokinet-layer-link
STATIC
link/link_manager.cpp
link/session.cpp
link/server.cpp
messages/dht_immediate.cpp
messages/link_intro.cpp
messages/link_message_parser.cpp
messages/relay.cpp
messages/relay_commit.cpp
messages/relay_status.cpp
net/address_info.cpp
net/exit_info.cpp
net/traffic_policy.cpp
nodedb.cpp
path/ihophandler.cpp
path/path_context.cpp
path/path.cpp
path/pathbuilder.cpp
path/pathset.cpp
path/transit_hop.cpp
peerstats/peer_db.cpp
peerstats/types.cpp
pow.cpp
profiling.cpp
)
# lokinet-plainquic is for holding the tunneled plainquic code, not quic wire protocol code
add_library(lokinet-plainquic
STATIC
quic/address.cpp
quic/client.cpp
quic/connection.cpp
@ -197,27 +227,63 @@ add_library(lokinet-amalgum
quic/server.cpp
quic/stream.cpp
quic/tunnel.cpp
)
router_contact.cpp
router_id.cpp
router_version.cpp
service/name.cpp
# lokinet-context holds the contextualized god objects for a lokinet instance
# it is what any main function would link to in practice but it is hidden behind an interface library (lokinet-amalgum)
add_library(lokinet-context
STATIC
context.cpp
link/link_manager.cpp
router/outbound_message_handler.cpp
router/outbound_session_maker.cpp
router/rc_lookup_handler.cpp
router/rc_gossiper.cpp
router/router.cpp
router/route_poker.cpp
)
# lokinet-rpc holds all rpc related compilation units
add_library(lokinet-rpc
STATIC
rpc/json_binary_proxy.cpp
rpc/lokid_rpc_client.cpp
rpc/rpc_request_parser.cpp
rpc/rpc_server.cpp
rpc/endpoint_rpc.cpp
)
# optional peer stats library
add_library(lokinet-peerstats
STATIC
peerstats/peer_db.cpp
peerstats/types.cpp
)
# lokinet-layer-routing holds logic related to the routing layer
# routing layer is anonymized over the onion layer
add_library(lokinet-layer-routing
STATIC
routing/dht_message.cpp
routing/message_parser.cpp
routing/path_confirm_message.cpp
routing/path_latency_message.cpp
routing/path_transfer_message.cpp
routing/transfer_traffic_message.cpp
rpc/lokid_rpc_client.cpp
rpc/rpc_server.cpp
rpc/endpoint_rpc.cpp
)
# kitchen sink to be removed after refactor
add_library(lokinet-service-deprecated-kitchensink
STATIC
endpoint_base.cpp
exit/context.cpp
exit/endpoint.cpp
exit/exit_messages.cpp
exit/policy.cpp
exit/session.cpp
handlers/exit.cpp
handlers/tun.cpp
service/name.cpp
service/address.cpp
service/async_key_exchange.cpp
service/auth.cpp
@ -242,65 +308,216 @@ add_library(lokinet-amalgum
service/tag.cpp
)
set(BOOTSTRAP_FALLBACKS)
foreach(bs IN ITEMS MAINNET TESTNET)
if(BOOTSTRAP_FALLBACK_${bs})
message(STATUS "Building with ${bs} fallback boostrap path \"${BOOTSTRAP_FALLBACK_${bs}}\"")
file(READ "${BOOTSTRAP_FALLBACK_${bs}}" bs_data HEX)
if(bs STREQUAL TESTNET)
set(network "gamma")
elseif(bs STREQUAL MAINNET)
set(network "lokinet")
else()
string(TOLOWER "${bs}" network)
endif()
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "\\\\x\\1" bs_data "${bs_data}")
set(BOOTSTRAP_FALLBACKS "${BOOTSTRAP_FALLBACKS}{\"${network}\"s, \"${bs_data}\"sv},\n")
endif()
endforeach()
configure_file("bootstrap-fallbacks.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp" @ONLY)
target_sources(lokinet-amalgum PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp")
add_library(lokinet-layer-platform
STATIC
layers/platform/stub.cpp # todo: remove me
)
if(WITH_PEERSTATS_BACKEND)
target_compile_definitions(lokinet-amalgum PRIVATE -DLOKINET_PEERSTATS_BACKEND)
target_link_libraries(lokinet-amalgum PUBLIC sqlite_orm)
endif()
# interal tooling for pybind
add_library(lokinet-tooling INTERFACE)
if(WITH_HIVE)
target_sources(lokinet-amalgum PRIVATE
add_library(lokinet-hive-tooling
STATIC
tooling/router_hive.cpp
tooling/hive_router.cpp
tooling/hive_context.cpp
)
target_link_libraries(lokinet-tooling INTERFACE lokinet-hive-tooling)
endif()
# TODO: make libunbound hidden behind a feature flag like sqlite for embedded lokinet
target_link_libraries(lokinet-amalgum PRIVATE libunbound)
target_link_libraries(lokinet-amalgum PUBLIC
CLI11
oxenc::oxenc
# interface library for setting commone includes, linkage and flags.
add_library(lokinet-base INTERFACE)
target_include_directories(lokinet-base
INTERFACE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include
)
target_link_libraries(lokinet-base INTERFACE oxen::logging lokinet-cryptography)
if(WITH_PEERSTATS)
target_compile_definitions(lokinet-base INTERFACE -DLOKINET_PEERSTATS_BACKEND)
target_link_libraries(lokinet-base INTERFACE sqlite_orm)
endif()
# interface libraries for internal linkage
add_library(lokinet-layers INTERFACE)
add_library(lokinet-amalgum INTERFACE)
# helper function to link a library to lokinet-base, enable lto, add to lokinet-amalgum and then link to other libs
function(lokinet_link_lib libname)
message(DEBUG "created target: ${libname}")
enable_lto(${libname})
target_link_libraries(${libname} PUBLIC lokinet-base ${ARGN})
target_link_libraries(lokinet-amalgum INTERFACE ${libname})
endfunction()
# internal public linkages of components
lokinet_link_lib(lokinet-util)
lokinet_link_lib(lokinet-cryptography lokinet-libcrypt lokinet-util)
lokinet_link_lib(lokinet-peerstats lokinet-context)
lokinet_link_lib(lokinet-consensus lokinet-context)
lokinet_link_lib(lokinet-layer-link lokinet-peerstats)
if(TARGET lokinet-hive-tooling)
lokinet_link_lib(lokinet-hive-tooling lokinet-context)
endif()
if(TARGET lokinet-dns-systemd)
lokinet_link_lib(lokinet-dns-systemd
lokinet-dns
lokinet-platform
lokinet-config
)
endif()
lokinet_link_lib(lokinet-platform lokinet-util)
lokinet_link_lib(lokinet-config
lokinet-util
lokinet-nodedb
lokinet-dns
lokinet-platform
)
lokinet_link_lib(lokinet-context
lokinet-config
lokinet-platform
lokinet-peerstats
lokinet-layers
lokinet-consensus
lokinet-rpc
)
lokinet_link_lib(lokinet-dht
lokinet-util
lokinet-nodedb
)
lokinet_link_lib(lokinet-plainquic
lokinet-platform
lokinet-config
)
lokinet_link_lib(lokinet-dns
lokinet-platform
lokinet-dns-platform
lokinet-config
)
lokinet_link_lib(lokinet-nodedb
lokinet-util
lokinet-platform
)
lokinet_link_lib(lokinet-util
lokinet-nodedb
lokinet-platform
)
lokinet_link_lib(lokinet-rpc
lokinet-context
lokinet-peerstats
lokinet-util
)
# inter lokinet-layer public/private linkage.
# when linking each layer, we consider the layer directly below private linkage and the layer above public linkage.
# this lets us hide functionality of layers below us when depended on by another component.
#
# from highest to lowest layer, the above layers are stacked as follows:
#
# platform (what lokinet snapps interact with, be it l3 os interaction or embedded lokinet)
# flow (how we want to route and stripe over our onion routing)
# routing (what we are onion routing)
# onion (how the onion routing happens)
# link (what we want to send over the wire and to where)
# wire (what is actually sent over the wire)
#
function(link_lokinet_layers)
set(lib ${ARGV0})
if(${ARGC} GREATER 1)
lokinet_link_lib(${ARGV1} ${lib})
list(REMOVE_AT ARGV 1)
target_link_libraries(${lib} PRIVATE ${ARGV1})
# recursion :D
link_lokinet_layers(${ARGV})
else()
lokinet_link_lib(${lib})
endif()
endfunction()
link_lokinet_layers(
lokinet-layer-platform
lokinet-layer-flow
lokinet-layer-routing
lokinet-layer-onion
lokinet-layer-link
lokinet-layer-wire
)
# set me to OFF to disable old codepath
set(use_old_impl ON)
if(use_old_impl)
# flow layer deprecated-kitchensink (remove me after refactor)
lokinet_link_lib(lokinet-service-deprecated-kitchensink
lokinet-dns
lokinet-util
lokinet-cryptography
ngtcp2_static
oxenmq::oxenmq)
enable_lto(lokinet-util lokinet-platform lokinet-dns lokinet-config lokinet-amalgum)
pkg_check_modules(CRYPT libcrypt IMPORTED_TARGET)
if(CRYPT_FOUND AND NOT CMAKE_CROSSCOMPILING)
add_definitions(-DHAVE_CRYPT)
add_library(libcrypt INTERFACE)
target_link_libraries(libcrypt INTERFACE PkgConfig::CRYPT)
target_link_libraries(lokinet-amalgum PRIVATE libcrypt)
message(STATUS "using libcrypt ${CRYPT_VERSION}")
lokinet-nodedb
lokinet-context
lokinet-plainquic
lokinet-layer-routing
lokinet-layer-onion
lokinet-dht
lokinet-platform
lokinet-rpc
)
target_link_libraries(lokinet-layers INTERFACE lokinet-service-deprecated-kitchensink)
endif()
target_link_libraries(lokinet-layers INTERFACE
lokinet-layer-platform
lokinet-layer-flow
lokinet-layer-routing
lokinet-layer-onion
lokinet-layer-link
lokinet-layer-wire
)
# per component external deps
target_link_libraries(lokinet-config PUBLIC oxenmq::oxenmq)
target_link_libraries(lokinet-platform PUBLIC oxenmq::oxenmq)
target_link_libraries(lokinet-dns PUBLIC libunbound)
target_link_libraries(lokinet-cryptography PUBLIC
oxenc::oxenc
sodium
)
target_link_libraries(lokinet-context PUBLIC
CLI11
oxenmq::oxenmq
uvw
)
target_link_libraries(lokinet-platform PUBLIC
Threads::Threads
base_libs
uvw
)
target_link_libraries(lokinet-util PUBLIC
nlohmann_json::nlohmann_json
filesystem
oxenc::oxenc
)
target_link_libraries(lokinet-plainquic PUBLIC
ngtcp2_static
uvw
)
if(BUILD_LIBLOKINET)
if(WITH_EMBEDDED_LOKINET)
include(GNUInstallDirs)
add_library(lokinet-shared SHARED lokinet_shared.cpp)
target_link_libraries(lokinet-shared PUBLIC lokinet-amalgum)
@ -316,11 +533,5 @@ if(BUILD_LIBLOKINET)
endif()
endif()
if(APPLE)
add_subdirectory(apple)
target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp)
endif()
file(GLOB_RECURSE docs_SRC */*.hpp *.hpp)
set(DOCS_SRC ${docs_SRC} PARENT_SCOPE)

@ -1,8 +1,7 @@
#include <chrono>
#include "config.hpp"
#include "config/definition.hpp"
#include "definition.hpp"
#include "ini.hpp"
#include <llarp/constants/files.hpp>
#include <llarp/constants/platform.hpp>
#include <llarp/constants/version.hpp>
@ -18,6 +17,7 @@
#include <llarp/service/name.hpp>
#include <chrono>
#include <cstdlib>
#include <ios>
#include <iostream>

@ -1,14 +1,15 @@
#pragma once
#include "ini.hpp"
#include "definition.hpp"
#include <chrono>
#include <llarp/bootstrap.hpp>
#include <llarp/crypto/types.hpp>
#include <llarp/router_contact.hpp>
#include <llarp/util/fs.hpp>
#include <llarp/util/str.hpp>
#include <llarp/util/logging.hpp>
#include "ini.hpp"
#include "definition.hpp"
#include <llarp/constants/files.hpp>
#include <llarp/net/ip_address.hpp>
#include <llarp/net/net_int.hpp>
@ -16,7 +17,6 @@
#include <llarp/service/address.hpp>
#include <llarp/service/auth.hpp>
#include <llarp/dns/srv_data.hpp>
#include <llarp/router_contact.hpp>
#include <cstdlib>

@ -9,6 +9,7 @@
#include <list>
#include <iostream>
#include <cassert>
#include <stdexcept>
namespace llarp
{
@ -30,6 +31,14 @@ namespace llarp
return Parse();
}
bool
ConfigParser::LoadNewFromStr(std::string_view str)
{
m_Data.resize(str.size());
std::copy(str.begin(), str.end(), m_Data.begin());
return ParseAll();
}
bool
ConfigParser::LoadFromStr(std::string_view str)
{
@ -52,6 +61,78 @@ namespace llarp
return std::isspace(static_cast<unsigned char>(ch)) != 0;
}
/// Differs from Parse() as ParseAll() does NOT skip comments
/// ParseAll() is only used by RPC endpoint 'config' for
/// reading new .ini files from string and writing them
bool
ConfigParser::ParseAll()
{
std::list<std::string_view> lines;
{
auto itr = m_Data.begin();
// split into lines
while (itr != m_Data.end())
{
auto beg = itr;
while (itr != m_Data.end() && *itr != '\n' && *itr != '\r')
++itr;
lines.emplace_back(std::addressof(*beg), std::distance(beg, itr));
if (itr == m_Data.end())
break;
++itr;
}
}
std::string_view sectName;
size_t lineno = 0;
for (auto line : lines)
{
lineno++;
// Trim whitespace
while (!line.empty() && whitespace(line.front()))
line.remove_prefix(1);
while (!line.empty() && whitespace(line.back()))
line.remove_suffix(1);
// Skip blank lines but NOT comments
if (line.empty())
continue;
if (line.front() == '[' && line.back() == ']')
{
// section header
line.remove_prefix(1);
line.remove_suffix(1);
sectName = line;
}
else if (auto kvDelim = line.find('='); kvDelim != std::string_view::npos)
{
// key value pair
std::string_view k = line.substr(0, kvDelim);
std::string_view v = line.substr(kvDelim + 1);
// Trim inner whitespace
while (!k.empty() && whitespace(k.back()))
k.remove_suffix(1);
while (!v.empty() && whitespace(v.front()))
v.remove_prefix(1);
if (k.empty())
{
throw std::runtime_error(
fmt::format("{} invalid line ({}): '{}'", m_FileName, lineno, line));
}
LogDebug(m_FileName, ": [", sectName, "]:", k, "=", v);
m_Config[std::string{sectName}].emplace(k, v);
}
else // malformed?
{
throw std::runtime_error(
fmt::format("{} invalid line ({}): '{}'", m_FileName, lineno, line));
}
}
return true;
}
bool
ConfigParser::Parse()
{
@ -82,7 +163,7 @@ namespace llarp
while (!line.empty() && whitespace(line.back()))
line.remove_suffix(1);
// Skip blank lines and comments
// Skip blank lines
if (line.empty() or line.front() == ';' or line.front() == '#')
continue;
@ -106,16 +187,16 @@ namespace llarp
if (k.empty())
{
LogError(m_FileName, " invalid line (", lineno, "): '", line, "'");
return false;
throw std::runtime_error(
fmt::format("{} invalid line ({}): '{}'", m_FileName, lineno, line));
}
LogDebug(m_FileName, ": [", sectName, "]:", k, "=", v);
m_Config[std::string{sectName}].emplace(k, v);
}
else // malformed?
{
LogError(m_FileName, " invalid line (", lineno, "): '", line, "'");
return false;
throw std::runtime_error(
fmt::format("{} invalid line ({}): '{}'", m_FileName, lineno, line));
}
}
return true;
@ -168,4 +249,31 @@ namespace llarp
m_Overrides.clear();
}
void
ConfigParser::SaveNew() const
{
if (not m_Overrides.empty())
{
throw std::invalid_argument("Override specified when attempting new .ini save");
}
if (m_Config.empty())
{
throw std::invalid_argument("New config not loaded when attempting new .ini save");
}
if (m_FileName.empty())
{
throw std::invalid_argument("New config cannot be saved with filepath specified");
}
std::ofstream ofs(m_FileName);
for (const auto& [section, values] : m_Config)
{
ofs << std::endl << "[" << section << "]" << std::endl;
for (const auto& [key, value] : values)
{
ofs << key << "=" << value << std::endl;
}
}
}
} // namespace llarp

@ -24,6 +24,12 @@ namespace llarp
bool
LoadFile(const fs::path& fname);
/// load new .ini file from string (calls ParseAll() rather than Parse())
/// return true on success
/// return false on error
bool
LoadNewFromStr(std::string_view str);
/// load from string
/// return true on success
/// return false on error
@ -47,6 +53,10 @@ namespace llarp
void
Save();
/// save new .ini config file to path
void
SaveNew() const;
inline void
Filename(fs::path f)
{
@ -54,6 +64,9 @@ namespace llarp
};
private:
bool
ParseAll();
bool
Parse();

@ -1,5 +1,5 @@
#include <constants/version.hpp>
#include <constants/proto.hpp>
#include <llarp/constants/version.hpp>
#include <llarp/constants/proto.hpp>
namespace llarp
{

@ -25,10 +25,10 @@
#include <pthread_np.h>
#endif
static auto logcat = llarp::log::Cat("llarp-context");
namespace llarp
{
static auto logcat = llarp::log::Cat("llarp-context");
bool
Context::CallSafe(std::function<void(void)> f)
{

@ -1,6 +1,6 @@
#include "rr.hpp"
#include "dns.hpp"
#include "util/formattable.hpp"
#include <llarp/util/formattable.hpp>
#include <llarp/util/mem.hpp>
#include <llarp/util/logging.hpp>

@ -1,7 +1,7 @@
#pragma once
#include "exit_messages.hpp"
#include "service/protocol_type.hpp"
#include <llarp/service/protocol_type.hpp>
#include <llarp/net/ip_packet.hpp>
#include <llarp/path/pathbuilder.hpp>
#include <llarp/routing/transfer_traffic_message.hpp>

@ -11,7 +11,7 @@
#include <llarp/router/i_rc_lookup_handler.hpp>
#include <cassert>
#include "service/protocol_type.hpp"
#include <llarp/service/protocol_type.hpp>
namespace llarp
{

@ -2,7 +2,7 @@
#include <llarp/crypto/encrypted_frame.hpp>
#include <llarp/crypto/types.hpp>
#include "link_message.hpp"
#include <llarp/messages/link_message.hpp>
#include <llarp/path/path_types.hpp>
#include <llarp/pow.hpp>

@ -24,6 +24,14 @@ namespace llarp
: addr{std::move(address)}, netmask_bits{std::move(netmask)}
{}
static IPRange
StringInit(std::string _range)
{
IPRange range{};
range.FromString(_range);
return range;
}
static constexpr IPRange
V4MappedRange()
{

@ -1,10 +1,10 @@
#include "pathbuilder.hpp"
#include "path_context.hpp"
#include <llarp/crypto/crypto.hpp>
#include <llarp/messages/relay_commit.hpp>
#include <llarp/nodedb.hpp>
#include "path_context.hpp"
#include "util/logging.hpp"
#include <llarp/util/logging.hpp>
#include <llarp/profiling.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <llarp/router/i_rc_lookup_handler.hpp>

@ -1,7 +1,7 @@
#pragma once
#include "path_types.hpp"
#include "service/protocol_type.hpp"
#include <llarp/service/protocol_type.hpp>
#include <llarp/router_id.hpp>
#include <llarp/routing/message.hpp>
#include <llarp/service/intro_set.hpp>

@ -33,7 +33,9 @@ namespace llarp
}
TransitHop::TransitHop()
: m_UpstreamGather(transit_hop_queue_size), m_DownstreamGather(transit_hop_queue_size)
: IHopHandler{}
, m_UpstreamGather{transit_hop_queue_size}
, m_DownstreamGather{transit_hop_queue_size}
{
m_UpstreamGather.enable();
m_DownstreamGather.enable();

@ -1,8 +1,8 @@
#pragma once
#include <llarp/constants/path.hpp>
#include "ihophandler.hpp"
#include "path_types.hpp"
#include <llarp/path/ihophandler.hpp>
#include <llarp/path/path_types.hpp>
#include <llarp/routing/handler.hpp>
#include <llarp/router_id.hpp>
#include <llarp/util/compare_ptr.hpp>

@ -1,7 +1,7 @@
#include "tunnel.hpp"
#include "service/convotag.hpp"
#include "service/endpoint.hpp"
#include "service/name.hpp"
#include <llarp/service/convotag.hpp>
#include <llarp/service/endpoint.hpp>
#include <llarp/service/name.hpp>
#include "stream.hpp"
#include <limits>
#include <llarp/util/logging.hpp>

@ -1,6 +1,6 @@
#include "route_poker.hpp"
#include "abstractrouter.hpp"
#include "net/sock_addr.hpp"
#include <llarp/router/abstractrouter.hpp>
#include <llarp/net/sock_addr.hpp>
#include <llarp/service/context.hpp>
#include <llarp/dns/platform.hpp>
#include <unordered_set>

@ -623,8 +623,7 @@ namespace llarp
}
if (IsServiceNode())
return SaveRC();
else
return true;
return true;
}
bool
@ -1256,7 +1255,7 @@ namespace llarp
Router::StartRpcServer()
{
if (m_Config->api.m_enableRPCServer)
m_RPCServer = std::make_unique<rpc::RpcServer>(m_lmq, this);
m_RPCServer = std::make_unique<rpc::RPCServer>(m_lmq, *this);
return true;
}

@ -298,7 +298,7 @@ namespace llarp
void
PumpLL();
std::unique_ptr<rpc::RpcServer> m_RPCServer;
std::unique_ptr<rpc::RPCServer> m_RPCServer;
const llarp_time_t _randomStartDelay;

@ -0,0 +1,63 @@
#include "json_binary_proxy.hpp"
#include <oxenc/hex.h>
#include <oxenc/base64.h>
namespace llarp::rpc
{
void
load_binary_parameter_impl(
std::string_view bytes, size_t raw_size, bool allow_raw, uint8_t* val_data)
{
if (allow_raw && bytes.size() == raw_size)
{
std::memcpy(val_data, bytes.data(), bytes.size());
return;
}
else if (bytes.size() == raw_size * 2)
{
if (oxenc::is_hex(bytes))
{
oxenc::from_hex(bytes.begin(), bytes.end(), val_data);
return;
}
}
else
{
const size_t b64_padded = (raw_size + 2) / 3 * 4;
const size_t b64_padding = raw_size % 3 == 1 ? 2 : raw_size % 3 == 2 ? 1 : 0;
const size_t b64_unpadded = b64_padded - b64_padding;
const std::string_view b64_padding_string = b64_padding == 2 ? "=="sv
: b64_padding == 1 ? "="sv
: ""sv;
if (bytes.size() == b64_unpadded
|| (b64_padding > 0 && bytes.size() == b64_padded
&& bytes.substr(b64_unpadded) == b64_padding_string))
{
if (oxenc::is_base64(bytes))
{
oxenc::from_base64(bytes.begin(), bytes.end(), val_data);
return;
}
}
}
throw std::runtime_error{"Invalid binary value: unexpected size and/or encoding"};
}
nlohmann::json&
json_binary_proxy::operator=(std::string_view binary_data)
{
switch (format)
{
case fmt::bt:
return e = binary_data;
case fmt::hex:
return e = oxenc::to_hex(binary_data);
case fmt::base64:
return e = oxenc::to_base64(binary_data);
}
throw std::runtime_error{"Internal error: invalid binary encoding"};
}
} // namespace llarp::rpc

@ -0,0 +1,181 @@
#pragma once
#include <string_view>
#include <nlohmann/json.hpp>
#include <unordered_set>
using namespace std::literals;
namespace llarp::rpc
{
// Binary types that we support for rpc input/output. For json, these must be specified as hex or
// base64; for bt-encoded requests these can be accepted as binary, hex, or base64.
template <typename T>
inline constexpr bool json_is_binary = false;
template <typename T>
inline constexpr bool json_is_binary_container = false;
template <typename T>
inline constexpr bool json_is_binary_container<std::vector<T>> = json_is_binary<T>;
template <typename T>
inline constexpr bool json_is_binary_container<std::unordered_set<T>> = json_is_binary<T>;
// De-referencing wrappers around the above:
template <typename T>
inline constexpr bool json_is_binary<const T&> = json_is_binary<T>;
template <typename T>
inline constexpr bool json_is_binary<T&&> = json_is_binary<T>;
template <typename T>
inline constexpr bool json_is_binary_container<const T&> = json_is_binary_container<T>;
template <typename T>
inline constexpr bool json_is_binary_container<T&&> = json_is_binary_container<T>;
void
load_binary_parameter_impl(
std::string_view bytes, size_t raw_size, bool allow_raw, uint8_t* val_data);
// Loads a binary value from a string_view which may contain hex, base64, and (optionally) raw
// bytes.
template <typename T, typename = std::enable_if_t<json_is_binary<T>>>
void
load_binary_parameter(std::string_view bytes, bool allow_raw, T& val)
{
load_binary_parameter_impl(bytes, sizeof(T), allow_raw, reinterpret_cast<uint8_t*>(&val));
}
// Wrapper around a nlohmann::json that assigns a binary value either as binary (for bt-encoding);
// or as hex or base64 (for json-encoding).
class json_binary_proxy
{
public:
nlohmann::json& e;
enum class fmt
{
bt,
hex,
base64
} format;
explicit json_binary_proxy(nlohmann::json& elem, fmt format) : e{elem}, format{format}
{}
json_binary_proxy() = delete;
json_binary_proxy(const json_binary_proxy&) = default;
json_binary_proxy(json_binary_proxy&&) = default;
/// Dereferencing a proxy element accesses the underlying nlohmann::json
nlohmann::json&
operator*()
{
return e;
}
nlohmann::json*
operator->()
{
return &e;
}
/// Descends into the json object, returning a new binary value proxy around the child element.
template <typename T>
json_binary_proxy
operator[](T&& key)
{
return json_binary_proxy{e[std::forward<T>(key)], format};
}
/// Returns a binary value proxy around the first/last element (requires an underlying list)
json_binary_proxy
front()
{
return json_binary_proxy{e.front(), format};
}
json_binary_proxy
back()
{
return json_binary_proxy{e.back(), format};
}
/// Assigns binary data from a string_view/string/etc.
nlohmann::json&
operator=(std::string_view binary_data);
/// Assigns binary data from a string_view over a 1-byte, non-char type (e.g. unsigned char or
/// uint8_t).
template <
typename Char,
std::enable_if_t<sizeof(Char) == 1 && !std::is_same_v<Char, char>, int> = 0>
nlohmann::json&
operator=(std::basic_string_view<Char> binary_data)
{
return *this = std::string_view{
reinterpret_cast<const char*>(binary_data.data()), binary_data.size()};
}
/// Takes a trivial, no-padding data structure (e.g. a crypto::hash) as the value and dumps its
/// contents as the binary value.
template <typename T, std::enable_if_t<json_is_binary<T>, int> = 0>
nlohmann::json&
operator=(const T& val)
{
return *this = std::string_view{reinterpret_cast<const char*>(&val), sizeof(val)};
}
/// Takes a vector of some json_binary_proxy-assignable type and builds an array by assigning
/// each one into a new array of binary values.
template <typename T, std::enable_if_t<json_is_binary_container<T>, int> = 0>
nlohmann::json&
operator=(const T& vals)
{
auto a = nlohmann::json::array();
for (auto& val : vals)
json_binary_proxy{a.emplace_back(), format} = val;
return e = std::move(a);
}
/// Emplaces a new nlohman::json to the end of an underlying list and returns a
/// json_binary_proxy wrapping it.
///
/// Example:
///
/// auto child = wrappedelem.emplace_back({"key1": 1}, {"key2": 2});
/// child["binary-key"] = some_binary_thing;
template <typename... Args>
json_binary_proxy
emplace_back(Args&&... args)
{
return json_binary_proxy{e.emplace_back(std::forward<Args>(args)...), format};
}
/// Adds an element to an underlying list, then copies or moves the given argument onto it via
/// json_binary_proxy assignment.
template <typename T>
void
push_back(T&& val)
{
emplace_back() = std::forward<T>(val);
}
};
} // namespace llarp::rpc
// Specializations of binary types for deserialization; when receiving these from json we expect
// them encoded in hex or base64. These may *not* be used for serialization, and will throw if so
// invoked; for serialization you need to use RPC_COMMAND::response_hex (or _b64) instead.
namespace nlohmann
{
template <typename T>
struct adl_serializer<T, std::enable_if_t<llarp::rpc::json_is_binary<T>>>
{
static_assert(std::is_trivially_copyable_v<T> && std::has_unique_object_representations_v<T>);
static void
to_json(const T&)
{
throw std::logic_error{"Internal error: binary types are not directly serializable"};
}
static void
from_json(const json& j, T& val)
{
llarp::rpc::load_binary_parameter(j.get<std::string_view>(), false /*no raw*/, val);
}
};
} // namespace nlohmann

@ -0,0 +1,46 @@
#pragma once
#include <nlohmann/json.hpp>
#include <oxenc/bt_value.h>
using nlohmann::json;
namespace llarp::rpc
{
inline oxenc::bt_value
json_to_bt(json&& j)
{
if (j.is_object())
{
oxenc::bt_dict res;
for (auto& [k, v] : j.items())
{
if (v.is_null())
continue; // skip k-v pairs with a null v (for other nulls we fail).
res[k] = json_to_bt(std::move(v));
}
return res;
}
if (j.is_array())
{
oxenc::bt_list res;
for (auto& v : j)
res.push_back(json_to_bt(std::move(v)));
return res;
}
if (j.is_string())
{
return std::move(j.get_ref<std::string&>());
}
if (j.is_boolean())
return j.get<bool>() ? 1 : 0;
if (j.is_number_unsigned())
return j.get<uint64_t>();
if (j.is_number_integer())
return j.get<int64_t>();
throw std::domain_error{
"internal error: encountered some unhandled/invalid type in json-to-bt translation"};
}
} // namespace llarp::rpc

@ -0,0 +1,359 @@
#pragma once
#include "json_binary_proxy.hpp"
#include <oxenc/bt_serialize.h>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <optional>
namespace llarp::rpc
{
using json_range = std::pair<nlohmann::json::const_iterator, nlohmann::json::const_iterator>;
using rpc_input = std::variant<std::monostate, nlohmann::json, oxenc::bt_dict_consumer>;
// Checks that key names are given in ascending order
template <typename... Ignore>
void
check_ascending_names(std::string_view name1, std::string_view name2, const Ignore&...)
{
if (!(name2 > name1))
throw std::runtime_error{
"Internal error: request values must be retrieved in ascending order"};
}
// Wrapper around a reference for get_values that is used to indicate that the value is
// required, in which case an exception will be raised if the value is not found. Usage:
//
// int a_optional = 0, b_required;
// get_values(input,
// "a", a_optional,
// "b", required{b_required},
// // ...
// );
template <typename T>
struct required
{
T& value;
required(T& ref) : value{ref}
{}
};
template <typename T>
constexpr bool is_required_wrapper = false;
template <typename T>
constexpr bool is_required_wrapper<required<T>> = true;
template <typename T>
constexpr bool is_std_optional = false;
template <typename T>
constexpr bool is_std_optional<std::optional<T>> = true;
// Wrapper around a reference for get_values that adds special handling to act as if the value was
// not given at all if the value is given as an empty string. This sucks, but is necessary for
// backwards compatibility (especially with wallet2 clients).
//
// Usage:
//
// std::string x;
// get_values(input,
// "x", ignore_empty_string{x},
// // ...
// );
template <typename T>
struct ignore_empty_string
{
T& value;
ignore_empty_string(T& ref) : value{ref}
{}
bool
should_ignore(oxenc::bt_dict_consumer& d)
{
if (d.is_string())
{
auto d2{d}; // Copy because we want to leave d intact
if (d2.consume_string_view().empty())
return true;
}
return false;
}
bool
should_ignore(json_range& it_range)
{
auto& e = *it_range.first;
return (e.is_string() && e.get<std::string_view>().empty());
}
};
template <typename T>
constexpr bool is_ignore_empty_string_wrapper = false;
template <typename T>
constexpr bool is_ignore_empty_string_wrapper<ignore_empty_string<T>> = true;
// Advances the dict consumer to the first element >= the given name. Returns true if found,
// false if it advanced beyond the requested name. This is exactly the same as
// `d.skip_until(name)`, but is here so we can also overload an equivalent function for json
// iteration.
inline bool
skip_until(oxenc::bt_dict_consumer& d, std::string_view name)
{
return d.skip_until(name);
}
// Equivalent to the above but for a json object iterator.
inline bool
skip_until(json_range& it_range, std::string_view name)
{
auto& [it, end] = it_range;
while (it != end && it.key() < name)
++it;
return it != end && it.key() == name;
}
// List types that are expandable; for these we emplace_back for each element of the input
template <typename T>
constexpr bool is_expandable_list = false;
template <typename T>
constexpr bool is_expandable_list<std::vector<T>> = true;
// Fixed size elements: tuples, pairs, and std::array's; we accept list input as long as the
// list length matches exactly.
template <typename T>
constexpr bool is_tuple_like = false;
template <typename T, size_t N>
constexpr bool is_tuple_like<std::array<T, N>> = true;
template <typename S, typename T>
constexpr bool is_tuple_like<std::pair<S, T>> = true;
template <typename... T>
constexpr bool is_tuple_like<std::tuple<T...>> = true;
// True if T is a `std::unordered_map<std::string, ANYTHING...>`
template <typename T>
constexpr bool is_unordered_string_map = false;
template <typename... ValueEtc>
constexpr bool is_unordered_string_map<std::unordered_map<std::string, ValueEtc...>> = true;
template <typename TupleLike, size_t... Is>
void
load_tuple_values(oxenc::bt_list_consumer&, TupleLike&, std::index_sequence<Is...>);
// Consumes the next value from the dict consumer into `val`
template <
typename BTConsumer,
typename T,
std::enable_if_t<
std::is_same_v<
BTConsumer,
oxenc::bt_dict_consumer> || std::is_same_v<BTConsumer, oxenc::bt_list_consumer>,
int> = 0>
void
load_value(BTConsumer& c, T& val)
{
if constexpr (std::is_integral_v<T>)
val = c.template consume_integer<T>();
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>)
val = c.consume_string_view();
else if constexpr (llarp::rpc::json_is_binary<T>)
llarp::rpc::load_binary_parameter(c.consume_string_view(), true /*allow raw*/, val);
else if constexpr (is_expandable_list<T>)
{
auto lc = c.consume_list_consumer();
val.clear();
while (!lc.is_finished())
load_value(lc, val.emplace_back());
}
else if constexpr (is_tuple_like<T>)
{
auto lc = c.consume_list_consumer();
load_tuple_values(lc, val, std::make_index_sequence<std::tuple_size_v<T>>{});
}
else if constexpr (is_unordered_string_map<T>)
{
auto dc = c.consume_dict_consumer();
val.clear();
while (!dc.is_finished())
load_value(dc, val[std::string{dc.key()}]);
}
else
static_assert(std::is_same_v<T, void>, "Unsupported load_value type");
}
// Copies the next value from the json range into `val`, and advances the iterator. Throws
// on unconvertible values.
template <typename T>
void
load_value(json_range& r, T& val)
{
auto& key = r.first.key();
auto& e = *r.first;
if constexpr (std::is_same_v<T, bool>)
{
if (e.is_boolean())
val = e.get<bool>();
else if (e.is_number_unsigned())
{
// Also accept 0 or 1 for bools (mainly to be compatible with bt-encoding which doesn't
// have a distinct bool type).
auto b = e.get<uint64_t>();
if (b <= 1)
val = b;
else
throw std::domain_error{"Invalid value for '" + key + "': expected boolean"};
}
else
{
throw std::domain_error{"Invalid value for '" + key + "': expected boolean"};
}
}
else if constexpr (std::is_unsigned_v<T>)
{
if (!e.is_number_unsigned())
throw std::domain_error{"Invalid value for '" + key + "': non-negative value required"};
auto i = e.get<uint64_t>();
if (sizeof(T) < sizeof(uint64_t) && i > std::numeric_limits<T>::max())
throw std::domain_error{"Invalid value for '" + key + "': value too large"};
val = i;
}
else if constexpr (std::is_integral_v<T>)
{
if (!e.is_number_integer())
throw std::domain_error{"Invalid value for '" + key + "': value is not an integer"};
auto i = e.get<int64_t>();
if (sizeof(T) < sizeof(int64_t))
{
if (i < std::numeric_limits<T>::lowest())
throw std::domain_error{
"Invalid value for '" + key + "': negative value magnitude is too large"};
if (i > std::numeric_limits<T>::max())
throw std::domain_error{"Invalid value for '" + key + "': value is too large"};
}
val = i;
}
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>)
{
val = e.get<std::string_view>();
}
else if constexpr (
llarp::rpc::json_is_binary<
T> || is_expandable_list<T> || is_tuple_like<T> || is_unordered_string_map<T>)
{
try
{
e.get_to(val);
}
catch (const std::exception& e)
{
throw std::domain_error{"Invalid values in '" + key + "'"};
}
}
else
{
static_assert(std::is_same_v<T, void>, "Unsupported load type");
}
++r.first;
}
template <typename TupleLike, size_t... Is>
void
load_tuple_values(oxenc::bt_list_consumer& c, TupleLike& val, std::index_sequence<Is...>)
{
(load_value(c, std::get<Is>(val)), ...);
}
// Takes a json object iterator or bt_dict_consumer and loads the current value at the iterator.
// This calls itself recursively, if needed, to unwrap optional/required/ignore_empty_string
// wrappers.
template <typename In, typename T>
void
load_curr_value(In& in, T& val)
{
if constexpr (is_required_wrapper<T>)
{
load_curr_value(in, val.value);
}
else if constexpr (is_ignore_empty_string_wrapper<T>)
{
if (!val.should_ignore(in))
load_curr_value(in, val.value);
}
else if constexpr (is_std_optional<T>)
{
load_curr_value(in, val.emplace());
}
else
{
load_value(in, val);
}
}
// Gets the next value from a json object iterator or bt_dict_consumer. Leaves the iterator at
// the next value, i.e. found + 1 if found, or the next greater value if not found. (NB:
// nlohmann::json objects are backed by an *ordered* map and so both nlohmann iterators and
// bt_dict_consumer behave analogously here).
template <typename In, typename T>
void
get_next_value(In& in, [[maybe_unused]] std::string_view name, T& val)
{
if constexpr (std::is_same_v<std::monostate, In>)
;
else if (skip_until(in, name))
load_curr_value(in, val);
else if constexpr (is_required_wrapper<T>)
throw std::runtime_error{"Required key '" + std::string{name} + "' not found"};
}
// Accessor for simple, flat value retrieval from a json or bt_dict_consumer. In the later
// case note that the given bt_dict_consumer will be advanced, so you *must* take care to
// process keys in order, both for the keys passed in here *and* for use before and after this
// call.
template <typename Input, typename T, typename... More>
void
get_values(Input& in, std::string_view name, T&& val, More&&... more)
{
if constexpr (std::is_same_v<rpc_input, Input>)
{
if (auto* json_in = std::get_if<nlohmann::json>(&in))
{
json_range r{json_in->cbegin(), json_in->cend()};
get_values(r, name, val, std::forward<More>(more)...);
}
else if (auto* dict = std::get_if<oxenc::bt_dict_consumer>(&in))
{
get_values(*dict, name, val, std::forward<More>(more)...);
}
else
{
// A monostate indicates that no parameters field was provided at all
get_values(var::get<std::monostate>(in), name, val, std::forward<More>(more)...);
}
}
else if constexpr (std::is_same_v<std::string_view, Input>)
{
if (in.front() == 'd')
{
oxenc::bt_dict_consumer d{in};
get_values(d, name, val, std::forward<More>(more)...);
}
else
{
auto json_in = nlohmann::json::parse(in);
json_range r{json_in.cbegin(), json_in.cend()};
get_values(r, name, val, std::forward<More>(more)...);
}
}
else
{
static_assert(
std::is_same_v<
json_range,
Input> || std::is_same_v<oxenc::bt_dict_consumer, Input> || std::is_same_v<std::monostate, Input>);
get_next_value(in, name, val);
if constexpr (sizeof...(More) > 0)
{
check_ascending_names(name, more...);
get_values(in, std::forward<More>(more)...);
}
}
}
} // namespace llarp::rpc

@ -0,0 +1,68 @@
#pragma once
#include "rpc_server.hpp"
#include "rpc_request_parser.hpp"
#include "rpc_request_decorators.hpp"
#include "rpc_request_definitions.hpp"
#include "json_bt.hpp"
#include <string_view>
#include <llarp/config/config.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
namespace llarp::rpc
{
using nlohmann::json;
template <typename RPC>
auto
make_invoke()
{
return [](oxenmq::Message& m, RPCServer& server) {
EndpointHandler<RPC> handler{server, m.send_later()};
auto& rpc = handler.rpc;
if (m.data.size() > 1)
m.send_reply(CreateJSONError(
"Bad Request: RPC requests must have at most one data part (received {})"_format(
m.data.size())));
// parsing input as bt or json
// hand off to parse_request (overloaded versions)
try
{
if (m.data.empty() or m.data[0].empty())
{
parse_request(rpc, nlohmann::json::object());
}
else if (m.data[0].front() == 'd')
{
rpc.set_bt();
parse_request(rpc, oxenc::bt_dict_consumer{m.data[0]});
}
else
{
parse_request(rpc, nlohmann::json::parse(m.data[0]));
}
}
catch (const std::exception& e)
{
m.send_reply(CreateJSONError("Failed to parse request parameters: "s + e.what()));
return;
}
if (not std::is_base_of_v<Immediate, RPC>)
{
server.m_Router.loop()->call_soon(std::move(handler));
}
else
{
handler();
}
};
}
} // namespace llarp::rpc

@ -0,0 +1,117 @@
#pragma once
#include "json_binary_proxy.hpp"
#include "json_bt.hpp"
#include <nlohmann/json_fwd.hpp>
#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
namespace tools
{
// Type wrapper that contains an arbitrary list of types.
template <typename...>
struct type_list
{};
} // namespace tools
namespace llarp::rpc
{
// Base class that all RPC requests will expand for each endpoint type
struct RPCRequest
{
private:
bool bt = false;
public:
// Returns true if response is bt-encoded, and false for json
// Note: do not set value
bool
is_bt() const
{
return bt;
}
// Callable method to indicate request is bt-encoded
void
set_bt()
{
bt = true;
response_b64.format = llarp::rpc::json_binary_proxy::fmt::bt;
response_hex.format = llarp::rpc::json_binary_proxy::fmt::bt;
}
// Invoked if this.replier is still present. If it is "stolen" by endpoint (moved from
// RPC struct), then endpoint handles sending reply
void
send_response()
{
replier->reply(is_bt() ? oxenc::bt_serialize(json_to_bt(std::move(response)))
: response.dump());
}
void
send_response(nlohmann::json _response)
{
response = std::move(_response);
send_response();
}
// Response Data:
// bt-encoded are converted in real-time
// - bool becomes 0 or 1
// - key:value where value == null are omitted
// - other nulls will raise an exception if found in json
// - no doubles
// - to store doubles: encode bt in endpoint-specific way
// - binary strings will fail json serialization; caller must
//
// std::string binary = some_binary_data();
// request.response["binary_value"] = is_bt ? binary : oxenmq::to_hex(binary)
//
nlohmann::json response;
// Proxy Object:
// Sets binary data in "response"
// - if return type is json, encodes as hex
// - if return type is bt, then binary is untouched
//
// Usage:
// std::string data = "abc";
// request.response_hex["foo"]["bar"] = data; // json: "616263", bt: "abc"
//
llarp::rpc::json_binary_proxy response_hex{response, llarp::rpc::json_binary_proxy::fmt::hex};
// Proxy Object:
// Encodes binary data as base_64 for json-encoded responses, leaves as binary for bt-encoded
// responses
//
// Usage:
// std::string data = "abc"
// request.response_b64["foo"]["bar"] = data; json: "YWJj", bt: "abc"
//
llarp::rpc::json_binary_proxy response_b64{
response, llarp::rpc::json_binary_proxy::fmt::base64};
// The oxenmq deferred send object into which the response will be set. If this optional is
// still set when the `invoke` call returns then the response is sent at that point; if it has
// been moved out (i.e. either just this instance or the whole request struct is stolen/moved by
// the invoke function) then it is the invoke function's job to send a reply. Typically this is
// done when a response cannot be sent immediately
std::optional<oxenmq::Message::DeferredSend> replier;
};
// Tag types that are inherited to set RPC endpoint properties
// RPC call wil take no input arguments
// Parameter dict can be passed, but will be ignored
struct NoArgs : virtual RPCRequest
{};
// RPC call will be executed immediately
struct Immediate : virtual RPCRequest
{};
} // namespace llarp::rpc

@ -0,0 +1,303 @@
#pragma once
#include "rpc_request_decorators.hpp"
#include "net/ip_range.hpp"
#include "router/abstractrouter.hpp"
#include "router/route_poker.hpp"
#include "service/address.hpp"
#include "service/endpoint.hpp"
#include "service/outbound_context.hpp"
#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
#include <unordered_map>
namespace llarp::rpc
{
// RPC: halt
// Stops lokinet router
//
// Inputs: none
//
struct Halt : NoArgs, Immediate
{
static constexpr auto name = "halt"sv;
};
// RPC: version
// Returns version and uptime information
//
// Inputs: none
//
// Returns: "OK"
// "uptime"
// "version"
//
struct Version : NoArgs, Immediate
{
static constexpr auto name = "version"sv;
};
// RPC: status
// Returns that current activity status of lokinet router
// Calls router::extractstatus
//
// Inputs: none
//
// Returns: massive dump of status info including
// "running"
// "numNodesKnown"
// "dht"
// "services"
// "exit"
// "links"
// "outboundMessages"
// etc
//
struct Status : NoArgs
{
static constexpr auto name = "status"sv;
};
// RPC: get_status
// Returns current summary status
//
// Inputs: none
//
// Returns: slightly smaller dump of status info including
// "authcodes"
// "exitMap"
// "lokiAddress"
// "networkReady"
// "numPathsBuilt"
// "numPeersConnected"
// etc
//
struct GetStatus : NoArgs
{
static constexpr auto name = "get_status"sv;
};
// RPC: quic_connect
// Initializes QUIC connection tunnel
// Passes request parameters in nlohmann::json format
//
// Inputs:
// "endpoint" : endpoint id (string)
// "bindAddr" : bind address (string, ex: "127.0.0.1:1142")
// "host" : remote host ID (string)
// "port" : port to bind to (int)
// "close" : close connection to port or host ID
//
// Returns:
// "id" : connection ID
// "addr" : connection local address
//
struct QuicConnect : RPCRequest
{
static constexpr auto name = "quic_connect"sv;
struct request_parameters
{
std::string bindAddr;
int closeID;
std::string endpoint;
uint16_t port;
std::string remoteHost;
} request;
};
// RPC: quick_listener
// Connects to QUIC interface on local endpoint
// Passes request parameters in nlohmann::json format
//
// Inputs:
// "endpoint" : endpoint id (string)
// "host" : remote host ID (string)
// "port" : port to bind to (int)
// "close" : close connection to port or host ID
// "srv-proto" :
//
// Returns:
// "id" : connection ID
// "addr" : connection local address
//
struct QuicListener : RPCRequest
{
static constexpr auto name = "quic_listener"sv;
struct request_parameters
{
int closeID;
std::string endpoint;
uint16_t port;
std::string remoteHost;
std::string srvProto;
} request;
};
// RPC: lookup_snode
// Look up service node
// Passes request parameters in nlohmann::json format
//
// Inputs:
// "routerID" : router ID to query (string)
//
// Returns:
// "ip" : snode IP address
//
struct LookupSnode : RPCRequest
{
static constexpr auto name = "lookup_snode"sv;
struct request_parameters
{
std::string routerID;
} request;
};
// RPC: exit
// Seems like this adds an exit node?
//
// Note: ask Jason about the internals of this
//
// Inputs:
// "endpoint" :
// "unmap" : if true, unmaps connection to exit node (bool)
// "range" : IP range to map to exit node
// "token" :
//
// Returns:
//
struct Exit : RPCRequest
{
static constexpr auto name = "exit"sv;
struct request_parameters
{
std::string address;
std::string ip_range;
std::string token;
bool unmap;
} request;
void
onGoodResult(std::string reason, bool hasClient)
{
response = (hasClient) ?
nlohmann::json{{"result", reason}}.dump() :
nlohmann::json{{"error", "We don't have an exit?"}}.dump();
}
void
onBadResult(std::string reason, AbstractRouter& abs, llarp::service::Endpoint_ptr eptr, IPRange range)
{
abs.routePoker()->Down();
eptr->UnmapExitRange(range);
response = nlohmann::json{{"result", reason}}.dump();
}
void
mapExit(service::Address addr, AbstractRouter& router, llarp::service::Endpoint_ptr eptr, IPRange range, service::Address exitAddr)
{
eptr->MapExitRange(range, addr);
bool sendAuth = (request.token.empty()) ? false : true;
if (sendAuth)
eptr->SetAuthInfoForEndpoint(exitAddr, service::AuthInfo{request.token});
if (addr.IsZero())
{
onGoodResult("Null exit added", router.HasClientExit());
return;
}
eptr->MarkAddressOutbound(addr);
eptr->EnsurePathToService(addr, [&](auto, service::OutboundContext* ctx) {
if (ctx == nullptr)
{
onBadResult("Could not find exit", router, eptr, range);
return;
}
if (not sendAuth)
{
onGoodResult("OK: connected to " + addr.ToString(), router.HasClientExit());
return;
}
// only lambda that we will keep
ctx->AsyncSendAuth([&](service::AuthResult result) {
if (result.code != service::AuthResultCode::eAuthAccepted)
{
onBadResult(result.reason, router, eptr, range);
return;
}
onGoodResult(result.reason, router.HasClientExit());
return;
});
});
}
};
// RPC: dns_query
// Attempts to query endpoint by domain name
//
// Note: ask Jason about the internals of this
//
// Inputs:
// "endpoint" : endpoint ID to query (string)
// "qname" : query name (string)
// "qtype" : query type (int)
//
// Returns:
//
struct DNSQuery : Immediate
{
static constexpr auto name = "dns_query"sv;
struct request_parameters
{
std::string endpoint;
uint16_t qtype;
std::string qname;
} request;
};
// RPC: config
// Runs lokinet router using .ini config file passed as path
//
// Inputs:
// "filename" : name of .ini file to either save or delete
// "ini" : .ini chunk to save in new file
// "del" : boolean specifying whether to delete file "filename" or save it
//
// Returns:
//
struct Config : Immediate
{
static constexpr auto name = "config"sv;
struct request_parameters
{
bool del;
std::string filename;
std::string ini;
} request;
};
// List of all RPC request structs to allow compile-time enumeration of all supported types
using rpc_request_types = tools::type_list<
Halt,
Version,
Status,
GetStatus,
QuicConnect,
QuicListener,
LookupSnode,
Exit,
DNSQuery,
Config>;
} // namespace llarp::rpc

@ -0,0 +1,94 @@
#include "rpc_request_parser.hpp"
#include "param_parser.hpp"
#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
namespace llarp::rpc
{
using nlohmann::json;
void
parse_request(QuicConnect& quicconnect, rpc_input input)
{
get_values(
input,
"bindAddr",
quicconnect.request.bindAddr,
"closeID",
quicconnect.request.closeID,
"endpoint",
quicconnect.request.endpoint,
"port",
quicconnect.request.port,
"remoteHost",
quicconnect.request.remoteHost);
}
void
parse_request(QuicListener& quiclistener, rpc_input input)
{
get_values(
input,
"closeID",
quiclistener.request.closeID,
"endpoint",
quiclistener.request.endpoint,
"port",
quiclistener.request.port,
"remoteHost",
quiclistener.request.remoteHost,
"srvProto",
quiclistener.request.srvProto);
}
void
parse_request(LookupSnode& lookupsnode, rpc_input input)
{
get_values(input, "routerID", lookupsnode.request.routerID);
}
void
parse_request(Exit& exit, rpc_input input)
{
get_values(
input,
"address",
exit.request.address,
"IP_range",
exit.request.ip_range,
"token",
exit.request.token,
"unmap",
exit.request.unmap);
}
void
parse_request(DNSQuery& dnsquery, rpc_input input)
{
get_values(
input,
"endpoint",
dnsquery.request.endpoint,
"qname",
dnsquery.request.qname,
"qtype",
dnsquery.request.qtype);
}
void
parse_request(Config& config, rpc_input input)
{
get_values(
input,
"delete",
config.request.del,
"filename",
config.request.filename,
"ini",
config.request.ini);
}
} // namespace llarp::rpc

@ -0,0 +1,31 @@
#pragma once
#include "rpc_request_definitions.hpp"
#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
namespace llarp::rpc
{
using rpc_input = std::variant<std::monostate, nlohmann::json, oxenc::bt_dict_consumer>;
inline void
parse_request(NoArgs&, rpc_input)
{}
void
parse_request(QuicConnect& quicconnect, rpc_input input);
void
parse_request(QuicListener& quiclistener, rpc_input input);
void
parse_request(LookupSnode& lookupsnode, rpc_input input);
void
parse_request(Exit& exit, rpc_input input);
void
parse_request(DNSQuery& dnsquery, rpc_input input);
void
parse_request(Config& config, rpc_input input);
} // namespace llarp::rpc

File diff suppressed because it is too large Load Diff

@ -1,35 +1,159 @@
#pragma once
#include "rpc_request_definitions.hpp"
#include "json_bt.hpp"
#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/message.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
namespace llarp
{
struct AbstractRouter;
}
} // namespace llarp
namespace
{
static auto logcat = llarp::log::Cat("lokinet.rpc");
} // namespace
namespace llarp::rpc
{
using LMQ_ptr = std::shared_ptr<oxenmq::OxenMQ>;
using DeferredSend = oxenmq::Message::DeferredSend;
class RPCServer;
struct RpcServer
// Stores RPC request callback
struct rpc_callback
{
explicit RpcServer(LMQ_ptr, AbstractRouter*);
~RpcServer() = default;
using result_type = std::variant<oxenc::bt_value, nlohmann::json, std::string>;
// calls with incoming request data; returns response body or throws exception
void (*invoke)(oxenmq::Message&, RPCServer&);
};
void
AddRPCCategories();
// RPC request registration
// Stores references to RPC requests in a unordered map for ease of reference
// when adding to server. To add endpoints, define in rpc_request_definitions.hpp
// and register in rpc_server.cpp
extern const std::unordered_map<std::string, std::shared_ptr<const rpc_callback>> rpc_request_map;
// Exception used to signal various types of errors with a request back to the caller. This
// exception indicates that the caller did something wrong: bad data, invalid value, etc., but
// don't indicate a local problem (and so we'll log them only at debug). For more serious,
// internal errors a command should throw some other stl error (e.g. std::runtime_error or
// perhaps std::logic_error), which will result in a local daemon warning (and a generic internal
// error response to the user).
//
// For JSON RPC these become an error response with the code as the error.code value and the
// string as the error.message.
// For HTTP JSON these become a 500 Internal Server Error response with the message as the body.
// For OxenMQ the code becomes the first part of the response and the message becomes the
// second part of the response.
struct rpc_error : std::runtime_error
{
/// \param message - a message to send along with the error code (see general description
/// above).
rpc_error(std::string message)
: std::runtime_error{"RPC error: " + message}, message{std::move(message)}
{}
std::string message;
};
template <typename Result_t>
std::string
CreateJSONResponse(Result_t result)
{
return nlohmann::json{{"result", result}}.dump();
}
inline std::string
CreateJSONError(std::string_view msg)
{
return nlohmann::json{{"error", msg}}.dump();
}
class RPCServer
{
public:
explicit RPCServer(LMQ_ptr, AbstractRouter&);
~RPCServer() = default;
private:
void
HandleLogsSubRequest(oxenmq::Message& m);
LMQ_ptr m_LMQ;
AbstractRouter* const m_Router;
void
AddCategories();
void
invoke(Halt& halt);
void
invoke(Version& version);
void
invoke(Status& status);
void
invoke(GetStatus& getstatus);
void
invoke(QuicConnect& quicconnect);
void
invoke(QuicListener& quiclistener);
void
invoke(LookupSnode& lookupsnode);
void
invoke(Exit& exit);
void
invoke(DNSQuery& dnsquery);
void
invoke(Config& config);
LMQ_ptr m_LMQ;
AbstractRouter& m_Router;
oxen::log::PubsubLogger log_subs;
};
template <typename RPC>
class EndpointHandler
{
public:
RPCServer& server;
RPC rpc{};
EndpointHandler(RPCServer& _server, DeferredSend _replier)
: server{_server}
{
rpc.replier.emplace(std::move(_replier));
}
void
operator()()
{
try
{
server.invoke(rpc);
}
catch (const rpc_error& e)
{
log::info(logcat, "RPC request 'rpc.{}' failed with: {}", rpc.name, e.what());
rpc.response = CreateJSONError(
fmt::format("RPC request 'rpc.{}' failed with: {}", rpc.name, e.what()));
}
catch (const std::exception& e)
{
log::info(logcat, "RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what());
rpc.response = CreateJSONError(
fmt::format("RPC request 'rpc.{}' raised an exception: {}", rpc.name, e.what()));
};
// check if std::optional in rpc is present
// then rpc.send_response
// else
// do nothing because invoke stole RPC
if (rpc.replier.has_value())
rpc.send_response();
}
};
} // namespace llarp::rpc

@ -1,5 +1,5 @@
#include "convotag.hpp"
#include "net/ip.hpp"
#include <llarp/net/ip.hpp>
namespace llarp::service
{

@ -1,7 +1,15 @@
#include <chrono>
#include <memory>
#include "endpoint.hpp"
#include "endpoint_state.hpp"
#include "endpoint_util.hpp"
#include "hidden_service_address_lookup.hpp"
#include "outbound_context.hpp"
#include "protocol.hpp"
#include "info.hpp"
#include "protocol_type.hpp"
#include <llarp/net/ip.hpp>
#include <llarp/dht/context.hpp>
#include <llarp/dht/key.hpp>
#include <llarp/dht/messages/findintro.hpp>
@ -16,27 +24,18 @@
#include <llarp/router/abstractrouter.hpp>
#include <llarp/routing/dht_message.hpp>
#include <llarp/routing/path_transfer_message.hpp>
#include "endpoint_state.hpp"
#include "endpoint_util.hpp"
#include "hidden_service_address_lookup.hpp"
#include "net/ip.hpp"
#include "outbound_context.hpp"
#include "protocol.hpp"
#include "service/info.hpp"
#include "service/protocol_type.hpp"
#include <llarp/util/str.hpp>
#include <llarp/util/buffer.hpp>
#include <llarp/util/meta/memfn.hpp>
#include <llarp/link/link_manager.hpp>
#include <llarp/tooling/dht_event.hpp>
#include <llarp/quic/server.hpp>
#include <llarp/quic/tunnel.hpp>
#include <llarp/util/priority_queue.hpp>
#include <optional>
#include <utility>
#include <llarp/quic/server.hpp>
#include <llarp/quic/tunnel.hpp>
#include <uvw.hpp>
#include <variant>

@ -6,24 +6,27 @@
#include <llarp/net/net.hpp>
#include <llarp/path/path.hpp>
#include <llarp/path/pathbuilder.hpp>
#include "address.hpp"
#include "handler.hpp"
#include "identity.hpp"
#include "pendingbuffer.hpp"
#include "protocol.hpp"
#include "sendcontext.hpp"
#include "service/protocol_type.hpp"
#include "session.hpp"
#include "lookup.hpp"
#include <llarp/util/compare_ptr.hpp>
// --- begin kitchen sink headers ----
#include <llarp/service/address.hpp>
#include <llarp/service/handler.hpp>
#include <llarp/service/identity.hpp>
#include <llarp/service/pendingbuffer.hpp>
#include <llarp/service/protocol.hpp>
#include <llarp/service/sendcontext.hpp>
#include <llarp/service/protocol_type.hpp>
#include <llarp/service/session.hpp>
#include <llarp/service/lookup.hpp>
#include <llarp/service/endpoint_types.hpp>
#include <llarp/endpoint_base.hpp>
#include <llarp/service/auth.hpp>
// ----- end kitchen sink headers -----
#include <optional>
#include <unordered_map>
#include <variant>
#include <oxenc/variant.h>
#include "endpoint_types.hpp"
#include "llarp/endpoint_base.hpp"
#include "auth.hpp"
#include <llarp/vpn/egres_packet_router.hpp>
#include <llarp/dns/server.hpp>

@ -1,5 +1,5 @@
#include "intro.hpp"
#include "util/time.hpp"
#include <llarp/util/time.hpp>
namespace llarp
{

@ -1,16 +1,15 @@
#include "outbound_context.hpp"
#include <llarp/router/abstractrouter.hpp>
#include "async_key_exchange.hpp"
#include "hidden_service_address_lookup.hpp"
#include "endpoint.hpp"
#include "endpoint_util.hpp"
#include "protocol_type.hpp"
#include <llarp/router/abstractrouter.hpp>
#include <llarp/nodedb.hpp>
#include <llarp/profiling.hpp>
#include <llarp/util/meta/memfn.hpp>
#include "endpoint_util.hpp"
#include "service/protocol_type.hpp"
#include <random>
#include <algorithm>

@ -3,8 +3,8 @@
#include "router_event.hpp"
#include <llarp.hpp>
#include <config/config.hpp>
#include <tooling/hive_context.hpp>
#include <llarp/config/config.hpp>
#include <llarp/tooling/hive_context.hpp>
#include <vector>
#include <deque>

@ -1,4 +1,4 @@
#include "vpn/win32.hpp"
#include "win32.hpp"
#include <llarp/win32/windivert.hpp>
#include <llarp/win32/wintun.hpp>
#include <fmt/core.h>

@ -3,7 +3,8 @@
#include <llarp/tooling/hive_context.hpp>
#include <llarp/router/router.hpp>
#include <llarp/handlers/pyhandler.hpp>
#include "service/protocol_type.hpp"
#include <llarp/service/protocol_type.hpp>
namespace llarp
{
void

@ -60,8 +60,4 @@ if(WIN32)
target_link_libraries(testAll PUBLIC ws2_32 iphlpapi shlwapi)
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
target_link_directories(testAll PRIVATE /usr/local/lib)
endif()
add_custom_target(check COMMAND testAll)

@ -1,8 +1,8 @@
#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>
#include <util/logging.hpp>
#include <util/service_manager.hpp>
#include <llarp/util/logging.hpp>
#include <llarp/util/service_manager.hpp>
#ifdef _WIN32
#include <winsock2.h>

@ -1,4 +1,4 @@
#include <config/definition.hpp>
#include <llarp/config/definition.hpp>
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
#include <config/ini.hpp>
#include <llarp/config/ini.hpp>
#include <catch2/catch.hpp>
@ -42,12 +42,12 @@ TEST_CASE("ConfigParser", "[config]")
SECTION("No key")
{
REQUIRE_FALSE(parser.LoadFromStr("[test]\n=1090\n"));
REQUIRE_THROWS(parser.LoadFromStr("[test]\n=1090\n"));
}
SECTION("Parse invalid")
{
REQUIRE_FALSE(
REQUIRE_THROWS(
parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
"g4syhgd5\n"));
}

@ -1,4 +1,4 @@
#include <config/definition.hpp>
#include <llarp/config/definition.hpp>
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
#include <crypto/crypto_libsodium.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <iostream>

@ -1,9 +1,9 @@
#include <crypto/types.hpp>
#include <llarp/crypto/types.hpp>
#include <fstream>
#include <string>
#include <test_util.hpp>
#include "test_util.hpp"
#include <catch2/catch.hpp>
extern "C" {

@ -1,14 +1,15 @@
#include <config/key_manager.hpp>
#include "llarp_test.hpp"
#include "test_util.hpp"
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <llarp/config/key_manager.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <functional>
#include <random>
#include <string>
#include <test_util.hpp>
#include <catch2/catch.hpp>
using namespace ::llarp;

@ -1,11 +1,11 @@
#include <catch2/catch.hpp>
#include <dns/dns.hpp>
#include <dns/message.hpp>
#include <dns/name.hpp>
#include <dns/rr.hpp>
#include <net/net.hpp>
#include <net/ip.hpp>
#include <util/buffer.hpp>
#include <llarp/dns/dns.hpp>
#include <llarp/dns/message.hpp>
#include <llarp/dns/name.hpp>
#include <llarp/dns/rr.hpp>
#include <llarp/net/net.hpp>
#include <llarp/net/ip.hpp>
#include <llarp/util/buffer.hpp>
#include <algorithm>

@ -1,6 +1,6 @@
#pragma once
#include <crypto/crypto_libsodium.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <catch2/catch.hpp>
namespace llarp::test

@ -1,4 +1,4 @@
#include <net/ip_address.hpp>
#include <llarp/net/ip_address.hpp>
#include <catch2/catch.hpp>

@ -1,7 +1,7 @@
#include <net/net_int.hpp>
#include <net/ip.hpp>
#include <net/ip_range.hpp>
#include <net/net.hpp>
#include <llarp/net/net_int.hpp>
#include <llarp/net/ip.hpp>
#include <llarp/net/ip_range.hpp>
#include <llarp/net/net.hpp>
#include <oxenc/hex.h>
#include <catch2/catch.hpp>

@ -1,6 +1,6 @@
#include <util/mem.hpp>
#include <net/sock_addr.hpp>
#include <net/net_if.hpp>
#include <llarp/util/mem.hpp>
#include <llarp/net/sock_addr.hpp>
#include <llarp/net/net_if.hpp>
#include <llarp/util/logging.hpp>
#include <catch2/catch.hpp>

@ -1,8 +1,8 @@
#include <catch2/catch.hpp>
#include "config/config.hpp"
#include <router_contact.hpp>
#include <nodedb.hpp>
#include <llarp/config/config.hpp>
#include <llarp/router_contact.hpp>
#include <llarp/nodedb.hpp>
using llarp_nodedb = llarp::NodeDB;

@ -1,4 +1,4 @@
#include <path/path.hpp>
#include <llarp/path/path.hpp>
#include <catch2/catch.hpp>
using Path_t = llarp::path::Path;

@ -1,12 +1,12 @@
#include <peerstats/peer_db.hpp>
#include <llarp/peerstats/peer_db.hpp>
#include <test_util.hpp>
#include <numeric>
#include <catch2/catch.hpp>
#include "peerstats/types.hpp"
#include "router_contact.hpp"
#include "util/logging.hpp"
#include "util/time.hpp"
#include <llarp/peerstats/types.hpp>
#include <llarp/router_contact.hpp>
#include <llarp/util/logging.hpp>
#include <llarp/util/time.hpp>
TEST_CASE("Test PeerDb PeerStats memory storage", "[PeerDb]")
{

@ -1,5 +1,5 @@
#include <numeric>
#include <peerstats/types.hpp>
#include <llarp/peerstats/types.hpp>
#include <test_util.hpp>
#include <catch2/catch.hpp>

@ -1,5 +1,5 @@
#include <router_version.hpp>
#include "router/router.hpp"
#include <llarp/router_version.hpp>
#include <llarp/router/router.hpp>
#include <catch2/catch.hpp>

@ -1,8 +1,7 @@
#include <exit/exit_messages.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include "llarp_test.hpp"
#include <llarp/exit/exit_messages.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
#include <routing/transfer_traffic_message.hpp>
#include <llarp/routing/transfer_traffic_message.hpp>
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
#include <service/address.hpp>
#include <llarp/service/address.hpp>
#include <catch2/catch.hpp>

@ -1,13 +1,13 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <sodium/crypto_scalarmult_ed25519.h>
#include <path/path.hpp>
#include <service/address.hpp>
#include <service/identity.hpp>
#include <service/intro_set.hpp>
#include <util/time.hpp>
#include <llarp/path/path.hpp>
#include <llarp/service/address.hpp>
#include <llarp/service/identity.hpp>
#include <llarp/service/intro_set.hpp>
#include <llarp/util/time.hpp>
#include <test_util.hpp>
#include "test_util.hpp"
#include <catch2/catch.hpp>
using namespace llarp;

@ -1,6 +1,6 @@
#include "catch2/catch.hpp"
#include <crypto/crypto_libsodium.hpp>
#include <service/name.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <llarp/service/name.hpp>
#include <oxenc/hex.h>
using namespace std::literals;

@ -1,12 +1,9 @@
#include <crypto/encrypted_frame.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <messages/relay_commit.hpp>
#include <test_util.hpp>
#include "llarp_test.hpp"
#include "test_util.hpp"
#include <llarp/crypto/encrypted_frame.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <llarp/messages/relay_commit.hpp>
#include <catch2/catch.hpp>
using namespace ::llarp;

@ -1,9 +1,9 @@
#include <catch2/catch.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <router_contact.hpp>
#include <net/net_int.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
#include <llarp/router_contact.hpp>
#include <llarp/net/net_int.hpp>
#include <llarp/util/time.hpp>
namespace

@ -1,4 +1,4 @@
#include <test_util.hpp>
#include "test_util.hpp"
#include <random>

@ -1,9 +1,8 @@
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include <util/fs.hpp>
#include <util/types.hpp>
#include <llarp/util/fs.hpp>
#include <llarp/util/types.hpp>
#include <bitset>
#include <vector>

@ -1,4 +1,4 @@
#include <util/meta/memfn.hpp>
#include <llarp/util/meta/memfn.hpp>
#include <catch2/catch.hpp>

@ -1,6 +1,6 @@
#include <catch2/catch.hpp>
#include <util/aligned.hpp>
#include <llarp/util/aligned.hpp>
#include <iostream>
#include <sstream>

@ -1,5 +1,5 @@
#include <util/bencode.h>
#include <util/bencode.hpp>
#include <llarp/util/bencode.h>
#include <llarp/util/bencode.hpp>
#include <iostream>
#include <string>

@ -1,5 +1,5 @@
#include <catch2/catch.hpp>
#include <util/bits.hpp>
#include <llarp/util/bits.hpp>
using namespace llarp::bits;

@ -1,5 +1,5 @@
#include <util/decaying_hashset.hpp>
#include <router_id.hpp>
#include <llarp/util/decaying_hashset.hpp>
#include <llarp/router_id.hpp>
#include <catch2/catch.hpp>
TEST_CASE("DecayingHashSet test decay static time", "[decaying-hashset]")

@ -1,6 +1,6 @@
#include <catch2/catch.hpp>
#include <util/logging.hpp>
#include <config/config.hpp>
#include <llarp/util/logging.hpp>
#include <llarp/config/config.hpp>
#include <oxen/log/level.hpp>
using TestString = std::string;

@ -1,4 +1,4 @@
#include <util/str.hpp>
#include <llarp/util/str.hpp>
#include <catch2/catch.hpp>
#include <vector>

@ -1,6 +1,6 @@
#include <util/thread/queue.hpp>
#include <util/thread/threading.hpp>
#include <util/thread/barrier.hpp>
#include <llarp/util/thread/queue.hpp>
#include <llarp/util/thread/threading.hpp>
#include <llarp/util/thread/barrier.hpp>
#include <array>
#include <condition_variable>

@ -1,4 +1,4 @@
#include <util/thread/queue_manager.hpp>
#include <llarp/util/thread/queue_manager.hpp>
#include <optional>
#include <vector>

Loading…
Cancel
Save