Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using subnetMatch in TypeScript? #170

Open
Manouchehri opened this issue Sep 10, 2023 · 1 comment
Open

Using subnetMatch in TypeScript? #170

Manouchehri opened this issue Sep 10, 2023 · 1 comment

Comments

@Manouchehri
Copy link

Related to #94. The example given does not work for me:

const rangeList = {
  documentationOnly: [ ipaddr.parse('2001:db8::'), 32 ],
  tunnelProviders: [
    [ ipaddr.parse('2001:470::'), 32 ], // he.net
    [ ipaddr.parse('2001:5c0::'), 32 ]  // freenet6
  ]
};
ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "tunnelProviders"

But.. This works:

const rangeList: {
  documentationOnly: [ipaddr.IPv6|ipaddr.IPv4, number];
  tunnelProviders: [ipaddr.IPv6|ipaddr.IPv4, number][];
} = {
  documentationOnly: [ipaddr.parse('2001:db8::'), 32],
  tunnelProviders: [
    [ipaddr.parse('2001:470::'), 32], // he.net
    [ipaddr.parse('2001:5c0::'), 32]  // freenet6
  ]
};

ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "tunnelProviders"       

I'm not really sure if this is the cleanest solution. Also, it isn't copy/pastable for those using JavaScript instead of TypeScript.

@Manouchehri
Copy link
Author

Ah, found a clean solution!

const rangeList = {
  documentationOnly: ipaddr.parseCIDR('2001:db8::/32'),
  tunnelProviders: [
    ipaddr.parseCIDR("2001:470::/32"), // he.net
    ipaddr.parseCIDR("2001:5c0::/32") // freenet6
  ]
};

ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "tunnelProviders"

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

No branches or pull requests

1 participant