146 lines
4 KiB
TypeScript
146 lines
4 KiB
TypeScript
|
|
import { database } from '../database/index.js';
|
||
|
|
import { Proxy, ProxyOptions } from '../types/index.js';
|
||
|
|
import logger from '../utils/logger.js';
|
||
|
|
|
||
|
|
export class ProxyModel {
|
||
|
|
static async findAll(): Promise<Proxy[]> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const stmt = db.prepare('SELECT * FROM proxies ORDER BY created_at DESC');
|
||
|
|
const rows = stmt.all() as any[];
|
||
|
|
|
||
|
|
const proxies = rows.map(row => ({
|
||
|
|
...row,
|
||
|
|
options: JSON.parse(row.options)
|
||
|
|
}));
|
||
|
|
return proxies;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error fetching all proxies:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static async findById(id: number): Promise<Proxy | null> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const stmt = db.prepare('SELECT * FROM proxies WHERE id = ?');
|
||
|
|
const row = stmt.get(id) as any;
|
||
|
|
|
||
|
|
if (row) {
|
||
|
|
row.options = JSON.parse(row.options);
|
||
|
|
}
|
||
|
|
return row || null;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error finding proxy by ID:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static async findByDomain(domain: string): Promise<Proxy | null> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const stmt = db.prepare('SELECT * FROM proxies WHERE domain = ?');
|
||
|
|
const row = stmt.get(domain) as any;
|
||
|
|
|
||
|
|
if (row) {
|
||
|
|
row.options = JSON.parse(row.options);
|
||
|
|
}
|
||
|
|
return row || null;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error finding proxy by domain:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static async create(proxyData: Omit<Proxy, 'id' | 'created_at' | 'updated_at'>): Promise<Proxy> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const optionsJson = JSON.stringify(proxyData.options);
|
||
|
|
|
||
|
|
const stmt = db.prepare(`
|
||
|
|
INSERT INTO proxies (domain, target, ssl_type, cert_path, key_path, options)
|
||
|
|
VALUES (?, ?, ?, ?, ?, ?) RETURNING *
|
||
|
|
`);
|
||
|
|
|
||
|
|
const result = stmt.get(
|
||
|
|
proxyData.domain,
|
||
|
|
proxyData.target,
|
||
|
|
proxyData.ssl_type,
|
||
|
|
proxyData.cert_path || null,
|
||
|
|
proxyData.key_path || null,
|
||
|
|
optionsJson
|
||
|
|
) as any;
|
||
|
|
|
||
|
|
result.options = JSON.parse(result.options);
|
||
|
|
return result;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error creating proxy:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static async update(id: number, proxyData: Partial<Proxy>): Promise<Proxy | null> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const fields: string[] = [];
|
||
|
|
const values: any[] = [];
|
||
|
|
|
||
|
|
if (proxyData.domain !== undefined) {
|
||
|
|
fields.push('domain = ?');
|
||
|
|
values.push(proxyData.domain);
|
||
|
|
}
|
||
|
|
if (proxyData.target !== undefined) {
|
||
|
|
fields.push('target = ?');
|
||
|
|
values.push(proxyData.target);
|
||
|
|
}
|
||
|
|
if (proxyData.ssl_type !== undefined) {
|
||
|
|
fields.push('ssl_type = ?');
|
||
|
|
values.push(proxyData.ssl_type);
|
||
|
|
}
|
||
|
|
if (proxyData.cert_path !== undefined) {
|
||
|
|
fields.push('cert_path = ?');
|
||
|
|
values.push(proxyData.cert_path);
|
||
|
|
}
|
||
|
|
if (proxyData.key_path !== undefined) {
|
||
|
|
fields.push('key_path = ?');
|
||
|
|
values.push(proxyData.key_path);
|
||
|
|
}
|
||
|
|
if (proxyData.options !== undefined) {
|
||
|
|
fields.push('options = ?');
|
||
|
|
values.push(JSON.stringify(proxyData.options));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (fields.length === 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
fields.push('updated_at = CURRENT_TIMESTAMP');
|
||
|
|
values.push(id);
|
||
|
|
|
||
|
|
const stmt = db.prepare(`UPDATE proxies SET ${fields.join(', ')} WHERE id = ?`);
|
||
|
|
const result = stmt.run(...values);
|
||
|
|
|
||
|
|
if (result.changes === 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return ProxyModel.findById(id);
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error updating proxy:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static async delete(id: number): Promise<boolean> {
|
||
|
|
try {
|
||
|
|
const db = database.getDb();
|
||
|
|
const stmt = db.prepare('DELETE FROM proxies WHERE id = ?');
|
||
|
|
const result = stmt.run(id);
|
||
|
|
return result.changes > 0;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Error deleting proxy:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|