Refactor VPN proxy client implementation
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { IncomingMessage, ServerResponse } from 'node:http';
|
||||
|
||||
import type { ConnectionService } from '../../features/connection/index.js';
|
||||
|
||||
interface ConnectionRuntimeRouteDependencies {
|
||||
connection: Pick<ConnectionService, 'stop' | 'restart'>;
|
||||
withOperation<T>(kind: string, operation: () => Promise<T>): Promise<T>;
|
||||
sendState(res: ServerResponse, extra: { singboxRunning: boolean }): Promise<void>;
|
||||
}
|
||||
|
||||
export function createConnectionRuntimeRoute(dependencies: ConnectionRuntimeRouteDependencies) {
|
||||
return {
|
||||
async handle(req: IncomingMessage, res: ServerResponse) {
|
||||
if (req.method === 'POST' && req.url === '/api/singbox/stop') {
|
||||
await dependencies.withOperation('stop', () => dependencies.connection.stop());
|
||||
await dependencies.sendState(res, { singboxRunning: false });
|
||||
return true;
|
||||
}
|
||||
if (req.method === 'POST' && req.url === '/api/singbox/restart') {
|
||||
await dependencies.withOperation('start', () => dependencies.connection.restart());
|
||||
await dependencies.sendState(res, { singboxRunning: true });
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user