Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ jobs:
pip install ruff
- name: Lint with ruff
run: |
ruff --output-format=github --exclude externals .
ruff check --output-format=github --exclude externals .
4 changes: 4 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DefaultKey = cert/server.key

[OPN]
IP = 0.0.0.0
ExternalIP =
Port = 8200
Name = OpnServer
MaxThread = 1
Expand All @@ -17,6 +18,7 @@ LogToWindow = ON

[LMP]
IP = 0.0.0.0
ExternalIP =
Port = 8201
Name = LmpServer
MaxThread = 1
Expand All @@ -30,6 +32,7 @@ LogToWindow = ON

[FMP]
IP = 0.0.0.0
ExternalIP =
Port = 8202
Name = FmpServer
MaxThread = 0
Expand All @@ -43,6 +46,7 @@ LogToWindow = ON

[RFP]
IP = 0.0.0.0
ExternalIP =
Port = 8203
Name = RfpServer
MaxThread = 1
Expand Down
2 changes: 1 addition & 1 deletion mh/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def __init__(self, name, server_type, gate_count=40, capacity=2000,
self.name = name
self.parent = None
self.server_type = server_type
self.addr = addr
self.addr = addr # public IP address
self.port = port
self.gates = [
Gate("City Gate{}".format(i), self)
Expand Down
6 changes: 3 additions & 3 deletions mh/pat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import traceback
from datetime import timedelta

from other.utils import Logger, get_config, get_ip, hexdump, to_str
from other.utils import Logger, get_config, get_external_ip, hexdump, to_str

import mh.pat_item as pati
import mh.server as server
Expand Down Expand Up @@ -583,7 +583,7 @@ def recvReqLmpConnect(self, packet_id, data, seq):
TODO: I don't think it's related to LMP protocol.
"""
config = get_config("LMP")
self.sendAnsLmpConnect(get_ip(config["IP"]), config["Port"], seq)
self.sendAnsLmpConnect(get_external_ip(config), config["Port"], seq)

def sendAnsLmpConnect(self, address, port, seq):
"""AnsLmpConnect packet.
Expand Down Expand Up @@ -1071,7 +1071,7 @@ def recvReqFmpInfo(self, packet_id, data, seq):
fields = pati.unpack_bytes(data, 4)
server = self.session.join_server(index)
config = get_config("FMP")
fmp_addr = get_ip(config["IP"])
fmp_addr = get_external_ip(config)
fmp_port = config["Port"]
fmp_data = pati.FmpData()
fmp_data.server_address = pati.String(server.addr or fmp_addr)
Expand Down
6 changes: 4 additions & 2 deletions mh/pat_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from collections import OrderedDict
from mh.constants import pad
from other.utils import to_bytearray, get_config, get_ip, GenericUnpacker
from other.utils import to_bytearray, get_config, get_external_ip, \
GenericUnpacker
from mh.database import Server, Gate, City


class ItemType:
Custom = 0
Byte = 1
Expand Down Expand Up @@ -960,7 +962,7 @@ def get_fmp_servers(session, first_index, count):
assert first_index > 0, "Invalid list index"

config = get_config("FMP")
fmp_addr = get_ip(config["IP"])
fmp_addr = get_external_ip(config)
fmp_port = config["Port"]

data = b""
Expand Down
16 changes: 15 additions & 1 deletion other/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# Python 3
basestring = str
import configparser as ConfigParser
from typing import Any # noqa: F401

CONFIG_FILE = "config.ini"
LOG_FOLDER = "logs"
Expand Down Expand Up @@ -221,6 +222,7 @@ def get_config(name, config_file=CONFIG_FILE):
config.read(config_file)
return {
"IP": config.get(name, "IP"),
"ExternalIP": config.get(name, "ExternalIP"),
"Port": config.getint(name, "Port"),
"Name": config.get(name, "Name"),
"MaxThread": config.getint(name, "MaxThread"),
Expand All @@ -239,19 +241,31 @@ def get_config(name, config_file=CONFIG_FILE):


def get_default_ip():
# type: () -> str
"""Get the default IP address"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
ip = s.getsockname()[0] # type: str
s.close()
return ip


def get_ip(ip):
# type: (str) -> str
"""Return the IP address that will be used."""
return get_default_ip() if ip == "0.0.0.0" else ip


def get_external_ip(config):
# type: (dict[str, Any]) -> str
"""Return the IP address advertised by the server.

It's useful when the public IP address can't easily be retrieved.
For instance, when behind a NAT or some cloud infrastructures.
"""
return config["ExternalIP"] or get_ip(config["IP"])


def argparse_from_config(config):
"""Argument parser from config."""
import argparse
Expand Down