-
Notifications
You must be signed in to change notification settings - Fork 3
/
plexus.cpp
212 lines (169 loc) · 7.24 KB
/
plexus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2022 Novemus Band. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
#include "plexus.h"
#include "features.h"
#include "utils.h"
#include <logger.h>
namespace plexus {
std::ostream& operator<<(std::ostream& stream, const reference& value)
{
if (stream.rdbuf())
return stream << value.endpoint << "/" << value.puzzle;
return stream;
}
std::ostream& operator<<(std::ostream& stream, const identity& value)
{
if (stream.rdbuf())
return stream << value.owner << "/" << value.pin;
return stream;
}
std::istream& operator>>(std::istream& in, reference& value)
{
std::string str;
in >> str;
std::smatch match;
if (std::regex_match(str, match, std::regex("^([^/]+)/([^/]+)$")))
{
value.endpoint = plexus::utils::parse_endpoint<boost::asio::ip::udp::endpoint>(match[1].str(), "");
value.puzzle = boost::lexical_cast<uint64_t>(match[2].str());
return in;
}
throw boost::bad_lexical_cast();
}
std::istream& operator>>(std::istream& in, identity& value)
{
std::string str;
in >> str;
std::smatch match;
if (std::regex_match(str, match, std::regex("^([^/]*)/([^/]*)$")))
{
value.owner = match[1].str();
value.pin = match[2].str();
return in;
}
throw boost::bad_lexical_cast();
}
void spawn_accept(boost::asio::io_service& io, const options& config, const identity& host, const identity& peer, const connector& connect, const fallback& notify) noexcept(true)
{
auto handler = [&io, config, connect, notify](boost::asio::yield_context yield, std::shared_ptr<plexus::pipe> pipe)
{
auto peer = pipe->peer();
auto host = pipe->host();
_inf_ << "accepting " << peer << " for " << host;
try
{
auto binder = plexus::create_stun_binder(io, config.stun, config.bind, config.hops);
auto hole = binder->punch_hole(yield);
if (hole.traits.mapping != network::traverse::independent)
throw plexus::bad_network();
reference faraway = pipe->pull_request(yield);
reference gateway = {hole.outer_endpoint, plexus::utils::random<uint64_t>()};
pipe->push_response(yield, gateway);
binder->await_peer(yield, faraway.endpoint, faraway.puzzle ^ gateway.puzzle);
connect(host, peer, hole.inner_endpoint, gateway, faraway);
}
catch (const std::exception& e)
{
_err_ << "accepting " << peer << " for " << host << " failed: " << e.what();
if (notify)
notify(host, peer, e.what());
}
};
config.mediator.index() == 0
? spawn_accept(io, context<emailer>(config.app, config.repo, std::get<emailer>(config.mediator)), host, peer, handler)
: spawn_accept(io, context<dhtnode>(config.app, config.repo, std::get<dhtnode>(config.mediator)), host, peer, handler);
}
void spawn_invite(boost::asio::io_service& io, const options& config, const identity& host, const identity& peer, const connector& connect, const fallback& notify) noexcept(true)
{
auto handler = [&io, config, connect, notify](boost::asio::yield_context yield, std::shared_ptr<plexus::pipe> pipe)
{
auto peer = pipe->peer();
auto host = pipe->host();
_inf_ << "inviting " << peer << " for " << host;
try
{
auto binder = plexus::create_stun_binder(io, config.stun, config.bind, config.hops);
auto hole = binder->punch_hole(yield);
if (hole.traits.mapping != network::traverse::independent)
throw plexus::bad_network();
plexus::reference gateway = { hole.outer_endpoint, plexus::utils::random<uint64_t>() };
pipe->push_request(yield, gateway);
plexus::reference faraway = pipe->pull_response(yield);
binder->reach_peer(yield, faraway.endpoint, faraway.puzzle ^ gateway.puzzle);
connect(host, peer, hole.inner_endpoint, gateway, faraway);
}
catch (const std::exception& e)
{
_err_ << "inviting " << peer << " for " << host << " failed: " << e.what();
if (notify)
notify(pipe->host(), pipe->peer(), e.what());
}
};
config.mediator.index() == 0
? spawn_invite(io, context<emailer>(config.app, config.repo, std::get<emailer>(config.mediator)), host, peer, handler)
: spawn_invite(io, context<dhtnode>(config.app, config.repo, std::get<dhtnode>(config.mediator)), host, peer, handler);
}
void spawn_accept(boost::asio::io_service& io, const options& config, const identity& host, const identity& peer, const collector& collect, const fallback& notify) noexcept(true)
{
spawn_accept(io, config, host, peer, [&io, collect, notify](const identity& host, const identity& peer, const udp::endpoint& bind, const reference& gateway, const reference& faraway)
{
_inf_ << "accepting from " << faraway.endpoint << " on " << bind;
auto socket = std::make_shared<tubus::socket>(io, faraway.puzzle ^ gateway.puzzle);
boost::system::error_code ec;
socket->open(bind, ec);
if (ec)
{
_err_ << "open socket " << bind << " failed: " << ec.message();
if (notify)
notify(host, peer, ec.message());
return;
}
socket->async_accept(faraway.endpoint, [socket, faraway, host, peer, collect, notify](const boost::system::error_code& ec)
{
if (ec)
{
_err_ << "accepting from " << faraway.endpoint << " failed: " << ec.message();
if (notify)
notify(host, peer, ec.message());
return;
}
collect(host, peer, std::move(*socket));
});
}, notify);
}
void spawn_invite(boost::asio::io_service& io, const options& config, const identity& host, const identity& peer, const collector& collect, const fallback& notify) noexcept(true)
{
spawn_invite(io, config, host, peer, [&io, collect, notify](const identity& host, const identity& peer, const udp::endpoint& bind, const reference& gateway, const reference& faraway)
{
_inf_ << "connecting to " << faraway.endpoint << " from " << bind;
auto socket = std::make_shared<tubus::socket>(io, faraway.puzzle ^ gateway.puzzle);
boost::system::error_code ec;
socket->open(bind, ec);
if (ec)
{
_err_ << "open socket " << bind << " failed: " << ec.message();
if (notify)
notify(host, peer, ec.message());
return;
}
socket->async_connect(faraway.endpoint, [socket, faraway, host, peer, collect, notify](const boost::system::error_code& ec)
{
if (ec)
{
_err_ << "connecting to " << faraway.endpoint << " failed: " << ec.message();
if (notify)
notify(host, peer, ec.message());
return;
}
collect(host, peer, std::move(*socket));
});
}, notify);
}
}