add automatic port forwarding
This commit is contained in:
parent
19f6abc87c
commit
b8e8e3904a
|
@ -568,6 +568,33 @@ async def console(ctx : Context):
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
def forward_port(port: int):
|
||||||
|
import upnpy
|
||||||
|
import socket
|
||||||
|
|
||||||
|
upnp = upnpy.UPnP()
|
||||||
|
upnp.discover()
|
||||||
|
device = upnp.get_igd()
|
||||||
|
|
||||||
|
service = device['WANPPPConnection.1']
|
||||||
|
|
||||||
|
#get own lan IP
|
||||||
|
ip = socket.gethostbyname(socket.gethostname())
|
||||||
|
|
||||||
|
# This specific action returns an empty dict: {}
|
||||||
|
service.AddPortMapping(
|
||||||
|
NewRemoteHost='',
|
||||||
|
NewExternalPort=port,
|
||||||
|
NewProtocol='TCP',
|
||||||
|
NewInternalPort=port,
|
||||||
|
NewInternalClient=ip,
|
||||||
|
NewEnabled=1,
|
||||||
|
NewPortMappingDescription='Berserker\s Multiworld',
|
||||||
|
NewLeaseDuration=60*60*24#24 hours
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.info(f"Attempted to forward port {port} to {ip}, your local ip address.")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
@ -581,13 +608,18 @@ async def main():
|
||||||
parser.add_argument('--location_check_points', default=1, type=int)
|
parser.add_argument('--location_check_points', default=1, type=int)
|
||||||
parser.add_argument('--hint_cost', default=1000, type=int)
|
parser.add_argument('--hint_cost', default=1000, type=int)
|
||||||
parser.add_argument('--disable_item_cheat', default=False, action='store_true')
|
parser.add_argument('--disable_item_cheat', default=False, action='store_true')
|
||||||
|
parser.add_argument('--disable_port_forward', default=False, action='store_true')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
file_options = Utils.parse_yaml(open("host.yaml").read())["server_options"]
|
file_options = Utils.parse_yaml(open("host.yaml").read())["server_options"]
|
||||||
for key, value in file_options.items():
|
for key, value in file_options.items():
|
||||||
if value is not None:
|
if value is not None:
|
||||||
setattr(args, key, value)
|
setattr(args, key, value)
|
||||||
logging.basicConfig(format='[%(asctime)s] %(message)s', level=getattr(logging, args.loglevel.upper(), logging.INFO))
|
logging.basicConfig(format='[%(asctime)s] %(message)s', level=getattr(logging, args.loglevel.upper(), logging.INFO))
|
||||||
|
if not args.disable_port_forward:
|
||||||
|
try:
|
||||||
|
forward_port(args.port)
|
||||||
|
except:
|
||||||
|
logging.exception("Automatic port forwarding failed with:")
|
||||||
ctx = Context(args.host, args.port, args.password, args.location_check_points, args.hint_cost,
|
ctx = Context(args.host, args.port, args.password, args.location_check_points, args.hint_cost,
|
||||||
not args.disable_item_cheat)
|
not args.disable_item_cheat)
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ server_options:
|
||||||
savefile: null
|
savefile: null
|
||||||
disable_save: null
|
disable_save: null
|
||||||
loglevel: null
|
loglevel: null
|
||||||
|
#disallow automatically forwarding the port that is used, then closing that port on shutdown
|
||||||
|
disable_port_forward: null
|
||||||
#Disallow !getitem. Old /getitem cannot be blocked this way
|
#Disallow !getitem. Old /getitem cannot be blocked this way
|
||||||
disable_item_cheat: null
|
disable_item_cheat: null
|
||||||
#Client hint system
|
#Client hint system
|
||||||
|
|
|
@ -5,3 +5,4 @@ PyYAML>=5.3
|
||||||
collections_extended>=1.0.3
|
collections_extended>=1.0.3
|
||||||
fuzzywuzzy>=0.18.0
|
fuzzywuzzy>=0.18.0
|
||||||
bsdiff4>=1.1.9
|
bsdiff4>=1.1.9
|
||||||
|
upnpy>=1.1.5
|
Loading…
Reference in New Issue