Skip to content

Commit d2fd334

Browse files
authored
Merge pull request #132 from sepalani/cfg-ext-ip
Add ExternalIP config field
2 parents 8167c4f + 78bb0db commit d2fd334

6 files changed

Lines changed: 28 additions & 8 deletions

File tree

.github/workflows/python-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ jobs:
6969
pip install ruff
7070
- name: Lint with ruff
7171
run: |
72-
ruff --output-format=github --exclude externals .
72+
ruff check --output-format=github --exclude externals .

config.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DefaultKey = cert/server.key
44

55
[OPN]
66
IP = 0.0.0.0
7+
ExternalIP =
78
Port = 8200
89
Name = OpnServer
910
MaxThread = 1
@@ -17,6 +18,7 @@ LogToWindow = ON
1718

1819
[LMP]
1920
IP = 0.0.0.0
21+
ExternalIP =
2022
Port = 8201
2123
Name = LmpServer
2224
MaxThread = 1
@@ -30,6 +32,7 @@ LogToWindow = ON
3032

3133
[FMP]
3234
IP = 0.0.0.0
35+
ExternalIP =
3336
Port = 8202
3437
Name = FmpServer
3538
MaxThread = 0
@@ -43,6 +46,7 @@ LogToWindow = ON
4346

4447
[RFP]
4548
IP = 0.0.0.0
49+
ExternalIP =
4650
Port = 8203
4751
Name = RfpServer
4852
MaxThread = 1

mh/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def __init__(self, name, server_type, gate_count=40, capacity=2000,
337337
self.name = name
338338
self.parent = None
339339
self.server_type = server_type
340-
self.addr = addr
340+
self.addr = addr # public IP address
341341
self.port = port
342342
self.gates = [
343343
Gate("City Gate{}".format(i), self)

mh/pat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import traceback
99
from datetime import timedelta
1010

11-
from other.utils import Logger, get_config, get_ip, hexdump, to_str
11+
from other.utils import Logger, get_config, get_external_ip, hexdump, to_str
1212

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

588588
def sendAnsLmpConnect(self, address, port, seq):
589589
"""AnsLmpConnect packet.
@@ -1071,7 +1071,7 @@ def recvReqFmpInfo(self, packet_id, data, seq):
10711071
fields = pati.unpack_bytes(data, 4)
10721072
server = self.session.join_server(index)
10731073
config = get_config("FMP")
1074-
fmp_addr = get_ip(config["IP"])
1074+
fmp_addr = get_external_ip(config)
10751075
fmp_port = config["Port"]
10761076
fmp_data = pati.FmpData()
10771077
fmp_data.server_address = pati.String(server.addr or fmp_addr)

mh/pat_item.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
from collections import OrderedDict
1010
from mh.constants import pad
11-
from other.utils import to_bytearray, get_config, get_ip, GenericUnpacker
11+
from other.utils import to_bytearray, get_config, get_external_ip, \
12+
GenericUnpacker
1213
from mh.database import Server, Gate, City
1314

15+
1416
class ItemType:
1517
Custom = 0
1618
Byte = 1
@@ -960,7 +962,7 @@ def get_fmp_servers(session, first_index, count):
960962
assert first_index > 0, "Invalid list index"
961963

962964
config = get_config("FMP")
963-
fmp_addr = get_ip(config["IP"])
965+
fmp_addr = get_external_ip(config)
964966
fmp_port = config["Port"]
965967

966968
data = b""

other/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Python 3
2424
basestring = str
2525
import configparser as ConfigParser
26+
from typing import Any # noqa: F401
2627

2728
CONFIG_FILE = "config.ini"
2829
LOG_FOLDER = "logs"
@@ -221,6 +222,7 @@ def get_config(name, config_file=CONFIG_FILE):
221222
config.read(config_file)
222223
return {
223224
"IP": config.get(name, "IP"),
225+
"ExternalIP": config.get(name, "ExternalIP"),
224226
"Port": config.getint(name, "Port"),
225227
"Name": config.get(name, "Name"),
226228
"MaxThread": config.getint(name, "MaxThread"),
@@ -239,19 +241,31 @@ def get_config(name, config_file=CONFIG_FILE):
239241

240242

241243
def get_default_ip():
244+
# type: () -> str
242245
"""Get the default IP address"""
243246
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
244247
s.connect(("8.8.8.8", 80))
245-
ip = s.getsockname()[0]
248+
ip = s.getsockname()[0] # type: str
246249
s.close()
247250
return ip
248251

249252

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

254258

259+
def get_external_ip(config):
260+
# type: (dict[str, Any]) -> str
261+
"""Return the IP address advertised by the server.
262+
263+
It's useful when the public IP address can't easily be retrieved.
264+
For instance, when behind a NAT or some cloud infrastructures.
265+
"""
266+
return config["ExternalIP"] or get_ip(config["IP"])
267+
268+
255269
def argparse_from_config(config):
256270
"""Argument parser from config."""
257271
import argparse

0 commit comments

Comments
 (0)