You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					93 lines
				
				3.1 KiB
			
		
		
			
		
	
	
					93 lines
				
				3.1 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								import os
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								import time
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								import json
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								import jwt
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								from datetime import datetime, timedelta
							 | 
						||
| 
								 | 
							
								from common.api import api_get
							 | 
						||
| 
								 | 
							
								from common.params import Params
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								from common.spinner import Spinner
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								from common.file_helpers import mkdirs_exists_ok
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								from common.basedir import PERSIST
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								from selfdrive.hardware import HARDWARE, PC
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								from selfdrive.swaglog import cloudlog
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								def register(show_spinner=False) -> str:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  params = Params()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  params.put("SubscriberInfo", HARDWARE.get_subscriber_info())
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  IMEI = params.get("IMEI", encoding='utf8')
							 | 
						||
| 
								 | 
							
								  HardwareSerial = params.get("HardwareSerial", encoding='utf8')
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  dongle_id = params.get("DongleId", encoding='utf8')
							 | 
						||
| 
								 | 
							
								  needs_registration = None in (IMEI, HardwareSerial, dongle_id)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  # create a key for auth
							 | 
						||
| 
								 | 
							
								  # your private key is kept on your device persist partition and never sent to our servers
							 | 
						||
| 
								 | 
							
								  # do not erase your persist partition
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  if not os.path.isfile(PERSIST+"/comma/id_rsa.pub"):
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    needs_registration = True
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    cloudlog.warning("generating your personal RSA key")
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    mkdirs_exists_ok(PERSIST+"/comma")
							 | 
						||
| 
								 | 
							
								    assert os.system("openssl genrsa -out "+PERSIST+"/comma/id_rsa.tmp 2048") == 0
							 | 
						||
| 
								 | 
							
								    assert os.system("openssl rsa -in "+PERSIST+"/comma/id_rsa.tmp -pubout -out "+PERSIST+"/comma/id_rsa.tmp.pub") == 0
							 | 
						||
| 
								 | 
							
								    os.rename(PERSIST+"/comma/id_rsa.tmp", PERSIST+"/comma/id_rsa")
							 | 
						||
| 
								 | 
							
								    os.rename(PERSIST+"/comma/id_rsa.tmp.pub", PERSIST+"/comma/id_rsa.pub")
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  if needs_registration:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if show_spinner:
							 | 
						||
| 
								 | 
							
								      spinner = Spinner()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      spinner.update("registering device")
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    # Create registration token, in the future, this key will make JWTs directly
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    with open(PERSIST+"/comma/id_rsa.pub") as f1, open(PERSIST+"/comma/id_rsa") as f2:
							 | 
						||
| 
								 | 
							
								      public_key = f1.read()
							 | 
						||
| 
								 | 
							
								      private_key = f2.read()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    # Block until we get the imei
							 | 
						||
| 
								 | 
							
								    imei1, imei2 = None, None
							 | 
						||
| 
								 | 
							
								    while imei1 is None and imei2 is None:
							 | 
						||
| 
								 | 
							
								      try:
							 | 
						||
| 
								 | 
							
								        imei1, imei2 = HARDWARE.get_imei(0), HARDWARE.get_imei(1)
							 | 
						||
| 
								 | 
							
								      except Exception:
							 | 
						||
| 
								 | 
							
								        cloudlog.exception("Error getting imei, trying again...")
							 | 
						||
| 
								 | 
							
								        time.sleep(1)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    serial = HARDWARE.get_serial()
							 | 
						||
| 
								 | 
							
								    params.put("IMEI", imei1)
							 | 
						||
| 
								 | 
							
								    params.put("HardwareSerial", serial)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    backoff = 0
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    while True:
							 | 
						||
| 
								 | 
							
								      try:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        register_token = jwt.encode({'register': True, 'exp': datetime.utcnow() + timedelta(hours=1)}, private_key, algorithm='RS256')
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        cloudlog.info("getting pilotauth")
							 | 
						||
| 
								 | 
							
								        resp = api_get("v2/pilotauth/", method='POST', timeout=15,
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								                       imei=imei1, imei2=imei2, serial=serial, public_key=public_key, register_token=register_token)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        if resp.status_code in (402, 403):
							 | 
						||
| 
								 | 
							
								          cloudlog.info(f"Unable to register device, got {resp.status_code}")
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								          dongle_id = None
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								          if PC:
							 | 
						||
| 
								 | 
							
								            dongle_id = "UnofficialDevice"
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        else:
							 | 
						||
| 
								 | 
							
								          dongleauth = json.loads(resp.text)
							 | 
						||
| 
								 | 
							
								          dongle_id = dongleauth["dongle_id"]
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        break
							 | 
						||
| 
								 | 
							
								      except Exception:
							 | 
						||
| 
								 | 
							
								        cloudlog.exception("failed to authenticate")
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        backoff = min(backoff + 1, 15)
							 | 
						||
| 
								 | 
							
								        time.sleep(backoff)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if show_spinner:
							 | 
						||
| 
								 | 
							
								      spinner.close()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  if dongle_id:
							 | 
						||
| 
								 | 
							
								    params.put("DongleId", dongle_id)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  return dongle_id
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								if __name__ == "__main__":
							 | 
						||
| 
								 | 
							
								  print(register())
							 |