Пример #1
0
def check_config(config):
  if 'local_port' in config:
    config['local_port'] = int(config['local_port'])

  if 'server_port' in config and type(config['server_port']) != list:
    config['server_port'] = int(config['server_port'])

  if config.get('local_address', '') in [b'0.0.0.0']:
    logging.warn('warning: local set to listen on 0.0.0.0, it\'s not safe')

  if config.get('server_address', '') in ['127.0.0.1', 'localhost']:
    logging.warn('warning: server address set to listen on %s:%s, are you sure?' %
           (to_str(config['server_address']), config['server_port']))

  if config.get('identity', None) is None:
    logging.error('identity can\'t default to none')
    sys.exit(1)

  if config.get('dbagent', None) is None:
    logging.error('dbagent can\'t default to none')
    sys.exit(1)
Пример #2
0
def get_config():
  global verbose, config

  logging.basicConfig(level=logging.INFO,
                      format='%(levelname)-s: %(message)s')

  shortopts = 'hc:vqs:p:l:a:bd:k:i:g:n:e:m:t:'
  longopts = ['help', 'version', 'log-file']

  try:
    config_path = find_config()
    optlist, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
    for key, value in optlist:
      if key == '-c':
        config_path = value

    if config_path:
      logging.info('loading config from %s' % config_path)
      with open(config_path, 'rb') as f:
        try:
          config = json.loads(f.read().decode('utf8'))
        except ValueError as e:
          logging.error('found an error in config.json: %s',
                         e.message)
          sys.exit(1)
    else:
      config = {}

    v_count = 0
    for key, value in optlist:
      if key == '-s':
        config['server_address'] = to_str(value)
      elif key == '-p':
          config['server_port'] = int(value)
      elif key == '-l':
        config['local_port'] = int(value)
      elif key == '-a':
        config['local_address'] = to_str(value)
      elif key == '-b':
        config['barrier'] = True
      elif key == '-d':
        config['identity'] = to_str(value)
      elif key == '-v':
        v_count += 1
        # '-vv' turns on more verbose mode
        config['verbose'] = v_count
      elif key in ('-h', '--help'):
        print_help()
        sys.exit(0)
      elif key == '--version':
        print(get_version())
        sys.exit(0)
      elif key == '--log-file':
        config['log-file'] = to_str(value)
      elif key == '-k':
        config['des3-key'] = to_str(value)
      elif key == '-i':
        config['des3-iv'] = to_str(value)
      elif key == '-g':
        config['dbagent'] = to_str(value)
      elif key == '-n':
        config['nodeid'] = int(value)
      elif key == '-e':
        config['encrypt'] = int(value)
      elif key == '-m':
        config['mobile'] = to_str(value)
      elif key == '-t':
        config['timeout_hb'] = int(value)
      elif key == '-q':
        v_count -= 1
        config['verbose'] = v_count
  except getopt.GetoptError as e:
    print(e, file=sys.stderr)
    print_help()
    sys.exit(2)

  if not config:
    logging.error('config not specified')
    print_help()
    sys.exit(2)

  config['log-file'] = config.get('log-file', '/var/log/zproxy.log')
  config['verbose'] = config.get('verbose', 0)
  config['local_address'] = to_str(config.get('local_address', '127.0.0.1'))
  config['local_port'] = config.get('local_port', 7070)
  config['server_address'] = to_str(config.get('server_address', '127.0.0.1'))
  config['server_port'] = config.get('server_port', 2181)
  config['des3-key'] = to_str(config.get('des3-key', 'D^=^vGfAdUTixobQP$HhsTsa'))
  config['des3-iv'] = to_str(config.get('des3-iv', 'aVtsvC#S'))
  config['barrier'] = config.get('barrier', False)
  config['nodeid'] = config.get('nodeid', 0)
  config['encrypt'] = config.get('encrypt', 1)
  config['mobile'] = to_str(config.get('mobile', ""))
  config['timeout_hb'] = config.get('timeout_hb', 60)

  check_config(config)

  logging.getLogger('').handlers = []

  logging.addLevelName(VERBOSE_LEVEL, 'VERBOSE')

  if config['verbose'] >= 2:
    level = VERBOSE_LEVEL
  elif config['verbose'] == 1:
    level = logging.DEBUG
  elif config['verbose'] == -1:
    level = logging.WARN
  elif config['verbose'] <= -2:
    level = logging.ERROR
  else:
    level = logging.INFO

  logging.basicConfig(level=level,
                      format='%(asctime)s %(levelname)-8s %(filename)s %(funcName)s %(lineno)s %(message)s',
                      datefmt='%Y-%m-%d %H:%M:%S',
                      #filename=config['log-file'],
                      filemode='w'
                      ),

  verbose = config['verbose']

  return config