Skip to content

Commit 208a931

Browse files
committed
relay sesh anyone?
1 parent 2520731 commit 208a931

File tree

8 files changed

+231
-112
lines changed

8 files changed

+231
-112
lines changed

llarp/constants/path.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace llarp::path
2020
inline constexpr size_t DEFAULT_PATHS_HELD{4};
2121

2222
/// TESTNET: default path lifetime in ms;
23-
inline constexpr std::chrono::milliseconds DEFAULT_LIFETIME{20min};
23+
inline constexpr std::chrono::milliseconds DEFAULT_LIFETIME{1min};
2424

2525
/// interval at which we try to build new paths for intros
2626
inline constexpr std::chrono::milliseconds PATH_ROTATION_INTERVAL{DEFAULT_LIFETIME / DEFAULT_PATHS_HELD};

llarp/handlers/session.cpp

+122-51
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ namespace llarp::handlers
666666
NetworkAddress initiator,
667667
HopID remote_pivot_txid,
668668
std::shared_ptr<path::Path> path,
669-
shared_kx_data kx_data,
669+
std::optional<shared_kx_data> kx_data,
670670
bool use_tun)
671671
{
672672
auto tag = session_tag::make(protoflags);
@@ -781,8 +781,7 @@ namespace llarp::handlers
781781
return ret;
782782
}
783783

784-
/** Session Initiation Message Structure:
785-
784+
/** Client Session Initiation Message Structure:
786785
- 'k' : next HopID
787786
- 'n' : symmetric nonce
788787
- 'x' : encrypted payload
@@ -811,7 +810,7 @@ namespace llarp::handlers
811810
- 'u' : Authentication field
812811
- bt-encoded dict, values TBD
813812
*/
814-
void SessionEndpoint::_make_session(
813+
void SessionEndpoint::_make_client_session(
815814
intro_set intros,
816815
NetworkAddress remote,
817816
ClientIntro remote_intro,
@@ -833,8 +832,7 @@ namespace llarp::handlers
833832

834833
log::trace(logcat, "inner payload: {}", buffer_printer{inner_payload});
835834

836-
auto pivot_payload =
837-
ONION::serialize_hop(remote_intro.pivot_txid.to_view(), SymmNonce::make_random(), inner_payload);
835+
auto pivot_payload = ONION::serialize_hop(pivot_txid.to_view(), SymmNonce::make_random(), inner_payload);
838836
log::trace(logcat, "pivot payload: {}", buffer_printer{pivot_payload});
839837

840838
auto intermediate_payload = PATH::CONTROL::serialize("path_control", std::move(pivot_payload));
@@ -852,7 +850,7 @@ namespace llarp::handlers
852850
session_keys = std::move(kx_data)](oxen::quic::message m) mutable {
853851
if (m)
854852
{
855-
log::debug(logcat, "Call to InitiateSession succeeded!");
853+
log::debug(logcat, "Call to initiate OutboundClientSession succeeded!");
856854
session_tag tag;
857855

858856
try
@@ -902,7 +900,102 @@ namespace llarp::handlers
902900
logcat,
903901
"Lokinet TUN failed to map route for session traffic to remote: {}",
904902
session->remote());
905-
// TESTNET: TODO: CLOSE THIS BISH HERE
903+
// TESTNET: TODO: CLOSE THIS HERE
904+
}
905+
else
906+
{
907+
log::info(logcat, "Starting TCP listener to route session traffic to backend...");
908+
session->tcp_backend_listen(std::move(hook));
909+
}
910+
}
911+
else
912+
{
913+
std::optional<std::string> status = std::nullopt;
914+
try
915+
{
916+
oxenc::bt_dict_consumer btdc{m.body()};
917+
918+
if (auto s = btdc.maybe<std::string>(messages::STATUS_KEY))
919+
status = s;
920+
}
921+
catch (const std::exception& e)
922+
{
923+
log::warning(logcat, "Exception: {}", e.what());
924+
}
925+
926+
log::critical(
927+
logcat,
928+
"Call to initiate OutboundClientSession FAILED; reason: {}",
929+
status.value_or("<none given>"));
930+
}
931+
});
932+
933+
log::debug(logcat, "message sent...");
934+
}
935+
936+
void SessionEndpoint::_make_relay_session(
937+
RemoteRC rc, NetworkAddress remote, std::shared_ptr<path::Path> path, on_session_init_hook cb)
938+
{
939+
std::string payload = InitiateSession::serialize(
940+
_router.local_rid(),
941+
path->pivot_txid(),
942+
path->pivot_txid(),
943+
fetch_auth_token(remote),
944+
_router.using_tun_if());
945+
946+
log::trace(logcat, "payload: {}", buffer_printer{payload});
947+
948+
path->send_path_control_message(
949+
"session_init",
950+
std::move(payload),
951+
[this, rc = std::move(rc), remote, path, hook = std::move(cb)](oxen::quic::message m) mutable {
952+
if (m)
953+
{
954+
log::debug(logcat, "Call to initiate OutboundRelaySession succeeded!");
955+
session_tag tag;
956+
957+
try
958+
{
959+
tag = InitiateSession::deserialize_response(oxenc::bt_dict_consumer{m.body()});
960+
}
961+
catch (const std::exception& e)
962+
{
963+
// TESTNET: TODO: close session here?
964+
log::warning(logcat, "Exception: {}", e.what());
965+
return;
966+
}
967+
968+
log::trace(logcat, "Remote relay has provided session tag: {}", tag);
969+
970+
auto pivot_txid = path->pivot_txid();
971+
972+
auto outbound = std::make_shared<session::OutboundRelaySession>(
973+
remote, *this, std::move(path), std::move(tag), std::move(pivot_txid));
974+
975+
auto [session, _] = _sessions.insert_or_assign(std::move(remote), std::move(outbound));
976+
session->activate();
977+
978+
log::trace(logcat, "Outbound session to {} successfully created...", session->remote());
979+
980+
if (session->using_tun())
981+
{
982+
log::trace(logcat, "Instructing lokinet TUN device to create mapped route...");
983+
if (auto maybe_ip = _router.tun_endpoint()->map_session_to_local_ip(session->remote()))
984+
{
985+
log::info(
986+
logcat,
987+
"TUN device successfully routing session (remote: {}) via local ip: {}",
988+
session->remote(),
989+
std::holds_alternative<ipv4>(*maybe_ip) ? std::get<ipv4>(*maybe_ip).to_string()
990+
: std::get<ipv6>(*maybe_ip).to_string());
991+
992+
return hook(*maybe_ip);
993+
}
994+
995+
log::critical(
996+
logcat,
997+
"Lokinet TUN failed to map route for session traffic to remote: {}",
998+
session->remote());
906999
}
9071000
else
9081001
{
@@ -926,30 +1019,32 @@ namespace llarp::handlers
9261019
}
9271020

9281021
log::critical(
929-
logcat, "Call to InitiateSession FAILED; reason: {}", status.value_or("<none given>"));
1022+
logcat,
1023+
"Call to initiate OutboundRelaySession FAILED; reason: {}",
1024+
status.value_or("<none given>"));
9301025
}
9311026
});
9321027

9331028
log::debug(logcat, "message sent...");
9341029
}
9351030

936-
void SessionEndpoint::_make_session_path(RemoteRC rc, NetworkAddress remote, on_session_init_hook cb)
1031+
void SessionEndpoint::_make_relay_session_path(RemoteRC rc, NetworkAddress remote, on_session_init_hook cb)
9371032
{
9381033
log::debug(logcat, "{} called", __PRETTY_FUNCTION__);
9391034

9401035
path_build_iterative(
9411036
SESSION_PATH_BUILD_ATTEMPTS,
9421037
rc,
9431038
remote,
944-
[this, rc, remote, cb](std::shared_ptr<path::Path> p) {
1039+
[this, rc, remote, cb](std::shared_ptr<path::Path> new_path) {
9451040
log::info(logcat, "Path build to remote:{} succeeded, initiating session!", remote);
9461041
(void)this;
947-
(void)p;
1042+
(void)new_path;
9481043
},
9491044
false);
9501045
}
9511046

952-
void SessionEndpoint::_make_session_path(intro_set intros, NetworkAddress remote, on_session_init_hook cb)
1047+
void SessionEndpoint::_make_client_session_path(intro_set intros, NetworkAddress remote, on_session_init_hook cb)
9531048
{
9541049
log::debug(logcat, "{} called", __PRETTY_FUNCTION__);
9551050

@@ -958,13 +1053,21 @@ namespace llarp::handlers
9581053
remote,
9591054
[this, intros, remote, cb](std::shared_ptr<path::Path> new_path, ClientIntro remote_intro) mutable {
9601055
log::info(logcat, "Path build to remote:{} succeeded, initiating session!", remote);
961-
return _make_session(
1056+
return _make_client_session(
9621057
std::move(intros), std::move(remote), std::move(remote_intro), std::move(new_path), std::move(cb));
9631058
},
9641059
false);
9651060
}
9661061

967-
bool SessionEndpoint::_initiate_client_session(NetworkAddress remote, on_session_init_hook cb)
1062+
void SessionEndpoint::initiate_remote_session(const NetworkAddress& remote, on_session_init_hook cb)
1063+
{
1064+
if (remote.is_client())
1065+
_initiate_client_session(remote, std::move(cb));
1066+
else
1067+
_initiate_relay_session(remote, std::move(cb));
1068+
}
1069+
1070+
void SessionEndpoint::_initiate_client_session(NetworkAddress remote, on_session_init_hook cb)
9681071
{
9691072
auto counter = std::make_shared<size_t>(num_paths_desired);
9701073

@@ -979,20 +1082,15 @@ namespace llarp::handlers
9791082
{
9801083
*counter = 0;
9811084
log::debug(logcat, "Session initiation returned client contact: {}", cc->to_string());
982-
_make_session_path(std::move(*cc).take_intros(), remote, std::move(hook));
1085+
_make_client_session_path(std::move(*cc).take_intros(), remote, std::move(hook));
9831086
}
9841087
else if (--*counter == 0)
985-
log::warning(
986-
logcat,
987-
"Failed to initiate session at 'find_cc' (target:{})",
988-
remote.router_id().short_string());
1088+
log::warning(logcat, "Failed to initiate session at 'find_cc' (target:{})", remote);
9891089
});
9901090
});
991-
992-
return true;
9931091
}
9941092

995-
bool SessionEndpoint::_initiate_relay_session(NetworkAddress remote, on_session_init_hook cb)
1093+
void SessionEndpoint::_initiate_relay_session(NetworkAddress remote, on_session_init_hook cb)
9961094
{
9971095
auto counter = std::make_shared<size_t>(num_paths_desired);
9981096

@@ -1007,39 +1105,12 @@ namespace llarp::handlers
10071105
{
10081106
*counter = 0;
10091107
log::debug(logcat, "Session initiation returned RC: {}", rc->to_string());
1010-
(void)this;
1108+
_make_relay_session_path(std::move(*rc), remote, std::move(hook));
10111109
}
10121110
else if (--*counter == 0)
10131111
log::warning(logcat, "Failed to initiate session at `fetch_rcs` (target:{})", remote);
10141112
});
10151113
});
1016-
1017-
return true;
1018-
}
1019-
1020-
bool SessionEndpoint::_initiate_session(NetworkAddress remote, on_session_init_hook cb)
1021-
{
1022-
auto counter = std::make_shared<size_t>(num_paths_desired);
1023-
1024-
_router.loop()->call([this, remote, handler = std::move(cb), counter]() mutable {
1025-
lookup_client_intro(
1026-
remote.router_id(),
1027-
[this, remote, hook = std::move(handler), counter](std::optional<ClientContact> cc) mutable {
1028-
if (*counter == 0)
1029-
return;
1030-
1031-
if (cc)
1032-
{
1033-
*counter = 0;
1034-
log::debug(logcat, "Session initiation returned client contact: {}", cc->to_string());
1035-
_make_session_path(std::move(*cc).take_intros(), remote, std::move(hook));
1036-
}
1037-
else if (--*counter == 0)
1038-
log::warning(logcat, "Failed to initiate session at 'find_cc' (target:{})", remote);
1039-
});
1040-
});
1041-
1042-
return true;
10431114
}
10441115

10451116
void SessionEndpoint::map_remote_to_local_addr(NetworkAddress remote, oxen::quic::Address local)

llarp/handlers/session.hpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace llarp
157157
// session_tag tag,
158158
HopID remote_pivot_txid,
159159
std::shared_ptr<path::Path> path,
160-
shared_kx_data kx_data,
160+
std::optional<shared_kx_data> kx_data,
161161
bool use_tun);
162162

163163
// lookup SNS address to return "{pubkey}.loki" hidden service or exit node operated on a remote client
@@ -173,10 +173,7 @@ namespace llarp
173173
// resolves any config mappings that parsed ONS addresses to their pubkey network address
174174
void resolve_ons_mappings();
175175

176-
bool initiate_remote_session(const NetworkAddress& remote, on_session_init_hook cb)
177-
{
178-
return _initiate_session(remote, std::move(cb));
179-
}
176+
void initiate_remote_session(const NetworkAddress& remote, on_session_init_hook cb);
180177

181178
void tick(std::chrono::milliseconds now) override;
182179

@@ -200,22 +197,23 @@ namespace llarp
200197

201198
void _update_and_publish_localcc();
202199

203-
bool _initiate_client_session(NetworkAddress remote, on_session_init_hook cb);
200+
void _initiate_client_session(NetworkAddress remote, on_session_init_hook cb);
204201

205-
bool _initiate_relay_session(NetworkAddress remote, on_session_init_hook cb);
202+
void _initiate_relay_session(NetworkAddress remote, on_session_init_hook cb);
206203

207-
bool _initiate_session(NetworkAddress remote, on_session_init_hook cb);
204+
void _make_client_session_path(intro_set intros, NetworkAddress remote, on_session_init_hook cb);
208205

209-
void _make_session_path(RemoteRC rc, NetworkAddress remote, on_session_init_hook cb);
206+
void _make_relay_session_path(RemoteRC rc, NetworkAddress remote, on_session_init_hook cb);
210207

211-
void _make_session_path(intro_set intros, NetworkAddress remote, on_session_init_hook cb);
212-
213-
void _make_session(
208+
void _make_client_session(
214209
intro_set remote_intros,
215210
NetworkAddress remote,
216211
ClientIntro remote_intro,
217212
std::shared_ptr<path::Path> path,
218213
on_session_init_hook cb);
214+
215+
void _make_relay_session(
216+
RemoteRC rc, NetworkAddress remote, std::shared_ptr<path::Path> path, on_session_init_hook cb);
219217
};
220218

221219
} // namespace handlers

0 commit comments

Comments
 (0)