Browse Source

added flag '--no-ssl-cert-validation'

Fabian Peter Hammerle 8 years ago
parent
commit
3901875c1e
3 changed files with 40 additions and 20 deletions
  1. 25 14
      scripts/tooncher
  2. 2 2
      setup.py
  3. 13 4
      tooncher/__init__.py

+ 25 - 14
scripts/tooncher

@@ -6,7 +6,8 @@ import sys
 import tooncher
 import yaml
 
-def run(username, config_path, engine_path = None):
+
+def run(username, config_path, engine_path=None, validate_ssl_certs=True):
 
     if os.path.exists(config_path):
         with open(config_path) as f:
@@ -14,9 +15,9 @@ def run(username, config_path, engine_path = None):
     else:
         config = {}
 
-    if engine_path is None:
-        engine_path = config['engine_path'] if 'engine_path' in config else None
-    if engine_path is None:
+    if engine_path is None and 'engine_path' in config:
+        engine_path = config['engine_path']
+    else:
         raise Exception('missing path to toontown engine')
 
     accounts = config['accounts'] if 'accounts' in config else []
@@ -24,26 +25,36 @@ def run(username, config_path, engine_path = None):
     for account in accounts:
         if account['username'] == username:
             tooncher.launch(
-                engine_path = engine_path,
-                username = account['username'],
-                password = account['password'],
-                )
+                engine_path=engine_path,
+                username=account['username'],
+                password=account['password'],
+                validate_ssl_certs=validate_ssl_certs,
+            )
+
 
 def _init_argparser():
 
     import argparse
-    argparser = argparse.ArgumentParser(description = None)
+    argparser = argparse.ArgumentParser(description=None)
     argparser.add_argument('username')
     argparser.add_argument(
         '--config',
         '-c',
-        metavar = 'path',
-        dest = 'config_path',
-        help = 'path to config file (default: %(default)s)',
-        default = os.path.join(os.path.expanduser('~'), '.tooncher'),
-        )
+        metavar='path',
+        dest='config_path',
+        help='path to config file (default: %(default)s)',
+        default=os.path.join(os.path.expanduser('~'), '.tooncher'),
+    )
+    argparser.add_argument(
+        '--no-ssl-cert-validation',
+        '-k',
+        dest='validate_ssl_certs',
+        help='do not validate ssl certificates',
+        action='store_false',
+    )
     return argparser
 
+
 def main(argv):
 
     argparser = _init_argparser()

+ 2 - 2
setup.py

@@ -7,12 +7,12 @@ import glob
 setup(
     name = 'tooncher',
     packages = ['tooncher'],
-    version = '0.1.1',
+    version = '0.1.2',
     description = "automates toontown rewritten's login process",
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',
     url = 'https://github.com/fphammerle/tooncher',
-    download_url = 'https://github.com/fphammerle/tooncher/tarball/0.1.1',
+    download_url = 'https://github.com/fphammerle/tooncher/tarball/0.1.2',
     keywords = ['game', 'launcher', 'toontown rewritten', 'ttr'],
     classifiers = [],
     scripts = glob.glob('scripts/*'),

+ 13 - 4
tooncher/__init__.py

@@ -1,5 +1,6 @@
 import json
 import os
+import ssl
 import subprocess
 import sys
 import urllib.parse
@@ -41,11 +42,13 @@ def start_engine(engine_path, gameserver, playcookie, **kwargs):
     )
 
 
-def api_request(url, params=None):
+def api_request(url, params=None, validate_ssl_cert=True):
     resp = urllib.request.urlopen(
         url=url,
         data=urllib.parse.urlencode(params).encode('ascii')
             if params else None,
+        context=None if validate_ssl_cert
+            else ssl._create_unverified_context(),
     )
     return json.loads(resp.read().decode('ascii'))
 
@@ -63,7 +66,8 @@ class LoginDelayed:
         self.queue_token = queue_token
 
 
-def login(username=None, password=None, queue_token=None):
+def login(username=None, password=None,
+          queue_token=None, validate_ssl_cert=True):
     if username is not None and queue_token is None:
         assert password is not None
         req_params = {
@@ -79,6 +83,7 @@ def login(username=None, password=None, queue_token=None):
     resp_data = api_request(
         url=LOGIN_API_URL,
         params=req_params,
+        validate_ssl_cert=validate_ssl_cert,
     )
     if resp_data['success'] == 'true':
         return LoginSuccessful(
@@ -93,13 +98,17 @@ def login(username=None, password=None, queue_token=None):
         raise Exception(repr(resp_data))
 
 
-def launch(engine_path, username, password):
+def launch(engine_path, username, password, validate_ssl_certs=True):
     result = login(
         username=username,
         password=password,
+        validate_ssl_cert=validate_ssl_certs,
     )
     if isinstance(result, LoginDelayed):
-        result = login(queue_token=result.queue_token)
+        result = login(
+            queue_token=result.queue_token,
+            validate_ssl_cert=validate_ssl_certs,
+        )
     if isinstance(result, LoginSuccessful):
         p = start_engine(
             engine_path=engine_path,