def __init__(self): self.TOKEN = get_config().TOKEN self.WEBAPI_URL = get_config().WEBAPI_URL self.session_pool = requests.Session() http_adapter = HTTPAdapter(max_retries=Retry( total=3, method_whitelist=frozenset(['GET', 'POST', 'PUT']))) self.session_pool.mount('https://', http_adapter)
def del_server(self, port): port = int(port) logging.info("del server at %d" % port) try: udpsock = socket(AF_INET, SOCK_DGRAM) udpsock.sendto('%s:%s:0:0' % (get_config().MANAGE_PASS, port), (get_config().MANAGE_BIND_IP, get_config().MANAGE_PORT)) udpsock.close() except Exception as e: logging.warn(e) return True
def pull_db_all_user(self): ''' 拉取符合要求的用户信息 ''' global webapi # api = EhcoApi() node_id = get_config().NODE_ID # 获取节点流量比例信息 nodeinfo = webapi.getApi('/nodes/{}'.format(node_id)) if not nodeinfo: logging.warn( '没有查询到满足要求的节点,请检查自己的node_id!,或者该节点流量已经用光,当前节点ID:{}'.format( node_id)) rows = [] return rows logging.info('节点id: {} 流量比例: {}'.format(node_id, nodeinfo[0])) # 流量比例设置 self.cfg['transfer_mul'] = float(nodeinfo[0]) # 获取符合条件的用户信息 data = webapi.getApi('/users/nodes/{}'.format(node_id)) if not data: rows = [] logging.warn('没有查询到满足要求的user,请检查自己的node_id!') return rows rows = data return rows
def load_cfg(self): import json config_path = get_config().MYSQL_CONFIG cfg = None with open(config_path, 'rb+') as f: cfg = json.loads(f.read().decode('utf8')) if cfg: self.cfg.update(cfg)
def thread_db(obj): import socket import time global db_instance global webapi timeout = 60 socket.setdefaulttimeout(timeout) last_rows = [] db_instance = obj() ServerPool.get_instance() shell.log_shadowsocks_version() webapi = EhcoApi() try: import resource logging.info( 'current process RLIMIT_NOFILE resource: soft %d hard %d' % resource.getrlimit(resource.RLIMIT_NOFILE)) except: pass try: while True: load_config() db_instance.load_cfg() try: db_instance.push_db_all_user() rows = db_instance.pull_db_all_user() if rows: db_instance.pull_ok = True config = shell.get_config(False) # 单端口多用户的支持 for port in config['additional_ports']: val = config['additional_ports'][port] val['port'] = int(port) val['enable'] = 1 val['transfer_enable'] = 1024**7 val['u'] = 0 val['d'] = 0 if "password" in val: val["passwd"] = val["password"] rows.append(val) db_instance.del_server_out_of_bound_safe(last_rows, rows) last_rows = rows except Exception as e: trace = traceback.format_exc() logging.error(trace) if db_instance.event.wait( get_config().UPDATE_TIME ) or not ServerPool.get_instance().thread.is_alive(): break except KeyboardInterrupt as e: pass db_instance.del_servers() ServerPool.get_instance().stop() db_instance = None
def main(): shell.check_python() if get_config().API_INTERFACE == 'mudbjson': thread = MainThread(db_transfer.MuJsonTransfer) elif get_config().API_INTERFACE == 'ehcomod': thread = MainThread(db_transfer.EhcoDbTransfer) elif get_config().API_INTERFACE == 'webapi': thread = MainThread(web_transfer.WebTransfer) else: print('请设置正确的接口模式!') sys.exit() thread.start() try: while thread.is_alive(): thread.join(10.0) except (KeyboardInterrupt, IOError, OSError) as e: import traceback traceback.print_exc() thread.stop()
def update_all_user(self, dt_transfer): global webapi node_id = get_config().NODE_ID update_transfer = {} # 用户流量上报 data = [] # print('dt_transfer.keys()', dt_transfer) for port in dt_transfer.keys(): if (port not in dt_transfer.keys()) or (port not in self.port_uid_table.keys()): continue elif dt_transfer[port][0] == 0 and dt_transfer[port][1] == 0: continue data.append({'u': dt_transfer[port][0] * self.cfg['transfer_mul'], 'd': dt_transfer[port][1] * self.cfg['transfer_mul'], 'user_id': self.port_uid_table[port]}) update_transfer[port] = dt_transfer[port] if len(data) > 0: tarffic_data = {'node_id': node_id, 'data': data} webapi.postApi('/traffic/upload', tarffic_data) # 节点在线ip上报 node_online_ip_list = ServerPool.get_instance().get_servers_ip_list() ip_data = {} for k, v in node_online_ip_list.items(): if k not in self.port_uid_table: continue ip_data[self.port_uid_table[k]] = v webapi.postApi('/nodes/aliveip', {'node_id': node_id, 'data': ip_data}) # 节点人数上报 alive_user_count = len(self.onlineuser_cache) online_data = {'node_id': node_id, 'online_user': alive_user_count} webapi.postApi('/nodes/online', online_data) return update_transfer
def pull_db_all_user(self): import json rows = None config_path = get_config().MUDB_FILE with open(config_path, 'rb+') as f: rows = json.loads(f.read().decode('utf8')) for row in rows: try: if 'forbidden_ip' in row: row['forbidden_ip'] = common.IPNetwork( row['forbidden_ip']) except Exception as e: logging.error(e) try: if 'forbidden_port' in row: row['forbidden_port'] = common.PortRange( row['forbidden_port']) except Exception as e: logging.error(e) if not rows: logging.warn('no user in json file') return rows
def update_all_user(self, dt_transfer): import json rows = None config_path = get_config().MUDB_FILE with open(config_path, 'rb+') as f: rows = json.loads(f.read().decode('utf8')) for row in rows: if "port" in row: port = row["port"] if port in dt_transfer: row["u"] += dt_transfer[port][0] row["d"] += dt_transfer[port][1] if rows: output = json.dumps(rows, sort_keys=True, indent=4, separators=(',', ': ')) with open(config_path, 'r+') as f: f.write(output) f.truncate() return dt_transfer
def __init__(self): self.session_pool = requests.Session() self.session_pool.mount('', HTTPAdapter(max_retries=3)) self.TOKEN = get_config().TOKEN self.WEBAPI_URL = get_config().WEBAPI_URL
def __init__(self): self.session_pool = requests.Session() self.TOKEN = get_config().TOKEN self.WEBAPI_URL = get_config().WEBAPI_URL