Last active
April 9, 2020 04:33
-
-
Save cobrce/1b2d132f3c4401ab5660d23d1d41315d to your computer and use it in GitHub Desktop.
a script to run ngrok with selected profiles (tunnels) then update a google keep note with the created urls, useful when setting ngrok in autostart (I use crontab -e)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import requests | |
import json | |
import time | |
import gkeepapi as g | |
import re | |
import arc4 | |
import hashlib | |
def find(k,title): | |
for a in k.all(): | |
if not a.deleted and not a.archived and a.title == title: | |
a.pinned = True | |
return a | |
def getserial(): | |
# Extract serial from cpuinfo file | |
cpuserial = "0000000000000000" | |
try: | |
f = open('/proc/cpuinfo','r') | |
for line in f: | |
if line[0:6]=='Serial': | |
cpuserial = line[10:26] | |
f.close() | |
except: | |
cpuserial = "ERROR000000000" | |
return cpuserial | |
def decrypt_key(): | |
digest = b'sha_hash_of_gkeep_2-steps_verification_password' | |
c = b'encrypted_gkeep_2-steps_verification_password' | |
r = arc4.ARC4(getserial()) | |
d = r.decrypt(c) | |
h = hashlib.sha256() | |
h.update(d) | |
if h.digest() != digest: | |
exit() | |
return d.decode() | |
class profile: | |
def __init__(self,name): | |
self.name = name | |
self.url = None | |
@property | |
def url(self): | |
return self._url | |
@url.setter | |
def url(self,url): | |
self._url = url | |
def format(self): | |
return "%s : %s" % (self.name,self.url) | |
def get_public_url(self): | |
try: | |
localhost_url = "http://localhost:4040/api/tunnels/%s" % self.name | |
tunnel_url = requests.get(localhost_url).text #Get the tunnel information | |
j = json.loads(tunnel_url) | |
self.url = j['public_url'] | |
return True | |
except: | |
return False | |
class ssh(profile): | |
def format(self): | |
m = re.match(".*?://(.*?):([1-9]*)",self.url) | |
if m is not None: | |
return "ssh pi@%s -p %s" % (m.groups()[0],m.groups()[1]) | |
else: | |
return "ssh : %s" % self.url | |
def main(): | |
key = decrypt_key() | |
print("Key decrypted") | |
profiles = (profile("qbit"), | |
profile("sonarr"), | |
profile("radarr"), | |
ssh("ssh")) | |
cmdln = ['./ngrok','start','-config','ngrok.yml',*(map(lambda x:x.name,profiles))] | |
subprocess.Popen(cmdln, | |
stdout=subprocess.PIPE) | |
print(" ".join(cmdln)) | |
for i in range(20): | |
print("reading urls, try %d" % i) | |
time.sleep(1) # to allow the ngrok to fetch the url from the server | |
found = True | |
for p in profiles: | |
if p.url is None: | |
if not p.get_public_url(): | |
found =False | |
if found: | |
break | |
for p in profiles: | |
if p.url is None: | |
p.url = "<error>" | |
urls = "\r\n".join(map(lambda x : x.format(),profiles)) | |
print("") | |
print("Extracted urls:") | |
print(urls) | |
print("") | |
k = g.Keep() | |
print("connecting to gkeep") | |
k.login("your_email@gmail.com",key) | |
print("connected") | |
title = 'Ngrok' | |
n = find(k,title) | |
if n is None: | |
print("Note not found, creating new") | |
n = k.createNote(title=title) | |
n.pinned = True | |
else: | |
print("Note found") | |
n.text = urls | |
print("Syncing") | |
k.sync() | |
print("Done") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment