Skip to content

Support IP/CIDR rules conversion from SwitchyOmega#538

Open
OriginVorfeed wants to merge 3 commits into
salarcode:masterfrom
OriginVorfeed:fix-ip-address-lib-missing
Open

Support IP/CIDR rules conversion from SwitchyOmega#538
OriginVorfeed wants to merge 3 commits into
salarcode:masterfrom
OriginVorfeed:fix-ip-address-lib-missing

Conversation

@OriginVorfeed
Copy link
Copy Markdown

@OriginVorfeed OriginVorfeed commented May 15, 2026

Currently, IP conditions from SwitchyOmega cannot be converted into internal RegExp rules.

This PR implements the conversion logic using Utils.ipCidrNotationToRegExp to ensure IP-based routing works correctly in PAC scripts.

Tested locally with these rules.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the SwitchyOmega rule importer/compiler so that IP/CIDR-based conditions can be converted into SmartProxy’s internal regex-based rule format, enabling IP-based routing rules to be carried through to generated rules/PAC-compatible expressions.

Changes:

  • Add ip-address dependency and wire RuleImporterSwitchy.js to use Address4/Address6 for IP parsing.
  • Update IpCondition.compile to convert CIDR ranges into a RegExp via Utils.ipCidrNotationToRegExp (instead of PAC isInNet/isInNetEx calls), making the condition convertible to internal regex rules.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/lib/RuleImporterSwitchy.js Adds IP parsing dependency and switches CIDR compilation from isInNet* to Utils.ipCidrNotationToRegExp + regex test for host matching.
package.json Adds runtime dependency on ip-address.
package-lock.json Locks ip-address and its transitive dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

operator: '&&',
right: hostIsInNet
});
return this.regTest('host', regex);
Comment on lines +9 to +14
const Utils = require('./Utils').Utils;
const { Address4, Address6 } = require('ip-address');
const IP = {
v4: { Address: Address4 },
v6: { Address: Address6 }
};
Comment on lines +882 to +886
let regex = Utils.ipCidrNotationToRegExp(cache.normalized, cache.addr.parsedSubnet);
if (!regex) {
throw new Error(`Failed to compile CIDR rule ${cache.normalized}/${cache.addr.parsedSubnet} to regex`);
}
hostIsInNet = new U2.AST_Call({
expression: new U2.AST_SymbolRef({
name: 'isInNet'
}),
args: [
new U2.AST_SymbolRef({
name: 'host'
}), new U2.AST_String({
value: cache.normalized
}), new U2.AST_String({
value: cache.mask
})
]
});
if (!cache.addr.v4) {
hostIsInNetEx = new U2.AST_Call({
expression: new U2.AST_SymbolRef({
name: 'isInNetEx'
}),
args: [
new U2.AST_SymbolRef({
name: 'host'
}), new U2.AST_String({
value: cache.normalized + cache.addr.subnet
})
]
});
hostIsInNet = new U2.AST_Conditional({
condition: new U2.AST_Binary({
left: new U2.AST_UnaryPrefix({
operator: 'typeof',
expression: new U2.AST_SymbolRef({
name: 'isInNetEx'
})
}),
operator: '===',
right: new U2.AST_String({
value: 'function'
})
}),
consequent: hostIsInNetEx,
alternative: hostIsInNet
});
}
return new U2.AST_Binary({
left: hostLooksLikeIp,
operator: '&&',
right: hostIsInNet
});
return this.regTest('host', regex);
Comment thread package.json
},
"license": "GPL-3.0",
"dependencies": {
"ip-address": "5.8.9",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants