Created
December 5, 2020 01:35
-
-
Save atoonk/a76854db4b3f8a82d2f5279d004d4a3b to your computer and use it in GitHub Desktop.
connect_mysocket.py
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
#!/usr/bin/env python3 | |
import os | |
import time | |
import jwt as jwt | |
from mysocketctl.tunnel import new_tunnel, delete_tunnel | |
from mysocketctl.login import get_token | |
from mysocketctl.utils import ssh_tunnel | |
username = os.getenv("email") | |
password = os.getenv("password") | |
socket_id = os.getenv("socket_id") | |
token = get_token(username, password) | |
global authorization_header | |
authorization_header = { | |
"x-access-token": token["token"], | |
"accept": "application/json", | |
"Content-Type": "application/json", | |
} | |
def get_user_id_from_token(): | |
data = jwt.decode(token["token"], verify=False) | |
if "user_id" in data: | |
return data["user_id"].replace("-", "") | |
else: | |
return False | |
# Create Tunnel | |
tunnel = new_tunnel(authorization_header, socket_id) | |
#few seconds to have it propagate | |
time.sleep(3) | |
#fetch username | |
ssh_user = get_user_id_from_token() | |
ssh_server = "ssh.mysocket.io" | |
port = 8000 # local port of our app | |
#set up tunnel | |
ssh_tunnel(port, tunnel["local_port"], ssh_server, ssh_user) | |
#delete tunnel when done | |
delete_tunnel(authorization_header, socket_id, tunnel["tunnel_id"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment