Last active
July 2, 2024 03:50
-
-
Save michelbl/cd9fa23968c7554b76567e3c093c9415 to your computer and use it in GitHub Desktop.
Making HTTPS requests with nodejs
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
/* | |
Several ways to make a HTTPS request using nodejs. | |
Going from the most simple ways to the most complicated ones. | |
See https://gist.github.com/mtigas/952344 to setup a test env | |
/* | |
/* | |
HTTPS with client certificate with NodeJS | |
*/ | |
var request = require('request-promise-native'); | |
var options = { | |
method: 'POST', | |
url: '<TARGET_URL>', | |
//strictSSL: true, | |
headers: { | |
... | |
}, | |
qs: { | |
... | |
}, | |
body: { | |
... | |
}, | |
json: true, | |
key: '<CLIENT_KEY>', | |
cert: '<CLIENT_CERTIFICATE>', | |
resolveWithFullResponse: true, | |
timeout: 30000, | |
}; | |
async function makeRequest() { | |
try { | |
var response = await request(options); | |
console.log(response); | |
console.log(response.statusCode); | |
console.log(response.body); | |
} catch(error) { | |
console.error(error); | |
} | |
} | |
makeRequest(); | |
/* | |
HTTPS over HTTP proxy with client certificate with NodeJS | |
*/ | |
var request = require('request-promise-native'); | |
var options = { | |
method: 'POST', | |
proxy: 'http://<USERNAME>:<PASSWORD>@proxyserver.com:9999', | |
url: '<TARGET_URL>', | |
headers: { | |
... | |
}, | |
qs: { | |
... | |
}, | |
body: { | |
... | |
}, | |
//strictSSL: true, | |
json: true, | |
key: '<CLIENT_KEY>', | |
cert: '<CLIENT_CERTIFICATE>', | |
resolveWithFullResponse: true, | |
timeout: 5000, | |
}; | |
async function makeRequest() { | |
try { | |
var response = await request(options); | |
console.log(response); | |
console.log(response.statusCode); | |
console.log(response.body); | |
} catch(error) { | |
console.error(error); | |
} | |
} | |
makeRequest(); | |
/* | |
HTTPS with using core node libraries (http + tls) | |
*/ | |
const tls = require('tls'); | |
const http = require('http'); | |
var tlsOptions = { | |
host: '<TARGET_HOST>', | |
port: '<TARGET_PORT>', | |
servername: '<TARGET_HOST>', | |
rejectUnauthorized: false, | |
key: '<CLIENT_KEY>', | |
cert: '<CLIENT_CERT>', | |
} | |
function createTlsStream(options, callback) { | |
var tlsStream = tls.connect(tlsOptions, () => { | |
console.log('event secureConnect'); | |
console.log(tlsStream.authorized); | |
}); | |
console.log('tls stream created'); | |
return tlsStream; | |
} | |
var httpOptions = { | |
host: '<TARGET_HOST>', | |
port: '<TARGET_PORT>', | |
method: 'GET', | |
path: '/', | |
headers: { | |
'User-Agent': 'node.js', | |
}, | |
createConnection: createTlsStream, | |
}; | |
function makeHttpRequest() { | |
var data = ''; | |
const httpRequest = http.request(httpOptions, (incomingMessage) => { | |
console.log('response event'); | |
console.log(incomingMessage.statusCode); | |
console.log(incomingMessage.headers); | |
incomingMessage.on('data', (chunk) => { | |
data += chunk; | |
}); | |
incomingMessage.on('end', () => { | |
console.log(data); | |
}); | |
}); | |
httpRequest.end(); | |
} | |
makeHttpRequest(); | |
/* | |
HTTPS with client certificate over SOCKS using socks5-https-client | |
*/ | |
var request = require('request-promise-native'); | |
var Agent = require('socks5-https-client/lib/Agent'); | |
var options = { | |
method: 'GET', | |
url: '<TARGET_URL>', | |
//strictSSL: true, | |
agentClass: Agent, | |
agentOptions: { | |
socksHost: '<PROXY_SOCKS_HOST>', | |
socksPort: '<PROXY_SOCKS_PORT>', | |
}, | |
key: '<CLIENT_KEY>', | |
cert: '<CLIENT_CERT>', | |
resolveWithFullResponse: true, | |
timeout: 5000, | |
}; | |
async function makeRequest() { | |
try { | |
var response = await request(options); | |
console.log(response); | |
console.log(response.statusCode); | |
console.log(response.body); | |
} catch(error) { | |
console.error(error); | |
} | |
} | |
makeRequest(); | |
/* | |
HTTPS with client certificate over SOCKS using socks | |
*/ | |
const { SocksClient } = require('socks'); | |
const options = { | |
proxy: { | |
ipaddress: <'PROXY_SOCKS_IP'>, | |
port: <'PROXY_SOCKS_PORT'>, | |
type: 5, | |
userId: <'PROXY_SOCKS_USERNAME>', | |
password: <'PROXY_SOCKS_PASSWORD>', | |
}, | |
command: 'connect', | |
destination: { | |
host: '<TARGET_HOST>', | |
port: '<TARGET_PORT>', | |
}, | |
}; | |
SocksClient.createConnection(options, (err, info) => { | |
if (!err) { | |
console.log('Success!'); | |
console.log(info.socket); | |
} else { | |
console.log('Error!'); | |
console.log(err); | |
} | |
}); | |
console.log('a') | |
/* | |
HTTPS with client certificate over SOCKS using socksjs | |
*/ | |
const SocksConnection = require('socksjs'); | |
var socksRemoteOptions = { | |
host: '<TARGET_HOST>', | |
port: '<TARGET_PORT'>, | |
ssl: true, | |
rejectUnauthorised: false, | |
key: '<CLIENT_KEY>', | |
cert: '<CLIENT_CERT>', | |
} | |
var socksOptions = { | |
host: <'PROXY_SOCKS_HOST>', | |
port: <'PROXY_SOCKS_PORT>', | |
user: <'PROXY_SOCKS_USERNAME>', | |
pass: <'PROXY_SOCKS_PASSWORD>', | |
} | |
SocksConnection.connect(socksRemoteOptions, socksOptions, (err, info) => { | |
if (!err) { | |
console.log('Success!'); | |
console.log(info.socket); | |
} else { | |
console.log('Error!'); | |
console.log(err); | |
} | |
}); | |
console.log('a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment