Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'tunnel' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const { IncomingWebhook } = require('../dist');
// Get a URL by creating an app at , and configuring an Incoming Webhook
// It's always a good idea to keep sensitive data like the url outside your source code. Prefer environment variables.
const url = process.env.SLACK_WEBHOOK_URL || '';
if (!url) { console.log('You must specify a webhook url to use this example'); process.exitCode = 1; return; }
const proxyUrl = process.env.SLACK_PROXY_URL || '';
const proxyPort = process.env.SLACK_PROXY_PORT || '';
const proxyScheme = process.env.SLACK_PROXY_SCHEME || 'http';
const proxyEndpoint = `${proxyScheme}://${proxyUrl}:${proxyPort}`;
// Initialize a custom agent for an HTTP proxy
const tunnel = require('tunnel');
const tunnelAgent = tunnel.httpsOverHttp({
proxy: {
host: proxyUrl,
port: proxyPort
}
});
// Initialize the IncomingWebhook using the custom agent
const webhookTunnelAgent = new IncomingWebhook(url, { agent: tunnelAgent } );
// Send IncomingWebhook Message over tunnel
sendWebhookMessage(webhookTunnelAgent, 'Hello World, over tunnel agent!');
// Initialize a custom agent again, but this time using a different package
const HttpsProxyAgent = require('https-proxy-agent');
const httpsProxyAgent = new HttpsProxyAgent(proxyEndpoint);
});
agent.https = tunnel.httpsOverHttp({
proxy: {
host: config.proxy.host,
port: parseInt(config.proxy.port),
},
});
break;
case 'https':
agent.http = tunnel.httpOverHttps({
proxy: {
host: config.proxy.host,
port: parseInt(config.proxy.port),
},
});
agent.https = tunnel.httpsOverHttps({
proxy: {
host: config.proxy.host,
port: parseInt(config.proxy.port),
},
});
break;
}
}
const requestWrapper = (url, options) => {
// agent
if (agent && new RegExp(config.proxy.url_regex).test(url)) {
let agentResult;
try {
agentResult = agent[(options.protocol || url.match(/(https?:)/)[1]).slice(0, -1)];
} catch (error) {
var headers = (optionalHeaders != null ? optionalHeaders : {});
headers.Authorization = auth;
if (proxy != null)
{
match = proxy.match(/^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i);
/*
if (match) {
host = match[2];
port = (match[4] != null ? match[4] : 80);
path = "https://" + config.url + url;
headers.Host = config.url;
}
*/
tunnelingAgent = tunnel.httpsOverHttp({
proxy: { // Proxy settings
host: match[2],
port: (match[4] != null ? match[4] : 80),
rejectUnauthorized : false
}
});
}
var options = {
"port": port,
"host": host,
"path": path,
"method": method,
"headers": headers,
"rejectUnauthorized": false,
} else if (options.proxy.protocol === 'https') {
delete options.proxy.protocol; // delete self-defined arg
options.agent = tunnel.httpOverHttps({proxy: options.proxy});
} else {
throw options.proxy.protocol + ' proxy is not supported!';
}
}
delete options.protocol; // delete self-defined arg
delete options.proxy; // delete self-defined arg
return http.request(options, callback);
}
if (options.protocol === 'https') {
if (options.proxy) {
if (options.proxy.protocol === 'http') {
delete options.proxy.protocol; // delete self-defined arg
options.agent = tunnel.httpsOverHttp({proxy: options.proxy});
} else if (options.proxy.protocol === 'https') {
delete options.proxy.protocol; // delete self-defined arg
options.agent = tunnel.httpsOverHttps({proxy: options.proxy});
} else {
throw options.proxy.protocol + ' proxy is not supported!';
}
}
delete options.protocol; // delete self-defined arg
delete options.proxy; // delete self-defined arg
return https.request(options, callback);
}
throw 'only allow http or https request!';
}
if (useCONNECT) {
if (conf.proxyHttpsOptions) {
assign(opts.proxy, conf.proxyHttpsOptions);
}
if (conf.originHttpsOptions) {
assign(opts, conf.originHttpsOptions);
}
if (outerProtocol === 'https:') {
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttps(opts);
}
return tunnel.httpOverHttps(opts);
}
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttp(opts);
}
return tunnel.httpOverHttp(opts);
}
if (conf.originHttpsOptions) {
throw new Error('originHttpsOptions must be combined with a tunnel:true option');
}
if (conf.proxyHttpsOptions) {
// NB: not opts.
assign(opts, conf.proxyHttpsOptions);
}
if (outerProtocol === 'https:') {
return new agents.OuterHttpsAgent(opts);
}
return new agents.OuterHttpAgent(opts);
};
if (!!this.options.connectionPolicy.ProxyUrl) {
const proxyUrl = url.parse(this.options.connectionPolicy.ProxyUrl);
const port = parseInt(proxyUrl.port, 10);
requestAgentOptions.proxy = {
host: proxyUrl.hostname,
port,
headers: {}
};
if (!!proxyUrl.auth) {
requestAgentOptions.proxy.proxyAuth = proxyUrl.auth;
}
this.options.agent =
proxyUrl.protocol.toLowerCase() === "https:"
? tunnel.httpsOverHttps(requestAgentOptions)
: tunnel.httpsOverHttp(requestAgentOptions); // TODO: type coersion
} else {
this.options.agent = new Agent(requestAgentOptions); // TODO: Move to request?
}
}
const globalEndpointManager = new GlobalEndpointManager(this.options, async (opts: RequestOptions) =>
this.getDatabaseAccount(opts)
);
this.clientContext = new ClientContext(options, globalEndpointManager);
this.databases = new Databases(this, this.clientContext);
this.offers = new Offers(this, this.clientContext);
}
proxy: pick(conf, 'host', 'port', 'protocol', 'localAddress', 'proxyAuth'),
maxSockets: conf.sockets
};
opts.proxy.innerProtocol = innerProtocol;
if (useCONNECT) {
if (conf.proxyHttpsOptions) {
assign(opts.proxy, conf.proxyHttpsOptions);
}
if (conf.originHttpsOptions) {
assign(opts, conf.originHttpsOptions);
}
if (outerProtocol === 'https:') {
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttps(opts);
}
return tunnel.httpOverHttps(opts);
}
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttp(opts);
}
return tunnel.httpOverHttp(opts);
}
if (conf.originHttpsOptions) {
throw new Error('originHttpsOptions must be combined with a tunnel:true option');
}
if (conf.proxyHttpsOptions) {
// NB: not opts.
assign(opts, conf.proxyHttpsOptions);
}
function doWebSocket(url, headers = {}, isProxy) {
let ws;
if (isProxy) {
headers['via-proxy'] = 'true';
let agent = new tunnel.httpOverHttp({
proxy: {
hostname: SOCKE_PROXY_URL_OBJ.hostname,
port: SOCKE_PROXY_URL_OBJ.port
}
})
if (url.indexOf('wss') === 0) {
agent = new tunnel.httpsOverHttp({
rejectUnauthorized: false,
proxy: {
hostname: SOCKE_PROXY_URL_OBJ.hostname,
port: SOCKE_PROXY_URL_OBJ.port
}
})
}
export function createTunnel(isRequestHttps: boolean, isProxyHttps: boolean, tunnelOptions: tunnel.HttpsOverHttpsOptions): http.Agent | https.Agent {
if (isRequestHttps && isProxyHttps) {
return tunnel.httpsOverHttps(tunnelOptions);
} else if (isRequestHttps && !isProxyHttps) {
return tunnel.httpsOverHttp(tunnelOptions);
} else if (!isRequestHttps && isProxyHttps) {
return tunnel.httpOverHttps(tunnelOptions);
} else {
return tunnel.httpOverHttp(tunnelOptions);
}
}
function createHttpsProxyAgent(host, port) {
return tunnel.httpsOverHttp({
proxy: { host, port }
});
}