Last active
December 26, 2022 06:48
-
-
Save whitebrandy/52ffa91abb2232ed0e541dbd386b1bd3 to your computer and use it in GitHub Desktop.
Sample script to initiate backup tunnel
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 python | |
import sys, errno | |
import select | |
import os | |
import urllib.request | |
import subprocess | |
from subprocess import Popen, PIPE | |
import time | |
import signal | |
import requests | |
def usage(): | |
print("Usage: " + sys.argv[0] + " enable|disable") | |
sys.exit(0) | |
def fetchdata(url): | |
f = urllib.request.urlopen(url) | |
bytesvalue = f.read() | |
value = bytesvalue.decode("utf-8").strip() | |
return value | |
def waitfortunnel(): | |
t_end = time.time() + 60 * 5 | |
print("Waiting for upto 5 minutes for tunnel to be created...") | |
count = 1 | |
fail = 1 | |
while time.time() < t_end: | |
print("Waiting for tunnel to be created... Try: " + str(count)) | |
p=subprocess.call("ssh <remote server> ping -i 0.2 -c 1 <local site wireguard IP>", shell=True) | |
if p == 0: | |
print("Tunnel established!") | |
fail = 0 | |
break | |
count += 1 | |
if fail: | |
print ("ERROR: Could not establish live tunnel to <local site wireguard IP>") | |
sys.exit(0) | |
def writeData(value, filename): | |
datafile = open(filename, "w") | |
n = datafile.write(value) | |
datafile.close() | |
awscmd="/usr/local/bin/aws s3api put-object --bucket <bucket name> --acl public-read --key cookie.txt --body " + filename | |
try: | |
p = subprocess.Popen(awscmd, shell=True, stdout=subprocess.PIPE) | |
out,err = p.communicate('hello world') | |
except Exception as e: | |
pass | |
def cleanup(): | |
name="\"ssh -D 20000\"" | |
for line in os.popen("ps ax | grep " + name + " | grep -v grep"): | |
fields = line.split() | |
# extracting Process ID from the output | |
pid = fields[0] | |
# terminating process | |
os.kill(int(pid), signal.SIGKILL) | |
#pip install -U requests[socks] | |
def testsocksproxy(): | |
proxies = {'http': 'socks5://127.0.0.1:20000','https': 'socks5://127.0.0.1:20000'} | |
t_end = time.time() + 60 * 5 | |
fail=1 | |
print("Waiting for upto 5 minutes for tunnel to work...") | |
while time.time() < t_end: | |
resp = requests.get('https://172.16.0.10', verify=False) | |
if str(resp) == "<Response [200]>": | |
print("Tunnel is working!") | |
fail=0 | |
break | |
time.sleep(60) | |
if fail == 1: | |
print("ERROR: Tunnel setup failed!") | |
def createsocksproxy(): | |
sockscmd="ssh -D 20000 -q -C -N -v <wireguard server IP>" | |
try: | |
p = subprocess.Popen(sockscmd, shell=True, stdout=subprocess.PIPE) | |
out,err = p.communicate('hello world') | |
except Exception as e: | |
pass | |
def processData(value, action): | |
match action: | |
case 'disable': | |
cleanup() | |
if value != '0': | |
writeData('0\n', "cookie.txt") | |
sys.exit(0) | |
case 'enable': | |
if value == '1': | |
print("Tunnel already active, press <enter> in 5 seconds to resend request...") | |
i, o, e = select.select( [sys.stdin], [], [], 5) | |
if i: | |
print("Killing existing socks proxies if they exist...") | |
cleanup() | |
else: | |
print("Quitting...") | |
sys.exit(0) | |
# now we are ready to write 1 and wait | |
writeData('1\n', "cookie.txt") | |
waitfortunnel() | |
createsocksproxy() | |
# print("Testing connection with socks proxy") | |
# testsocksproxy() | |
if __name__ == "__main__": | |
action=sys.argv[1] | |
if len(sys.argv) != 2: | |
usage() | |
url="<s3 url>" | |
val=fetchdata(url) | |
processData(val, action) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment