int main(int argc, char** argv) { setup_crash_handlers(); po::options_description desc ("Start Multilink server child"); desc.add_options() ("help", "produce help message") ("sock-fd", po::value<int>(), "FD of socket to start RPC server on") ("target-port", po::value<int>(), "target port (if using connect target)") ("target-host", po::value<string>()->default_value("127.0.0.1"), "target host") ("connect-bind", po::value<string>()->default_value("0.0.0.0"), "bind to this host when creating outbound TCP connections") ("tun-prefix", po::value<string>()->default_value("mlc"), "TUN device name prefix (if using terminate target)") ("dry-run", "only verify that options make sense"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << "\n"; return 0; } if (vm.count("target-port") && vm.count("tun-prefix")) { std::cout << desc << "\n"; return 1; } if (vm.count("dry-run")) return 0; Process::init(); Reactor reactor; Server server {reactor}; auto callback = [&](std::shared_ptr<RPCStream> stream, Json message) { server.callback(stream, message); }; if (vm.count("target-port")) { server.setup_connect_target(vm["target-host"].as<string>(), vm["target-port"].as<int>()); } else { server.setup_terminate_target(vm["connect-bind"].as<string>()); } if (vm.count("tun-prefix")) { string prefix = vm["tun-prefix"].as<string>(); string name = prefix + random_hex_string(15 - prefix.size()); server.setup_tun(name); } auto rpcserver = RPCServer::create(reactor, vm["sock-fd"].as<int>(), callback); reactor.run(); }
int main(int argc, char** argv) { Reactor *reactor = Reactor::getInstance(); reactor->callLater(30, &print_hello_world); reactor->run(); return 0; }
void run(string listen_host, int listen_port, std::vector<string> child_options, TlsStream::ServerPskFunc identity_callback) { this->child_options = child_options; TCP::listen(reactor, listen_host, listen_port, [&](FDPtr fd) { LOG("incoming connection"); auto stream = TlsStream::create(reactor, fd); stream->set_cipher_list("PSK-AES256-CBC-SHA"); stream->set_psk_server_callback(identity_callback); stream->handshake_as_server(); ioutil::read(stream, 128).then([this, stream](ByteString welcome) -> unit { // TODO: check identity string multilink_id = welcome.as_buffer().slice(0, 16); string link_name = welcome.as_buffer().slice(16); while (link_name.back() == 0) link_name.pop_back(); get_or_create(multilink_id).add_link(stream, link_name); return {}; }).ignore(); }).ignore(); reactor.run(); }