Skip to content

Instantly share code, notes, and snippets.

@JuoCode
Last active July 18, 2016 08:00
Show Gist options
  • Save JuoCode/ccabcfc4dd0ec123494c24eee2ed0ab5 to your computer and use it in GitHub Desktop.
Save JuoCode/ccabcfc4dd0ec123494c24eee2ed0ab5 to your computer and use it in GitHub Desktop.
/// <reference path="../../typings/index.d.ts" />
const fs = require('fs')
const regedit = require('regedit')
const path = require('path')
const PBK_FILE = path.join(process.env.APPDATA, 'Microsoft', 'Network', 'Connections', 'pbk', 'rasphone.pbk')
const REGISTRY_PATH_LOCAL_NETWORK = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
const REGISTRY_PATH_CONNECTIONS = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections'
const changeLocalNetwork = (value) => {
return new Promise((resolve, reject) => {
regedit.putValue({
[REGISTRY_PATH_LOCAL_NETWORK]: {
AutoConfigURL: { value, type: 'REG_SZ' }
}
}, err => {
if (err) {
return reject(err)
}
resolve()
})
})
}
const changeAllConnections = (url) => {
// Read phone book to get connects
var pkbContent = ini.parse(fs.readFileSync(PBK_FILE).toString())
var connections = Object.keys(pkbContent)
const itemValue = `F${Array(8).join('\0')}${String.fromCharCode(5)}${Array(12).join('\0')}${String.fromCharCode(url.length)}${Array(4).join('\0')}${url}${Array(33).join('\0')}`
const item = {
value: new Buffer(itemValue).toJSON().data,
type: 'REG_BINARY'
}
return new Promise((resolve, reject) => {
regedit.putValue({
[REGISTRY_PATH_CONNECTIONS]: connections.reduce((obj, conn) => {
obj[conn] = item
return obj
}, {})
}, err => {
if (err) {
return reject(err)
}
resolve()
})
})
}
const setPac = (pacUrl) => {
return Promise.all([
changeLocalNetwork(pacUrl),
changeAllConnections(pacUrl)
])
}
const getLocalNetwork = () => {
return new Promise((resolve, reject) => {
regedit.list(REGISTRY_PATH_LOCAL_NETWORK, (err, result) => {
if (err) {
return reject(err)
}
console.log('RESULT', result[REGISTRY_PATH_LOCAL_NETWORK].values.AutoConfigURL.value)
resolve(result[REGISTRY_PATH_LOCAL_NETWORK].values.AutoConfigURL.value)
})
})
}
const getAllConnections = () => {
return new Promise((resolve, reject) => {
regedit.list(REGISTRY_PATH_CONNECTIONS, (err, result) => {
if (err) {
return reject(err)
}
const conns = result[REGISTRY_PATH_CONNECTIONS].values
Object.keys(conns).map(key => {
// console.log(key)
const bytes = conns[key].value
const length = bytes[20]
const pacUrl = bytes.slice(21, length)
console.log(pacUrl.map(i => String.fromCharCode(i)).join(''));
})
resolve(result[REGISTRY_PATH_CONNECTIONS].values)
})
})
}
const readPac = () => {
return Promise.all([
readLocalNetwork(),
getADSLConnections()
])
}
const clearPac = () => {
}
module.exports = {
setPac,
readPac,
clearPac,
getLocalNetwork,
getAllConnections
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment