Support IP/CIDR rules conversion from SwitchyOmega#538
Open
OriginVorfeed wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
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-addressdependency and wireRuleImporterSwitchy.jsto useAddress4/Address6for IP parsing. - Update
IpCondition.compileto convert CIDR ranges into aRegExpviaUtils.ipCidrNotationToRegExp(instead of PACisInNet/isInNetExcalls), 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); |
| }, | ||
| "license": "GPL-3.0", | ||
| "dependencies": { | ||
| "ip-address": "5.8.9", |
added 2 commits
May 16, 2026 19:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.