option = {};
handle = {
    ["Accept"] = "text/html, application/xhtml+xml, */*",
    ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER",
    ["Accept-Language"] = "zh-CN",
    ["Accept-Encoding"] = "deflate",
    ["Connection"] = "Keep-Alive"
};
    
function Disconnection(host,wan,user,pass)
    local r = Login(host, wan, user, pass);
    if r == 1 then
        r = Disconn(host, wan, user, pass);
        Sleep(500);
        Logout(host, wan, user, pass);
        return r;
    else
        return r;
    end
end

function Connection(host,wan,user,pass)
    local r = Login(host, wan, user, pass);
    if r == 1 then
        r = Conn(host, wan, user, pass);
        Sleep(500);
        Logout(host, wan, user, pass);
        return r;
    else
        return r;
    end
end

function ChangeIP(host,wan,user,pass)
    local r = Login(host, wan, user, pass);
    if r == 1 then
        r = Disconn(host, wan, user, pass);
        if r == 1 then
            Sleep(5000);
            r = Conn(host, wan, user, pass);
            Sleep(500);
            Logout(host, wan, user, pass);
            return r;
        else
            return r;
        end
    else
        return r;
    end
end

function Login(host, wan, user, pass)
    local reslut = {};
    handle["Referer"] = host.."/";

    net_GET(host.."/", true, "gb2312", option, handle, reslut);
    cookie_NewCookies();
    cookie_SetCookie("Authorization", "Basic "..hash_Base64(pass, "gb2312"), host, -1);
    net_GET(host.."/", true, "gb2312", option, handle, reslut);

    if reslut["Status"] == -1 then
        return -1;
    else
        s,_ = string.find(reslut["Text"], '%$.loadMain%("status.htm"%);');
        if s and s > 0 then
            return 1;
        end
        
        _,_,c = string.find(reslut["Text"], 'var authTimes = (%d)');
        if c then
            cn = tonumber(c);
            if cn > 0 then
                return -2;
            end
        end

        return 0;
    end
end

function Disconn(host,wan,user,pass)
    local reslut = {};
    handle["Referer"] = host.."/";
--    handle["Content-Type"] = "text/plain";

    net_POST(host.."/cgi?7", "[ACT_PPP_DISCONN#1,1,1,0,0,0#0,0,0,0,0,0]0,0\r\n", true, "gb2312", option, handle, reslut);
    if reslut["Status"] == -1 then
        return -1;
    else
        local StatusClass,_ = math.modf(reslut["Status"] / 100.0);
        if StatusClass == 4 or StatusClass == 5 then
            return -2;
        elseif StatusClass == 2 or StatusClass == 3 then
            return 1;
        else
            return 0;
        end
    end
end

function Conn(host,wan,user,pass)
    local reslut = {};
    handle["Referer"] = host.."/";
--    handle["Content-Type"] = "text/plain";

    net_POST(host.."/cgi?7", "[ACT_PPP_CONN#1,1,1,0,0,0#0,0,0,0,0,0]0,0\r\n", true, "gb2312", option, handle, reslut);
    if reslut["Status"] == -1 then
        return -1;
    else
        local StatusClass,_ = math.modf(reslut["Status"] / 100.0);
        if StatusClass == 4 or StatusClass == 5 then
            return -2;
        elseif StatusClass == 2 or StatusClass == 3 then
            return 1;
        else
            return 0;
        end
    end
end

function Logout(host,wan,user,pass)
    cookie_NewCookies();
    return 1;
end