1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <map>
17 #include <string>
18 
19 #include <android-base/logging.h>
20 #include <gflags/gflags.h>
21 
22 #include "host/frontend/webrtc_operator/client_handler.h"
23 #include "host/frontend/webrtc_operator/device_handler.h"
24 #include "host/frontend/webrtc_operator/device_list_handler.h"
25 #include "host/libs/websocket/websocket_handler.h"
26 #include "host/libs/websocket/websocket_server.h"
27 
28 #include "host/libs/config/logging.h"
29 
30 DEFINE_int32(http_server_port, 8443, "The port for the http server.");
31 DEFINE_bool(use_secure_http, true, "Whether to use HTTPS or HTTP.");
32 DEFINE_string(assets_dir, "webrtc",
33               "Directory with location of webpage assets.");
34 DEFINE_string(certs_dir, "webrtc/certs",
35               "Directory to certificates. It must contain a server.crt file, a "
36               "server.key file and (optionally) a CA.crt file.");
37 DEFINE_string(stun_server, "stun.l.google.com:19302",
38               "host:port of STUN server to use for public address resolution");
39 
40 namespace {
41 
42 constexpr auto kRegisterDeviceUriPath = "/register_device";
43 constexpr auto kConnectClientUriPath = "/connect_client";
44 constexpr auto kListDevicesUriPath = "/devices";
45 const constexpr auto kInfraConfigPath = "/infra_config";
46 const constexpr auto kConnectPath = "/connect";
47 const constexpr auto kForwardPath = "/forward";
48 const constexpr auto kPollPath = "/poll_messages";
49 
50 }  // namespace
51 
main(int argc,char ** argv)52 int main(int argc, char** argv) {
53   cuttlefish::DefaultSubprocessLogging(argv);
54   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
55 
56   cuttlefish::DeviceRegistry device_registry;
57   cuttlefish::PollConnectionStore poll_store;
58   cuttlefish::ServerConfig server_config({FLAGS_stun_server});
59 
60   cuttlefish::WebSocketServer wss =
61       FLAGS_use_secure_http
62           ? cuttlefish::WebSocketServer("webrtc-operator", FLAGS_certs_dir,
63                                         FLAGS_assets_dir,
64                                         FLAGS_http_server_port)
65           : cuttlefish::WebSocketServer("webrtc-operator", FLAGS_assets_dir,
66                                         FLAGS_http_server_port);
67 
68   // Device list endpoint
69   wss.RegisterDynHandlerFactory(
70       kListDevicesUriPath, [&device_registry](struct lws* wsi) {
71         return std::unique_ptr<cuttlefish::DynHandler>(
72             new cuttlefish::DeviceListHandler(wsi, device_registry));
73       });
74 
75   // Websocket signaling endpoints
76   auto device_handler_factory_p =
77       std::unique_ptr<cuttlefish::WebSocketHandlerFactory>(
78           new cuttlefish::DeviceHandlerFactory(&device_registry,
79                                                server_config));
80   wss.RegisterHandlerFactory(kRegisterDeviceUriPath,
81                              std::move(device_handler_factory_p));
82   auto client_handler_factory_p =
83       std::unique_ptr<cuttlefish::WebSocketHandlerFactory>(
84           new cuttlefish::ClientWSHandlerFactory(&device_registry,
85                                                  server_config));
86   wss.RegisterHandlerFactory(kConnectClientUriPath,
87                              std::move(client_handler_factory_p));
88 
89   // Polling signaling endpoints
90   wss.RegisterDynHandlerFactory(
91       kInfraConfigPath, [&server_config](struct lws* wsi) {
92         return std::unique_ptr<cuttlefish::DynHandler>(
93             new cuttlefish::ConfigHandler(wsi, server_config));
94       });
95   wss.RegisterDynHandlerFactory(
96       kConnectPath, [&device_registry, &poll_store](struct lws* wsi) {
97         return std::unique_ptr<cuttlefish::DynHandler>(
98             new cuttlefish::ConnectHandler(wsi, &device_registry, &poll_store));
99       });
100   wss.RegisterDynHandlerFactory(
101       kForwardPath, [&poll_store](struct lws* wsi) {
102         return std::unique_ptr<cuttlefish::DynHandler>(
103             new cuttlefish::ForwardHandler(wsi, &poll_store));
104       });
105   wss.RegisterDynHandlerFactory(
106       kPollPath, [&poll_store](struct lws* wsi) {
107         return std::unique_ptr<cuttlefish::DynHandler>(
108             new cuttlefish::PollHandler(wsi, &poll_store));
109       });
110 
111   wss.Serve();
112   return 0;
113 }
114