raspiblitz/home.admin/config.scripts/blitz.ip2tor.py

152 lines
5.2 KiB
Python
Raw Normal View History

2020-05-22 19:24:46 +02:00
#!/usr/bin/python3
import sys
import locale
2020-05-22 20:06:59 +02:00
import requests
import json
2020-05-23 02:44:08 +02:00
import math
2020-05-22 19:24:46 +02:00
# display config script info
if len(sys.argv) <= 1 or sys.argv[1] == "-h" or sys.argv[1] == "help":
2020-05-23 02:44:08 +02:00
print("# manage ip2tor subscriptions for raspiblitz")
print("# blitz.ip2tor.py menu")
2020-05-22 19:24:46 +02:00
sys.exit(1)
# basic settings
locale.setlocale(locale.LC_ALL, '')
2020-05-23 02:44:08 +02:00
# TODO: use TOR proxy session
# TODO: check is still works when shopurl is an onion address
def apiGetHosts(shopurl):
print("# apiGetHosts")
hosts=[]
# make HTTP request
try:
response = requests.get("https://"+shopurl+"/api/v1/public/hosts/")
except Exception as e:
print("error='FAILED HTTP REQUEST'")
return
if response.status_code != 200:
print("error='FAILED HTTP CODE ({0})'".format(response.status_code))
return
# parse & validate data
try:
jData = json.loads(response.content)
except Exception as e:
print("error='FAILED JSON PARSING'")
return
if not isinstance(jData, list):
print("error='NOT A JSON LIST'")
return
for idx, hostEntry in enumerate(jData):
try:
# ignore if not offering tor bridge
if not hostEntry['offers_tor_bridges']: continue
# ignore if duration is less than an hour
if hostEntry['tor_bridge_duration'] < 3600: continue
# add duration per hour value
hostEntry['tor_bridge_duration_hours'] = math.floor(hostEntry['tor_bridge_duration']/3600)
# ignore if prices are negative or below one sat (maybe msats later)
if hostEntry['tor_bridge_price_initial'] < 1000: continue
if hostEntry['tor_bridge_price_extension'] < 1000: continue
# add price in sats
hostEntry['tor_bridge_price_initial_sats'] = math.ceil(hostEntry['tor_bridge_price_initial']/1000)
hostEntry['tor_bridge_price_extension_sats'] = math.ceil(hostEntry['tor_bridge_price_extension']/1000)
# ignore name is less then 3 chars
if len(hostEntry['name']) < 3: continue
# ignore id with zero value
if len(hostEntry['id']) < 1: continue
# shorten names to 20 chars max
hostEntry['name'] = hostEntry['name'][:20]
except Exception as e:
print("error='PARSING HOST ENTRY'")
return
print("({0}) {1} ({2} hours, first: {3} sats, next: {4} sats)".format(idx, hostEntry['name'].ljust(20), hostEntry['tor_bridge_duration_hours'], hostEntry['tor_bridge_price_initial_sats'], hostEntry['tor_bridge_price_extension_sats']))
#print(hostEntry)
hosts.append(hostEntry)
print("# found {0} valid torbridge hosts".format(len(hosts)))
return hosts
# TODO: use TOR proxy session
def apiPlaceOrder(shopurl, hostid, toraddressWithPort):
print("# apiPlaceOrder")
postData={
'product': "tor_bridge",
'host_id': hostid,
'tos_accepted': True,
'comment': 'test',
'target': toraddressWithPort,
'public_key': ''
}
try:
response = requests.post("https://"+shopurl+"/api/v1/public/order/", data=postData)
except Exception as e:
print("error='FAILED HTTP REQUEST'")
return
if response.status_code != 201:
print("error='FAILED HTTP CODE ({0})'".format(response.status_code))
return
# parse & validate data
try:
jData = json.loads(response.content)
except Exception as e:
print("error='FAILED JSON PARSING'")
return
print(jData)
apiGetHosts("shop.ip2t.org")
#apiPlaceOrder("shop.ip2t.org", "fc747bae-6dbb-498d-89c2-f2445210c8f8", "facebookcorewwwi.onion:80")
if False: '''
2020-05-22 19:24:46 +02:00
###############
# MENU
###############
if sys.argv[1] == "menu":
2020-05-23 02:44:08 +02:00
from dialog import Dialog
2020-05-22 19:24:46 +02:00
d = Dialog(dialog="dialog",autowidgetsize=True)
d.set_background_title("IP2TOR Subscription Service")
code, tag = d.menu("OK, then you have two options:",
2020-05-22 20:06:59 +02:00
choices=[("(1)", "Test HTTP REQUEST thru TOR PROXY"),
2020-05-22 20:28:27 +02:00
("(2)", "Make REST API - JSON request"),
("(3)", "TOML test"),
2020-05-23 02:44:08 +02:00
("(4)", "Working with .conf files")])
2020-05-22 19:24:46 +02:00
if code == d.OK:
2020-05-22 20:06:59 +02:00
if tag == "(1)":
2020-05-22 20:28:27 +02:00
print("Needs: pip3 install pysocks\n")
2020-05-22 20:06:59 +02:00
session = requests.session()
session.proxies = {'http': 'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
print("Call 'http://httpbin.org/ip' thru TOR proxy:\n")
print(session.get("http://httpbin.org/ip").text)
print("Call 'http://httpbin.org/ip' normal:\n")
print(requests.get("http://httpbin.org/ip").text)
print("Call 'https://shop.ip2t.org/api/v1/public/hosts/' thru TOR:\n")
print(session.get("https://shop.ip2t.org/api/v1/public/hosts/").text)
if tag == "(2)":
2020-05-23 02:44:08 +02:00
2020-05-22 20:28:27 +02:00
if tag == "(3)":
2020-05-22 20:32:17 +02:00
print ("Needs: pip3 install toml")
2020-05-22 20:28:27 +02:00
import toml
toml_string = """
"""
if tag == "(4)":
with open('/mnt/hdd/raspiblitz.conf', 'r') as myfile:
2020-05-22 20:29:43 +02:00
data=myfile.read()
2020-05-22 20:32:17 +02:00
print(data)
2020-05-22 20:33:04 +02:00
import toml
2020-05-22 20:32:17 +02:00
parsed_toml = toml.loads(data)
print(parsed_toml)
2020-05-22 20:28:27 +02:00
2020-05-22 19:24:46 +02:00
else:
2020-05-23 02:44:08 +02:00
print("Cancel")
'''