use argparse and make yam an executable script

This commit is contained in:
Kevin Froman 2020-04-23 04:23:57 -05:00
parent 4e545d8e02
commit 594df495c3
7 changed files with 96 additions and 31 deletions

View File

@ -2,6 +2,10 @@
This project uses Semantic Versioning This project uses Semantic Versioning
## 1.0.0
Made yam a proper script (install and you can run from path with $ yam.py <args>)
## 0.0.0 ## 0.0.0
Initial release Initial release

View File

@ -4,13 +4,16 @@ this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read() long_description = f.read()
packages = find_packages(exclude=['contrib', 'docs', 'tests'])
setup(name='youandme', setup(name='youandme',
version='0.0.2', version='1.0.0',
description='Simple private data sharing via bytearrays, Tor tunneling and metadata paranoia', description='Simple private data sharing via bytearrays, Tor tunneling and metadata paranoia',
long_description=long_description, long_description=long_description,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',
author='Kevin Froman', author='Kevin Froman',
packages=packages,
scripts=['src/yam/yam.py'],
author_email='beardog@mailbox.org', author_email='beardog@mailbox.org',
url='https://chaoswebs.net', url='https://chaoswebs.net',
install_requires=[ install_requires=[

0
src/yam/__init__.py Normal file
View File

View File

@ -1,7 +1,10 @@
#!/usr/bin/env python3
import sys import sys
import socket import socket
from time import time, sleep from time import time, sleep
from threading import Thread from threading import Thread
import tempfile
import argparse
from stem.control import Controller from stem.control import Controller
@ -16,12 +19,26 @@ from youandme.client import client
class _Address: class _Address:
address = "" address = ""
def _get_open_port():
# taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/
# changes from source: import moved to top of file, bind specifically to localhost
# vulnerable to race condition
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1",0))
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
def connector(host, send_data, recv_data, address="", control_port=1337, socks_port=1338): def connector(host, send_data, recv_data, address="", control_port=1337, socks_port=1338):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', socks_port)) result = sock.connect_ex(('127.0.0.1', socks_port))
if result != 0: if result != 0:
try:
launch_tor(control_port=control_port, socks_port=socks_port) launch_tor(control_port=control_port, socks_port=socks_port)
except OSError:
with tempfile.TemporaryDirectory() as tmpdirname:
launch_tor(control_port=control_port, socks_port=socks_port, data_dir=tmpdirname)
if host: if host:
with Controller.from_port(port=control_port) as controller: with Controller.from_port(port=control_port) as controller:
controller.authenticate() controller.authenticate()
@ -79,18 +96,33 @@ def chat(mode, send_data, recv_data):
break break
sleep(1) sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
pass break
PORT_MESSAGE = "Specify any free ports above 1023"
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) >= 2: parser = argparse.ArgumentParser(description='End-to-end encrpyted instant messaging with metadata privacy and perfect forward secrecy')
if sys.argv[1] == 'chat': parser.add_argument('connection', choices=['host', 'conn'])
parser.add_argument('--socks-port', help='Socks proxy port (will use random if not given)', default=0, type=int)
parser.add_argument('--control-port', help='Tor control port (will use random if not given)', default=0, type=int)
parser.add_argument('--address', help='Address to connect to. No port.', default='')
args = parser.parse_args()
if args.socks_port == 0: args.socks_port = _get_open_port()
if args.control_port == 0: args.control_port = _get_open_port()
send_data = bytearray() send_data = bytearray()
recv_data = bytearray() recv_data = bytearray()
if sys.argv[2] == 'host': if args.connection == 'conn':
Thread(target=connector, args=[True, send_data, recv_data], kwargs={'socks_port': int(input("socks")), 'control_port': int(input('control port'))}, daemon=True).start() if not args.address:
elif sys.argv[2].startswith('conn'): print("Must specify address if connecting (--address)", file=sys.stderr)
Thread(target=connector, args=[False, send_data, recv_data], kwargs={'address': sys.argv[3], 'socks_port': int(input("socks")), 'control_port': int(input('control port'))}, daemon=True).start() sys.exit(3)
Thread(target=connector,
args=[False, send_data, recv_data],
kwargs={'address': args.address,
'socks_port': args.socks_port,
'control_port': args.control_port}, daemon=True).start()
chat('conn', send_data, recv_data)
else: else:
print('Must specify host or conn') Thread(target=connector, args=[True, send_data, recv_data],
sys.exit(1) kwargs={'socks_port': args.socks_port,
chat(sys.argv[2], send_data, recv_data) 'control_port': args.control_port}, daemon=True).start()
chat('host', send_data, recv_data)

View File

@ -1,14 +1,17 @@
import stem.process import stem.process
def launch_tor(control_port="1336", socks_port="1337"): def launch_tor(control_port="1336", socks_port="1337", data_dir=""):
control_port = str(control_port) control_port = str(control_port)
socks_port = str(socks_port) socks_port = str(socks_port)
stem.process.launch_tor_with_config(
config = { config = {
'ControlPort': control_port, 'ControlPort': control_port,
'SocksPort': socks_port, 'SocksPort': socks_port,
'Log': [ 'Log': [
'NOTICE stdout' 'NOTICE stdout'
], ],
}, take_ownership=True) }
if data_dir:
config['DataDirectory'] = data_dir
stem.process.launch_tor_with_config(
config = config, take_ownership=True)

View File

@ -7,8 +7,19 @@ import stem.process
from stem.control import Controller from stem.control import Controller
from youandme import client from youandme import client
control_port = str(1353) def get_open_port():
socks_port = str(1354) # taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/
# changes from source: import moved to top of file, bind specifically to localhost
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1",0))
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
control_port = str(get_open_port())
socks_port = str(get_open_port())
assert control_port != socks_port
stem.process.launch_tor_with_config( stem.process.launch_tor_with_config(
config = { config = {

View File

@ -1,23 +1,35 @@
import unittest import unittest
import socket import socket
from threading import Thread from threading import Thread
import tempfile
import time import time
import stem import stem
import stem.process import stem.process
from stem.control import Controller from stem.control import Controller
from youandme import server from youandme import server
control_port = str(1353) def get_open_port():
socks_port = str(1354) # taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/
# changes from source: import moved to top of file, bind specifically to localhost
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1",0))
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
stem.process.launch_tor_with_config( control_port = str(get_open_port())
config = { socks_port = str(get_open_port())
assert control_port != socks_port
with tempfile.TemporaryDirectory() as tmpdirname:
stem.process.launch_tor_with_config(
config = {
'ControlPort': control_port, 'ControlPort': control_port,
'SocksPort': socks_port, 'SocksPort': socks_port,
'Log': [ 'DataDirectory': tmpdirname
'NOTICE stdout'
], }, take_ownership=True, init_msg_handler=lambda i: print(i))
}, take_ownership=True)
def send_test_data(ip, port): def send_test_data(ip, port):
time.sleep(2) time.sleep(2)
@ -52,7 +64,7 @@ class TestServer(unittest.TestCase):
) )
Thread(target=send_test_data, args=[ip, port], daemon=True).start() Thread(target=send_test_data, args=[ip, port], daemon=True).start()
conn, addr = s.accept() conn, addr = s.accept()
Thread(target=server, args=[0.1, controller, conn, send_data, recv_data], daemon=True).start() Thread(target=server.server, args=[0.1, controller, conn, send_data, recv_data], daemon=True).start()
time.sleep(1) time.sleep(1)
max_iters = 10000000 max_iters = 10000000
c = 0 c = 0