require( "facepunch" )


r,c,cookie=facepunch.http.get("http://www.baidu.com/")
print (r.."\n"..c)
print ("\n"..cookie)
local string = require"string"

local configfile = io.open("config.txt", "r")
username=configfile:read()
password=configfile:read()
configfile:close()
print(username,password)

local patters = { "wlanacip","wlanacname","wlanuserip", }
login_data={wlancssid="CMCC",ualistindex="UA0999",loginmode="static",staticusername=username,staticpassword=password}
for _,p in pairs(patters) do
	i,j=string.find(r, string.format("%s=([0-9\.]*)",p))
	i=i+string.len(p)+1
	login_data[p]=string.sub(r,i,j)
end
login_data_post=""
for k,v in pairs(login_data) do
	print(k,v)
	login_data_post=login_data_post..k.."="..v.."&"
end
print(login_data_post)
login_url="http://221.176.1.140/do_login.php"
r,c,cookie=facepunch.http.post(login_url,cookie,login_data_post)
print (r.."\n"..c)
print ("\n"..cookie)

-- r="<frameset rows='600,*' border='0' framespacing='0' frameborder=0 marginwidth='0' marginheight='0'><frame src='http://221.176.1.140:80/user_status.php?username=13873772907&logonsessid=0361.0371.371.00;201209211451330416D6;ps003new&wlanacip=221.176.119.178&wlanacname=0361.0371.371.00&wlanuserip=111.7.61.189&logintime=2012-09-21 14:51:33&remaintime=0&areacode=731&wlanacssid=&productid=22&effecttime=20120327 000000&expiretime=20380101 000000&keystr=13f38b0aa34870b4037da6b101fb93ef3d17078d42e14504&cf=disabled' name='status' scrolling='no' marginwidth='0' marginheight='0'><noframes>"

i,j=string.find(r,"src='(.*)' name='status'")
ser=string.sub(r,i+5,j-15)
print(ser)

local cookiefile = io.open("cmcc_login_cookie_lua", "w+")
cookiefile:write(cookie)
cookiefile:close()
