1import argparse
2import sys
3import os
4import locale
5import re
6import configparser
7import logging
8import logging.handlers
9import stat
10
11
12class Config(object):
13
14    def __init__(self, argv):
15        self.version = "0.7.0"
16        self.rev = 4206
17        self.argv = argv
18        self.action = None
19        self.pending_changes = {}
20        self.need_restart = False
21        self.keys_api_change_allowed = set([
22            "tor", "fileserver_port", "language", "tor_use_bridges", "trackers_proxy", "trackers",
23            "trackers_file", "open_browser", "log_level", "fileserver_ip_type", "ip_external", "offline"
24        ])
25        self.keys_restart_need = set(["tor", "fileserver_port", "fileserver_ip_type"])
26        self.start_dir = self.getStartDir()
27
28        self.config_file = self.start_dir + "/zeronet.conf"
29        self.data_dir = self.start_dir + "/data"
30        self.log_dir = self.start_dir + "/log"
31
32        self.trackers_file = False
33        self.createParser()
34        self.createArguments()
35
36    def createParser(self):
37        # Create parser
38        self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
39        self.parser.register('type', 'bool', self.strToBool)
40        self.subparsers = self.parser.add_subparsers(title="Action to perform", dest="action")
41
42    def __str__(self):
43        return str(self.arguments).replace("Namespace", "Config")  # Using argparse str output
44
45    # Convert string to bool
46    def strToBool(self, v):
47        return v.lower() in ("yes", "true", "t", "1")
48
49    def getStartDir(self):
50        this_file = os.path.abspath(__file__).replace("\\", "/").rstrip("cd")
51
52        if this_file.endswith("/Contents/Resources/core/src/Config.py"):
53            # Running as ZeroNet.app
54            if this_file.startswith("/Application") or this_file.startswith("/private") or this_file.startswith(os.path.expanduser("~/Library")):
55                # Runnig from non-writeable directory, put data to Application Support
56                start_dir = os.path.expanduser("~/Library/Application Support/ZeroNet")
57            else:
58                # Running from writeable directory put data next to .app
59                start_dir = re.sub("/[^/]+/Contents/Resources/core/src/Config.py", "", this_file)
60        elif this_file.endswith("/core/src/Config.py"):
61            # Running as exe or source is at Application Support directory, put var files to outside of core dir
62            start_dir = this_file.replace("/core/src/Config.py", "")
63        elif this_file.endswith("usr/share/zeronet/src/Config.py"):
64            # Running from non-writeable location, e.g., AppImage
65            start_dir = os.path.expanduser("~/ZeroNet")
66        else:
67            start_dir = "."
68
69        return start_dir
70
71    # Create command line arguments
72    def createArguments(self):
73        trackers = [
74            "zero://boot3rdez4rzn36x.onion:15441",
75            "zero://zero.booth.moe#f36ca555bee6ba216b14d10f38c16f7769ff064e0e37d887603548cc2e64191d:443",  # US/NY
76            "udp://tracker.coppersurfer.tk:6969",  # DE
77            "udp://amigacity.xyz:6969",  # US/NY
78            "udp://104.238.198.186:8000",  # US/LA
79            "http://tracker01.loveapp.com:6789/announce",  # Google
80            "http://open.acgnxtracker.com:80/announce",  # DE
81            "http://open.trackerlist.xyz:80/announce",  # Cloudflare
82            "zero://2602:ffc5::c5b2:5360:26312"  # US/ATL
83        ]
84        # Platform specific
85        if sys.platform.startswith("win"):
86            coffeescript = "type %s | tools\\coffee\\coffee.cmd"
87        else:
88            coffeescript = None
89
90        try:
91            language, enc = locale.getdefaultlocale()
92            language = language.lower().replace("_", "-")
93            if language not in ["pt-br", "zh-tw"]:
94                language = language.split("-")[0]
95        except Exception:
96            language = "en"
97
98        use_openssl = True
99
100        if repr(1483108852.565) != "1483108852.565":  # Fix for weird Android issue
101            fix_float_decimals = True
102        else:
103            fix_float_decimals = False
104
105        config_file = self.start_dir + "/zeronet.conf"
106        data_dir = self.start_dir + "/data"
107        log_dir = self.start_dir + "/log"
108
109        ip_local = ["127.0.0.1", "::1"]
110
111        # Main
112        action = self.subparsers.add_parser("main", help='Start UiServer and FileServer (default)')
113
114        # SiteCreate
115        action = self.subparsers.add_parser("siteCreate", help='Create a new site')
116
117        # SiteNeedFile
118        action = self.subparsers.add_parser("siteNeedFile", help='Get a file from site')
119        action.add_argument('address', help='Site address')
120        action.add_argument('inner_path', help='File inner path')
121
122        # SiteDownload
123        action = self.subparsers.add_parser("siteDownload", help='Download a new site')
124        action.add_argument('address', help='Site address')
125
126        # SiteSign
127        action = self.subparsers.add_parser("siteSign", help='Update and sign content.json: address [privatekey]')
128        action.add_argument('address', help='Site to sign')
129        action.add_argument('privatekey', help='Private key (default: ask on execute)', nargs='?')
130        action.add_argument('--inner_path', help='File you want to sign (default: content.json)',
131                            default="content.json", metavar="inner_path")
132        action.add_argument('--remove_missing_optional', help='Remove optional files that is not present in the directory', action='store_true')
133        action.add_argument('--publish', help='Publish site after the signing', action='store_true')
134
135        # SitePublish
136        action = self.subparsers.add_parser("sitePublish", help='Publish site to other peers: address')
137        action.add_argument('address', help='Site to publish')
138        action.add_argument('peer_ip', help='Peer ip to publish (default: random peers ip from tracker)',
139                            default=None, nargs='?')
140        action.add_argument('peer_port', help='Peer port to publish (default: random peer port from tracker)',
141                            default=15441, nargs='?')
142        action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)',
143                            default="content.json", metavar="inner_path")
144
145        # SiteVerify
146        action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address')
147        action.add_argument('address', help='Site to verify')
148
149        # SiteCmd
150        action = self.subparsers.add_parser("siteCmd", help='Execute a ZeroFrame API command on a site')
151        action.add_argument('address', help='Site address')
152        action.add_argument('cmd', help='API command name')
153        action.add_argument('parameters', help='Parameters of the command', nargs='?')
154
155        # dbRebuild
156        action = self.subparsers.add_parser("dbRebuild", help='Rebuild site database cache')
157        action.add_argument('address', help='Site to rebuild')
158
159        # dbQuery
160        action = self.subparsers.add_parser("dbQuery", help='Query site sql cache')
161        action.add_argument('address', help='Site to query')
162        action.add_argument('query', help='Sql query')
163
164        # PeerPing
165        action = self.subparsers.add_parser("peerPing", help='Send Ping command to peer')
166        action.add_argument('peer_ip', help='Peer ip')
167        action.add_argument('peer_port', help='Peer port', nargs='?')
168
169        # PeerGetFile
170        action = self.subparsers.add_parser("peerGetFile", help='Request and print a file content from peer')
171        action.add_argument('peer_ip', help='Peer ip')
172        action.add_argument('peer_port', help='Peer port')
173        action.add_argument('site', help='Site address')
174        action.add_argument('filename', help='File name to request')
175        action.add_argument('--benchmark', help='Request file 10x then displays the total time', action='store_true')
176
177        # PeerCmd
178        action = self.subparsers.add_parser("peerCmd", help='Request and print a file content from peer')
179        action.add_argument('peer_ip', help='Peer ip')
180        action.add_argument('peer_port', help='Peer port')
181        action.add_argument('cmd', help='Command to execute')
182        action.add_argument('parameters', help='Parameters to command', nargs='?')
183
184        # CryptSign
185        action = self.subparsers.add_parser("cryptSign", help='Sign message using Bitcoin private key')
186        action.add_argument('message', help='Message to sign')
187        action.add_argument('privatekey', help='Private key')
188
189        # Crypt Verify
190        action = self.subparsers.add_parser("cryptVerify", help='Verify message using Bitcoin public address')
191        action.add_argument('message', help='Message to verify')
192        action.add_argument('sign', help='Signiture for message')
193        action.add_argument('address', help='Signer\'s address')
194
195        # Crypt GetPrivatekey
196        action = self.subparsers.add_parser("cryptGetPrivatekey", help='Generate a privatekey from master seed')
197        action.add_argument('master_seed', help='Source master seed')
198        action.add_argument('site_address_index', help='Site address index', type=int)
199
200        action = self.subparsers.add_parser("getConfig", help='Return json-encoded info')
201        action = self.subparsers.add_parser("testConnection", help='Testing')
202        action = self.subparsers.add_parser("testAnnounce", help='Testing')
203
204        # Config parameters
205        self.parser.add_argument('--verbose', help='More detailed logging', action='store_true')
206        self.parser.add_argument('--debug', help='Debug mode', action='store_true')
207        self.parser.add_argument('--silent', help='Only log errors to terminal output', action='store_true')
208        self.parser.add_argument('--debug_socket', help='Debug socket connections', action='store_true')
209        self.parser.add_argument('--merge_media', help='Merge all.js and all.css', action='store_true')
210
211        self.parser.add_argument('--batch', help="Batch mode (No interactive input for commands)", action='store_true')
212
213        self.parser.add_argument('--config_file', help='Path of config file', default=config_file, metavar="path")
214        self.parser.add_argument('--data_dir', help='Path of data directory', default=data_dir, metavar="path")
215
216        self.parser.add_argument('--console_log_level', help='Level of logging to console', default="default", choices=["default", "DEBUG", "INFO", "ERROR", "off"])
217
218        self.parser.add_argument('--log_dir', help='Path of logging directory', default=log_dir, metavar="path")
219        self.parser.add_argument('--log_level', help='Level of logging to file', default="DEBUG", choices=["DEBUG", "INFO", "ERROR", "off"])
220        self.parser.add_argument('--log_rotate', help='Log rotate interval', default="daily", choices=["hourly", "daily", "weekly", "off"])
221        self.parser.add_argument('--log_rotate_backup_count', help='Log rotate backup count', default=5, type=int)
222
223        self.parser.add_argument('--language', help='Web interface language', default=language, metavar='language')
224        self.parser.add_argument('--ui_ip', help='Web interface bind address', default="127.0.0.1", metavar='ip')
225        self.parser.add_argument('--ui_port', help='Web interface bind port', default=43110, type=int, metavar='port')
226        self.parser.add_argument('--ui_restrict', help='Restrict web access', default=False, metavar='ip', nargs='*')
227        self.parser.add_argument('--ui_host', help='Allow access using this hosts', metavar='host', nargs='*')
228        self.parser.add_argument('--ui_trans_proxy', help='Allow access using a transparent proxy', action='store_true')
229
230        self.parser.add_argument('--open_browser', help='Open homepage in web browser automatically',
231                                 nargs='?', const="default_browser", metavar='browser_name')
232        self.parser.add_argument('--homepage', help='Web interface Homepage', default='1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',
233                                 metavar='address')
234        self.parser.add_argument('--updatesite', help='Source code update site', default='1uPDaT3uSyWAPdCv1WkMb5hBQjWSNNACf',
235                                 metavar='address')
236        self.parser.add_argument('--dist_type', help='Type of installed distribution', default='source')
237
238        self.parser.add_argument('--size_limit', help='Default site size limit in MB', default=10, type=int, metavar='limit')
239        self.parser.add_argument('--file_size_limit', help='Maximum per file size limit in MB', default=10, type=int, metavar='limit')
240        self.parser.add_argument('--connected_limit', help='Max connected peer per site', default=8, type=int, metavar='connected_limit')
241        self.parser.add_argument('--global_connected_limit', help='Max connections', default=512, type=int, metavar='global_connected_limit')
242        self.parser.add_argument('--workers', help='Download workers per site', default=5, type=int, metavar='workers')
243
244        self.parser.add_argument('--fileserver_ip', help='FileServer bind address', default="*", metavar='ip')
245        self.parser.add_argument('--fileserver_port', help='FileServer bind port (0: randomize)', default=0, type=int, metavar='port')
246        self.parser.add_argument('--fileserver_port_range', help='FileServer randomization range', default="10000-40000", metavar='port')
247        self.parser.add_argument('--fileserver_ip_type', help='FileServer ip type', default="dual", choices=["ipv4", "ipv6", "dual"])
248        self.parser.add_argument('--ip_local', help='My local ips', default=ip_local, type=int, metavar='ip', nargs='*')
249        self.parser.add_argument('--ip_external', help='Set reported external ip (tested on start if None)', metavar='ip', nargs='*')
250        self.parser.add_argument('--offline', help='Disable network communication', action='store_true')
251
252        self.parser.add_argument('--disable_udp', help='Disable UDP connections', action='store_true')
253        self.parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port')
254        self.parser.add_argument('--bind', help='Bind outgoing sockets to this address', metavar='ip')
255        self.parser.add_argument('--trackers', help='Bootstraping torrent trackers', default=trackers, metavar='protocol://address', nargs='*')
256        self.parser.add_argument('--trackers_file', help='Load torrent trackers dynamically from a file', metavar='path', nargs='*')
257        self.parser.add_argument('--trackers_proxy', help='Force use proxy to connect to trackers (disable, tor, ip:port)', default="disable")
258        self.parser.add_argument('--use_libsecp256k1', help='Use Libsecp256k1 liblary for speedup', type='bool', choices=[True, False], default=True)
259        self.parser.add_argument('--use_openssl', help='Use OpenSSL liblary for speedup', type='bool', choices=[True, False], default=True)
260        self.parser.add_argument('--disable_db', help='Disable database updating', action='store_true')
261        self.parser.add_argument('--disable_encryption', help='Disable connection encryption', action='store_true')
262        self.parser.add_argument('--force_encryption', help="Enforce encryption to all peer connections", action='store_true')
263        self.parser.add_argument('--disable_sslcompression', help='Disable SSL compression to save memory',
264                                 type='bool', choices=[True, False], default=True)
265        self.parser.add_argument('--keep_ssl_cert', help='Disable new SSL cert generation on startup', action='store_true')
266        self.parser.add_argument('--max_files_opened', help='Change maximum opened files allowed by OS to this value on startup',
267                                 default=2048, type=int, metavar='limit')
268        self.parser.add_argument('--stack_size', help='Change thread stack size', default=None, type=int, metavar='thread_stack_size')
269        self.parser.add_argument('--use_tempfiles', help='Use temporary files when downloading (experimental)',
270                                 type='bool', choices=[True, False], default=False)
271        self.parser.add_argument('--stream_downloads', help='Stream download directly to files (experimental)',
272                                 type='bool', choices=[True, False], default=False)
273        self.parser.add_argument("--msgpack_purepython", help='Use less memory, but a bit more CPU power',
274                                 type='bool', choices=[True, False], default=False)
275        self.parser.add_argument("--fix_float_decimals", help='Fix content.json modification date float precision on verification',
276                                 type='bool', choices=[True, False], default=fix_float_decimals)
277        self.parser.add_argument("--db_mode", choices=["speed", "security"], default="speed")
278        self.parser.add_argument("--download_optional", choices=["manual", "auto"], default="manual")
279
280        self.parser.add_argument('--coffeescript_compiler', help='Coffeescript compiler for developing', default=coffeescript,
281                                 metavar='executable_path')
282
283        self.parser.add_argument('--tor', help='enable: Use only for Tor peers, always: Use Tor for every connection', choices=["disable", "enable", "always"], default='enable')
284        self.parser.add_argument('--tor_controller', help='Tor controller address', metavar='ip:port', default='127.0.0.1:9051')
285        self.parser.add_argument('--tor_proxy', help='Tor proxy address', metavar='ip:port', default='127.0.0.1:9050')
286        self.parser.add_argument('--tor_password', help='Tor controller password', metavar='password')
287        self.parser.add_argument('--tor_use_bridges', help='Use obfuscated bridge relays to avoid Tor block', action='store_true')
288        self.parser.add_argument('--tor_hs_limit', help='Maximum number of hidden services in Tor always mode', metavar='limit', type=int, default=10)
289        self.parser.add_argument('--tor_hs_port', help='Hidden service port in Tor always mode', metavar='limit', type=int, default=15441)
290
291        self.parser.add_argument('--version', action='version', version='ZeroNet %s r%s' % (self.version, self.rev))
292        self.parser.add_argument('--end', help='Stop multi value argument parsing', action='store_true')
293
294        return self.parser
295
296    def loadTrackersFile(self):
297        if not self.trackers_file:
298            return None
299
300        self.trackers = self.arguments.trackers[:]
301
302        for trackers_file in self.trackers_file:
303            try:
304                if trackers_file.startswith("/"):  # Absolute
305                    trackers_file_path = trackers_file
306                elif trackers_file.startswith("{data_dir}"):  # Relative to data_dir
307                    trackers_file_path = trackers_file.replace("{data_dir}", self.data_dir)
308                else:  # Relative to zeronet.py
309                    trackers_file_path = self.start_dir + "/" + trackers_file
310
311                for line in open(trackers_file_path):
312                    tracker = line.strip()
313                    if "://" in tracker and tracker not in self.trackers:
314                        self.trackers.append(tracker)
315            except Exception as err:
316                print("Error loading trackers file: %s" % err)
317
318    # Find arguments specified for current action
319    def getActionArguments(self):
320        back = {}
321        arguments = self.parser._subparsers._group_actions[0].choices[self.action]._actions[1:]  # First is --version
322        for argument in arguments:
323            back[argument.dest] = getattr(self, argument.dest)
324        return back
325
326    # Try to find action from argv
327    def getAction(self, argv):
328        actions = [list(action.choices.keys()) for action in self.parser._actions if action.dest == "action"][0]  # Valid actions
329        found_action = False
330        for action in actions:  # See if any in argv
331            if action in argv:
332                found_action = action
333                break
334        return found_action
335
336    # Move plugin parameters to end of argument list
337    def moveUnknownToEnd(self, argv, default_action):
338        valid_actions = sum([action.option_strings for action in self.parser._actions], [])
339        valid_parameters = []
340        plugin_parameters = []
341        plugin = False
342        for arg in argv:
343            if arg.startswith("--"):
344                if arg not in valid_actions:
345                    plugin = True
346                else:
347                    plugin = False
348            elif arg == default_action:
349                plugin = False
350
351            if plugin:
352                plugin_parameters.append(arg)
353            else:
354                valid_parameters.append(arg)
355        return valid_parameters + plugin_parameters
356
357    # Parse arguments from config file and command line
358    def parse(self, silent=False, parse_config=True):
359        if silent:  # Don't display messages or quit on unknown parameter
360            original_print_message = self.parser._print_message
361            original_exit = self.parser.exit
362
363            def silencer(parser, function_name):
364                parser.exited = True
365                return None
366            self.parser.exited = False
367            self.parser._print_message = lambda *args, **kwargs: silencer(self.parser, "_print_message")
368            self.parser.exit = lambda *args, **kwargs: silencer(self.parser, "exit")
369
370        argv = self.argv[:]  # Copy command line arguments
371        self.parseCommandline(argv, silent)  # Parse argv
372        self.setAttributes()
373        if parse_config:
374            argv = self.parseConfig(argv)  # Add arguments from config file
375
376        self.parseCommandline(argv, silent)  # Parse argv
377        self.setAttributes()
378
379        if not silent:
380            if self.fileserver_ip != "*" and self.fileserver_ip not in self.ip_local:
381                self.ip_local.append(self.fileserver_ip)
382
383        if silent:  # Restore original functions
384            if self.parser.exited and self.action == "main":  # Argument parsing halted, don't start ZeroNet with main action
385                self.action = None
386            self.parser._print_message = original_print_message
387            self.parser.exit = original_exit
388
389        self.loadTrackersFile()
390
391    # Parse command line arguments
392    def parseCommandline(self, argv, silent=False):
393        # Find out if action is specificed on start
394        action = self.getAction(argv)
395        if not action:
396            argv.append("--end")
397            argv.append("main")
398            action = "main"
399        argv = self.moveUnknownToEnd(argv, action)
400        if silent:
401            res = self.parser.parse_known_args(argv[1:])
402            if res:
403                self.arguments = res[0]
404            else:
405                self.arguments = {}
406        else:
407            self.arguments = self.parser.parse_args(argv[1:])
408
409    # Parse config file
410    def parseConfig(self, argv):
411        # Find config file path from parameters
412        if "--config_file" in argv:
413            self.config_file = argv[argv.index("--config_file") + 1]
414        # Load config file
415        if os.path.isfile(self.config_file):
416            config = configparser.RawConfigParser(allow_no_value=True, strict=False)
417            config.read(self.config_file)
418            for section in config.sections():
419                for key, val in config.items(section):
420                    if val == "True":
421                        val = None
422                    if section != "global":  # If not global prefix key with section
423                        key = section + "_" + key
424
425                    if key == "open_browser":  # Prefer config file value over cli argument
426                        while "--%s" % key in argv:
427                            pos = argv.index("--open_browser")
428                            del argv[pos:pos + 2]
429
430                    argv_extend = ["--%s" % key]
431                    if val:
432                        for line in val.strip().split("\n"):  # Allow multi-line values
433                            argv_extend.append(line)
434                        if "\n" in val:
435                            argv_extend.append("--end")
436
437                    argv = argv[:1] + argv_extend + argv[1:]
438        return argv
439
440    # Expose arguments as class attributes
441    def setAttributes(self):
442        # Set attributes from arguments
443        if self.arguments:
444            args = vars(self.arguments)
445            for key, val in args.items():
446                if type(val) is list:
447                    val = val[:]
448                if key in ("data_dir", "log_dir"):
449                    val = val.replace("\\", "/")
450                setattr(self, key, val)
451
452    def loadPlugins(self):
453        from Plugin import PluginManager
454
455        @PluginManager.acceptPlugins
456        class ConfigPlugin(object):
457            def __init__(self, config):
458                self.parser = config.parser
459                self.createArguments()
460
461            def createArguments(self):
462                pass
463
464        ConfigPlugin(self)
465
466    def saveValue(self, key, value):
467        if not os.path.isfile(self.config_file):
468            content = ""
469        else:
470            content = open(self.config_file).read()
471        lines = content.splitlines()
472
473        global_line_i = None
474        key_line_i = None
475        i = 0
476        for line in lines:
477            if line.strip() == "[global]":
478                global_line_i = i
479            if line.startswith(key + " =") or line == key:
480                key_line_i = i
481            i += 1
482
483        if key_line_i and len(lines) > key_line_i + 1:
484            while True:  # Delete previous multiline values
485                is_value_line = lines[key_line_i + 1].startswith(" ") or lines[key_line_i + 1].startswith("\t")
486                if not is_value_line:
487                    break
488                del lines[key_line_i + 1]
489
490        if value is None:  # Delete line
491            if key_line_i:
492                del lines[key_line_i]
493
494        else:  # Add / update
495            if type(value) is list:
496                value_lines = [""] + [str(line).replace("\n", "").replace("\r", "") for line in value]
497            else:
498                value_lines = [str(value).replace("\n", "").replace("\r", "")]
499            new_line = "%s = %s" % (key, "\n ".join(value_lines))
500            if key_line_i:  # Already in the config, change the line
501                lines[key_line_i] = new_line
502            elif global_line_i is None:  # No global section yet, append to end of file
503                lines.append("[global]")
504                lines.append(new_line)
505            else:  # Has global section, append the line after it
506                lines.insert(global_line_i + 1, new_line)
507
508        open(self.config_file, "w").write("\n".join(lines))
509
510    def getServerInfo(self):
511        from Plugin import PluginManager
512        import main
513
514        info = {
515            "platform": sys.platform,
516            "fileserver_ip": self.fileserver_ip,
517            "fileserver_port": self.fileserver_port,
518            "ui_ip": self.ui_ip,
519            "ui_port": self.ui_port,
520            "version": self.version,
521            "rev": self.rev,
522            "language": self.language,
523            "debug": self.debug,
524            "plugins": PluginManager.plugin_manager.plugin_names,
525
526            "log_dir": os.path.abspath(self.log_dir),
527            "data_dir": os.path.abspath(self.data_dir),
528            "src_dir": os.path.dirname(os.path.abspath(__file__))
529        }
530
531        try:
532            info["ip_external"] = main.file_server.port_opened
533            info["tor_enabled"] = main.file_server.tor_manager.enabled
534            info["tor_status"] = main.file_server.tor_manager.status
535        except Exception:
536            pass
537
538        return info
539
540    def initConsoleLogger(self):
541        if self.action == "main":
542            format = '[%(asctime)s] %(name)s %(message)s'
543        else:
544            format = '%(name)s %(message)s'
545
546        if self.console_log_level == "default":
547            if self.silent:
548                level = logging.ERROR
549            elif self.debug:
550                level = logging.DEBUG
551            else:
552                level = logging.INFO
553        else:
554            level = logging.getLevelName(self.console_log_level)
555
556        console_logger = logging.StreamHandler()
557        console_logger.setFormatter(logging.Formatter(format, "%H:%M:%S"))
558        console_logger.setLevel(level)
559        logging.getLogger('').addHandler(console_logger)
560
561    def initFileLogger(self):
562        if self.action == "main":
563            log_file_path = "%s/debug.log" % self.log_dir
564        else:
565            log_file_path = "%s/cmd.log" % self.log_dir
566
567        if self.log_rotate == "off":
568            file_logger = logging.FileHandler(log_file_path, "w", "utf-8")
569        else:
570            when_names = {"weekly": "w", "daily": "d", "hourly": "h"}
571            file_logger = logging.handlers.TimedRotatingFileHandler(
572                log_file_path, when=when_names[self.log_rotate], interval=1, backupCount=self.log_rotate_backup_count,
573                encoding="utf8"
574            )
575
576            if os.path.isfile(log_file_path):
577                file_logger.doRollover()  # Always start with empty log file
578        file_logger.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)-8s %(name)s %(message)s'))
579        file_logger.setLevel(logging.getLevelName(self.log_level))
580        logging.getLogger('').setLevel(logging.getLevelName(self.log_level))
581        logging.getLogger('').addHandler(file_logger)
582
583    def initLogging(self, console_logging=None, file_logging=None):
584        if console_logging == None:
585            console_logging = self.console_log_level != "off"
586
587        if file_logging == None:
588            file_logging = self.log_level != "off"
589
590        # Create necessary files and dirs
591        if not os.path.isdir(self.log_dir):
592            os.mkdir(self.log_dir)
593            try:
594                os.chmod(self.log_dir, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
595            except Exception as err:
596                print("Can't change permission of %s: %s" % (self.log_dir, err))
597
598        # Make warning hidden from console
599        logging.WARNING = 15  # Don't display warnings if not in debug mode
600        logging.addLevelName(15, "WARNING")
601
602        logging.getLogger('').name = "-"  # Remove root prefix
603        logging.getLogger("geventwebsocket.handler").setLevel(logging.WARNING)  # Don't log ws debug messages
604
605        if console_logging:
606            self.initConsoleLogger()
607        if file_logging:
608            self.initFileLogger()
609
610config = Config(sys.argv)
611