This repository has been archived by the owner on Jan 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon.js
64 lines (62 loc) · 1.67 KB
/
on.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let cheerio = require('cheerio')
var fetch = require('node-fetch')
var fs = require('fs')
var co = require('co')
var userInfo = require('./user.json')
function * fetchHtml (url, option) {
option.credentials = 'include'
const res = yield fetch(url, option)
return res.text()
}
function getValues ($, keys) {
let data = {}
for (let key of keys) {
data[key] = $('#' + key).val()
}
return data
}
function getFormString (data) {
return Object.keys(data).reduce((str, key) => {
str += key + '=' + encodeURIComponent(data[key]) + '&'
return str
}, '')
}
function * signIn () {
let $ = cheerio.load(yield fetchHtml('http://www.baidu.com', { credentials: 'include' }))
const userName = process.argv[2] || userInfo.userName
const userPwd = process.argv[3] || userInfo.userPwd
$('#userName').val(userName)
$('#userPwd').val(userPwd)
const formData = getValues($, [
'wlanAcName',
'wlanAcIp',
'wlanUserIp',
'ssid',
'userName',
'userPwd'
])
const formString = getFormString(formData)
const { action } = $('#Wlan_Login').attr()
const result = yield fetchHtml(action, {
method: 'POST',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded'
},
credentials: 'include',
body: formString
})
const remoteHost = result.match(/action="(.*)\/zmcc\/portalLoginRedirect\.wlan"/)[1]
fs.writeFileSync('./wlan.json', JSON.stringify({
formString,
remoteHost
}))
fs.writeFileSync('./user.json', JSON.stringify({
userName,
userPwd
}))
console.log('login success')
}
co(function * () {
yield signIn()
})