-
Notifications
You must be signed in to change notification settings - Fork 0
/
old-crfut.js
147 lines (124 loc) · 4.29 KB
/
old-crfut.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
This file exists as an archive of some of the other crap I tried to get
this working. It is not neccessary to application function. I'm just
keeping it because it's a waste to throw out all of that hard work, right?
:)
*/
// The switches all have some bespoke path in their URL. For example,
// one of our switches, when you log in, takes you to:
// http://10.1.0.203/csb6ce4e37/home.htm
// We need to figure out the csb6ce4e37 bit for ... some reason?
// But we probably don't actually need to anymore, since the chromium
// clicker arounder thing shouldn't care about this.
function getAddressBaseName(host, callback) {
console.log(`Resolving the web interface path ${host}`)
request({
method: 'GET',
uri: `http://${host}/`,
}, function(err, res, body) {
if (err) { throw `Failed to get base name - ${err}`; }
callback && callback(res.headers.location.replace(/\/$/, ''))
})
}
// Launch a Chromium headless window, to browse to the magic page
// then upload config, and then close Chrome again.
// getAddressBaseName(host, function(baseAddressLocal) {
// console.log(baseAddressLocal);
// baseAddress = baseAddressLocal;
// var h = new headless(baseAddressLocal, username, password, null, function() { //"/home/sirsquidness/respawn/pax/linksys/LGS500-11027.ros"
// doConfigurationUpload(function() {
// h.close();
// });
// });
// });
var jar = request.jar();
var requester = request.defaults({
headers: {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0",
"Accept": "*/*",
"Connection": "keep-alive",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
},
jar: jar,
followRedirect: false
});
function doLoginRequest(base, user, pass, cb) {
requester.get(`${base}/System.xml?action=login&user=${user}&password=${pass}`, function(err, res, body) {
console.log(res.headers)
if (!res.headers.sessionid) {
console.log(body);
throw "No session ID cookie returned";
}
cb(res.headers.sessionid.replace(";path=/",""))
})
}
var dlDataResponse = makeRequester(`/FileMgmt/dlData.html?`, `/FileMgmt/dlData.htm`);
var logout = makeRequester(`/config/log_off_page.html`);
function fillJar(address, sessionId) {
var sessionCookie = request.cookie(`sessionID=${sessionId}`);
jar.setCookie(request.cookie('admin_numberOfEntriesPerPage=50'), address);
jar.setCookie(request.cookie('activeLangId=English'), address);
jar.setCookie(request.cookie('isStackableDevice=false'), address);
jar.setCookie(request.cookie('userStatus=ok'), address);
jar.setCookie(sessionCookie, address);
jar.setCookie(request.cookie('usernme=admin'), address);
jar.setCookie(request.cookie('firstWelcomeBanner=false'), address);
jar.setCookie(request.cookie('pg=00000000000000000000000000000000000000000000000000000'), address);
}
function doConfigurationUpload(callback) {
jar.setCookie("sessionID=", baseAddress);
doLoginRequest(baseAddress, username, password, function(sessionId) {
fillJar(baseAddress, sessionId);
doConfigUpdateRequest(baseAddress, sessionId, config, function() {
logout(function() {
callback && callback();
})
})
});
}
function makeRequester(path, referer) {
if (referer) referer = `${baseAddress}${referer}`;
return function(callback) {
requester({
method: 'GET',
uri: `${baseAddress}${path}`,
headers: {
Referer: referer
}
}, function(err, res, body) {
callback && callback()
})
}
}
function doConfigUpdateRequest(address, sessionId, configFileContents, callback) {
console.log(`${address}/FileMgmt/httpConfigProcess.htm`);
request({
method: 'POST',
uri: `${address}/FileMgmt/httpConfigProcess.htm?`,
jar: jar,
formData: {
restoreUrl: "",
errorCollector: "",
"rlCopyFreeHistoryIndex$scalar": "5",
rlCopyDestinationFileType: "2",
rlCopyDestinationFileName: "",
"rlCopyFreeHistoryIndex": "5",
srcFileName: {
value: configFileContents,
options: {
filename: `running-config-${ Math.floor(Math.random()*100000).toString() }.txt`,
contentType: "text/plain"
}
}
},
headers: {
"Referer": `${address}/home.htm`,
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
}, function(err, res, body) {
console.log(res.statusCode, body);
callback && callback()
})
}