2020-02-06 20:42:46 -05:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-19 18:13:48 +00:00
|
|
|
"context"
|
2020-02-06 20:42:46 -05:00
|
|
|
"net"
|
2020-07-11 21:03:55 +00:00
|
|
|
"sync"
|
2020-02-06 20:42:46 -05:00
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/routing"
|
2020-02-06 20:42:46 -05:00
|
|
|
"github.com/qdm12/golibs/command"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-23 19:12:16 +00:00
|
|
|
type Config struct { //nolint:maligned
|
2022-03-13 13:26:09 +00:00
|
|
|
runner command.Runner
|
|
|
|
|
logger Logger
|
|
|
|
|
iptablesMutex sync.Mutex
|
|
|
|
|
ip6tablesMutex sync.Mutex
|
|
|
|
|
defaultRoutes []routing.DefaultRoute
|
|
|
|
|
localNetworks []routing.LocalNetwork
|
2020-07-11 21:03:55 +00:00
|
|
|
|
2021-04-19 14:35:29 -04:00
|
|
|
// Fixed state
|
2022-02-26 22:55:22 +00:00
|
|
|
ipTables string
|
|
|
|
|
ip6Tables string
|
2021-07-23 16:06:19 +00:00
|
|
|
customRulesPath string
|
2021-04-19 14:35:29 -04:00
|
|
|
|
2020-07-11 21:03:55 +00:00
|
|
|
// State
|
2020-07-20 00:39:59 +00:00
|
|
|
enabled bool
|
2021-08-19 14:09:41 +00:00
|
|
|
vpnConnection models.Connection
|
2021-08-19 23:22:55 +00:00
|
|
|
vpnIntf string
|
2020-10-29 19:23:44 -04:00
|
|
|
outboundSubnets []net.IPNet
|
2022-03-13 13:26:09 +00:00
|
|
|
allowedInputPorts map[uint16]map[string]struct{} // port to interfaces set mapping
|
2020-07-20 00:39:59 +00:00
|
|
|
stateMutex sync.Mutex
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-26 22:55:22 +00:00
|
|
|
// NewConfig creates a new Config instance and returns an error
|
|
|
|
|
// if no iptables implementation is available.
|
|
|
|
|
func NewConfig(ctx context.Context, logger Logger,
|
2022-03-13 13:26:09 +00:00
|
|
|
runner command.Runner, defaultRoutes []routing.DefaultRoute,
|
|
|
|
|
localNetworks []routing.LocalNetwork) (config *Config, err error) {
|
2022-03-30 08:39:32 +00:00
|
|
|
iptables, err := checkIptablesSupport(ctx, runner, "iptables", "iptables-nft")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ip6tables, err := findIP6tablesSupported(ctx, runner)
|
2022-02-26 22:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 19:04:17 +00:00
|
|
|
return &Config{
|
2021-07-24 17:59:22 +00:00
|
|
|
runner: runner,
|
2021-05-12 22:57:15 +00:00
|
|
|
logger: logger,
|
2022-03-13 13:26:09 +00:00
|
|
|
allowedInputPorts: make(map[uint16]map[string]struct{}),
|
2022-02-26 22:55:22 +00:00
|
|
|
ipTables: iptables,
|
2022-03-30 08:39:32 +00:00
|
|
|
ip6Tables: ip6tables,
|
2021-07-23 16:06:19 +00:00
|
|
|
customRulesPath: "/iptables/post-rules.txt",
|
2021-07-23 19:04:17 +00:00
|
|
|
// Obtained from routing
|
2022-03-13 13:26:09 +00:00
|
|
|
defaultRoutes: defaultRoutes,
|
|
|
|
|
localNetworks: localNetworks,
|
2022-02-26 22:55:22 +00:00
|
|
|
}, nil
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|