summaryrefslogtreecommitdiffstats
path: root/remoting/host/simple_host_process.cc
blob: 7635e544439a9d8daa5315e56a0c0fdbdf7f71ef (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This is an application of a minimal host process in a Chromoting
// system. It serves the purpose of gluing different pieces together
// to make a functional host process for testing.
//
// It peforms the following functionality:
// 1. Connect to the GTalk network and register the machine as a host.
// 2. Accepts connection through libjingle.
// 3. Receive mouse / keyboard events through libjingle.
// 4. Sends screen capture through libjingle.

#include <iostream>
#include <string>

#include "build/build_config.h"

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "crypto/nss_util.h"
#include "net/base/network_change_notifier.h"
#include "net/socket/ssl_server_socket.h"
#include "remoting/base/constants.h"
#include "remoting/host/capturer_fake.h"
#include "remoting/host/chromoting_host.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/constants.h"
#include "remoting/host/desktop_environment.h"
#include "remoting/host/event_executor.h"
#include "remoting/host/heartbeat_sender.h"
#include "remoting/host/host_key_pair.h"
#include "remoting/host/host_secret.h"
#include "remoting/host/it2me_host_user_interface.h"
#include "remoting/host/json_host_config.h"
#include "remoting/host/log_to_server.h"
#include "remoting/host/network_settings.h"
#include "remoting/host/register_support_host_request.h"
#include "remoting/host/session_manager_factory.h"
#include "remoting/host/signaling_connector.h"
#include "remoting/jingle_glue/xmpp_signal_strategy.h"
#include "remoting/proto/video.pb.h"
#include "remoting/protocol/it2me_host_authenticator_factory.h"
#include "remoting/protocol/me2me_host_authenticator_factory.h"

#if defined(TOOLKIT_GTK)
#include "ui/gfx/gtk_util.h"
#elif defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#elif defined(OS_WIN)
// TODO(garykac) Make simple host into a proper GUI app on Windows so that we
// have an hModule for the dialog resource.
HMODULE g_hModule = NULL;
#endif

using remoting::protocol::CandidateSessionConfig;
using remoting::protocol::ChannelConfig;

namespace {

const FilePath::CharType kDefaultConfigPath[] =
    FILE_PATH_LITERAL(".ChromotingConfig.json");

const char kHomeDrive[] = "HOMEDRIVE";
const char kHomePath[] = "HOMEPATH";

const char kFakeSwitchName[] = "fake";
const char kIT2MeSwitchName[] = "it2me";
const char kConfigSwitchName[] = "config";
const char kVideoSwitchName[] = "video";
const char kDisableNatTraversalSwitchName[] = "disable-nat-traversal";
const char kMinPortSwitchName[] = "min-port";
const char kMaxPortSwitchName[] = "max-port";

const char kVideoSwitchValueVerbatim[] = "verbatim";
const char kVideoSwitchValueZip[] = "zip";
const char kVideoSwitchValueVp8[] = "vp8";

}  // namespace

namespace remoting {

class SimpleHost : public HeartbeatSender::Listener {
 public:
  SimpleHost()
      : message_loop_(MessageLoop::TYPE_UI),
        context_(message_loop_.message_loop_proxy()),
        fake_(false),
        is_it2me_(false),
        shutting_down_(false),
        exit_code_(kSuccessExitCode) {
    context_.Start();
    network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
  }

  // Overridden from HeartbeatSender::Listener
  virtual void OnUnknownHostIdError() OVERRIDE {
    LOG(ERROR) << "Host ID not found.";
    Shutdown(kInvalidHostIdExitCode);
  }

  int Run() {
    FilePath config_path = GetConfigPath();
    JsonHostConfig config(config_path);
    if (!config.Read()) {
      LOG(ERROR) << "Failed to read configuration file "
                 << config_path.value();
      return 1;
    }

    if (!config.GetString(kHostIdConfigPath, &host_id_)) {
      LOG(ERROR) << "host_id is not defined in the config.";
      return 1;
    }

    if (!key_pair_.Load(config)) {
      return 1;
    }

    std::string host_secret_hash_string;
    if (!config.GetString(kHostSecretHashConfigPath,
                          &host_secret_hash_string)) {
      host_secret_hash_string = "plain:";
    }

    if (!host_secret_hash_.Parse(host_secret_hash_string)) {
      LOG(ERROR) << "Invalid host_secret_hash.";
      return false;
    }

    // Use an XMPP connection to the Talk network for session signalling.
    if (!config.GetString(kXmppLoginConfigPath, &xmpp_login_) ||
        !config.GetString(kXmppAuthTokenConfigPath, &xmpp_auth_token_)) {
      LOG(ERROR) << "XMPP credentials are not defined in the config.";
      return 1;
    }
    if (!config.GetString(kXmppAuthServiceConfigPath, &xmpp_auth_service_)) {
      // For the simple host, we assume we always use the ClientLogin token for
      // chromiumsync because we do not have an HTTP stack with which we can
      // easily request an OAuth2 access token even if we had a RefreshToken for
      // the account.
      xmpp_auth_service_ = kChromotingTokenDefaultServiceName;
    }

    context_.network_message_loop()->PostTask(FROM_HERE, base::Bind(
        &SimpleHost::StartHost, base::Unretained(this)));

    message_loop_.MessageLoop::Run();

    return exit_code_;
  }

  void set_config_path(const FilePath& config_path) {
    config_path_ = config_path;
  }
  void set_fake(bool fake) { fake_ = fake; }
  void set_is_it2me(bool is_it2me) { is_it2me_ = is_it2me; }
  void set_protocol_config(CandidateSessionConfig* protocol_config) {
    protocol_config_.reset(protocol_config);
  }

  NetworkSettings* network_settings() { return &network_settings_; }

 private:
  static void SetIT2MeAccessCode(scoped_refptr<ChromotingHost> host,
                                 HostKeyPair* key_pair,
                                 bool successful,
                                 const std::string& support_id,
                                 const base::TimeDelta& lifetime) {
    if (successful) {
      std::string host_secret = GenerateSupportHostSecret();
      std::string access_code = support_id + host_secret;
      std::cout << "Support id: " << access_code << std::endl;

      scoped_ptr<protocol::AuthenticatorFactory> factory(
          new protocol::It2MeHostAuthenticatorFactory(
              key_pair->GenerateCertificate(), *key_pair->private_key(),
              access_code));
      host->SetAuthenticatorFactory(factory.Pass());
    } else {
      LOG(ERROR) << "If you haven't done so recently, try running"
                 << " remoting/tools/register_host.py.";
    }
  }

  FilePath GetConfigPath() {
    if (!config_path_.empty())
      return config_path_;

    scoped_ptr<base::Environment> env(base::Environment::Create());

#if defined(OS_WIN)
    std::string home_drive;
    env->GetVar(kHomeDrive, &home_drive);
    std::string home_path;
    env->GetVar(kHomePath, &home_path);
    return FilePath(UTF8ToWide(home_drive))
        .Append(UTF8ToWide(home_path))
        .Append(kDefaultConfigPath);
#else
    std::string home_path;
    env->GetVar(base::env_vars::kHome, &home_path);
    return FilePath(home_path).Append(kDefaultConfigPath);
#endif
  }

  void StartHost() {
    signal_strategy_.reset(
        new XmppSignalStrategy(context_.jingle_thread(), xmpp_login_,
                               xmpp_auth_token_, xmpp_auth_service_));
    signaling_connector_.reset(new SignalingConnector(
        signal_strategy_.get(),
        base::Bind(&SimpleHost::OnAuthFailed, base::Unretained(this))));

    if (fake_) {
      scoped_ptr<Capturer> capturer(new CapturerFake());
      scoped_ptr<EventExecutor> event_executor = EventExecutor::Create(
          context_.desktop_message_loop()->message_loop_proxy(),
          context_.ui_message_loop(), capturer.get());
      desktop_environment_ = DesktopEnvironment::CreateFake(
          &context_, capturer.Pass(), event_executor.Pass());
    } else {
      desktop_environment_ = DesktopEnvironment::Create(&context_);
    }

    host_ = new ChromotingHost(
        &context_, signal_strategy_.get(), desktop_environment_.get(),
        CreateHostSessionManager(network_settings_,
                                 context_.url_request_context_getter()));

    ServerLogEntry::Mode mode =
        is_it2me_ ? ServerLogEntry::IT2ME : ServerLogEntry::ME2ME;
    log_to_server_.reset(new LogToServer(host_, mode, signal_strategy_.get()));

    if (is_it2me_) {
      it2me_host_user_interface_.reset(new It2MeHostUserInterface(&context_));
      it2me_host_user_interface_->Start(
          host_,
          base::Bind(&ChromotingHost::Shutdown, host_, base::Closure()));
    }

    if (protocol_config_.get()) {
      host_->set_protocol_config(protocol_config_.release());
    }

    if (is_it2me_) {
      register_request_.reset(new RegisterSupportHostRequest(
          signal_strategy_.get(), &key_pair_,
          base::Bind(&SimpleHost::SetIT2MeAccessCode, host_, &key_pair_)));
    } else {
      heartbeat_sender_.reset(new HeartbeatSender(
          this, host_id_, signal_strategy_.get(), &key_pair_));
    }

    host_->Start();

    // Create a Me2Me authenticator factory.
    if (!is_it2me_) {
      scoped_ptr<protocol::AuthenticatorFactory> factory(
          new protocol::Me2MeHostAuthenticatorFactory(
              key_pair_.GenerateCertificate(), *key_pair_.private_key(),
              host_secret_hash_));
      host_->SetAuthenticatorFactory(factory.Pass());
    }
  }

  void OnAuthFailed() {
    Shutdown(kInvalidOauthCredentialsExitCode);
  }

  void Shutdown(int exit_code) {
    DCHECK(context_.network_message_loop()->BelongsToCurrentThread());

    if (shutting_down_)
      return;

    shutting_down_ = true;
    exit_code_ = exit_code;
    host_->Shutdown(base::Bind(
        &SimpleHost::OnShutdownFinished, base::Unretained(this)));
  }

  void OnShutdownFinished() {
    DCHECK(context_.network_message_loop()->BelongsToCurrentThread());

    // Destroy networking objects while we are on the network thread.
    host_ = NULL;
    log_to_server_.reset();
    heartbeat_sender_.reset();
    signaling_connector_.reset();
    signal_strategy_.reset();

    message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
  }

  MessageLoop message_loop_;
  ChromotingHostContext context_;
  scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;

  FilePath config_path_;
  bool fake_;
  bool is_it2me_;
  NetworkSettings network_settings_;
  scoped_ptr<CandidateSessionConfig> protocol_config_;

  std::string host_id_;
  HostKeyPair key_pair_;
  protocol::SharedSecretHash host_secret_hash_;
  std::string xmpp_login_;
  std::string xmpp_auth_token_;
  std::string xmpp_auth_service_;

  scoped_ptr<XmppSignalStrategy> signal_strategy_;
  scoped_ptr<SignalingConnector> signaling_connector_;
  scoped_ptr<DesktopEnvironment> desktop_environment_;
  scoped_ptr<LogToServer> log_to_server_;
  scoped_ptr<It2MeHostUserInterface> it2me_host_user_interface_;
  scoped_ptr<RegisterSupportHostRequest> register_request_;
  scoped_ptr<HeartbeatSender> heartbeat_sender_;

  scoped_refptr<ChromotingHost> host_;

  bool shutting_down_;
  int exit_code_;
};

} // namespace remoting

int main(int argc, char** argv) {
#if defined(OS_MACOSX)
  // Needed so we don't leak objects when threads are created.
  base::mac::ScopedNSAutoreleasePool pool;
#endif

  CommandLine::Init(argc, argv);
  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();

  base::AtExitManager exit_manager;
  crypto::EnsureNSPRInit();

#if defined(TOOLKIT_GTK)
  gfx::GtkInitFromCommandLine(*cmd_line);
#endif  // TOOLKIT_GTK

  // Enable support for SSL server sockets, which must be done while still
  // single-threaded.
  net::EnableSSLServerSockets();

  remoting::SimpleHost simple_host;

  if (cmd_line->HasSwitch(kConfigSwitchName)) {
    simple_host.set_config_path(
        cmd_line->GetSwitchValuePath(kConfigSwitchName));
  }
  simple_host.set_fake(cmd_line->HasSwitch(kFakeSwitchName));
  simple_host.set_is_it2me(cmd_line->HasSwitch(kIT2MeSwitchName));

  if (cmd_line->HasSwitch(kVideoSwitchName)) {
    std::string video_codec = cmd_line->GetSwitchValueASCII(kVideoSwitchName);
    scoped_ptr<CandidateSessionConfig> config(
        CandidateSessionConfig::CreateDefault());
    config->mutable_video_configs()->clear();

    ChannelConfig::Codec codec;
    if (video_codec == kVideoSwitchValueVerbatim) {
      codec = ChannelConfig::CODEC_VERBATIM;
    } else if (video_codec == kVideoSwitchValueZip) {
      codec = ChannelConfig::CODEC_ZIP;
    } else if (video_codec == kVideoSwitchValueVp8) {
      codec = ChannelConfig::CODEC_VP8;
    } else {
      LOG(ERROR) << "Unknown video codec: " << video_codec;
      return 1;
    }
    config->mutable_video_configs()->push_back(ChannelConfig(
        ChannelConfig::TRANSPORT_STREAM,
        remoting::protocol::kDefaultStreamVersion, codec));
    simple_host.set_protocol_config(config.release());
  }

  simple_host.network_settings()->nat_traversal_mode =
      cmd_line->HasSwitch(kDisableNatTraversalSwitchName) ?
      remoting::NetworkSettings::NAT_TRAVERSAL_DISABLED :
      remoting::NetworkSettings::NAT_TRAVERSAL_ENABLED;

  if (cmd_line->HasSwitch(kMinPortSwitchName)) {
    std::string min_port_str =
        cmd_line->GetSwitchValueASCII(kMinPortSwitchName);
    int min_port = 0;
    if (!base::StringToInt(min_port_str, &min_port) ||
        min_port < 0 || min_port > 65535) {
      LOG(ERROR) << "Invalid min-port value: " << min_port
                 << ". Expected integer in range [0, 65535].";
      return 1;
    }
    simple_host.network_settings()->min_port = min_port;
  }

  if (cmd_line->HasSwitch(kMaxPortSwitchName)) {
    std::string max_port_str =
        cmd_line->GetSwitchValueASCII(kMaxPortSwitchName);
    int max_port = 0;
    if (!base::StringToInt(max_port_str, &max_port) ||
        max_port < 0 || max_port > 65535) {
      LOG(ERROR) << "Invalid max-port value: " << max_port
                 << ". Expected integer in range [0, 65535].";
      return 1;
    }
    simple_host.network_settings()->max_port = max_port;
  }

  return simple_host.Run();
}