Пример #1
0
def system_init():
    lock_file_exists = os.path.exists(get_file_directory()+"/.init.lock")

    if lock_file_exists == False:
        init()
    else:
        send_pack = {
            "command": "register_server",
            "info":{
                "listen_port":config["SERVER_LISTEN_PORT"],
                "location":config["SERVER_LOCATION"],
                "service_quota":config["SERVICE_QUOTA"]
            }
        }
        # send
        for i in range(0,3):
            return_data = send_socket_request(
                config["CONTROL_SERVER_IP"],
                config["CONTROL_SERVER_TCP_PORT"],
                json.dumps(send_pack),
                type="TCP"
            )

            if return_data["status"] == "success":
                print("register succeed.")
                break
            else:
                time.sleep(1)
        # initialize database
        ssServerDatabase()
        serviceInfo()
        # remove the lock
        os.remove(get_file_directory()+"/.init.lock")
        init()
    pass
Пример #2
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)
Пример #3
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"
        )
Пример #4
0
def revoke(service_idf):
    rtn  = returnModel()
    inDB = serviceInfo()
    rDB  = redisDevice(service_idf)
    # first get item
    item_result = inDB.getItem(service_idf)

    if item_result == None:
        return rtn.error(500)
    elif item_result["status"] == "error":
        return rtn.error(item_result["code"])
    else:
        info = item_result["info"]
        # delete server instance in the database and halt the process
        service_type = info["service_type"]

        if service_type == "shadowsocks":
            res = halt_shadowsocks_service(service_idf)
        elif service_type == "shadowsocks-obfs":
            res = halt_shadowsocks_obfs_service(service_idf)
        else:
            return rtn.error(405)

        if res["status"] == "success":
            result = inDB.deleteItem(service_idf)
            rDB.deleteSet()
            if result["status"] == "success":
                return rtn.success(200)
            else:
                return result
        else:
            return res
    pass
Пример #5
0
def decrease_traffic(service_idf,add_traffic):
    rtn  = returnModel()
    siDB = serviceInfo()
    add_result = siDB.increaseTraffic(service_idf,add_traffic)

    if add_result == None:
        return rtn.error(500)
    elif add_result["status"] == "success":
        return rtn.success(200)
    else:
        return rtn.error(add_result["code"])
Пример #6
0
def postpone(service_idf,postpone_timestamp):
    rtn  = returnModel()
    siDB = serviceInfo()
    p_time = int(postpone_timestamp)
    postpone_result = siDB.updateExpireTimestamp(service_idf,p_time)

    if postpone_result == None:
        return rtn.error(500)
    elif postpone_result["status"] == "success":
        return rtn.success(200)
    else:
        return rtn.error(postpone_result["code"])
Пример #7
0
def connect(service_idf,mac_addr):
    rtn  = returnModel()
    rd   = redisDevice(service_idf)
    siDB = serviceInfo()
    # first, find if service_idf can be found
    result = siDB.getItem(service_idf)
    if result == None:
        return rtn.error(500)
    elif result["status"] == "error":
        return rtn.error(430)
    else:
        info = result['info']
        # check if devices num is not full
        if (rd.countDevice()+int(rd.deviceInList(mac_addr))) > int(info["max_devices"]):
            return rtn.error(431)
        else:
            conf_return_model = {
                "service_type":"",
                "expire_time":"",
                "info":{}
            }
            # register device into device_db
            rd.newDevice(mac_addr)
            conf_return_model["expire_time"] = info["expire_time"]
            # get configuration
            if info["service_type"] == "shadowsocks":
                conf_return_model["service_type"] = "shadowsocks"
                res = get_shadowsocks_conf(service_idf)
            elif info["service_type"] == "shadowsocks-obfs":
                conf_return_model["service_type"] = "shadowsocks-obfs"
                res = get_shadowsocks_obfs_conf(service_idf)
            else:
                return rtn.error(405)

            if res["status"] == "error":
                return rtn.error(res["code"])
            else:
                conf_return_model['info'] = res['info']

            # get conf string then return
            return rtn.success(conf_return_model)
        pass
Пример #8
0
def new_service(max_traffic,max_devices,type,expire_timestamp,strategy=""):
    # first get active service nums
    sDB = serviceInfo()
    rtn = returnModel()
    count_res = sDB.countActiveService()

    # set max num
    if max_traffic == -1 or max_traffic >= config["MMAX_TRAFFIC"]:
        max_traffic = config["MMAX_TRAFFIC"]
    if max_devices == -1 or max_traffic >= config["MMAX_DEVICES"]:
        max_devices = config["MMAX_DEVICES"]

    if count_res == None:
        return rtn.error(420)
    else:
        active_num = int(count_res["info"])
        # if out of quota
        if active_num >= config["SERVICE_QUOTA"]:
            return rtn.error(421)
        else:
            # service type switch
            if type == "shadowsocks":
                result = start_shadowsocks()
            elif type == "shadowsocks-obfs":
                result = start_shadowsocks_obfs(strategy)
            else:
                return rtn.error(405)
            # handle callback
            if result["status"] == "error":
                return rtn.error(result['code'])
            elif result['status'] == "success":
                # add service info
                service_idf = result["info"]["service_idf"]
                sDB.createNewService(service_idf,max_devices,max_traffic,expire_timestamp,type)
                return rtn.success(result['info'])
            else:
                return rtn.error(420)
Пример #9
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)