Created
December 10, 2020 17:14
-
-
Save jchadwick/9eb9e8a2b3e6ce01491d5b05050cd47a to your computer and use it in GitHub Desktop.
Tedious encrypted MSSQL connection
This file contains hidden or 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
var Connection = require("tedious").Connection; | |
var Request = require("tedious").Request; | |
const server = "sqlproxy"; | |
const port = 10009; | |
const database = "database"; | |
const user = "user"; | |
const password = "ffffffff"; | |
const query = `select app_name()`; | |
var connection = new Connection({ | |
server, | |
authentication: { | |
type: "default", | |
options: { | |
userName: user, | |
password, | |
}, | |
}, | |
options: { | |
encrypt: true, | |
trustServerCertificate: true, | |
database, | |
port, | |
enableArithAbort: false, | |
debug: { | |
packet: true, | |
data: true, | |
payload: true, | |
token: true, | |
log: true, | |
}, | |
}, | |
}); | |
connection.on("debug", (message) => console.log("debug:", message)); | |
connection.on("connect", (err) => { | |
if (err) console.log("Error: ", err); | |
else { | |
console.log("Connected"); | |
Read((x) => console.dir(x)); | |
} | |
}); | |
function Read(callback) { | |
if (connection.state !== connection.STATE.LOGGED_IN) { | |
// Put the request back on the dispatcher if connection is not in LoggedIn state | |
console.log("not logged in - waiting..."); | |
setTimeout(Read, 5, callback); | |
return; | |
} | |
console.log("Reading..."); | |
request = new Request(query, (err) => { | |
if (err) callback(err); | |
else callback(`done`); | |
}); | |
request.on("row", (columns) => | |
columns.forEach((column) => console.log(column.value)) | |
); | |
connection.execSql(request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment