Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdikSS committed Jan 20, 2023
0 parents commit 0f67d97
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TereStun — STUN over Teredo
===========================

TereStun is a very simple unitility to determine external IPv4 address and source port over NAT, using Teredo protocol.

In other words, this is a STUN-like client over Teredo.

```
$ ./terestun.pyz --help
usage: Teredo STUN [-h] [-s SERVER] [-p PORT] [--ip-only] [--port-only]
Get IPv4 address and source port over Teredo network
options:
-h, --help show this help message and exit
-s SERVER, --server SERVER
Teredo server
-p PORT, --port PORT Source port to use
--ip-only Show only IPv4 address, without port
--port-only Show only source port, without IPv4 address
$ ./terestun.pyz
142.251.36.110:44254
```
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
python -m pip install . --target build
find build -path '*/__pycache__*' -delete
mv build/terestun/__main__.py build/
python -m zipapp -c -p '/usr/bin/env python3' build -o terestun.pyz
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "terestun"
version = "0.0.1"
authors = [
{ name="ValdikSS", email="[email protected]" },
]
description = "STUN client using Teredo protocol"
readme = "README.md"
requires-python = ">=3.4"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/valdikss/terestun"
"Bug Tracker" = "https://github.com/valdikss/terestun/issues"
Empty file added src/terestun/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions src/terestun/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

import sys
import socket
import argparse

DATA = b"\x00\x01\x00\x00\x2a\x3c\xfa\x40\x75\xe9\xbf\x57\x00\x60\x00\x00" \
b"\x00\x00\x08\x3a\xff\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\xff" \
b"\xff\xff\xff\xff\xff\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
b"\x00\x00\x00\x00\x02\x85\x00\x7d\x37\x00\x00\x00\x00"

parser = argparse.ArgumentParser(
prog = 'Teredo STUN',
description = 'Get IPv4 address and source port over Teredo network')
parser.add_argument('-s', '--server', default='win10.ipv6.microsoft.com',
help='Teredo server')
parser.add_argument('-p', '--port', type=int, default=0,
help='Source port to use')
parser.add_argument('--ip-only', action='store_true',
help='Show only IPv4 address, without port')
parser.add_argument('--port-only', action='store_true',
help='Show only source port, without IPv4 address')
args = parser.parse_args()

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(10)
try:
s.bind(('0.0.0.0', args.port))
s.sendto(DATA, (args.server, 3544))
response, source = s.recvfrom(512)
s.close()
except socket.error as e:
print(e, file=sys.stderr)
sys.exit(1)
except (KeyboardInterrupt, SystemExit):
sys.exit(2)
if not len(response) > 64:
print("Not enough data", file=sys.stderr)
sys.exit(3)

port_raw = response[15:17]
port = int.from_bytes(port_raw, 'big') ^ 0xFFFF

ip_raw = response[17:21]
ip = int.from_bytes(ip_raw, 'big') ^ 0xFFFFFFFF
ip1 = (ip >> 24) % 256
ip2 = (ip >> 16) % 256
ip3 = (ip >> 8) % 256
ip4 = ip % 256

if args.port_only:
print(port)
elif args.ip_only:
print("{}.{}.{}.{}".format(ip1, ip2, ip3, ip4))
else:
print("{}.{}.{}.{}:{}".format(ip1, ip2, ip3, ip4, port))

0 comments on commit 0f67d97

Please sign in to comment.