-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·167 lines (146 loc) · 5.54 KB
/
app.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
const request = require('request');
const parseXML = require('xml2js').parseString;
const chalk = require('chalk');
const readline = require('readline');
const fs = require('fs');
const opn = require('opn');
const config = require('./config.json');
const option = process.argv[2];
const help = `
${chalk.yellow.bold('---------------BITnet Help---------------')}
${chalk.bold('Command')}\t\t${chalk.bold('Function')}
bitnet help\t\tview all bitnet commands
bitnet config\t\tsave login credentials
bitnet login\t\tlogin to cyberoam
bitnet li\t\tshort for 'bitnet login'
bitnet logout\t\tlogout from cyberaom
bitnet lo\t\tshort for 'bitnet logout'
`;
switch (option) {
case 'li':
case 'login':
login();
break;
case 'lo':
case 'logout':
logout();
break;
case 'config':
configFunc();
break;
case 'help':
console.log(help);
break;
default:
console.log(chalk.red.bold('\n\t\tInvalid command!\n'));
console.log(chalk.yellow.bold(`Run 'bitnet help' to see the list of available commands.\n`));
break;
}
function login() {
if (config.username && config.password) {
request.post({
url: config.loginURL,
rejectUnauthorized : false,
form: {
mode: '191',
password: config.password,
username: config.username,
a: '1519891235551',
producttype: '0'
}
}, (err, res, body) => {
if (err) {
console.log(chalk.red.bold(`\nAn error occured. Please check your network connection.\n`));
} else {
parseXML(res.body, (err, data) => {
if(data.requestresponse.status[0] === 'LIVE') {
console.log(chalk.green.bold(`\n\t${ data.requestresponse.message[0] }.\n`));
if (config.siteURLs.length) {
config.siteURLs.forEach(site => {
opn(site, { wait: false });
});
}
} else {
console.log(chalk.red.bold(`\n\t${ data.requestresponse.message[0] }.\n`));
console.log(chalk.yellow.bold(`Try resetting your credentials with 'bitnet config'.\n`));
}
});
}
})
} else {
console.log(chalk.red.bold('\n\t\tUsername or password not found!\n'));
console.log(chalk.yellow.bold(`Please save your credentials by running 'bitnet config' to login.\n`));
}
}
function logout() {
if(config.username && config.password) {
request.post({
url: config.logoutURL,
rejectUnauthorized : false,
form: {
mode: '193',
username: config.username,
a: '1519891235551',
producttype: '0'
}
}, (err, res, body) => {
if (err) {
console.log(chalk.red.bold(`\nAn error occured. Please check your network connection.\n`));
} else {
parseXML(res.body, (err, data) => {
if(data.requestresponse.status[0] === 'LOGIN') {
console.log(chalk.green.bold(`\n\t${data.requestresponse.message[0]}.\n`));
} else {
console.log(chalk.red.bold(`\n\t${data.requestresponse.message[0]}.\n`));
console.log(chalk.yellow.bold(`Try resetting your credentials with 'bitnet config'.\n`));
}
});
}
});
} else {
console.log(chalk.red.bold('\n\t\tUsername or password not found!\n'));
console.log(chalk.yellow.bold(`Please save your credentials by running 'bitnet config' to login.\n`));
}
}
function configFunc() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.stdoutMuted = true;
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (!rl.stdoutMuted)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
rl.question('Enter cyberoam username: ', (username) => {
config.username = username;
rl.query = 'Enter password: ';
rl.setPrompt('Enter Password: ');
rl.prompt(true);
rl.stdoutMuted = false;
rl.on('line', (password) => {
config.password = password;
rl.stdoutMuted = true;
rl.question('Enter default website URLs seperated by space (example - https://facebook.com https://google.com): ', (sites) => {
config.siteURLs = [];
const sitesArray = sites.split(' ');
sitesArray.forEach(site => {
config.siteURLs.push(site);
});
fs.writeFile(__dirname + '/config.json', JSON.stringify(config, null, 4), 'utf8', (err) => {
if (err) {
console.log('\n' + err);
console.log(chalk.red.bold('\n\tError saving details. Please check permissions.\n'));
} else {
console.log(chalk.green.bold(`\n\t\tDetails saved successfully!\n`));
console.log(chalk.yellow.bold(`Run 'bitnet login' or 'bitnet li' to log into cyberoam.\n`));
}
rl.close();
});
})
});
});
}