Browse Source

Converted to yaml for config file.

Steve Thielemann 5 years ago
parent
commit
04e04915e2
4 changed files with 89 additions and 11 deletions
  1. 13 0
      config_.yaml
  2. 13 0
      config_4dev.yaml
  3. 41 0
      convert.py
  4. 22 11
      tcp-proxy.py

+ 13 - 0
config_.yaml

@@ -0,0 +1,13 @@
+# Connect to:
+host: "twgs"
+port: 2002
+# Listen on:
+listen_port: 2002
+listen_on: "0.0.0.0"
+
+# Yes, save .raw files
+# raw: True
+logfile: True
+
+# log_lines: False
+log_lines: True

+ 13 - 0
config_4dev.yaml

@@ -0,0 +1,13 @@
+# Connect to:
+host: "127.0.0.1"
+port: 2002
+# Listen on:
+listen_port: 9999
+listen_on: "127.0.0.1"
+
+# Yes, save .raw files
+raw: True
+logfile: False
+
+# logfile: True
+log_lines: True

+ 41 - 0
convert.py

@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+import sys
+import os
+
+if len(sys.argv) == 1:
+    print("I need a config.py (filename) to convert.")
+    sys.exit(2)
+
+filename = sys.argv[1]
+
+print("Processing:", filename)
+
+yamlout = filename
+pos = yamlout.rindex('.')
+if pos != -1:
+    yamlout = yamlout[0:pos+1] + "yaml"
+else:
+    print("I can't determine the extension.", filename)
+    sys.exit(2)
+
+if os.path.exists(yamlout):
+    print("Sorry, the file {0} already exists.".format(yamlout))
+    sys.exit(2)
+
+print("Converting {0} to {1}...".format(filename, yamlout))
+with open(filename, 'r') as fp:
+    with open(yamlout, 'w') as fpout:
+        for line in fp:
+            line = line.strip()
+
+            if " = " in line:
+                var, _, data = line.partition('=')
+                var = var.strip().lower()
+                line = "{0}: {1}".format(var, data.strip())
+
+            print("{0}".format(line), file=fpout)
+
+print("Done")
+
+  

+ 22 - 11
tcp-proxy.py

@@ -11,18 +11,22 @@ from twisted.python import log
 from twisted.python.logfile import DailyLogFile
 import pendulum
 from subprocess import check_output
-
+import os
 from colorama import Fore, Back, Style
 
 from pprint import pformat
 
-# This isn't the best configuration, but it's simple
-# and works.  Mostly.
+import yaml
+
+def config_load(filename):
+    global config
+    with open(filename, 'r') as fp:
+        config = yaml.safe_load(fp)
 
-try:
-    from config_dev import *
-except ModuleNotFoundError:
-    from config import *
+if os.path.exists('config_dev.yaml'):
+    config_load('config_dev.yaml')
+else:
+    config_load('config.yaml')
 
 # Extract the version information from git.
 # The match gives us only tags starting with v[0-9]*  Using anything else trips up on double digits.
@@ -127,7 +131,7 @@ class Game(protocol.Protocol):
 
     def lineReceived(self, line):
         """ line received from the game. """
-        if LOG_LINES:
+        if 'log_lines' in config and config['log_lines']:
             log.msg("<< [{0}]".format(line))
 
         # if "TWGS v2.20b" in line and "www.eisonline.com" in line:
@@ -292,7 +296,7 @@ class Player(protocol.Protocol):
         self.glue = factory
 
         # Make connection to the game server
-        reactor.connectTCP(HOST, PORT, factory, 5)
+        reactor.connectTCP(config['host'], config['port'], factory, 5)
 
     def setGameReceived(self):
         """ Get deferred from client queue, callback clientDataReceived. """
@@ -382,7 +386,7 @@ class Player(protocol.Protocol):
 
 
 if __name__ == "__main__":
-    if LOGFILE:
+    if 'logfile' in config and config['logfile']:
         log.startLogging(DailyLogFile("proxy.log", "."))
     else:
         log.startLogging(sys.stdout)
@@ -390,5 +394,12 @@ if __name__ == "__main__":
     log.msg("This is version: %s" % version)
     factory = protocol.Factory()
     factory.protocol = Player
-    reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
+    reactor.listenTCP(config['listen_port'], factory, interface=config['listen_on'])
     reactor.run()
+else:
+    # I can't seem to get twistd -y tcp-proxy.py 
+    # to work.  Missing imports?
+    application = service.Application("TradeWarsGameServer-Proxy")
+    factory = protocol.Factory()
+    factory.protocol = Player
+    internet.TCPServer(config['listen_port'], factory).setServiceParent(application)