-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62c5580
commit da00dd3
Showing
12 changed files
with
218 additions
and
160 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env python3 | ||
from src.net.main import main | ||
from src.net.cmd.main import main | ||
|
||
main() |
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import abc | ||
from argparse import ArgumentParser, Namespace | ||
|
||
|
||
class Command(abc.ABC): | ||
command: str | ||
help: str | ||
|
||
@staticmethod | ||
@abc.abstractmethod | ||
def add_arguments(parser: ArgumentParser) -> None: | ||
... | ||
|
||
@staticmethod | ||
@abc.abstractmethod | ||
def run(args: Namespace) -> None: | ||
... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from argparse import ArgumentParser, RawDescriptionHelpFormatter | ||
|
||
from .run_server import RunServerCommand | ||
from .setup import SetupCommand | ||
from .vouch import VouchCommand | ||
|
||
|
||
def main() -> None: | ||
parser = ArgumentParser( | ||
description="permuter@home - run the permuter across the Internet!\n\n" | ||
"To use p@h as a client, just pass -J when running the permuter. " | ||
"This script is\nonly necessary for configuration or when running a server.", | ||
formatter_class=RawDescriptionHelpFormatter, | ||
) | ||
|
||
commands = [ | ||
RunServerCommand, | ||
SetupCommand, | ||
VouchCommand, | ||
] | ||
|
||
subparsers = parser.add_subparsers(metavar="<command>") | ||
for command in commands: | ||
subparser = subparsers.add_parser( | ||
command.command, | ||
help=command.help, | ||
description=command.help, | ||
) | ||
command.add_arguments(subparser) | ||
subparser.set_defaults(subcommand_handler=command.run) | ||
|
||
args = parser.parse_args() | ||
if "subcommand_handler" in args: | ||
args.subcommand_handler(args) | ||
else: | ||
parser.print_help() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from argparse import ArgumentParser, Namespace | ||
import base64 | ||
import os | ||
import random | ||
import string | ||
import sys | ||
import time | ||
from typing import Optional | ||
|
||
from nacl.public import SealedBox | ||
from nacl.signing import SigningKey, VerifyKey | ||
|
||
from .base import Command | ||
from ..common import RawConfig, read_config, sign_with_magic, write_config | ||
from .util import ask | ||
|
||
|
||
class SetupCommand(Command): | ||
command = "setup" | ||
help = ( | ||
"Set up permuter@home. This will require someone else to grant you " | ||
"access to the central server." | ||
) | ||
|
||
@staticmethod | ||
def add_arguments(parser: ArgumentParser) -> None: | ||
pass | ||
|
||
@staticmethod | ||
def run(args: Namespace) -> None: | ||
config = read_config() | ||
_run_initial_setup(config) | ||
|
||
|
||
def _random_name() -> str: | ||
return "".join(random.choice(string.ascii_lowercase) for _ in range(5)) | ||
|
||
|
||
def _run_initial_setup(config: RawConfig) -> None: | ||
signing_key: Optional[SigningKey] = config.signing_key | ||
if not signing_key or not ask("Keep previous secret key", default=True): | ||
signing_key = SigningKey.generate() | ||
config.signing_key = signing_key | ||
write_config(config) | ||
verify_key = signing_key.verify_key | ||
|
||
nickname: Optional[str] = config.initial_setup_nickname | ||
if not nickname or not ask(f"Keep previous nickname [{nickname}]", default=True): | ||
default_nickname = os.environ.get("USER") or _random_name() | ||
nickname = ( | ||
input(f"Nickname [default: {default_nickname}]: ") or default_nickname | ||
) | ||
config.initial_setup_nickname = nickname | ||
write_config(config) | ||
|
||
signed_nickname = sign_with_magic(b"NICK", signing_key, nickname.encode("utf-8")) | ||
|
||
vouch_data = verify_key.encode() + signed_nickname | ||
vouch_text = base64.b64encode(vouch_data).decode("utf-8") | ||
print("Ask someone to run the following command:") | ||
print(f"./permuter.py --vouch {vouch_text}") | ||
print() | ||
print("They should give you a token back in return. Paste that here:") | ||
inp = input().strip() | ||
|
||
try: | ||
token = base64.b64decode(inp.encode("utf-8")) | ||
data = SealedBox(signing_key.to_curve25519_private_key()).decrypt(token) | ||
auth_verify_key = VerifyKey(data[:32]) | ||
auth_server = data[32:].decode("utf-8") | ||
print(f"Server URL: {auth_server}") | ||
print("Testing connection...") | ||
time.sleep(1) | ||
|
||
# TODO: verify that contacting auth server works and signs its messages | ||
|
||
print("permuter@home successfully set up!") | ||
print() | ||
config.auth_server = auth_server | ||
config.auth_verify_key = auth_verify_key | ||
config.initial_setup_nickname = None | ||
write_config(config) | ||
except Exception: | ||
print("Invalid token!") | ||
sys.exit(1) |
Oops, something went wrong.