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

accept port ranges for ExposePorts config #68

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ The web interface itself cannot add administrative users.
`HelpMail`: The email address that is shown on the prompt page
`Lockout`: Number of times a person can attempt mfa authentication before their account locks
`NAT`: Turn on or off masquerading
`ExposePorts`: Expose ports on the VPN server to the client (adds rules to IPtables) example: [ "443/tcp" ]
`ExposePorts`: Expose ports on the VPN server to the client (adds rules to IPtables) example: [ "443/tcp", "100-200/udp" ]
`CheckUpdates`: If enabled (off by default) the management UI will show an alert if a new version of wag is available. This talks to api.github.com
`MFATemplatesDirectory`: A string path option, when set templates will be queried from disk rather than the embedded copies. Allows you to customise the MFA registration, entry, and success pages, allows custom `js` and `css` in the `MFATemplatesDirectory /custom/` directory
`DownloadConfigFileName`: The filename of the wireguard config that is downloaded, defaults to `wg0.conf`
Expand Down Expand Up @@ -286,7 +286,8 @@ Full config example
{
"Proxied": true,
"ExposePorts": [
"443/tcp"
"443/tcp",
"100-200/udp"
],
"CheckUpdates": true,
"Lockout": 5,
Expand Down
20 changes: 16 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func load(path string) (c Config, err error) {
for _, port := range c.ExposePorts {
parts := strings.Split(port, "/")
if len(parts) < 2 {
return c, errors.New(port + " is not in a valid port format. E.g 80/tcp")
return c, errors.New(port + " is not in a valid port format. E.g 80/tcp, 100-200/udp")
}

if c.Proxied {
Expand All @@ -559,9 +559,21 @@ func load(path string) (c Config, err error) {

switch strings.ToLower(parts[1]) {
case "tcp", "udp":
_, err := strconv.Atoi(parts[0])
if err != nil {
return c, errors.New(parts[0] + " is not in a valid port number. E.g 80/tcp")
scope := strings.Split(parts[0], "-")
if len(scope) == 2 {
start, errStart := strconv.Atoi(scope[0])
end, errEnd := strconv.Atoi(scope[1])
if (errStart != nil) || ( errEnd != nil) {
return c, errors.New(parts[0] + " Could not convert lower port or upper port to number. E.g 100:200/udp")
}
if (end < start) {
return c, errors.New(parts[0] + " port have to be smaller than end port . E.g 100-200/udp")
}
} else {
_, err := strconv.Atoi(parts[0])
if err != nil {
return c, errors.New(parts[0] + " is not in a valid port number. E.g 80/tcp, 100-200/udp")
}
}
default:
return c, errors.New(port + " invalid protocol (supports tcp/udp)")
Expand Down
6 changes: 5 additions & 1 deletion internal/config/test_in_memory_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"Address": "10.2.43.1/24",
"MTU": 1420
},
"ExposePorts": [
"443/tcp",
"100-200/udp"
],
"Acls": {
"Groups": {
"group:nerds": [
Expand Down Expand Up @@ -71,4 +75,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions internal/router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func TearDown() {
for _, port := range config.Values().ExposePorts {
parts := strings.Split(port, "/")
if len(parts) < 2 {
log.Println(port + " is not in a valid port format. E.g 80/tcp")
log.Println(port + " is not in a valid port format. E.g 80/tcp, 100-200/tcp")
}

err = ipt.Delete("filter", "INPUT", "-m", parts[1], "-p", parts[1], "-i", config.Values().Wireguard.DevName, "--dport", parts[0], "-j", "ACCEPT")
err = ipt.Delete("filter", "INPUT", "-m", parts[1], "-p", parts[1], "-i", config.Values().Wireguard.DevName, "--dport", strings.Replace(parts[0], "-", ":", 1), "-j", "ACCEPT")
if err != nil {
log.Println("unable to cleanup custom defined port", port, ":", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/router/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func setupIptables() error {
return errors.New(port + " is not in a valid port format. E.g 80/tcp")
}

err = ipt.Append("filter", "INPUT", "-m", parts[1], "-p", parts[1], "-i", devName, "--dport", parts[0], "-j", "ACCEPT")
err = ipt.Append("filter", "INPUT", "-m", parts[1], "-p", parts[1], "-i", devName, "--dport", strings.Replace(parts[0], "-", ":", 1), "-j", "ACCEPT")
if err != nil {
return err
}
Expand Down