Files
parcer/server/src/routes/proxy.ts

76 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-10-01 15:28:37 +05:30
import { Router, Request, Response } from 'express';
2024-10-02 23:47:45 +05:30
import User from '../models/User';
2024-10-05 16:02:06 +05:30
import { encrypt, decrypt } from '../utils/auth';
2024-10-02 23:49:39 +05:30
import { requireSignIn } from '../middlewares/auth';
2024-10-02 23:47:32 +05:30
2024-10-01 15:28:37 +05:30
export const router = Router();
2024-10-02 23:50:12 +05:30
interface AuthenticatedRequest extends Request {
user?: { id: string };
}
router.post('/config', requireSignIn, async (req: AuthenticatedRequest, res: Response) => {
2024-10-02 23:47:32 +05:30
const { server_url, username, password } = req.body;
2024-10-01 15:28:37 +05:30
try {
2024-10-02 23:47:32 +05:30
2024-10-02 23:51:55 +05:30
if (!req.user) {
return res.status(401).json({ ok: false, error: 'Unauthorized' });
}
2024-10-02 23:52:09 +05:30
2024-10-02 23:51:55 +05:30
const user = await User.findByPk(req.user.id, {
attributes: { exclude: ['password'] },
});
2024-10-02 23:47:32 +05:30
if (!user) {
2024-10-02 23:51:55 +05:30
return res.status(404).json({ message: 'User not found' });
}
if (!server_url) {
return res.status(400).send('Proxy URL is required');
2024-10-01 15:28:37 +05:30
}
2024-10-02 23:47:32 +05:30
2024-10-05 15:59:31 +05:30
const encryptedProxyUrl = encrypt(server_url);
let encryptedProxyUsername: string | null = null;
let encryptedProxyPassword: string | null = null;
2024-10-02 23:47:32 +05:30
if (username && password) {
2024-10-05 15:59:31 +05:30
encryptedProxyUsername = encrypt(username);
encryptedProxyPassword = encrypt(password);
2024-10-02 23:47:32 +05:30
} else if (username && !password) {
return res.status(400).send('Proxy password is required when proxy username is provided');
}
2024-10-05 15:59:31 +05:30
user.proxy_url = encryptedProxyUrl;
user.proxy_username = encryptedProxyUsername;
user.proxy_password = encryptedProxyPassword;
2024-10-02 23:47:32 +05:30
await user.save();
res.status(200).send('Proxy configuration saved successfully');
2024-10-01 15:28:37 +05:30
} catch (error: any) {
2024-10-02 23:47:32 +05:30
res.status(500).send(`Could not save proxy configuration - ${error.message}`);
2024-10-01 15:28:37 +05:30
}
2024-10-02 23:47:32 +05:30
});
2024-10-05 16:01:07 +05:30
2024-10-05 16:01:49 +05:30
// TODO: Move this from here
2024-10-05 16:01:27 +05:30
export const getDecryptedProxyConfig = async (userId: string) => {
2024-10-05 16:01:07 +05:30
const user = await User.findByPk(userId, {
attributes: ['proxy_url', 'proxy_username', 'proxy_password'],
});
if (!user) {
throw new Error('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;
return {
proxy_url: decryptedProxyUrl,
proxy_username: decryptedProxyUsername,
proxy_password: decryptedProxyPassword,
};
};