OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
6 # | 5 |
7 # register_host.py registers new hosts in chromoting directory. It | 6 """Registers new hosts in chromoting directory. |
8 # asks for username/password and then writes these settings to config file. | 7 |
| 8 It asks for username/password and then writes these settings to config file. |
| 9 """ |
9 | 10 |
10 import getpass | 11 import getpass |
11 import os | 12 import os |
12 import urllib | 13 import urllib |
13 import urllib2 | 14 import urllib2 |
14 import random | 15 import random |
15 import socket | 16 import socket |
16 import sys | 17 import sys |
17 | 18 |
18 import gaia_auth | 19 import gaia_auth |
19 import keygen | 20 import keygen |
20 | 21 |
21 def random_uuid(): | 22 def random_uuid(): |
22 return ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x" % | 23 return ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x" % |
23 tuple(map(lambda x: random.randrange(0,65536), range(8)))) | 24 tuple(map(lambda x: random.randrange(0,65536), range(8)))) |
24 | 25 |
25 server = 'www.googleapis.com' | |
26 url = 'https://' + server + '/chromoting/v1/@me/hosts' | |
27 | 26 |
28 settings_filepath = os.path.join(os.path.expanduser('~'), | 27 def main(): |
29 '.ChromotingConfig.json') | 28 server = 'www.googleapis.com' |
| 29 url = 'https://' + server + '/chromoting/v1/@me/hosts' |
30 | 30 |
31 print "Email:", | 31 settings_filepath = os.path.join(os.path.expanduser('~'), |
32 email = raw_input() | 32 '.ChromotingConfig.json') |
33 password = getpass.getpass("Password: ") | |
34 | 33 |
35 chromoting_auth = gaia_auth.GaiaAuthenticator('chromoting') | 34 print "Email:", |
36 auth_token = chromoting_auth.authenticate(email, password) | 35 email = raw_input() |
| 36 password = getpass.getpass("Password: ") |
37 | 37 |
38 host_id = random_uuid() | 38 chromoting_auth = gaia_auth.GaiaAuthenticator('chromoting') |
39 print "HostId:", host_id | 39 auth_token = chromoting_auth.authenticate(email, password) |
40 host_name = socket.gethostname() | |
41 print "HostName:", host_name | |
42 | 40 |
43 print "Generating RSA key pair...", | 41 host_id = random_uuid() |
44 (private_key, public_key) = keygen.generateRSAKeyPair() | 42 print "HostId:", host_id |
45 print "Done" | 43 host_name = socket.gethostname() |
| 44 print "HostName:", host_name |
46 | 45 |
47 params = ('{"data":{' + \ | 46 print "Generating RSA key pair...", |
48 '"hostId": "%(hostId)s",' + \ | 47 (private_key, public_key) = keygen.generateRSAKeyPair() |
49 '"hostName": "%(hostName)s",' + \ | 48 print "Done" |
50 '"publicKey": "%(publicKey)s"}}') % \ | |
51 {'hostId': host_id, 'hostName': host_name, | |
52 'publicKey': public_key} | |
53 headers = {"Authorization": "GoogleLogin auth=" + auth_token, | |
54 "Content-Type": "application/json" } | |
55 request = urllib2.Request(url, params, headers) | |
56 | 49 |
57 opener = urllib2.OpenerDirector() | 50 params = ('{"data":{' + |
58 opener.add_handler(urllib2.HTTPDefaultErrorHandler()) | 51 '"hostId": "%(hostId)s",' + |
| 52 '"hostName": "%(hostName)s",' + |
| 53 '"publicKey": "%(publicKey)s"}}') % |
| 54 {'hostId': host_id, 'hostName': host_name, |
| 55 'publicKey': public_key} |
| 56 headers = {"Authorization": "GoogleLogin auth=" + auth_token, |
| 57 "Content-Type": "application/json" } |
| 58 request = urllib2.Request(url, params, headers) |
59 | 59 |
60 print | 60 opener = urllib2.OpenerDirector() |
61 print "Registering host with directory service..." | 61 opener.add_handler(urllib2.HTTPDefaultErrorHandler()) |
62 try: | |
63 res = urllib2.urlopen(request) | |
64 data = res.read() | |
65 except urllib2.HTTPError, err: | |
66 print >> sys.stderr, "Directory returned error:", err | |
67 print >> sys.stderr, err.fp.read() | |
68 sys.exit(1) | |
69 | 62 |
70 print "Done" | 63 print |
| 64 print "Registering host with directory service..." |
| 65 try: |
| 66 res = urllib2.urlopen(request) |
| 67 data = res.read() |
| 68 except urllib2.HTTPError, err: |
| 69 print >> sys.stderr, "Directory returned error:", err |
| 70 print >> sys.stderr, err.fp.read() |
| 71 return 1 |
71 | 72 |
72 # Get token that the host will use to athenticate in talk network. | 73 print "Done" |
73 authenticator = gaia_auth.GaiaAuthenticator('chromiumsync'); | |
74 auth_token = authenticator.authenticate(email, password) | |
75 | 74 |
76 # Write settings file. | 75 # Get token that the host will use to athenticate in talk network. |
77 os.umask(0066) # Set permission mask for created file. | 76 authenticator = gaia_auth.GaiaAuthenticator('chromiumsync'); |
78 settings_file = open(settings_filepath, 'w') | 77 auth_token = authenticator.authenticate(email, password) |
79 settings_file.write('{\n'); | |
80 settings_file.write(' "xmpp_login" : "' + email + '",\n') | |
81 settings_file.write(' "xmpp_auth_token" : "' + auth_token + '",\n') | |
82 settings_file.write(' "host_id" : "' + host_id + '",\n') | |
83 settings_file.write(' "host_name" : "' + host_name + '",\n') | |
84 settings_file.write(' "private_key" : "' + private_key + '",\n') | |
85 settings_file.write('}\n') | |
86 settings_file.close() | |
87 | 78 |
88 print 'Configuration saved in', settings_filepath | 79 # Write settings file. |
| 80 os.umask(0066) # Set permission mask for created file. |
| 81 settings_file = open(settings_filepath, 'w') |
| 82 settings_file.write('{\n'); |
| 83 settings_file.write(' "xmpp_login" : "' + email + '",\n') |
| 84 settings_file.write(' "xmpp_auth_token" : "' + auth_token + '",\n') |
| 85 settings_file.write(' "host_id" : "' + host_id + '",\n') |
| 86 settings_file.write(' "host_name" : "' + host_name + '",\n') |
| 87 settings_file.write(' "private_key" : "' + private_key + '",\n') |
| 88 settings_file.write('}\n') |
| 89 settings_file.close() |
| 90 |
| 91 print 'Configuration saved in', settings_filepath |
| 92 return 0 |
| 93 |
| 94 |
| 95 if __name__ == '__main__': |
| 96 sys.exit(main()) |
OLD | NEW |