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

How to add port range? #60

Open
sinanbozkus opened this issue May 4, 2023 · 5 comments
Open

How to add port range? #60

sinanbozkus opened this issue May 4, 2023 · 5 comments

Comments

@sinanbozkus
Copy link

sinanbozkus commented May 4, 2023

Hello,

I want to add a port range to a rule but it allows me o add just as short/int; I need to add a port range like "5000-7000" as a string. I can't add 2000 different rules for it.

How can I handle this?

Thank you.

@falahati
Copy link
Owner

falahati commented May 4, 2023

I don't think you can. we are limited by the constraints of the windows firewall here. but I will take a look.

@sinanbozkus
Copy link
Author

sinanbozkus commented May 4, 2023

Windows firewall (and its FirewallAPI.dll) allows me to add "5000-7000" as string. All ports will be allowed from 5000 to 7000. But I'd like to use your library, I've already prepared my software with this, I don't want to change it.

Thanks.

@ahwm
Copy link

ahwm commented Jun 22, 2023

I'd like to add my vote for this. In the Windows Firewall console our setting is "21, 990, 5000-5500" but this library just shows "21, 990, 5000, 5001, 5002, ..."

If the API is limited to only individual ports, it would be nice if the library could group them into a range.

@ahwm
Copy link

ahwm commented Jun 30, 2023

So in my solution I just added an extension method to IFirewallRule based on this answer: https://codereview.stackexchange.com/a/219223/274517

I now have

rule.AsRangeString()

which outputs something like this: 5004-5005, 7777-7781, 50004-50013

image

Which is very much like Windows Firewall shows it

image

I actually copied both Ranges and Str from that answer and renamed Str to RangeStr and added the extension method like so:

public static string AsRangeString(this IFirewallRule rule) => RangeStr(Ranges(rule.LocalPorts.Select(x => Convert.ToInt32(x)).ToList()));

I could have modified the original answer code to use ushort instead of int and then I would have only needed to convert the ushort[] to a List<ushort> but a List<int> is more usable and I wouldn't have to have multiple versions of it.

@humbleice
Copy link

There's a class in this repo called PortHelper with a method for converting port strings (including those with ranges) into an array of ports. However, the class is internal so you can't use it directly. Ideally, there would be a public method that allows setting ports for a rule using a string value, which uses PortHelper.StringToPorts. There's a similar class mapping IpAddress string that would also be useful to have for public consumption.

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

4 participants