From 74aeea7ec352969c72fb53db189805ce1a92ea4a Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sat, 26 Oct 2024 05:22:09 +0530 Subject: [PATCH] feat: test proxy connection --- server/src/routes/proxy.ts | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts index 5e3383ee..11e0af00 100644 --- a/server/src/routes/proxy.ts +++ b/server/src/routes/proxy.ts @@ -1,4 +1,5 @@ import { Router, Request, Response } from 'express'; +import { chromium } from "playwright"; import User from '../models/User'; import { encrypt, decrypt } from '../utils/auth'; import { requireSignIn } from '../middlewares/auth'; @@ -53,6 +54,49 @@ router.post('/config', requireSignIn, async (req: AuthenticatedRequest, res: Res } }); +router.post('/proxy/test', requireSignIn, async (req: AuthenticatedRequest, res: Response) => { + const { server_url, username, password } = req.body; + + try { + if (!req.user) { + return res.status(401).json({ ok: false, error: 'Unauthorized' }); + } + + const user = await User.findByPk(req.user.id, { + attributes: ['proxy_url', 'proxy_username', 'proxy_password'], + }); + + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + const decryptedProxyUrl = user.proxy_url ? decrypt(user.proxy_url) : null; + const decryptedProxyUsername = user.proxy_username ? decrypt(user.proxy_username) : null; + const decryptedProxyPassword = user.proxy_password ? decrypt(user.proxy_password) : null; + + const proxyOptions: any = { + server: decryptedProxyUrl, + ...(decryptedProxyUsername && decryptedProxyPassword && { + username: decryptedProxyUsername, + password: decryptedProxyPassword, + }), + }; + + const browser = await chromium.launch({ + headless: true, + proxy: proxyOptions, + }); + const page = await browser.newPage(); + await page.goto('https://example.com'); + await browser.close(); + + res.status(200).send({ success: true }); + } catch (error) { + res.status(500).send({ success: false, error: 'Proxy connection failed' }); + } +}); + + // TODO: Move this from here export const getDecryptedProxyConfig = async (userId: string) => { const user = await User.findByPk(userId, {