Ejemplo n.º 1
0
def reset_traffic(strategy_name):
    def split_traffic_strategy(strategy_str):
        # remove spaces
        strategy_str = strategy_str.replace(" ","")
        return strategy_str.split(",")

    trafficDB = serviceTraffic()
    servDB    = serviceInfo()
    result    = trafficDB.getTrafficInstancesByStrategy(strategy_name)
    if result == None:
        return None
    elif result["status"] == "error":
        return None
    else:
        traffic_arr = result["info"]
        for item in traffic_arr:
            s_str = item["service_strategy"]
            s_idf = item["service_idf"]
            stgy_arr = split_traffic_strategy(s_str)

            new_traffic = float(stgy_arr[1])
            # total traffic reset to maximum
            servDB.resetTraffic(s_idf,new_traffic)
            # reset zero for this service
            trafficDB.resetZero(s_idf)
Ejemplo n.º 2
0
def send_heart_beat_package():
    infoDB    = serviceInfo()
    trafficDB = serviceTraffic()
    HB        = redisHeartBeatPacket()

    idfs = infoDB.checkExpiredService()
    exceed = trafficDB.getExceedTrafficService()

    # update traffic of all services
    update_traffic()

    if idfs["status"] == "success":
        idfs_info = idfs["info"]
        for item in idfs_info:
            revoke(item)
        HB.updateExpireIdfs(idfs_info)

    if exceed["status"] == 'success':
        exceed_info = exceed["info"]
        for item in exceed_info:
            revoke(item)
        HB.updateTrafficExceedIdfs(exceed_info)

   # send heart_beat package
    pack_json = HB.generatePakcetContent()

    if config["CONTROL_SERVER_IP"] != "": #and config["SEND_HEARTBEAT_PACK"] == True:
        send_socket_request(
            config["CONTROL_SERVER_IP"],
            config["CONTROL_SERVER_UDP_PORT"],
            json.dumps(pack_json),
            type="UDP"
        )
Ejemplo n.º 3
0
def halt_shadowsocks_obfs_service(service_idf):
    rtn = returnModel()
    from model.db_ss_obfs_server import ssOBFSServerDatabase
    from model.db_traffic import serviceTraffic
    from proc.proc_ss import ssOBFS_Process
    ssDB = ssOBFSServerDatabase()
    ssProc = ssOBFS_Process()
    trafficDB = serviceTraffic()

    item = ssDB.getItem(service_idf)

    if item == None:
        return rtn.error(500)
    elif item["status"] == "error":
        return rtn.error(item["code"])
    else:
        port = int(item["info"]["server_port"])
        result = ssProc.deleteServer(port)
        if result == False:
            return rtn.error(450)
        else:
            # kill record in database
            ssDB.deleteInstance(service_idf)
            trafficDB.deleteItem(service_idf)
            return rtn.success(200)
Ejemplo n.º 4
0
def start_shadowsocks_obfs(traffic_strategy):
    return_data = {
        "service_idf":"",
        "config":""
    }
    rtn = returnModel()
    from model.db_ss_obfs_server import ssOBFSServerDatabase
    from model.db_traffic import serviceTraffic
    from proc.proc_ss import ssOBFS_Process
    ssDB = ssOBFSServerDatabase()
    ssProc = ssOBFS_Process()
    ssT    = serviceTraffic()
    port = 0
    # lock: to prevent infinite loop (for any reason)
    lock = 20
    while lock > 0:
        lock -= 1
        rand_port = randint(config["SHADOWSOCKS_MIN_PORT"],config["SHADOWSOCKS_MAX_PORT"])
        if ssDB.portCollision(int(rand_port)) == False:
            port = rand_port
            break

    if port == 0:
        return rtn.error(422)
    else:
        # first generate params
        service_idf  = gen_service_idf()
        passwd = gen_password(16)
        res    = ssDB.insertServerInstance(service_idf,port,passwd)

        if res == None:
            return rtn.error(423)
        elif res["status"] == "error":
            return rtn.error(423)
        elif res["status"] == "success":
            result = ssProc.createServer(port,passwd)
            ssT.createNewTraffic(service_idf,traffic_strategy)
            if result == True:
                return_data["service_idf"] = service_idf
                return_data["config"]      = ssProc.generateLocalConfig(port,passwd)
                return rtn.success(return_data)
            else:
                return rtn.error(1200)
    pass
Ejemplo n.º 5
0
def update_traffic():
    servDB    = serviceInfo()
    trafficDB = serviceTraffic()
    HB        = redisHeartBeatPacket()
    item_result = servDB.getItems()
    if item_result == None:
        return None
    elif item_result["status"] == "error":
        return None
    else:
        # get all items
        items = item_result["info"]

        for item in items:
            serv_type = item["service_type"]
            serv_idf  = item["service_idf"]
            # TODO 暂时只对使用shadowsocks-obfs的服务进行流量更新
            if serv_type == "shadowsocks-obfs":
                from model.db_ss_obfs_server import ssOBFSServerDatabase
                from proc.proc_ss import ssOBFS_Process
                ssProc = ssOBFS_Process()
                ssDB   = ssOBFSServerDatabase()

                # get service listen port
                port_result = ssDB.getItem(serv_idf)
                if port_result != None:
                    if port_result["status"] == "success":
                        port = int(port_result["info"]["server_port"])
                        # get traffic
                        t_info = ssProc.getTraffic(port)

                        # change to MBs X.YYY MB
                        u_t    = round(float(t_info["upload"]) / (1000 * 1000),3)
                        d_t    = round(float(t_info["download"]) / (1000 * 1000),3)
                        trafficDB.updateTraffic(serv_idf,u_t,d_t)
                        HB.updateTraffic(serv_idf,u_t,d_t)
Ejemplo n.º 6
0
    def handle(self):
        data = str(self.request.recv(1024).strip(), 'utf-8')
        try:
            json_data = json.loads(data)
            if json_data["from"] == "host":
                if self.checkHostIP() == False:
                    return self.sendSocket("error",403)
            # "ping"
            # just test if the server is OK. returns "pong"
            if json_data["command"] == "ping":
                return self.sendSocket("success","pong")
            # "new"
            # create new instance when
            elif json_data["command"] == "new":
                if json_data["from"] == "host":
                    max_traffic = json_data["info"]["max_traffic"]
                    max_devices = json_data["info"]["max_devices"]
                    expire_timestamp = json_data["info"]["expire_timestamp"]
                    traffic_strategy = json_data["info"]["traffic_strategy"]
                    service_type = json_data["info"]["type"]
                    dispatch_result = new_service(max_traffic,max_devices,service_type,expire_timestamp,strategy=traffic_strategy)

                    if dispatch_result == None:
                        return self.sendSocket("error",500)
                    elif dispatch_result["status"] == "error":
                        return self.sendSocket("error", dispatch_result["code"])
                    else:
                        # return service_idf and expire_date
                        rtn_model = {
                            "expire_timestamp": int(expire_timestamp),
                            "service_idf"     : dispatch_result["info"]["service_idf"],
                            "config"          : dispatch_result["info"]["config"]
                        }
                        return self.sendSocket("success",rtn_model)
                else:
                    return self.sendSocket("error",402)
            # connect to the remote server first.the server return the configuration.
            elif json_data["command"] == "connect":
                if json_data["from"] == "client":
                    mac_addr = json_data["mac_addr"]
                    service_idf = json_data["service_idf"]

                    #OP
                    result = connect(service_idf,mac_addr)

                    if result["status"] == "error":
                        return self.sendSocket("error",int(result["code"]))
                    else:
                        _data = json.dumps(result["info"])
                        return self.sendSocket("success",_data)
                    pass
                else:
                    return self.sendSocket("error",402)
            # if the instance expired , just kill it
            # if not necessary, plz don't call IT
            elif json_data["command"] == "revoke":
                if json_data["from"] == "host":
                    service_idf = json_data["service_idf"]

                    #OP
                    res         = revoke(service_idf)

                    if res["status"] == "error":
                        return self.sendSocket("error",res["code"])
                    else:
                        return self.sendSocket("success",200)
                else:
                    return self.sendSocket("error",402)

            # postpone
            elif json_data["command"] == "postpone":
                if json_data["from"] == "host":
                    service_idf      = json_data["service_idf"]
                    expire_timestamp = int(json_data["postpone_timestamp"])
                    res              = postpone(service_idf,expire_timestamp)

                    if res["status"] == "error":
                        return self.sendSocket("error",res["code"])
                    else:
                        return self.sendSocket("success",200)
                else:
                    return self.sendSocket("error",402)

            elif json_data["command"] == "increase_traffic":
                if json_data["from"] == "host":
                    service_idf      = json_data["service_idf"]
                    traffic          = json_data["traffic"]

                    res              = increase_traffic(service_idf,traffic)
                    if res["status"] == "error":
                        return self.sendSocket("error",res["code"])
                    else:
                        return self.sendSocket("success",200)
                else:
                    return self.sendSocket("error",402)

            elif json_data["command"] == "decrease_traffic":
                if json_data["from"] == "host":
                    service_idf      = json_data["service_idf"]
                    traffic          = json_data["traffic"]

                    res              = decrease_traffic(service_idf,traffic)
                    if res["status"] == "error":
                        return self.sendSocket("error",res["code"])
                    else:
                        return self.sendSocket("success",200)
                else:
                    return self.sendSocket("error",402)

            elif json_data["command"] == "adjust_quota":
                if json_data["from"] == "host":
                    new_quota        = json_data["new_quota"]

                    #注意:这个调整只是临时的,服务只要一重启就会恢复原状
                    config["SERVICE_QUOTA"] = int(new_quota)
                    return self.sendSocket("success",200)
                else:
                    return self.sendSocket("error",402)
            # get real time traffic (debug)
            elif json_data["command"] == "__get_traffic":
                if json_data["from"] == "host":
                    service_idf     = json_data["service_idf"]
                    trafficDB       = serviceTraffic()
                    traffic         = trafficDB.getTraffic(service_idf)
                    return self.sendSocket("success",traffic["info"])
                else:
                    return self.sendSocket("error",402)
            else:
                return self.sendSocket("error",405)
        except ValueError:
            return self.sendSocket("error",400)
        pass