forked from chxru/WiFi-Cost-Changer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (77 loc) · 2.28 KB
/
index.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
const path = require('path')
const fs = require('fs')
const cmd = require('node-cmd')
const schedule = require('node-schedule')
const WindowsToaster = require('node-notifier').WindowsToaster
const notifier = new WindowsToaster()
const icon = path.join(__dirname, 'assets', 'icon.png')
let ssid
const toUnrestricted = () => {
cmd.get(`netsh wlan set profileparameter name="${ssid}" cost=Unrestricted`,
(err) => {
if(err) return errLogger(err)
notify('unrestricted')
})
}
const toFixed = () => {
cmd.get(`netsh wlan set profileparameter name="${ssid}" cost=Fixed`,
(err) => {
if(err) return errLogger(err)
notify('fixed')
})
}
const onStart = () => {
cmd.get(`netsh wlan show interfaces | findstr /R /C:\" SSID\"`, //get SSID
(err, data) => {
if(err) return errLogger(err, 'ssiderr')
ssid = data.split(':')[1].trim()
let h = new Date().getHours()
if ( h < 8 ) {
toUnrestricted()
} else {
toFixed()
}
schedule.scheduleJob('0 0 * * *', toUnrestricted) // Changes to unrestricted at 0000h
schedule.scheduleJob('0 8 * * *', toFixed) // Changes to fixed at 0800h
})
}
const errLogger = (err, code='stderr') => {
let d = new Date()
let error = `${d.getMonth()}:${d.getDate()}/${d.getHours()}:${d.getMinutes()}:${d.getSeconds()} \n ${err} \n \n`
fs.appendFile(path.join(__dirname, 'log.txt'), error, () => {
notify('', code)
})
}
const notify = (state, err = 0) => {
let values
switch(err) {
case 0:
values = {t: 'WiFi Cost Changed', m: `Connection changed to ${state} state`}
break
case 'stderr':
values = {t: 'Oops :(', m: `Error occured, check log.txt`}
break
case 'ssiderr':
values = {t: 'No SSID', m: `Something is wrong. Are you connected to your wifi?.`}
break
}
notifier.notify({
title: values.t,
message: values.m,
sound: false,
wait: false,
icon: icon,
//appID: 'com.wtfisthis.wificostchanger' //uncomment before make the exe
})
}
onStart()
// Fire a quick time check when windows wakeup after sleep/hibernation
let lastTime = new Date().getTime()
const timeOut = 2000
setInterval(()=>{
let currentTime = new Date().getTime()
if (currentTime > lastTime + timeOut + 10000) {
onStart()
}
lastTime = currentTime
}, timeOut)