Skip to content

Commit

Permalink
gpxd: remove secret (commaai#135)
Browse files Browse the repository at this point in the history
* gpxd: remove secret

* make it stick

* enforce dongle ID check

* redirect

* static

* small cleanup
  • Loading branch information
sunnyhaibin authored Mar 26, 2023
1 parent 5ae00ea commit 03f8b45
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions selfdrive/gpxd/gpx_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import time
from common.params import Params
from common.realtime import Ratekeeper
from selfdrive.athena.registration import register
from system.version import get_version

# for uploader
Expand All @@ -45,9 +46,8 @@
LOG_PATH = '/data/media/0/gpx_logs/'

# osm api
API_HEADER = {'Authorization': 'Bearer DjlXVi1kL9bQ4GBj_3ixiKufVXzsHhpYCTi3ayNYw7k'}
VERSION_URL = 'https://api.openstreetmap.org/api/versions'
UPLOAD_URL = 'https://api.openstreetmap.org/api/0.6/gpx/create'
UPLOAD_URL = 'https://sunnypilot.com/osm/gpx_uploader.php'

_DEBUG = False

Expand All @@ -58,6 +58,25 @@ def _debug(msg):
print(msg, flush=True)


def _get_is_uploaded(filename):
_debug("%s is uploaded: %s" % (filename, getxattr(filename, UPLOAD_ATTR_NAME) is not None))
return getxattr(filename, UPLOAD_ATTR_NAME) is not None


def _set_is_uploaded(filename):
_debug("%s set to uploaded" % filename)
setxattr(filename, UPLOAD_ATTR_NAME, UPLOAD_ATTR_VALUE)


def _get_files_to_be_uploaded():
files = sorted(filter(os.path.isfile, glob.glob(LOG_PATH + '*')))
files_to_be_uploaded = []
for file in files:
if not _get_is_uploaded(file):
files_to_be_uploaded.append(file)
return files_to_be_uploaded


class GpxUploader():
def __init__(self):
self.param_s = Params()
Expand All @@ -73,10 +92,11 @@ def __init__(self):
self._sp_version = get_version()
_debug("GpxUploader init - _delete_after_upload = %s" % self._delete_after_upload)
_debug("GpxUploader init - _car_model = %s" % self._car_model)
self.api_headers = {"User-Agent": f"sunnypilot-{self._sp_version}-{register()}"}

def update(self):
while True:
files = self._get_files_to_be_uploaded()
files = _get_files_to_be_uploaded()
if len(files) == 0 or not self._is_online():
_debug("run - not online or no files")
elif not self.param_s.get_bool("DisableOnroadUploads") or self.param_s.get_bool("IsOffroad"):
Expand All @@ -87,39 +107,21 @@ def update(self):
os.remove(file)
else:
_debug("run - set_is_uploaded")
self._set_is_uploaded(file)
_set_is_uploaded(file)
# sleep for 300 secs if offroad
# otherwise sleep 60 secs
time.sleep(300 if self.param_s.get_bool("IsOffroad") else 60)
self.rk.keep_time()

def _is_online(self):
try:
r = requests.get(VERSION_URL, headers=API_HEADER)
r = requests.get(VERSION_URL, headers=self.api_headers)
_debug("is_online? status_code = %s" % r.status_code)
return r.status_code >= 200
except:
except Exception as e:
print(f'Online check error: {e}')
return False

def _get_is_uploaded(self, filename):
_debug("%s is uploaded: %s" % (filename, getxattr(filename, UPLOAD_ATTR_NAME) is not None))
return getxattr(filename, UPLOAD_ATTR_NAME) is not None

def _set_is_uploaded(self, filename):
_debug("%s set to uploaded" % filename)
setxattr(filename, UPLOAD_ATTR_NAME, UPLOAD_ATTR_VALUE)

def _get_files(self):
return sorted( filter( os.path.isfile, glob.glob(LOG_PATH + '*') ) )

def _get_files_to_be_uploaded(self):
files = self._get_files()
files_to_be_uploaded = []
for file in files:
if not self._get_is_uploaded(file):
files_to_be_uploaded.append(file)
return files_to_be_uploaded

def _do_upload(self, filename):
fn = os.path.basename(filename)
data = {
Expand All @@ -130,10 +132,11 @@ def _do_upload(self, filename):
"file": (fn, open(filename, 'rb'))
}
try:
r = requests.post(UPLOAD_URL, files=files, data=data, headers=API_HEADER)
r = requests.post(UPLOAD_URL, files=files, data=data, headers=self.api_headers)
_debug("do_upload - %s - %s" % (filename, r.status_code))
return r.status_code == 200
except:
except Exception as e:
print(f'Upload error: {e}')
return False


Expand Down

0 comments on commit 03f8b45

Please sign in to comment.