示例#1
0
def halt_shadowsocks_service(service_idf):
    rtn = returnModel()
    from model.db_ss_server import ssServerDatabase
    from proc.proc_ss import ssProcess
    ssDB = ssServerDatabase()
    ssProc = ssProcess()
    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"])
        kill_proc = ssProc.checkProcess(port=port)
        # can not find pid in the system
        if len(kill_proc) == 0:
            return rtn.error(450)
        else:
            _pid = kill_proc[0]["pid"]
            result = ssProc.killProcess(_pid)
            if result == False:
                return rtn.error(450)
            else:
                # kill record in database
                ssDB.deleteInstance(service_idf)
                return rtn.success(200)
示例#2
0
def start_shadowsocks():
    return_data_config = {
        "server_port" : "",
        "password"    : "",
        "method"      : "",
        "timeout"     : ""
    }

    return_data = {
        "service_idf":"",
        "config":return_data_config
    }
    rtn = returnModel()
    from model.db_ss_server import ssServerDatabase
    from proc.proc_ss import ssProcess
    ssDB = ssServerDatabase()
    ssProc = ssProcess()
    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()
        service_port = port
        service_password = gen_password(16)
        service_method = config["SS_DEFAULT_METHOD"]
        service_timeout = config["SS_DEFAULT_TIMEOUT"]

        res = ssDB.insertServerInstance(service_idf,service_port
                                        ,service_password,service_method,service_timeout)
        if res == None:
            return rtn.error(423)
        elif res["status"] == "error":
            return rtn.error(423)
        elif res["status"] == "success":
            # insert success, then create real process
            result = ssProc.createProcess(service_port,service_password,service_timeout,service_method)
            # if the process not open successfully (maybe... just not install ss-server LOL)
            # (2015-12-06)UPDATE : DO NOT CHECK THE RESULT OF PROCESS CREATION!!

            # insert config data
            return_data_config["server_port"] = service_port
            return_data_config["password"]    = service_password
            return_data_config["method"]      = service_method
            return_data_config["timeout"]     = service_timeout
            return_data["service_idf"]        = service_idf
            return rtn.success(return_data)
    pass