2020-07-11 21:03:55 +00:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2021-07-23 17:36:08 +00:00
|
|
|
"strconv"
|
2020-07-11 21:03:55 +00:00
|
|
|
)
|
|
|
|
|
|
2021-07-23 19:12:16 +00:00
|
|
|
func (c *Config) SetAllowedPort(ctx context.Context, port uint16, intf string) (err error) {
|
2020-07-11 21:03:55 +00:00
|
|
|
c.stateMutex.Lock()
|
|
|
|
|
defer c.stateMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
if port == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.enabled {
|
2020-07-20 00:39:59 +00:00
|
|
|
c.logger.Info("firewall disabled, only updating allowed ports internal state")
|
2022-03-13 13:26:09 +00:00
|
|
|
existingInterfaces, ok := c.allowedInputPorts[port]
|
|
|
|
|
if !ok {
|
|
|
|
|
existingInterfaces = make(map[string]struct{})
|
|
|
|
|
}
|
|
|
|
|
existingInterfaces[intf] = struct{}{}
|
|
|
|
|
c.allowedInputPorts[port] = existingInterfaces
|
2020-07-11 21:03:55 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 13:26:09 +00:00
|
|
|
netInterfaces, has := c.allowedInputPorts[port]
|
|
|
|
|
if !has {
|
|
|
|
|
netInterfaces = make(map[string]struct{})
|
|
|
|
|
} else if _, exists := netInterfaces[intf]; exists {
|
|
|
|
|
return nil
|
2020-07-11 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-13 13:26:09 +00:00
|
|
|
c.logger.Info("setting allowed input port " + fmt.Sprint(port) + " through interface " + intf + "...")
|
|
|
|
|
|
2020-07-11 21:03:55 +00:00
|
|
|
const remove = false
|
2020-07-20 00:39:59 +00:00
|
|
|
if err := c.acceptInputToPort(ctx, intf, port, remove); err != nil {
|
2023-04-01 16:53:04 +00:00
|
|
|
return fmt.Errorf("allowing input to port %d through interface %s: %w",
|
2022-03-13 13:26:09 +00:00
|
|
|
port, intf, err)
|
2020-07-11 21:03:55 +00:00
|
|
|
}
|
2022-03-13 13:26:09 +00:00
|
|
|
netInterfaces[intf] = struct{}{}
|
2022-11-11 09:19:03 +00:00
|
|
|
c.allowedInputPorts[port] = netInterfaces
|
2020-07-11 21:03:55 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 19:12:16 +00:00
|
|
|
func (c *Config) RemoveAllowedPort(ctx context.Context, port uint16) (err error) {
|
2020-07-11 21:03:55 +00:00
|
|
|
c.stateMutex.Lock()
|
|
|
|
|
defer c.stateMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
if port == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.enabled {
|
|
|
|
|
c.logger.Info("firewall disabled, only updating allowed ports internal list")
|
2020-07-20 00:39:59 +00:00
|
|
|
delete(c.allowedInputPorts, port)
|
2020-07-11 21:03:55 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 13:26:09 +00:00
|
|
|
c.logger.Info("removing allowed port " + strconv.Itoa(int(port)) + "...")
|
2020-07-11 21:03:55 +00:00
|
|
|
|
2022-03-13 13:26:09 +00:00
|
|
|
interfacesSet, ok := c.allowedInputPorts[port]
|
2020-07-20 00:39:59 +00:00
|
|
|
if !ok {
|
2020-07-11 21:03:55 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const remove = true
|
2022-03-13 13:26:09 +00:00
|
|
|
for netInterface := range interfacesSet {
|
|
|
|
|
err := c.acceptInputToPort(ctx, netInterface, port, remove)
|
|
|
|
|
if err != nil {
|
2023-04-01 16:53:04 +00:00
|
|
|
return fmt.Errorf("removing allowed port %d on interface %s: %w",
|
2022-03-13 13:26:09 +00:00
|
|
|
port, netInterface, err)
|
|
|
|
|
}
|
|
|
|
|
delete(interfacesSet, netInterface)
|
2020-07-11 21:03:55 +00:00
|
|
|
}
|
2022-03-13 13:26:09 +00:00
|
|
|
|
|
|
|
|
// All interfaces were removed successfully, so remove the port entry.
|
2020-07-20 00:39:59 +00:00
|
|
|
delete(c.allowedInputPorts, port)
|
2020-07-11 21:03:55 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|