Created
January 18, 2023 00:25
-
-
Save courtc/948fe080429160d6c963f7589484ff78 to your computer and use it in GitHub Desktop.
Innernet host HTTP query
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 | |
# $ ./inn-resolve.py fire.example.wg | |
# 172.28.208.2 | |
import configparser | |
import urllib.request | |
import json | |
import sys | |
query = sys.argv[1] | |
if not query.endswith(".wg"): | |
print("BAD") | |
sys.exit(1) | |
parts = query.split('.') | |
if len(parts) != 3: | |
print("BAD") | |
sys.exit(2) | |
intf = parts[1] | |
hname = parts[0] | |
print(f"# intf = {intf}") | |
config = configparser.ConfigParser() | |
config.read(f"/etc/innernet/{intf}.conf") | |
pubkey = config['server']['public-key'].strip('"') | |
saddr = config['server']['internal-endpoint'].strip('"') | |
print(f"# saddr = {saddr}") | |
req = urllib.request.Request(f'http://{saddr}/v1/user/state') | |
req.add_header("X-Innernet-Server-Key", pubkey) | |
with urllib.request.urlopen(req) as r: | |
resp = json.loads(r.read().decode("utf-8")) | |
peers = resp["peers"] | |
for peer in peers: | |
if peer["name"] == hname: | |
print(peer["ip"]) | |
sys.exit(0) | |
print("NX") | |
sys.exit(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment