예제 #1
0
파일: shell.py 프로젝트: cultcode/zproxy
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
예제 #2
0
파일: remote.py 프로젝트: cultcode/zproxy
def svr_init():
  global myDes3Key, myDes3Iv, myDes3
  myDes3Key = shell.config['des3-key']
  myDes3Iv = shell.config['des3-iv']
  myDes3 = encrypt.myDes3Cipher(myDes3Key, myDes3Iv, DES3.MODE_CBC)

  payload = {'EpochTime':common.get_epochtime(),'NodeName':common.get_hostname(),'Version':common.get_version(),"SvrType":15}
  #payload = {'EpochTime':common.get_epochtime(),'NodeName':'CSDX-TintanCDN.15-143','Version':common.get_version(),"SvrType":15}
  payload = json.dumps(payload, encoding='UTF-8')
  logging.info('Sent:     '+payload)
  payload = myDes3.myEncrypt(payload)
  r = requests.post(shell.config['dbagent']+'/DBAgentSvr/SvrInit', data=payload)

  r.raise_for_status()

  content = myDes3.myDecrypt(r.text)
  logging.info('Received: '+content)
  decodejson = json.loads(content, encoding='UTF-8')

  if decodejson['Status'] == 0:
    logging.error('Failure status returned')
    sys.exit(1)
  elif decodejson['Status'] == 1:
    shell.config['nodeid'] = decodejson['NodeId']
  else:
    logging.error('Unknown status returned')
    sys.exit(1)

  return