Skip to content

Commit

Permalink
default mtu read
Browse files Browse the repository at this point in the history
  • Loading branch information
pseusys committed Dec 25, 2023
1 parent d781c64 commit 8e4ecae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
5 changes: 2 additions & 3 deletions viridian/algae/docker/Dockerfile.algae
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ ENV LOG_LEVEL DEBUG

ENV OWNER_KEY none
ENV TUNNEL seatun
ENV CONN_MTU 1500
ENV NODE_ADDR 127.0.0.1
ENV NET_PORT 8587
ENV SEA_PORT 8542
Expand All @@ -51,7 +50,7 @@ ENV MAX_HC_TIME 5

EXPOSE $SEA_PORT/udp
EXPOSE $CTRL_PORT/tcp
ENTRYPOINT python3 -m sources.main $OWNER_KEY -t $TUNNEL -m $CONN_MTU -a $NODE_ADDR -n $NET_PORT -p $SEA_PORT -c $CTRL_PORT -i $MIN_HC_TIME -x $MAX_HC_TIME
ENTRYPOINT python3 -m sources.main $OWNER_KEY -t $TUNNEL -a $NODE_ADDR -n $NET_PORT -p $SEA_PORT -c $CTRL_PORT -i $MIN_HC_TIME -x $MAX_HC_TIME


FROM seaside-algae-default as sleeping
Expand All @@ -68,6 +67,6 @@ ENV ARG_NETWORK_GATEWAY $NETWORK_GATEWAY

RUN apt-get update && apt-get install -y --no-install-recommends iproute2 iputils-ping && rm -rf /var/lib/apt/lists/*
ENTRYPOINT ip route replace default via $ARG_NETWORK_GATEWAY && \
python3 -m sources.main $OWNER_KEY -t $TUNNEL -m $CONN_MTU -a $NODE_ADDR -n $NET_PORT -p $SEA_PORT -c $CTRL_PORT -i $MIN_HC_TIME -x $MAX_HC_TIME
python3 -m sources.main $OWNER_KEY -t $TUNNEL -a $NODE_ADDR -n $NET_PORT -p $SEA_PORT -c $CTRL_PORT -i $MIN_HC_TIME -x $MAX_HC_TIME

HEALTHCHECK --interval=3s --timeout=1s --retries=3 --start-period=10s CMD ls /sys/class/net | grep $TUNNEL
4 changes: 2 additions & 2 deletions viridian/algae/sources/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@


class Controller:
def __init__(self, key: str, name: str, mtu: int, addr: IPv4Address, sea_port: int, net_port: int, ctrl_port: int, hc_min: int, hc_max: int):
def __init__(self, key: str, name: str, addr: IPv4Address, sea_port: int, net_port: int, ctrl_port: int, hc_min: int, hc_max: int):
self._key = key
self._address = str(addr)
self._net_port = net_port
self._ctrl_port = ctrl_port
self._interface = Tunnel(name, mtu, addr, sea_port)
self._interface = Tunnel(name, addr, sea_port)
self._gravity = int(key.split(":")[1])
self._user_id = 0
self._min_hc_time = hc_min
Expand Down
2 changes: 0 additions & 2 deletions viridian/algae/sources/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from .outputs import logger

_DEFAULT_NAME = "seatun"
_DEFAULT_MTU = 1500
_DEFAULT_ADDRESS = "127.0.0.1"
_DEFAULT_NET_PORT = 8587
_DEFAULT_SEA_PORT = 8542
Expand All @@ -23,7 +22,6 @@
parser = ArgumentParser()
parser.add_argument("key", help=f"Whirlpool node owner key (required!)")
parser.add_argument("-t", "--tunnel", dest="name", default=_DEFAULT_NAME, help=f"Tunnel interface name (default: {_DEFAULT_NAME})")
parser.add_argument("-m", "--max-trans-unit", dest="mtu", default=_DEFAULT_MTU, type=int, help=f"Tunnel interface MTU (default: {_DEFAULT_MTU})")
parser.add_argument("-a", "--address", dest="addr", default=_DEFAULT_ADDRESS, type=IPv4Address, help=f"Caerulean remote IP address (default: {_DEFAULT_ADDRESS})")
parser.add_argument("-n", "--net-port", dest="net_port", default=_DEFAULT_NET_PORT, type=int, help=f"Caerulean remote network port number (default: {_DEFAULT_NET_PORT})")
parser.add_argument("-p", "--sea-port", dest="sea_port", default=_DEFAULT_SEA_PORT, type=int, help=f"Caerulean remote port number (default: {_DEFAULT_SEA_PORT})")
Expand Down
27 changes: 15 additions & 12 deletions viridian/algae/sources/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def _create_tunnel(name: str) -> Tuple[int, str]:
return descriptor, tunnel_dev


def _get_default_interface(seaside_address: str) -> Tuple[IPv4Interface, str]:
def _get_default_interface(seaside_address: str) -> Tuple[IPv4Interface, str, int]:
with IPRoute() as ip:
caerulean_dev = dict(ip.route("get", dst=seaside_address)[0]["attrs"])["RTA_OIF"]
caerulean_iface_opts = ip.get_addr(index=caerulean_dev)[0]
default_iface_attrs = dict(caerulean_iface_opts["attrs"])
default_iface = default_iface_attrs["IFA_LABEL"]
default_ip = default_iface_attrs["IFA_ADDRESS"]
default_cidr = caerulean_iface_opts["prefixlen"]
return IPv4Interface(f"{default_ip}/{default_cidr}"), default_iface
caerulean_dev = ip.route("get", dst=seaside_address)[0].get_attr("RTA_OIF")
addr_iface = ip.get_addr(index=caerulean_dev)[0]
default_cidr = addr_iface["prefixlen"]
default_iface = addr_iface.get_attr("IFA_LABEL")
default_ip = addr_iface.get_attr("IFA_ADDRESS")
default_mtu = int(ip.get_links(index=caerulean_dev)[0].get_attr("IFLA_MTU"))
return IPv4Interface(f"{default_ip}/{default_cidr}"), default_iface, default_mtu


def _create_caerulean_rule(default_ip: IPv4Interface, seaside_address: str, default_interface: str) -> Rule:
Expand All @@ -70,15 +70,14 @@ def _create_internet_rule(default_ip: IPv4Interface, default_interface: str) ->


class Tunnel:
def __init__(self, name: str, mtu: int, addr: IPv4Address, sea_port: int):
self._mtu = mtu
def __init__(self, name: str, addr: IPv4Address, sea_port: int):
self._name = name
self._address = str(addr)
self.sea_port = sea_port

self._tunnel_ip = "192.168.0.65"
self._tunnel_cdr = 24
self._def_iface, def_iface_name = _get_default_interface(self._address)
self._def_iface, def_iface_name, self._mtu = _get_default_interface(self._address)

self._operational = False
self._cipher = None
Expand Down Expand Up @@ -151,13 +150,17 @@ def down(self) -> None:
self._operational = False

def send_to_caerulean(self, gate: socket, gravity: int, user_id: int) -> None:
if self._cipher is None:
raise ValueError("Cipher must be set before launching sender thread!")
while self._operational:
packet = read(self._descriptor, MAX_MESSAGE_SIZE)
logger.debug(f"Sending {len(packet)} bytes to caerulean {self._address}:{self.sea_port}")
payload = obfuscate(gravity, self._cipher.encrypt(packet), user_id, False)
gate.sendto(payload, (self._address, self.sea_port))

def receive_from_caerulean(self, gate: socket, gravity: int, user_id: int) -> None:
def receive_from_caerulean(self, gate: socket, gravity: int, _: int) -> None:
if self._cipher is None:
raise ValueError("Cipher must be set before launching receiver thread!")
while self._operational:
packet = gate.recv(MAX_MESSAGE_SIZE)
payload = self._cipher.decrypt(deobfuscate(gravity, packet, False)[0])
Expand Down
3 changes: 1 addition & 2 deletions viridian/algae/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
def controller() -> Generator[Controller, None, None]:
key = environ["OWNER_KEY"]
name = getenv("IFACE_NAME", "sea-tun")
mtu = int(getenv("IFACE_MTU", "1500"))
addr = IPv4Address(environ["NODE_ADDR"])
sea_port = int(getenv("SEA_PORT", "8542"))
net_port = int(getenv("NET_PORT", "8587"))
ctrl_port = int(getenv("CTRL_PORT", "8543"))
min_healthcheck = int(getenv("MIN_HEALTHCHECK", "1"))
max_healthcheck = int(getenv("MAX_HEALTHCHECK", "5"))
yield Controller(key, name, mtu, addr, sea_port, net_port, ctrl_port, min_healthcheck, max_healthcheck)
yield Controller(key, name, addr, sea_port, net_port, ctrl_port, min_healthcheck, max_healthcheck)


@pytest.mark.dependency()
Expand Down

0 comments on commit 8e4ecae

Please sign in to comment.