option = {};

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 = {};
	local handle = {
		["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"
	};

	--新建Cookies
	cookie_NewCookies();
	
	--访问登录页，让路由器指派Cookie
	net_GET(host.."/logon/logon.htm", true, "gb2312", option, handle, reslut);
	if reslut["Status"] == -1 then
		return -1;
	end
	
	--取Cookie和计算密码hash
	COOKIE = cookie_GetCookie(host, "COOKIE");
	passHash = hash_MD5(hash_MD5(pass, "gb2312", true)..":"..COOKIE, "gb2312", true);

	--登录
	net_POST(host.."/logon/loginJump.htm", "encoded="..user.."%3A"..passHash.."&nonce="..COOKIE.."&URL=..%2Flogon%2FloginJump.htm",true, "gb2312", option, handle, reslut);
	if reslut["Status"] == -1 then
		return -1;
	end
	
	--如果有其他人登录则直接踢下线
	if string.find(reslut["Text"], "../logon/loginConfirm.htm") ~= nil then
		net_GET(host.."/logon/loginConfirm.htm", true, "gb2312", option, handle, reslut);
		if reslut["Status"] == -1 then
			return -1;
		end
	end
	
	--登录成功
	if string.find(reslut["Text"], "../userRpm/Index.htm") ~= nil then
		return 1;
	end
	
	--如果登录错误则判断错误
	_,_,ErrType = string.find(reslut["Text"], 'var logonInfo = new Array%(\n(%d*),');
	if ErrType == "1" then
		return -2;
	elseif ErrType == "2" then
		return -1;
	else
		return 0;
	end	
end

function Disconn(host,wan,user,pass)
	local reslut = {};
	local handle = {
		["Accept"] = "text/html, application/xhtml+xml, */*",
		["Referer"] = host.."/userRpm/Interface_WanSettingPPPoE.htm?wanindex="..(wan - 1),
		["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"] = "gzip, deflate",
		["Connection"] = "Keep-Alive"
	};
	net_GET(host.."/userRpm/Interface_WanSettingPPPoE.htm?btn_disconnect=btn_disconnect&wanindex="..(wan - 1), true, "gb2312", option, handle, reslut);
	if reslut["Status"] == -1 then
		return -1;
	end
	return 1;
end

function Conn(host,wan,user,pass)
	local reslut = {};
	local handle = {
		["Accept"] = "text/html, application/xhtml+xml, */*",
		["Referer"] = host.."/userRpm/Interface_WanSettingPPPoE.htm?wanindex="..(wan - 1),
		["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"] = "gzip, deflate",
		["Connection"] = "Keep-Alive"
	};
	net_GET(host.."/userRpm/Interface_WanSettingPPPoE.htm?btn_connect=btn_connect&wanindex="..(wan - 1), true, "gb2312", option, handle, reslut);
	if reslut["Status"] == -1 then
		return -1;
	end
	return 1;
end

function Logout(host,wan,user,pass)
	local reslut = {};
	local handle = {
		["Accept"] = "text/html, application/xhtml+xml, */*",
		["Referer"] = host.."/userRpm/Interface_WanSettingPPPoE.htm?wanindex="..(wan - 1),
		["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"] = "gzip, deflate",
		["Connection"] = "Keep-Alive"
	};
	net_GET(host.."/logon/logout.htm", true, "gb2312", option, handle, reslut);
	if reslut["Status"] == -1 then
		return -1;
	end
	return 1;
end