Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: remoting/host/setup/start_host.cc

Issue 2049173003: Updating remoting_start_host to use remoting_core.dll (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing CR feedback Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/predefines_win.gni ('k') | remoting/host/setup/start_host_entry_point.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A simple command-line app that registers and starts a host.
6
7 #include <stddef.h>
8 #include <stdio.h>
9
10 #include "base/at_exit.h"
11 #include "base/command_line.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/threading/thread.h"
15 #include "build/build_config.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_request_context_getter.h"
18 #include "remoting/base/logging.h"
19 #include "remoting/base/url_request_context_getter.h"
20 #include "remoting/host/service_urls.h"
21 #include "remoting/host/setup/host_starter.h"
22 #include "remoting/host/setup/oauth_helper.h"
23 #include "remoting/host/setup/pin_validator.h"
24
25 #if defined(OS_POSIX)
26 #include <termios.h>
27 #include <unistd.h>
28 #endif // defined(OS_POSIX)
29
30 #if defined(OS_WIN)
31 #include "remoting/host/win/elevation_helpers.h"
32 #endif // defined(OS_WIN)
33
34 using remoting::HostStarter;
35
36 // True if the host was started successfully.
37 bool g_started = false;
38
39 // The main message loop.
40 base::MessageLoop* g_message_loop = nullptr;
41
42 // Lets us hide the PIN that a user types.
43 void SetEcho(bool echo) {
44 #if defined(OS_WIN)
45 DWORD mode;
46 HANDLE console_handle = GetStdHandle(STD_INPUT_HANDLE);
47 if (!GetConsoleMode(console_handle, &mode)) {
48 LOG(ERROR) << "GetConsoleMode failed";
49 return;
50 }
51 SetConsoleMode(console_handle,
52 (mode & ~ENABLE_ECHO_INPUT) | (echo ? ENABLE_ECHO_INPUT : 0));
53 #else
54 termios term;
55 tcgetattr(STDIN_FILENO, &term);
56 if (echo) {
57 term.c_lflag |= ECHO;
58 } else {
59 term.c_lflag &= ~ECHO;
60 }
61 tcsetattr(STDIN_FILENO, TCSANOW, &term);
62 #endif // !defined(OS_WIN)
63 }
64
65 // Reads a newline-terminated string from stdin.
66 std::string ReadString(bool no_echo) {
67 if (no_echo)
68 SetEcho(false);
69 const int kMaxLen = 1024;
70 std::string str(kMaxLen, 0);
71 char* result = fgets(&str[0], kMaxLen, stdin);
72 if (no_echo) {
73 printf("\n");
74 SetEcho(true);
75 }
76 if (!result)
77 return std::string();
78 size_t newline_index = str.find('\n');
79 if (newline_index != std::string::npos)
80 str[newline_index] = '\0';
81 str.resize(strlen(&str[0]));
82 return str;
83 }
84
85 // Called when the HostStarter has finished.
86 void OnDone(HostStarter::Result result) {
87 if (base::MessageLoop::current() != g_message_loop) {
88 g_message_loop->PostTask(FROM_HERE, base::Bind(&OnDone, result));
89 return;
90 }
91 switch (result) {
92 case HostStarter::START_COMPLETE:
93 g_started = true;
94 break;
95 case HostStarter::NETWORK_ERROR:
96 fprintf(stderr, "Couldn't start host: network error.\n");
97 break;
98 case HostStarter::OAUTH_ERROR:
99 fprintf(stderr, "Couldn't start host: OAuth error.\n");
100 break;
101 case HostStarter::START_ERROR:
102 fprintf(stderr, "Couldn't start host.\n");
103 break;
104 }
105
106 g_message_loop->QuitNow();
107 }
108
109 std::string GetAuthorizationCodeUri() {
110 return remoting::GetOauthStartUrl(remoting::GetDefaultOauthRedirectUrl());
111 }
112
113 int main(int argc, char** argv) {
114 // google_apis::GetOAuth2ClientID/Secret need a static CommandLine.
115 base::CommandLine::Init(argc, argv);
116 const base::CommandLine* command_line =
117 base::CommandLine::ForCurrentProcess();
118
119 // This object instance is required by Chrome code (for example,
120 // FilePath, LazyInstance, MessageLoop).
121 base::AtExitManager exit_manager;
122
123 logging::LoggingSettings settings;
124 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
125 logging::InitLogging(settings);
126
127 std::string host_name = command_line->GetSwitchValueASCII("name");
128 std::string host_pin = command_line->GetSwitchValueASCII("pin");
129 std::string auth_code = command_line->GetSwitchValueASCII("code");
130 std::string redirect_url = command_line->GetSwitchValueASCII("redirect-url");
131
132 #if defined(OS_POSIX)
133 // Check if current user is root. If it is root, then throw an error message.
134 // This is because start_host should be run in user mode.
135 if (geteuid() == 0) {
136 fprintf(stderr, "Refusing to run %s as root.", argv[0]);
137 return 1;
138 }
139 #endif // defined(OS_POSIX)
140
141 #if defined(OS_WIN)
142 // The tool must be run elevated on Windows so the host has access to the
143 // directories used to store the configuration JSON files.
144 if (!remoting::IsProcessElevated()) {
145 fprintf(stderr, "Error: %s must be run as an elevated process.", argv[0]);
146 return 1;
147 }
148 #endif // defined(OS_WIN)
149
150 if (host_name.empty()) {
151 fprintf(stderr,
152 "Usage: %s --name=<hostname> [--code=<auth-code>] [--pin=<PIN>] "
153 "[--redirect-url=<redirectURL>]\n",
154 argv[0]);
155 fprintf(stderr, "\nAuthorization URL for Production services:\n");
156 fprintf(stderr, "%s\n", GetAuthorizationCodeUri().c_str());
157 return 1;
158 }
159
160 if (host_pin.empty()) {
161 while (true) {
162 fprintf(stdout, "Enter a PIN of at least six digits: ");
163 fflush(stdout);
164 host_pin = ReadString(true);
165 if (!remoting::IsPinValid(host_pin)) {
166 fprintf(stdout,
167 "Please use a PIN consisting of at least six digits.\n");
168 fflush(stdout);
169 continue;
170 }
171 std::string host_pin_confirm;
172 fprintf(stdout, "Enter the same PIN again: ");
173 fflush(stdout);
174 host_pin_confirm = ReadString(true);
175 if (host_pin != host_pin_confirm) {
176 fprintf(stdout, "You entered different PINs.\n");
177 fflush(stdout);
178 continue;
179 }
180 break;
181 }
182 } else {
183 if (!remoting::IsPinValid(host_pin)) {
184 fprintf(stderr, "Please use a PIN consisting of at least six digits.\n");
185 return 1;
186 }
187 }
188
189 if (auth_code.empty()) {
190 fprintf(stdout, "Enter an authorization code: ");
191 fflush(stdout);
192 auth_code = ReadString(true);
193 }
194
195 // Provide message loops and threads for the URLRequestContextGetter.
196 base::MessageLoop message_loop;
197 g_message_loop = &message_loop;
198 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0);
199 base::Thread io_thread("IO thread");
200 io_thread.StartWithOptions(io_thread_options);
201 base::Thread file_thread("file thread");
202 file_thread.StartWithOptions(io_thread_options);
203
204 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter(
205 new remoting::URLRequestContextGetter(io_thread.task_runner(),
206 file_thread.task_runner()));
207
208 net::URLFetcher::SetIgnoreCertificateRequests(true);
209
210 // Start the host.
211 std::unique_ptr<HostStarter> host_starter(HostStarter::Create(
212 remoting::ServiceUrls::GetInstance()->directory_hosts_url(),
213 url_request_context_getter.get()));
214 if (redirect_url.empty()) {
215 redirect_url = remoting::GetDefaultOauthRedirectUrl();
216 }
217 host_starter->StartHost(host_name, host_pin,
218 /*consent_to_data_collection=*/true, auth_code,
219 redirect_url, base::Bind(&OnDone));
220
221 // Run the message loop until the StartHost completion callback.
222 base::RunLoop run_loop;
223 run_loop.Run();
224
225 g_message_loop = nullptr;
226
227 // Destroy the HostStarter and URLRequestContextGetter before stopping the
228 // IO thread.
229 host_starter.reset();
230 url_request_context_getter = nullptr;
231
232 io_thread.Stop();
233
234 return g_started ? 0 : 1;
235 }
OLDNEW
« no previous file with comments | « remoting/host/predefines_win.gni ('k') | remoting/host/setup/start_host_entry_point.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698