Last active
April 4, 2023 01:49
-
-
Save nirev/11a4e9bf9e0c396cb6e20af5c6f112b3 to your computer and use it in GitHub Desktop.
Using Proxies with Elixir and Node
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
function getBrowser() { | |
const proxyHost = process.env.PROXY_HOST; | |
const proxyPort = process.env.PROXY_PORT; | |
const proxyServer = `${proxyHost}:${proxyPort}`; | |
const browserlessToken = process.env.BROWSERLESS_TOKEN; | |
if (LOCAL_DEBUG) { | |
return puppeteer.launch({ | |
headless: false, | |
devtools: false, | |
args: [ | |
'--start-maximized', | |
'--no-sandbox', | |
'--disable-setuid-sandbox', | |
proxyHost ? `--proxy-server=${proxyServer}` : ``, | |
], | |
}); | |
} else { | |
let proxy_opt = proxyHost ? `&--proxy-server=${proxyServer}` : `` | |
let endpoint = `wss://chrome.browserless.io?token=${browserlessToken}&--no-sandbox=&--disable-setuid-sandbox=` + proxy_opt; | |
return puppeteer.connect({ browserWSEndpoint: endpoint }); | |
} | |
} | |
const run = async (email, password) => { | |
const userEmail = email; | |
const userPassword = password; | |
const proxyUser = process.env.PROXY_USER; | |
const proxyPass = process.env.PROXY_PASS; | |
let browser = null; | |
let page = null; | |
try { | |
browser = await getBrowser(); | |
page = await browser.newPage(); | |
await page.authenticate({ username: proxyUser, password: proxyPass }); | |
... | |
} catch (error) { | |
console.error(error); | |
return "error:unexpected_error"; | |
} finally { | |
await browser.close(); | |
browser = null; | |
} | |
} |
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
adapter_opts = [ | |
opts: [ | |
adapter: [ | |
:insecure, | |
proxy: {host, port}, | |
proxy_auth: {user, pass} | |
] | |
] | |
] | |
client = | |
Tesla.client( | |
[ | |
{Tesla.Middleware.BaseUrl, "https://example.com/"}, | |
Tesla.Middleware.JSON, | |
Tesla.Middleware.DecompressResponse, | |
Tesla.Middleware.Logger | |
], | |
{Tesla.Adapter.Hackney, adapter_opts} | |
) | |
Tesla.post(client, url, body, query: query, headers: headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment