Browse Source

5.1.0: Add TOR_PROXY environment variable to configure SOCKS proxy selection

master 5.1.0
/dev/null 2 weeks ago
parent
commit
be0ac1006a
  1. 2
      setup.py
  2. 42
      src/sporestack/api.py
  3. 13
      src/sporestack/api_client.py
  4. 10
      src/sporestack/cli.py
  5. 12
      tests/test_cli.py

2
setup.py

@ -4,7 +4,7 @@ from pathlib import Path
from setuptools import find_packages, setup
VERSION = "5.0.2"
VERSION = "5.1.0"
DOWNLOAD_HOST = "https://git.sporestack.com"
REPO_URL = f"{DOWNLOAD_HOST}/SporeStack/sporestack-python"
DOWNLOAD_URL = f"{REPO_URL}/archive/{VERSION}.tar.gz"

42
src/sporestack/api.py

@ -12,47 +12,6 @@ from pydantic import BaseModel
from .models import NetworkInterface, Payment
class deprecated_settlement_enable(object):
url = "/settlement/enable"
method = "POST"
class Request(BaseModel):
settlement_token: str
currency: str
cents: int
class Response(BaseModel):
payment_uri: Optional[str]
txid: Optional[str]
paid: bool
usd: str
class deprecated_settlement_add(object):
url = "/settlement/add"
method = "POST"
class Request(BaseModel):
settlement_token: str
currency: str
cents: int
class Response(BaseModel):
payment_uri: Optional[str]
txid: Optional[str]
paid: bool
usd: str
class deprecated_settlement_balance(object):
url = "/settlement/balance"
method = "GET"
class Response(BaseModel):
cents: int
usd: str
class token_enable(object):
url = "/token/{token}/enable"
method = "POST"
@ -185,6 +144,7 @@ class deprecated_server_info(object):
machine_id: str
network_interfaces: List[NetworkInterface]
region: str
warning: str = "Deprecated endpoint. Upgrade sporestack-python!"
class deprecated_server_start(object):

13
src/sporestack/api_client.py

@ -1,4 +1,5 @@
import logging
import os
from time import sleep
from typing import Any, Dict, Optional
@ -20,9 +21,17 @@ API_ENDPOINT = CLEARNET_ENDPOINT
GET_TIMEOUT = 60
POST_TIMEOUT = 90
USE_TOR_PROXY = "auto"
TOR_PROXY = "socks5h://127.0.0.1:9050"
def _get_tor_proxy() -> str:
"""
This makes testing easier.
"""
return os.getenv("TOR_PROXY", "socks5h://127.0.0.1:9050")
# For requests module
TOR_PROXY_REQUESTS = {"http": TOR_PROXY, "https": TOR_PROXY}
TOR_PROXY_REQUESTS = {"http": _get_tor_proxy(), "https": _get_tor_proxy()}
def is_onion_url(url: str) -> bool:

10
src/sporestack/cli.py

@ -25,6 +25,8 @@ Optional environment variables:
SPORESTACK_ENDPOINT
*or*
SPORESTACK_USE_TOR_ENDPOINT
TOR_PROXY (defaults to socks5h://127.0.0.1:9050 which is fine for most)
"""
cli = typer.Typer(help=HELP)
@ -553,7 +555,13 @@ def api_endpoint() -> None:
Prints the selected API endpoint: Env var: SPORESTACK_ENDPOINT,
or, SPORESTACK_USE_TOR=1
"""
typer.echo(get_api_endpoint())
endpoint = get_api_endpoint()
if ".onion" in endpoint:
typer.echo(f"{endpoint} using {api_client._get_tor_proxy()}")
return
else:
typer.echo(endpoint)
return
if __name__ == "__main__":

12
tests/test_cli.py

@ -2,6 +2,7 @@ from _pytest.monkeypatch import MonkeyPatch
from typer.testing import CliRunner
from sporestack import cli
from sporestack.api_client import TOR_ENDPOINT
runner = CliRunner()
@ -31,6 +32,17 @@ def test_cli_api_endpoint(monkeypatch: MonkeyPatch) -> None:
# So tests pass locally, even if these are set.
monkeypatch.delenv("SPORESTACK_ENDPOINT", raising=False)
monkeypatch.delenv("SPORESTACK_USE_TOR_ENDPOINT", raising=False)
monkeypatch.delenv("TOR_PROXY", raising=False)
result = runner.invoke(cli.cli, ["api-endpoint"])
assert result.output == "https://api.sporestack.com" + "\n"
assert result.exit_code == 0
monkeypatch.setenv("SPORESTACK_USE_TOR_ENDPOINT", "1")
result = runner.invoke(cli.cli, ["api-endpoint"])
assert result.output == TOR_ENDPOINT + " using socks5h://127.0.0.1:9050\n"
assert result.exit_code == 0
monkeypatch.setenv("TOR_PROXY", "socks5h://127.0.0.1:1337")
result = runner.invoke(cli.cli, ["api-endpoint"])
assert result.output == TOR_ENDPOINT + " using socks5h://127.0.0.1:1337\n"
assert result.exit_code == 0

Loading…
Cancel
Save