Skip to content

Support IP/CIDR rules conversion from SwitchyOmega#538

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

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",
@salarcode
Copy link
Copy Markdown
Owner

Thank you for this PR I had forgotten about this missing feature.

I think your PR is importing ip-address is unnecessary and we can achieve the intention easily.

I have asked AI to fix this and it came up with #541
have a look please, seems to be working fine

@OriginVorfeed
Copy link
Copy Markdown
Author

@salarcode

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.

@salarcode
Copy link
Copy Markdown
Owner

#542 is looking good now, i'm going to merge it for release

@OriginVorfeed
Copy link
Copy Markdown
Author

@salarcode

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.

@salarcode
Copy link
Copy Markdown
Owner

salarcode commented May 19, 2026

Thanks @OriginVorfeed you helped a lot.
the main problem I had with this PR was addition of a npm package, seemed unnecessary.

I forgot about line 144 of ProxyEngineChrome.ts I had it in the other PR, damn it

@OriginVorfeed
Copy link
Copy Markdown
Author

@salarcode

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.

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.

3 participants