Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ClashConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ export class ClashConfigBuilder extends BaseConfigBuilder {

addProxyToConfig(proxy) {
this.config.proxies = this.config.proxies || [];

// Find proxies with the same or partially matching name
const similarProxies = this.config.proxies.filter(p => p.name.includes(proxy.name));

// Check if there is a proxy with identical data excluding the 'name' field
const isIdentical = similarProxies.some(p => {
const { name: _, ...restOfProxy } = proxy; // Exclude the 'name' attribute
const { name: __, ...restOfP } = p; // Exclude the 'name' attribute
return JSON.stringify(restOfProxy) === JSON.stringify(restOfP);
});

if (isIdentical) {
// If there is a proxy with identical data, skip adding it
return;
}

// If there are proxies with similar names but different data, modify the name
if (similarProxies.length > 0) {
proxy.name = `${proxy.name} ${similarProxies.length + 1}`;
}

// Add the proxy to the configuration
this.config.proxies.push(proxy);
}

Expand Down Expand Up @@ -251,4 +273,4 @@ export class ClashConfigBuilder extends BaseConfigBuilder {

return yaml.dump(this.config);
}
}
}
20 changes: 20 additions & 0 deletions src/SingboxConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ export class SingboxConfigBuilder extends BaseConfigBuilder {
}

addProxyToConfig(proxy) {
// Check if there are proxies with similar tags in existing outbounds
const similarProxies = this.config.outbounds.filter(p => p.tag && p.tag.includes(proxy.tag));

// Check if there is a proxy with identical data (excluding the tag)
const isIdentical = similarProxies.some(p => {
const { tag: _, ...restOfProxy } = proxy; // Exclude the tag attribute
const { tag: __, ...restOfP } = p; // Exclude the tag attribute
return JSON.stringify(restOfProxy) === JSON.stringify(restOfP);
});

if (isIdentical) {
// If there is a proxy with identical data, skip adding it
return;
}

// If there are proxies with similar tags but different data, modify the tag name
if (similarProxies.length > 0) {
proxy.tag = `${proxy.tag} ${similarProxies.length + 1}`;
}

this.config.outbounds.push(proxy);
}

Expand Down
30 changes: 30 additions & 0 deletions src/SurgeConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,36 @@ export class SurgeConfigBuilder extends BaseConfigBuilder {

addProxyToConfig(proxy) {
this.config.proxies = this.config.proxies || [];

// Get the name of the proxy to be added
const proxyName = this.getProxyName(proxy);

// Check if there are proxies with similar names in existing proxies
const similarProxies = this.config.proxies
.map(p => this.getProxyName(p))
.filter(name => name.includes(proxyName));

// Check if there is a proxy with identical configuration
const isIdentical = this.config.proxies.some(p =>
// Compare the remaining configuration after removing the name part
p.substring(p.indexOf('=')) === proxy.substring(proxy.indexOf('='))
);

if (isIdentical) {
// If there is a proxy with identical configuration, skip adding it
return;
}

// If there are proxies with similar names but different configurations, modify the name
if (similarProxies.length > 0) {
// Get the position of the equals sign
const equalsPos = proxy.indexOf('=');
if (equalsPos > 0) {
// Create a new proxy string with a number appended to the name
proxy = `${proxyName} ${similarProxies.length + 1}${proxy.substring(equalsPos)}`;
}
}

this.config.proxies.push(proxy);
}

Expand Down