Browse Source

Add connection-details script

connection-details
Luke Childs 5 years ago
parent
commit
b4f26dbe92
  1. 7
      stage2/06-connection-details/00-run.sh
  2. 102
      stage2/06-connection-details/files/connection-details

7
stage2/06-connection-details/00-run.sh

@ -0,0 +1,7 @@
#!/bin/bash -e
echo "Installing connection-details and dependencies"
on_chroot pip3 install qrcode
chmod +x files/connection-details
cp files/connection-details ${ROOTFS_DIR}/usr/local/bin/connection-details

102
stage2/06-connection-details/files/connection-details

@ -0,0 +1,102 @@
#!/usr/bin/env python3
import sys
import os
import time
from io import TextIOWrapper, BytesIO
import qrcode
umbrel_ascii = '''
,;###GGGGGGGGGGl#Sp
,##GGGlW""^' '`""%GGGG#S,
,#GGG" "lGG#o
#GGl^ '$GG#
,#GGb \GGG,
lGG" "GGG
#GGGlGGGl##p,,p##lGGl##p,,p###ll##GGGG
!GGGlW"""*GGGGGGG#""""WlGGGGG#W""*WGGGGS
"" "^ '" ""
@GGS lG#
!GGG !GGG
!GGG !GGG
!GGG !GGG
!GGG !GGG
!GGG !GGG
'GGG $GGl
"GGG#psqp##GG#
"%GGGGGG#"
'''.strip('\n')
def create_qr(data):
output_buffer = TextIOWrapper(BytesIO(), sys.stdout.encoding)
qr = qrcode.QRCode(border=0, error_correction=qrcode.constants.ERROR_CORRECT_H)
qr.add_data(data)
qr.print_ascii(out=output_buffer)
output_buffer.seek(0)
qr_ascii = output_buffer.read().strip()
return qr_ascii
def combine_ascii(left_ascii, right_ascii, spacing=0):
left_lines = left_ascii.split('\n')
right_lines = right_ascii.split('\n')
no_left_lines = len(left_lines)
no_right_lines = len(right_lines)
left_width = max(list(map(lambda line: len(line), left_lines)))
max_height = max(no_left_lines, no_right_lines)
lines = []
for i in range(max_height):
left_line = left_lines[i] if i < no_left_lines else ""
right_line = right_lines[i] if i < no_right_lines else ""
padding = " " * ((left_width - len(left_line)) + spacing)
lines.append(left_line + padding + right_line)
return "\n".join(lines)
def read_file_when_available(file_path, timeout):
start = time.time()
while not os.path.exists(file_path):
if (time.time() - start) > timeout:
return False
time.sleep(1)
with open(file_path, "r") as file:
file_contents = file.read()
return file_contents
def main():
timeout = 30
print(f"Attempting to get connection details for up to {timeout} seconds")
tor_hostname_file = "/home/umbrel/tor/data/web/hostname"
tor_hostname = read_file_when_available(tor_hostname_file, timeout)
if not tor_hostname:
print("Couldn't get connection details")
return
tor_hostname_qr_ascii = create_qr(tor_hostname.strip())
ascii_banner = combine_ascii(umbrel_ascii, tor_hostname_qr_ascii, spacing=10)
print()
print()
print()
print(ascii_banner)
print()
print(" Your Umbrel is up and running at:")
print()
print(" http://umbrel.local")
print(f" http://{tor_hostname}")
main()
Loading…
Cancel
Save