def create_node_job(): with __lock: print("进入创建节点") # if not v2_util.is_running(): # return failedNodeJobs = mysqlsesson.query(FailedNodeJob).filter( FailedNodeJob.count < 20, FailedNodeJob.status == 1) if not failedNodeJobs: return for nodejob in failedNodeJobs: try: requests.post(nodejob.server, nodejob.json, timeout=13) except: print("Failed http") mysqlsesson.query(FailedNodeJob).filter( FailedNodeJob.id == nodejob.id).update( {FailedNodeJob.count: nodejob.count + 1}) else: mysqlsesson.query(FailedNodeJob).filter( FailedNodeJob.id == nodejob.id).update({ FailedNodeJob.count: nodejob.count + 1, FailedNodeJob.status: 0 }) mysqlsesson.commit()
def subscribeqx(setting_id): shuchu = '' Userfirst = mysqlsesson.query(UserSubscribe).filter( UserSubscribe.code == setting_id).first() if not Userfirst: return '{code":200,"msg":"不存在该订阅}' else: expire_time = V2ray("nextTime:" + str(Userfirst.expire_time), "8.8.8.8", 9999, "b5a811d6-c13e-4000-9204-0c45b47e586a", "22", "ws", "") openurl = "vmess=" + expire_time.add + ":" + str(expire_time.port) + ", method=chacha20-ietf-poly1305, password="******", obfs=" + expire_time.net + ", obfs-uri=" + expire_time.path \ + ",fast-open=false, udp-relay=false, tag=" + expire_time.ps shuchu = shuchu + openurl + "\n" NodeList = mysqlsesson.query(VpsNode).filter( VpsNode.v2_port == Userfirst.user_port).all() for node in NodeList: v2 = V2ray(node.desc, node.server, node.v2_port, node.v2_id, node.v2_alter_id, node.v2_net, node.v2_path) openurl = "vmess=" + v2.add + ":" + str(v2.port) + ", method=chacha20-ietf-poly1305, password="******", obfs=" + v2.net + ", obfs-uri=" + v2.path \ + ",fast-open=false, udp-relay=false, tag=" + v2.ps shuchu = shuchu + openurl + "\n" encodestr = base64.b64encode(shuchu.encode('utf-8')) print(str(encodestr, 'utf-8')) return str(encodestr, 'utf-8')
def add_inbound(): port = int(request.form['port']) print("进来了") if Inbound.query.filter_by(port=port).count() > 0: return jsonify(Msg(False, gettext('port exists'))) listen = request.form['listen'] protocol = request.form['protocol'] settings = request.form['settings'] email = random_email() remail = '"email":"' + email + '",' str_list = list(settings) str_list.insert(13, remail) # 插入堆积 newsettings = ''.join(str_list) stream_settings = request.form['stream_settings'] sniffing = request.form['sniffing'] remark = request.form['remark'] # 当前用户等级 user_level = request.form['level'] # 是否更新所有服务器 allvps = request.form['allvps'] inbound = Inbound(port, listen, protocol, newsettings, stream_settings, sniffing, remark, user_level) local_ip = get_ip() if allvps == 'true': print("更新所有vps") devices = mysqlsesson.query(VpsDevice).filter(VpsDevice.level <= int(user_level), VpsDevice.status == 1).all() inbound.allvps = 'false' for device in devices: if local_ip != device.ip: try: inbound.remark = device.country_code requests.post("http://" + device.ip + ":"+str(config.get_port())+"/v2ray/inbound/add", inbound.to_json_vps(), timeout=13) except: print("Failed http") mysqlsesson.add(FailedNodeJob(local_ip,device.ip,"http://" + device.ip + ":"+str(config.get_port())+"/v2ray/inbound/add", str(inbound.to_json_vps()))) # requests.post("http://127.0.0.1:5000/v2ray/inbound/add", inbound.to_json_vps(), timeout=3) # 插入mysql 用户表,生成订阅 userSubscribe = UserSubscribe(base64.b64encode(email.encode('utf-8')), port, user_level, 1) mysqlsesson.add(userSubscribe) db.session.add(inbound) # 插入mysql inbound #查询自己服务器重新赋值 device = mysqlsesson.query(VpsDevice).filter(VpsDevice.server==local_ip).first() inboundMysql = InboundMysql(local_ip, port, listen, protocol, newsettings, stream_settings, sniffing, device.country_code) mysqlsesson.add(inboundMysql) # 插入mysql 节点表 Node = VpsNode(protocol, local_ip, json.loads(settings)['clients'][0]['id'], json.loads(settings)['clients'][0]['alterId'], port, json.loads(stream_settings)['wsSettings']['path'], device.country_code, json.loads(stream_settings)['network']) mysqlsesson.add(Node) mysqlsesson.commit() db.session.commit() return jsonify( Msg(True, gettext(u'Successfully added, will take effect within %(seconds)d seconds', seconds=__check_interval) ) )
def subscriberoot(setting_id): Usertest = mysqlsesson.query(UserSubscribe).filter( UserSubscribe.code == setting_id).first() shuchu = '' if not Usertest: return '{code":200,"msg":"订阅不存在}' else: shuchu = Usertest.fq_text encodestr = base64.b64encode(shuchu.encode('utf-8')) print(str(encodestr, 'utf-8')) return str(encodestr, 'utf-8')
def check_traffic_job(): with __lock: print("进入节点流量统计:") # if not v2_util.is_running(): # return local_ip = get_ip() vpsNode = mysqlsesson.query(VpsNode).filter(VpsNode.server == local_ip) for node in vpsNode: if node.up + node.down >= node.alllink: global __v2_config_changed __v2_config_changed = True mysqlsesson.query(VpsNode).filter( VpsNode.tag == node.tag, VpsNode.server == local_ip).update({ VpsNode.status: 0, VpsNode.is_subscribe: 0 }) Inbound.query.filter_by(tag=node.tag).update({'enable': False}) db.session.commit() mysqlsesson.commit()
def subscribe(setting_id): shuchu = '' User = mysqlsesson.query(UserSubscribe).filter( UserSubscribe.code == setting_id).first() if not User: return '{code":200,"msg":"不存在该订阅}' else: expire_time = V2ray("nextTime:" + str(User.expire_time), "8.8.8.8", 9999, "b5a811d6-c13e-4000-9204-0c45b47e586a", "22", "ws", "").v2link() genery = mysqlsesson.query(UserSubscribe).filter( UserSubscribe.code == 'ZGVtbwxx').first() NodeList = mysqlsesson.query(VpsNode).filter( VpsNode.v2_port == User.user_port).all() for node in NodeList: v2 = V2ray(node.desc, node.server, node.v2_port, node.v2_id, node.v2_alter_id, node.v2_net, node.v2_path).v2link() shuchu = shuchu + v2 + '\n' shuchu = shuchu + genery.fq_text + '\n' encodestr = base64.b64encode(shuchu.encode('utf-8')) print(encodestr) return encodestr
def update_inbound(in_id): update = {} port = request.form.get('port') add_if_not_none(update, 'port', port) if port: if Inbound.query.filter(Inbound.port == port).count() > 1: return jsonify(Msg(False, gettext('port exists'))) add_if_not_none(update, 'tag', 'inbound-' + port) add_if_not_none(update, 'listen', request.form.get('listen')) add_if_not_none(update, 'protocol', request.form.get('protocol')) add_if_not_none(update, 'settings', request.form.get('settings')) add_if_not_none(update, 'stream_settings', request.form.get('stream_settings')) add_if_not_none(update, 'sniffing', request.form.get('sniffing')) add_if_not_none(update, 'remark', request.form.get('remark')) add_if_not_none(update, 'enable', request.form.get('enable') == 'true') add_if_not_none(update, 'level', request.form.get('level')) listen = request.form['listen'] protocol = request.form['protocol'] settings = request.form['settings'] email = random_email() remail = '"email":"' + email + '",' str_list = list(settings) str_list.insert(13, remail) # 插入堆积 newsettings = ''.join(str_list) stream_settings = request.form['stream_settings'] sniffing = request.form['sniffing'] remark = request.form['remark'] # 当前用户等级 user_level = request.form['level'] # 是否更新所有服务器 allvps = request.form['allvps'] local_ip = get_ip() if allvps == "true": inbound = Inbound(int(port), listen, protocol, newsettings, stream_settings, sniffing, remark, user_level) inbound.allvps = 'false' devices = mysqlsesson.query(VpsDevice).filter(VpsDevice.level <= int(user_level), VpsDevice.status == 1).all() for device in devices: if local_ip != device.ip: requests.post("http://" + device.ip + ":"+str(config.get_port())+"/v2ray/inbound/update/" + str(in_id), inbound.to_json_vps(), timeout=3) # requests.post("http://127.0.0.1:5000/v2ray/inbound/add", inbound.to_json_vps(), timeout=3) print("vps更新") Inbound.query.filter_by(port=in_id).update(update) db.session.commit() return jsonify( Msg(True, gettext(u'Successfully updated, will take effect within %(seconds)d seconds', seconds=__check_interval) ) )
def traffic_job(): with __lock: print("进入流量统计") # if not v2_util.is_running(): # return traffics = v2_util.get_inbounds_traffic() if not traffics: print("没查到流量") return for traffic in traffics: upload = int(traffic.get('uplink', 0)) download = int(traffic.get('downlink', 0)) print("down:" + str(download) + ":up:" + str(upload)) tag = traffic['tag'] local_ip = get_ip() inbound = Inbound.query.filter_by(tag=tag).first() if inbound and download < inbound.down: Inbound.query.filter_by(tag=tag).update({ 'up': Inbound.up + upload, 'down': Inbound.down + download }) else: Inbound.query.filter_by(tag=tag).update({ 'up': upload, 'down': download }) # 更新mysql inbounding = mysqlsesson.query(InboundMysql).filter( InboundMysql.tag == tag).first() if inbounding and download < inbounding.down: mysqlsesson.query(InboundMysql).filter( InboundMysql.tag == tag, InboundMysql.server == local_ip).update( { InboundMysql.up: InboundMysql.up + upload, InboundMysql.down: InboundMysql.down + download }, synchronize_session=False) mysqlsesson.query(VpsNode).filter( VpsNode.tag == tag, VpsNode.server == local_ip).update( { VpsNode.up: VpsNode.up + upload, VpsNode.down: VpsNode.down + download }, synchronize_session=False) else: mysqlsesson.query(InboundMysql).filter( InboundMysql.tag == tag, InboundMysql.server == local_ip).update( { InboundMysql.up: upload, InboundMysql.down: download }, synchronize_session=False) mysqlsesson.query(VpsNode).filter( VpsNode.tag == tag, VpsNode.server == local_ip).update( { VpsNode.up: upload, VpsNode.down: download }, synchronize_session=False) db.session.commit() mysqlsesson.commit()