Commit 07a44b74 authored by David Seaward's avatar David Seaward

Merge branch '7_prompt_for_address' into 'master'

Prompt for address instead of username.

See merge request !4
parents 794397ea b4b714d6
......@@ -11,17 +11,17 @@ import sys
import os
import requests
import gi
import re
from tempfile import mkstemp
from getpass import getpass
from json.decoder import JSONDecodeError
gi.require_version('NM', '1.0')
from gi.repository import NM, GLib
PROTO = 'https://'
HOST = 'librem.one'
PATH = '/api/v1/user/tunnel_account'
CERT_PATH = '/public/certificate.ovpn'
DOMAIN = 'librem.one'
QQN = 'US1'
......@@ -33,11 +33,20 @@ def nm_tunnel_setup():
4. Setup new VPN connection in your NetworkManager.
"""
# Get tunnel credentials
user = input('Enter your Librem One username: ')
address = input('Enter your Librem One address: ')
regex = r'^[A-Za-z][A-Za-z0-9]*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)+$'
if not re.match(regex, address):
print(address, 'is not a valid email address.')
sys.exit(1)
(user, host) = address.split('@')
passwd = getpass('Enter your password: ')
url = PROTO + HOST + PATH
r = requests.get(url, auth=(user, passwd))
url = PROTO + host + PATH
try:
r = requests.get(url, auth=(user, passwd))
except requests.exceptions.ConnectionError as e:
print(repr(e))
sys.exit(1)
if r.status_code == 200:
tunnel_user = r.json().get('tunnel_user')
tunnel_password = r.json().get('tunnel_password')
......@@ -54,13 +63,18 @@ def nm_tunnel_setup():
sys.exit(1)
# Download certificate
cert_url = PROTO + HOST + CERT_PATH
cert_url = PROTO + host + CERT_PATH
(fd, fname) = mkstemp(suffix='.ovpn')
with requests.get(cert_url, stream=True) as r:
with open(fd, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
try:
with requests.get(cert_url, stream=True) as r:
with open(fd, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
except requests.exceptions.ConnectionError as e:
print(repr(e))
os.remove(fname)
sys.exit(1)
# Configure NetworkManager
client = NM.Client.new(None)
......@@ -93,7 +107,7 @@ def nm_tunnel_setup():
# Set Connection Name
con_name = '{user}@{domain} {country_code}'.format(user=user,
domain=DOMAIN,
domain=host,
country_code=QQN)
new_con_settings = new_con.get_setting_connection()
new_con_settings.set_property(NM.SETTING_CONNECTION_ID, con_name)
......
[metadata]
name = ldh_client
version = 0.0.1
version = 0.0.2
description = Liberty CLI is a user-facing command-line client for interacting with a Liberty Deckplan Host (LDH).
license = AGPL-3.0-or-later
author = Purism SPC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment