Skip to content

Commit 4de4747

Browse files
committed
refactor: simplify host formatting logic in FormatHostsService
- Replaced repetitive code for creating formatted hosts with a new private method, createFallbackHosts, to enhance maintainability. - Updated the handling of empty hosts and user inbounds to utilize the new method
1 parent e2f7cc5 commit 4de4747

File tree

1 file changed

+39
-81
lines changed

1 file changed

+39
-81
lines changed

src/modules/subscription-template/generators/format-hosts.service.ts

Lines changed: 39 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -56,97 +56,31 @@ export class FormatHostsService {
5656
break;
5757
}
5858

59-
specialRemarks.forEach((remark) => {
60-
formattedHosts.push({
61-
remark,
62-
address: '0.0.0.0',
63-
port: 0,
64-
protocol: 'trojan',
65-
path: '',
66-
host: '',
67-
tls: 'tls',
68-
sni: '',
69-
alpn: '',
70-
publicKey: '',
71-
fingerprint: '',
72-
shortId: '',
73-
spiderX: '',
74-
network: 'tcp',
75-
password: {
76-
trojanPassword: '00000',
77-
vlessPassword: randomUUID(),
78-
ssPassword: '00000',
79-
},
80-
});
81-
});
59+
formattedHosts.push(...this.createFallbackHosts(specialRemarks));
8260

8361
return formattedHosts;
8462
}
8563

8664
if (hosts.length === 0 && user.activeUserInbounds.length > 0) {
87-
const emptyHostRemarks = [
88-
'→ Remnawave',
89-
'→ Did you forget to add hosts?',
90-
'→ No hosts found',
91-
];
92-
93-
emptyHostRemarks.forEach((remark) => {
94-
formattedHosts.push({
95-
remark,
96-
address: '0.0.0.0',
97-
port: 0,
98-
protocol: 'trojan',
99-
path: '',
100-
host: '',
101-
tls: 'tls',
102-
sni: '',
103-
alpn: '',
104-
publicKey: '',
105-
fingerprint: '',
106-
shortId: '',
107-
spiderX: '',
108-
network: 'tcp',
109-
password: {
110-
trojanPassword: '00000',
111-
vlessPassword: randomUUID(),
112-
ssPassword: '00000',
113-
},
114-
});
115-
});
65+
formattedHosts.push(
66+
...this.createFallbackHosts([
67+
'→ Remnawave',
68+
'→ Did you forget to add hosts?',
69+
'→ No hosts found',
70+
]),
71+
);
11672

11773
return formattedHosts;
11874
}
11975

12076
if (user.activeUserInbounds.length === 0) {
121-
const emptyHostRemarks = [
122-
'→ Remnawave',
123-
'→ User has no active inbounds',
124-
'→ No active inbounds found',
125-
];
126-
127-
emptyHostRemarks.forEach((remark) => {
128-
formattedHosts.push({
129-
remark,
130-
address: '0.0.0.0',
131-
port: 0,
132-
protocol: 'trojan',
133-
path: '',
134-
host: '',
135-
tls: 'tls',
136-
sni: '',
137-
alpn: '',
138-
publicKey: '',
139-
fingerprint: '',
140-
shortId: '',
141-
spiderX: '',
142-
network: 'tcp',
143-
password: {
144-
trojanPassword: '00000',
145-
vlessPassword: randomUUID(),
146-
ssPassword: '00000',
147-
},
148-
});
149-
});
77+
formattedHosts.push(
78+
...this.createFallbackHosts([
79+
'→ Remnawave',
80+
'→ User has no active inbounds',
81+
'→ No active inbounds found',
82+
]),
83+
);
15084

15185
return formattedHosts;
15286
}
@@ -374,4 +308,28 @@ export class FormatHostsService {
374308

375309
return settingsResponse.response;
376310
}
311+
312+
private createFallbackHosts(remarks: string[]): IFormattedHost[] {
313+
return remarks.map((remark) => ({
314+
remark,
315+
address: '0.0.0.0',
316+
port: 0,
317+
protocol: 'trojan',
318+
path: '',
319+
host: '',
320+
tls: 'tls',
321+
sni: '',
322+
alpn: '',
323+
publicKey: '',
324+
fingerprint: '',
325+
shortId: '',
326+
spiderX: '',
327+
network: 'tcp',
328+
password: {
329+
trojanPassword: '00000',
330+
vlessPassword: randomUUID(),
331+
ssPassword: '00000',
332+
},
333+
}));
334+
}
377335
}

0 commit comments

Comments
 (0)