Skip to content

Commit 356f22d

Browse files
committed
Optimize code and remove unused code
1 parent 2f2093d commit 356f22d

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

src/plugins/auth-proxy-adapter/backend/main.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import net from 'net';
22

3-
import * as console from 'node:console';
4-
53
import { SocksClient, SocksClientOptions } from 'socks';
64

75
import is from 'electron-is';
@@ -24,9 +22,9 @@ const parseSocksUrl = (socksUrl: string) => {
2422
return {
2523
host: url.hostname,
2624
port: parseInt(url.port, 10),
27-
type: (url.protocol === 'socks5:' ? 5 : 4) as number,
28-
username: url.username || undefined,
29-
password: url.password || undefined,
25+
type: url.protocol === 'socks5:' ? 5 : 4,
26+
username: url.username,
27+
password: url.password,
3028
};
3129
};
3230

@@ -46,7 +44,6 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
4644
this.oldConfig = config;
4745
return;
4846
}
49-
console.log('config', config);
5047
this.stopServer();
5148
this.startServer(config);
5249

@@ -78,21 +75,21 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
7875
});
7976

8077
socket.on('error', (err) => {
81-
console.error('[SOCKS] Socket error:', err.message);
78+
console.error(LoggerPrefix, '[SOCKS] Socket error:', err.message);
8279
});
8380
});
8481

8582
// Listen for errors
8683
socksServer.on('error', (err) => {
87-
console.error('[SOCKS Server Error]', err.message);
84+
console.error(LoggerPrefix, '[SOCKS Server Error]', err.message);
8885
});
8986

9087
// Start server
9188
socksServer.listen(port, hostname, () => {
9289
console.log(LoggerPrefix, '===========================================');
9390
console.log(
9491
LoggerPrefix,
95-
`[Auth-Proxy-Adapter] SOCKS proxy enabled at ${hostname}:${port}`,
92+
`[Auth-Proxy-Adapter] Enable SOCKS proxy at socks5://${hostname}:${port}`,
9693
);
9794
console.log(
9895
LoggerPrefix,
@@ -124,7 +121,7 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
124121
this.processSocks5Request(clientSocket, data, upstreamProxyUrl);
125122
});
126123
} else {
127-
// Client doesn't support our desired authentication method
124+
// Authentication methods not supported by the client
128125
clientSocket.write(Buffer.from([0x05, 0xff]));
129126
clientSocket.end();
130127
}
@@ -155,7 +152,7 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
155152
targetHost = `${data[4]}.${data[5]}.${data[6]}.${data[7]}`;
156153
targetPort = data.readUInt16BE(8);
157154
} else if (atyp === 0x03) {
158-
// Domain name
155+
// Domain
159156
const hostLen = data[4];
160157
targetHost = data.subarray(5, 5 + hostLen).toString();
161158
targetPort = data.readUInt16BE(5 + hostLen);
@@ -223,17 +220,17 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
223220
clientSocket.pipe(proxySocket);
224221

225222
proxySocket.on('error', (error) => {
226-
console.error('[SOCKS5] Proxy socket error:', error);
223+
console.error(LoggerPrefix, '[SOCKS5] Proxy socket error:', error);
227224
if (clientSocket.writable) clientSocket.end();
228225
});
229226

230227
clientSocket.on('error', (error) => {
231-
console.error('[SOCKS5] Client socket error:', error);
228+
console.error(LoggerPrefix, '[SOCKS5] Client socket error:', error);
232229
if (proxySocket.writable) proxySocket.end();
233230
});
234231
})
235232
.catch((error) => {
236-
console.error('[SOCKS5] Connection error:', error);
233+
console.error(LoggerPrefix, '[SOCKS5] Connection error:', error);
237234
// Send failure response to client
238235
clientSocket.write(
239236
Buffer.from([0x05, 0x05, 0x00, 0x01, 0, 0, 0, 0, 0, 0]),
@@ -304,17 +301,17 @@ export const backend = createBackend<BackendType, AuthProxyConfig>({
304301
clientSocket.pipe(proxySocket);
305302

306303
proxySocket.on('error', (error) => {
307-
console.error('[SOCKS4] Proxy socket error:', error);
304+
console.error(LoggerPrefix, '[SOCKS4] Proxy socket error:', error);
308305
if (clientSocket.writable) clientSocket.end();
309306
});
310307

311308
clientSocket.on('error', (error) => {
312-
console.error('[SOCKS4] Client socket error:', error);
309+
console.error(LoggerPrefix, '[SOCKS4] Client socket error:', error);
313310
if (proxySocket.writable) proxySocket.end();
314311
});
315312
})
316313
.catch((error) => {
317-
console.error('[SOCKS4] Connection error:', error);
314+
console.error(LoggerPrefix, '[SOCKS4] Connection error:', error);
318315
// Send failure response to client
319316
clientSocket.write(Buffer.from([0x00, 0x5b, 0, 0, 0, 0, 0, 0]));
320317
clientSocket.end();

src/plugins/auth-proxy-adapter/backend/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import type { Server } from 'http';
66
export type BackendType = {
77
server?: Server | net.Server;
88
oldConfig?: AuthProxyConfig;
9-
startServer: (server_config: AuthProxyConfig) => void;
9+
startServer: (serverConfig: AuthProxyConfig) => void;
1010
stopServer: () => void;
11-
proxyString?: string;
12-
_savedProxy?: string;
1311
handleSocks5: (
1412
clientSocket: net.Socket,
1513
chunk: Buffer,

src/plugins/auth-proxy-adapter/menu.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export const onMenu = async ({
3636
config.hostname ??
3737
defaultAuthProxyConfig.hostname;
3838

39-
if (newHostname !== config.hostname) {
40-
setConfig({ ...config, hostname: newHostname });
41-
}
39+
setConfig({ ...config, hostname: newHostname });
4240
},
4341
},
4442
{
@@ -63,9 +61,7 @@ export const onMenu = async ({
6361
config.port ??
6462
defaultAuthProxyConfig.port;
6563

66-
if (newPort !== config.port) {
67-
setConfig({ ...config, port: newPort });
68-
}
64+
setConfig({ ...config, port: newPort });
6965
},
7066
},
7167
];

0 commit comments

Comments
 (0)