Support IP/CIDR rules conversion from SwitchyOmega#538
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); |
| const Utils = require('./Utils').Utils; | ||
| const { Address4, Address6 } = require('ip-address'); | ||
| const IP = { | ||
| v4: { Address: Address4 }, | ||
| v6: { Address: Address6 } | ||
| }; |
| 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", |
|
Thank you for this PR I had forgotten about this missing feature. I think your PR is importing I have asked AI to fix this and it came up with #541 |
|
Hi, thank you for the reply. There’s quite a bit of new code in #541, so I’m going to review it properly to understand the implementation. I'll reply to that PR. |
|
#542 is looking good now, i'm going to merge it for release |
|
Thanks for the updates. I tested #542. The generated PAC script looks almost the same as this PR, with just one exception at line 144 of ProxyEngineChrome.ts . Although it doesn't affect the current output, it might cause potential issues down the road. |
|
Thanks @OriginVorfeed you helped a lot. I forgot about line 144 of ProxyEngineChrome.ts I had it in the other PR, damn it |
|
I didn’t do much, you did all the hard work. The main reason I imported the npm package was that its specific version fit our code perfectly at the time. Now that we have our own implementation, I'll close this PR. Thanks again for the updates. |
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.