Created
August 23, 2023 09:30
-
-
Save theArina/75abeeadd2b31cf9f9467e5a7b712d0f to your computer and use it in GitHub Desktop.
Node.js Rasdial module for broken native Rasdial on Windows 11 (22621.2134)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ffi = require('ffi-napi'); // ^4.0.3 | |
const ref = require('ref-napi'); // ^3.0.3 | |
const wchar_t = require('ref-wchar-napi'); // ^1.0.3 | |
const Struct = require('ref-struct-di')(ref); // ^1.1.1 | |
const ArrayType = require('ref-array-di')(ref); // ^1.2.2 | |
const Iconv = require('iconv').Iconv; // ^3.0.1 | |
module.exports = (connectionName, userName, password) => { | |
const RASDIALPARAMS = Struct({ | |
dwSize: ref.types.uint32, | |
szEntryName: ArrayType(wchar_t, 257), | |
szPhoneNumber: ArrayType(wchar_t, 129), | |
szCallbackNumber: ArrayType(wchar_t, 129), | |
szUserName: ArrayType(wchar_t, 257), | |
szPassword: ArrayType(wchar_t, 257), | |
szDomain: ArrayType(wchar_t, 16), | |
dwSubEntry: ref.types.uint32, | |
dwCallbackId: ref.types.uint64, | |
dwIfIndex: ref.types.uint32, | |
}); | |
const librasapi = ffi.Library('rasapi32.dll', { | |
RasDialW: [ | |
'long', | |
[ | |
ref.refType('void'), | |
'string', | |
ref.refType(RASDIALPARAMS), | |
ref.types.uint32, | |
'long', | |
ref.refType('void') | |
] | |
] | |
}); | |
const rasDialParams = new RASDIALPARAMS(); | |
rasDialParams.dwSize = RASDIALPARAMS.size; | |
function fillBuffer(string, value) { | |
const iconv = new Iconv('UTF-8', 'UTF-16' + ref.endianness); | |
const buffer = iconv.convert(value); | |
buffer.copy(string.buffer); | |
} | |
fillBuffer(rasDialParams.szDomain, '*'); | |
fillBuffer(rasDialParams.szEntryName, connectionName); | |
fillBuffer(rasDialParams.szUserName, userName); | |
fillBuffer(rasDialParams.szPassword, password); | |
const out = ref.alloc('uint64 *'); | |
const result = librasapi.RasDialW(null, null, rasDialParams.ref(), 0, 0, out); | |
if (result !== 0) { | |
throw new Error(`Error connecting to VPN. Error code: ${result}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment