Using WithPrefix for loggers
This commit is contained in:
@@ -51,7 +51,7 @@ func main() {
|
|||||||
client := network.NewClient(15 * time.Second)
|
client := network.NewClient(15 * time.Second)
|
||||||
// Create configurators
|
// Create configurators
|
||||||
fileManager := files.NewFileManager()
|
fileManager := files.NewFileManager()
|
||||||
alpineConf := alpine.NewConfigurator(logger, fileManager)
|
alpineConf := alpine.NewConfigurator(fileManager)
|
||||||
ovpnConf := openvpn.NewConfigurator(logger, fileManager)
|
ovpnConf := openvpn.NewConfigurator(logger, fileManager)
|
||||||
dnsConf := dns.NewConfigurator(logger, client, fileManager)
|
dnsConf := dns.NewConfigurator(logger, client, fileManager)
|
||||||
firewallConf := firewall.NewConfigurator(logger)
|
firewallConf := firewall.NewConfigurator(logger)
|
||||||
|
|||||||
@@ -4,11 +4,8 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/files"
|
"github.com/qdm12/golibs/files"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "alpine configurator"
|
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
CreateUser(username string, uid int) error
|
CreateUser(username string, uid int) error
|
||||||
}
|
}
|
||||||
@@ -19,7 +16,7 @@ type configurator struct {
|
|||||||
lookupUser func(username string) (*user.User, error)
|
lookupUser func(username string) (*user.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigurator(logger logging.Logger, fileManager files.FileManager) Configurator {
|
func NewConfigurator(fileManager files.FileManager) Configurator {
|
||||||
return &configurator{
|
return &configurator{
|
||||||
fileManager: fileManager,
|
fileManager: fileManager,
|
||||||
lookupUID: user.LookupId,
|
lookupUID: user.LookupId,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) Start(verbosityDetailsLevel uint8) (stdout io.ReadCloser, waitFn func() error, err error) {
|
func (c *configurator) Start(verbosityDetailsLevel uint8) (stdout io.ReadCloser, waitFn func() error, err error) {
|
||||||
c.logger.Info("%s: starting unbound", logPrefix)
|
c.logger.Info("starting unbound")
|
||||||
args := []string{"-d", "-c", string(constants.UnboundConf)}
|
args := []string{"-d", "-c", string(constants.UnboundConf)}
|
||||||
if verbosityDetailsLevel > 0 {
|
if verbosityDetailsLevel > 0 {
|
||||||
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
|
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func Test_Start(t *testing.T) {
|
|||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: starting unbound", logPrefix).Times(1)
|
logger.EXPECT().Info("starting unbound").Times(1)
|
||||||
commander := mock_command.NewMockCommander(mockCtrl)
|
commander := mock_command.NewMockCommander(mockCtrl)
|
||||||
commander.EXPECT().Start("unbound", "-d", "-c", string(constants.UnboundConf), "-vv").
|
commander.EXPECT().Start("unbound", "-d", "-c", string(constants.UnboundConf), "-vv").
|
||||||
Return(nil, nil, nil, nil).Times(1)
|
Return(nil, nil, nil, nil).Times(1)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) MakeUnboundConf(settings settings.DNS, uid, gid int) (err error) {
|
func (c *configurator) MakeUnboundConf(settings settings.DNS, uid, gid int) (err error) {
|
||||||
c.logger.Info("%s: generating Unbound configuration", logPrefix)
|
c.logger.Info("generating Unbound configuration")
|
||||||
lines, warnings, err := generateUnboundConf(settings, c.client, c.logger)
|
lines, warnings, err := generateUnboundConf(settings, c.client, c.logger)
|
||||||
for _, warning := range warnings {
|
for _, warning := range warnings {
|
||||||
c.logger.Warn(warning)
|
c.logger.Warn(warning)
|
||||||
@@ -76,8 +76,8 @@ func generateUnboundConf(settings settings.DNS, client network.Client, logger lo
|
|||||||
settings.BlockMalicious, settings.BlockAds, settings.BlockSurveillance,
|
settings.BlockMalicious, settings.BlockAds, settings.BlockSurveillance,
|
||||||
settings.AllowedHostnames, settings.PrivateAddresses,
|
settings.AllowedHostnames, settings.PrivateAddresses,
|
||||||
)
|
)
|
||||||
logger.Info("%s: %d hostnames blocked overall", logPrefix, len(hostnamesLines))
|
logger.Info("%d hostnames blocked overall", len(hostnamesLines))
|
||||||
logger.Info("%s: %d IP addresses blocked overall", logPrefix, len(ipsLines))
|
logger.Info("%d IP addresses blocked overall", len(ipsLines))
|
||||||
sort.Slice(hostnamesLines, func(i, j int) bool { // for unit tests really
|
sort.Slice(hostnamesLines, func(i, j int) bool { // for unit tests really
|
||||||
return hostnamesLines[i] < hostnamesLines[j]
|
return hostnamesLines[i] < hostnamesLines[j]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ func Test_generateUnboundConf(t *testing.T) {
|
|||||||
client.EXPECT().GetContent(string(constants.MaliciousBlockListIPsURL)).
|
client.EXPECT().GetContent(string(constants.MaliciousBlockListIPsURL)).
|
||||||
Return([]byte("c\nd\n"), 200, nil).Times(1)
|
Return([]byte("c\nd\n"), 200, nil).Times(1)
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: %d hostnames blocked overall", logPrefix, 2).Times(1)
|
logger.EXPECT().Info("%d hostnames blocked overall", 2).Times(1)
|
||||||
logger.EXPECT().Info("%s: %d IP addresses blocked overall", logPrefix, 3).Times(1)
|
logger.EXPECT().Info("%d IP addresses blocked overall", 3).Times(1)
|
||||||
lines, warnings, err := generateUnboundConf(settings, client, logger)
|
lines, warnings, err := generateUnboundConf(settings, client, logger)
|
||||||
require.Len(t, warnings, 0)
|
require.Len(t, warnings, 0)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import (
|
|||||||
"github.com/qdm12/private-internet-access-docker/internal/settings"
|
"github.com/qdm12/private-internet-access-docker/internal/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "dns configurator"
|
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
DownloadRootHints(uid, gid int) error
|
DownloadRootHints(uid, gid int) error
|
||||||
DownloadRootKey(uid, gid int) error
|
DownloadRootKey(uid, gid int) error
|
||||||
@@ -34,7 +32,7 @@ type configurator struct {
|
|||||||
|
|
||||||
func NewConfigurator(logger logging.Logger, client network.Client, fileManager files.FileManager) Configurator {
|
func NewConfigurator(logger logging.Logger, client network.Client, fileManager files.FileManager) Configurator {
|
||||||
return &configurator{
|
return &configurator{
|
||||||
logger: logger,
|
logger: logger.WithPrefix("dns configurator: "),
|
||||||
client: client,
|
client: client,
|
||||||
fileManager: fileManager,
|
fileManager: fileManager,
|
||||||
commander: command.NewCommander(),
|
commander: command.NewCommander(),
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
// UseDNSInternally is to change the Go program DNS only
|
// UseDNSInternally is to change the Go program DNS only
|
||||||
func (c *configurator) UseDNSInternally(IP net.IP) {
|
func (c *configurator) UseDNSInternally(IP net.IP) {
|
||||||
c.logger.Info("%s: using DNS address %s internally", logPrefix, IP.String())
|
c.logger.Info("using DNS address %s internally", IP.String())
|
||||||
net.DefaultResolver = &net.Resolver{
|
net.DefaultResolver = &net.Resolver{
|
||||||
PreferGo: true,
|
PreferGo: true,
|
||||||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
@@ -22,7 +22,7 @@ func (c *configurator) UseDNSInternally(IP net.IP) {
|
|||||||
|
|
||||||
// UseDNSSystemWide changes the nameserver to use for DNS system wide
|
// UseDNSSystemWide changes the nameserver to use for DNS system wide
|
||||||
func (c *configurator) UseDNSSystemWide(IP net.IP) error {
|
func (c *configurator) UseDNSSystemWide(IP net.IP) error {
|
||||||
c.logger.Info("%s: using DNS address %s system wide", logPrefix, IP.String())
|
c.logger.Info("using DNS address %s system wide", IP.String())
|
||||||
data, err := c.fileManager.ReadFile(string(constants.ResolvConf))
|
data, err := c.fileManager.ReadFile(string(constants.ResolvConf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func Test_UseDNSSystemWide(t *testing.T) {
|
|||||||
Return(tc.writeErr).Times(1)
|
Return(tc.writeErr).Times(1)
|
||||||
}
|
}
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: using DNS address %s system wide", logPrefix, "127.0.0.1").Times(1)
|
logger.EXPECT().Info("using DNS address %s system wide", "127.0.0.1").Times(1)
|
||||||
c := &configurator{
|
c := &configurator{
|
||||||
fileManager: fileManager,
|
fileManager: fileManager,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) DownloadRootHints(uid, gid int) error {
|
func (c *configurator) DownloadRootHints(uid, gid int) error {
|
||||||
c.logger.Info("%s: downloading root hints from %s", logPrefix, constants.NamedRootURL)
|
c.logger.Info("downloading root hints from %s", constants.NamedRootURL)
|
||||||
content, status, err := c.client.GetContent(string(constants.NamedRootURL))
|
content, status, err := c.client.GetContent(string(constants.NamedRootURL))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -23,7 +23,7 @@ func (c *configurator) DownloadRootHints(uid, gid int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) DownloadRootKey(uid, gid int) error {
|
func (c *configurator) DownloadRootKey(uid, gid int) error {
|
||||||
c.logger.Info("%s: downloading root key from %s", logPrefix, constants.RootKeyURL)
|
c.logger.Info("downloading root key from %s", constants.RootKeyURL)
|
||||||
content, status, err := c.client.GetContent(string(constants.RootKeyURL))
|
content, status, err := c.client.GetContent(string(constants.RootKeyURL))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func Test_DownloadRootHints(t *testing.T) {
|
|||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: downloading root hints from %s", logPrefix, constants.NamedRootURL).Times(1)
|
logger.EXPECT().Info("downloading root hints from %s", constants.NamedRootURL).Times(1)
|
||||||
client := mock_network.NewMockClient(mockCtrl)
|
client := mock_network.NewMockClient(mockCtrl)
|
||||||
client.EXPECT().GetContent(string(constants.NamedRootURL)).
|
client.EXPECT().GetContent(string(constants.NamedRootURL)).
|
||||||
Return(tc.content, tc.status, tc.clientErr).Times(1)
|
Return(tc.content, tc.status, tc.clientErr).Times(1)
|
||||||
@@ -115,7 +115,7 @@ func Test_DownloadRootKey(t *testing.T) {
|
|||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: downloading root key from %s", logPrefix, constants.RootKeyURL).Times(1)
|
logger.EXPECT().Info("downloading root key from %s", constants.RootKeyURL).Times(1)
|
||||||
client := mock_network.NewMockClient(mockCtrl)
|
client := mock_network.NewMockClient(mockCtrl)
|
||||||
client.EXPECT().GetContent(string(constants.RootKeyURL)).
|
client.EXPECT().GetContent(string(constants.RootKeyURL)).
|
||||||
Return(tc.content, tc.status, tc.clientErr).Times(1)
|
Return(tc.content, tc.status, tc.clientErr).Times(1)
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"github.com/qdm12/private-internet-access-docker/internal/models"
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "firewall configurator"
|
|
||||||
|
|
||||||
// Configurator allows to change firewall rules and modify network routes
|
// Configurator allows to change firewall rules and modify network routes
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
Version() (string, error)
|
Version() (string, error)
|
||||||
@@ -32,6 +30,6 @@ type configurator struct {
|
|||||||
func NewConfigurator(logger logging.Logger) Configurator {
|
func NewConfigurator(logger logging.Logger) Configurator {
|
||||||
return &configurator{
|
return &configurator{
|
||||||
commander: command.NewCommander(),
|
commander: command.NewCommander(),
|
||||||
logger: logger,
|
logger: logger.WithPrefix("firewall configurator: "),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func (c *configurator) runIptablesInstruction(instruction string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) Clear() error {
|
func (c *configurator) Clear() error {
|
||||||
c.logger.Info("%s: clearing all rules", logPrefix)
|
c.logger.Info("clearing all rules")
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
"--flush",
|
"--flush",
|
||||||
"--delete-chain",
|
"--delete-chain",
|
||||||
@@ -49,7 +49,7 @@ func (c *configurator) Clear() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) AcceptAll() error {
|
func (c *configurator) AcceptAll() error {
|
||||||
c.logger.Info("%s: accepting all traffic", logPrefix)
|
c.logger.Info("accepting all traffic")
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
"-P INPUT ACCEPT",
|
"-P INPUT ACCEPT",
|
||||||
"-P OUTPUT ACCEPT",
|
"-P OUTPUT ACCEPT",
|
||||||
@@ -58,7 +58,7 @@ func (c *configurator) AcceptAll() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) BlockAll() error {
|
func (c *configurator) BlockAll() error {
|
||||||
c.logger.Info("%s: blocking all traffic", logPrefix)
|
c.logger.Info("blocking all traffic")
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
"-P INPUT DROP",
|
"-P INPUT DROP",
|
||||||
"-F OUTPUT",
|
"-F OUTPUT",
|
||||||
@@ -68,7 +68,7 @@ func (c *configurator) BlockAll() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) CreateGeneralRules() error {
|
func (c *configurator) CreateGeneralRules() error {
|
||||||
c.logger.Info("%s: creating general rules", logPrefix)
|
c.logger.Info("creating general rules")
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
"-A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT",
|
"-A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT",
|
||||||
"-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT",
|
"-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT",
|
||||||
@@ -79,8 +79,8 @@ func (c *configurator) CreateGeneralRules() error {
|
|||||||
|
|
||||||
func (c *configurator) CreateVPNRules(dev models.VPNDevice, defaultInterface string, connections []models.OpenVPNConnection) error {
|
func (c *configurator) CreateVPNRules(dev models.VPNDevice, defaultInterface string, connections []models.OpenVPNConnection) error {
|
||||||
for _, connection := range connections {
|
for _, connection := range connections {
|
||||||
c.logger.Info("%s: allowing output traffic to VPN server %s through %s on port %s %d",
|
c.logger.Info("allowing output traffic to VPN server %s through %s on port %s %d",
|
||||||
logPrefix, connection.IP, defaultInterface, connection.Protocol, connection.Port)
|
connection.IP, defaultInterface, connection.Protocol, connection.Port)
|
||||||
if err := c.runIptablesInstruction(
|
if err := c.runIptablesInstruction(
|
||||||
fmt.Sprintf("-A OUTPUT -d %s -o %s -p %s -m %s --dport %d -j ACCEPT",
|
fmt.Sprintf("-A OUTPUT -d %s -o %s -p %s -m %s --dport %d -j ACCEPT",
|
||||||
connection.IP, defaultInterface, connection.Protocol, connection.Protocol, connection.Port)); err != nil {
|
connection.IP, defaultInterface, connection.Protocol, connection.Protocol, connection.Port)); err != nil {
|
||||||
@@ -95,7 +95,7 @@ func (c *configurator) CreateVPNRules(dev models.VPNDevice, defaultInterface str
|
|||||||
|
|
||||||
func (c *configurator) CreateLocalSubnetsRules(subnet net.IPNet, extraSubnets []net.IPNet, defaultInterface string) error {
|
func (c *configurator) CreateLocalSubnetsRules(subnet net.IPNet, extraSubnets []net.IPNet, defaultInterface string) error {
|
||||||
subnetStr := subnet.String()
|
subnetStr := subnet.String()
|
||||||
c.logger.Info("%s: accepting input and output traffic for %s", logPrefix, subnetStr)
|
c.logger.Info("accepting input and output traffic for %s", subnetStr)
|
||||||
if err := c.runIptablesInstructions([]string{
|
if err := c.runIptablesInstructions([]string{
|
||||||
fmt.Sprintf("-A INPUT -s %s -d %s -j ACCEPT", subnetStr, subnetStr),
|
fmt.Sprintf("-A INPUT -s %s -d %s -j ACCEPT", subnetStr, subnetStr),
|
||||||
fmt.Sprintf("-A OUTPUT -s %s -d %s -j ACCEPT", subnetStr, subnetStr),
|
fmt.Sprintf("-A OUTPUT -s %s -d %s -j ACCEPT", subnetStr, subnetStr),
|
||||||
@@ -104,13 +104,13 @@ func (c *configurator) CreateLocalSubnetsRules(subnet net.IPNet, extraSubnets []
|
|||||||
}
|
}
|
||||||
for _, extraSubnet := range extraSubnets {
|
for _, extraSubnet := range extraSubnets {
|
||||||
extraSubnetStr := extraSubnet.String()
|
extraSubnetStr := extraSubnet.String()
|
||||||
c.logger.Info("%s: accepting input traffic through %s from %s to %s", logPrefix, defaultInterface, extraSubnetStr, subnetStr)
|
c.logger.Info("accepting input traffic through %s from %s to %s", defaultInterface, extraSubnetStr, subnetStr)
|
||||||
if err := c.runIptablesInstruction(
|
if err := c.runIptablesInstruction(
|
||||||
fmt.Sprintf("-A INPUT -i %s -s %s -d %s -j ACCEPT", defaultInterface, extraSubnetStr, subnetStr)); err != nil {
|
fmt.Sprintf("-A INPUT -i %s -s %s -d %s -j ACCEPT", defaultInterface, extraSubnetStr, subnetStr)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Thanks to @npawelek
|
// Thanks to @npawelek
|
||||||
c.logger.Info("%s: accepting output traffic through %s from %s to %s", logPrefix, defaultInterface, subnetStr, extraSubnetStr)
|
c.logger.Info("accepting output traffic through %s from %s to %s", defaultInterface, subnetStr, extraSubnetStr)
|
||||||
if err := c.runIptablesInstruction(
|
if err := c.runIptablesInstruction(
|
||||||
fmt.Sprintf("-A OUTPUT -o %s -s %s -d %s -j ACCEPT", defaultInterface, subnetStr, extraSubnetStr)); err != nil {
|
fmt.Sprintf("-A OUTPUT -o %s -s %s -d %s -j ACCEPT", defaultInterface, subnetStr, extraSubnetStr)); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -121,7 +121,7 @@ func (c *configurator) CreateLocalSubnetsRules(subnet net.IPNet, extraSubnets []
|
|||||||
|
|
||||||
// Used for port forwarding
|
// Used for port forwarding
|
||||||
func (c *configurator) AllowInputTrafficOnPort(device models.VPNDevice, port uint16) error {
|
func (c *configurator) AllowInputTrafficOnPort(device models.VPNDevice, port uint16) error {
|
||||||
c.logger.Info("%s: accepting input traffic through %s on port %d", logPrefix, device, port)
|
c.logger.Info("accepting input traffic through %s on port %d", device, port)
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
fmt.Sprintf("-A INPUT -i %s -p tcp --dport %d -j ACCEPT", device, port),
|
fmt.Sprintf("-A INPUT -i %s -p tcp --dport %d -j ACCEPT", device, port),
|
||||||
fmt.Sprintf("-A INPUT -i %s -p udp --dport %d -j ACCEPT", device, port),
|
fmt.Sprintf("-A INPUT -i %s -p udp --dport %d -j ACCEPT", device, port),
|
||||||
@@ -129,7 +129,7 @@ func (c *configurator) AllowInputTrafficOnPort(device models.VPNDevice, port uin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) AllowAnyIncomingOnPort(port uint16) error {
|
func (c *configurator) AllowAnyIncomingOnPort(port uint16) error {
|
||||||
c.logger.Info("%s: accepting any input traffic on port %d", logPrefix, port)
|
c.logger.Info("accepting any input traffic on port %d", port)
|
||||||
return c.runIptablesInstructions([]string{
|
return c.runIptablesInstructions([]string{
|
||||||
fmt.Sprintf("-A INPUT -p tcp --dport %d -j ACCEPT", port),
|
fmt.Sprintf("-A INPUT -p tcp --dport %d -j ACCEPT", port),
|
||||||
fmt.Sprintf("-A INPUT -p udp --dport %d -j ACCEPT", port),
|
fmt.Sprintf("-A INPUT -p udp --dport %d -j ACCEPT", port),
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"github.com/qdm12/private-internet-access-docker/internal/models"
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "Mullvad configurator"
|
|
||||||
|
|
||||||
// Configurator contains methods to download, read and modify the openvpn configuration to connect as a client
|
// Configurator contains methods to download, read and modify the openvpn configuration to connect as a client
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
GetOpenVPNConnections(country models.MullvadCountry, city models.MullvadCity, provider models.MullvadProvider, protocol models.NetworkProtocol, customPort uint16, targetIP net.IP) (connections []models.OpenVPNConnection, err error)
|
GetOpenVPNConnections(country models.MullvadCountry, city models.MullvadCity, provider models.MullvadProvider, protocol models.NetworkProtocol, customPort uint16, targetIP net.IP) (connections []models.OpenVPNConnection, err error)
|
||||||
@@ -23,5 +21,8 @@ type configurator struct {
|
|||||||
|
|
||||||
// NewConfigurator returns a new Configurator object
|
// NewConfigurator returns a new Configurator object
|
||||||
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
||||||
return &configurator{fileManager, logger}
|
return &configurator{
|
||||||
|
fileManager: fileManager,
|
||||||
|
logger: logger.WithPrefix("Mullvad configurator: "),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ func (c *configurator) WriteAuthFile(user, password string, uid, gid int) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if authExists { // in case of container stop/start
|
} else if authExists { // in case of container stop/start
|
||||||
c.logger.Info("%s: %s already exists", logPrefix, constants.OpenVPNAuthConf)
|
c.logger.Info("%s already exists", constants.OpenVPNAuthConf)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c.logger.Info("%s: writing auth file %s", logPrefix, constants.OpenVPNAuthConf)
|
c.logger.Info("writing auth file %s", constants.OpenVPNAuthConf)
|
||||||
return c.fileManager.WriteLinesToFile(
|
return c.fileManager.WriteLinesToFile(
|
||||||
string(constants.OpenVPNAuthConf),
|
string(constants.OpenVPNAuthConf),
|
||||||
[]string{user, password},
|
[]string{user, password},
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
|
func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
|
||||||
c.logger.Info("%s: starting openvpn", logPrefix)
|
c.logger.Info("starting openvpn")
|
||||||
stdout, _, waitFn, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf))
|
stdout, _, waitFn, err = c.commander.Start("openvpn", "--config", string(constants.OpenVPNConf))
|
||||||
return stdout, waitFn, err
|
return stdout, waitFn, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import (
|
|||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "openvpn configurator"
|
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
Version() (string, error)
|
Version() (string, error)
|
||||||
WriteAuthFile(user, password string, uid, gid int) error
|
WriteAuthFile(user, password string, uid, gid int) error
|
||||||
@@ -32,7 +30,7 @@ type configurator struct {
|
|||||||
func NewConfigurator(logger logging.Logger, fileManager files.FileManager) Configurator {
|
func NewConfigurator(logger logging.Logger, fileManager files.FileManager) Configurator {
|
||||||
return &configurator{
|
return &configurator{
|
||||||
fileManager: fileManager,
|
fileManager: fileManager,
|
||||||
logger: logger,
|
logger: logger.WithPrefix("openvpn configurator: "),
|
||||||
commander: command.NewCommander(),
|
commander: command.NewCommander(),
|
||||||
openFile: os.OpenFile,
|
openFile: os.OpenFile,
|
||||||
mkDev: unix.Mkdev,
|
mkDev: unix.Mkdev,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
// CheckTUN checks the tunnel device is present and accessible
|
// CheckTUN checks the tunnel device is present and accessible
|
||||||
func (c *configurator) CheckTUN() error {
|
func (c *configurator) CheckTUN() error {
|
||||||
c.logger.Info("%s: checking for device %s", logPrefix, constants.TunnelDevice)
|
c.logger.Info("checking for device %s", constants.TunnelDevice)
|
||||||
f, err := c.openFile(string(constants.TunnelDevice), os.O_RDWR, 0)
|
f, err := c.openFile(string(constants.TunnelDevice), os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("TUN device is not available: %w", err)
|
return fmt.Errorf("TUN device is not available: %w", err)
|
||||||
@@ -22,7 +22,7 @@ func (c *configurator) CheckTUN() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) CreateTUN() error {
|
func (c *configurator) CreateTUN() error {
|
||||||
c.logger.Info("%s: creating %s", logPrefix, constants.TunnelDevice)
|
c.logger.Info("creating %s", constants.TunnelDevice)
|
||||||
if err := c.fileManager.CreateDir("/dev/net"); err != nil {
|
if err := c.fileManager.CreateDir("/dev/net"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import (
|
|||||||
"github.com/qdm12/private-internet-access-docker/internal/models"
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "PIA configurator"
|
|
||||||
|
|
||||||
// Configurator contains methods to download, read and modify the openvpn configuration to connect as a client
|
// Configurator contains methods to download, read and modify the openvpn configuration to connect as a client
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
GetOpenVPNConnections(region models.PIARegion, protocol models.NetworkProtocol,
|
GetOpenVPNConnections(region models.PIARegion, protocol models.NetworkProtocol,
|
||||||
@@ -37,5 +35,12 @@ type configurator struct {
|
|||||||
|
|
||||||
// NewConfigurator returns a new Configurator object
|
// NewConfigurator returns a new Configurator object
|
||||||
func NewConfigurator(client network.Client, fileManager files.FileManager, firewall firewall.Configurator, logger logging.Logger) Configurator {
|
func NewConfigurator(client network.Client, fileManager files.FileManager, firewall firewall.Configurator, logger logging.Logger) Configurator {
|
||||||
return &configurator{client, fileManager, firewall, logger, random.NewRandom(), verification.NewVerifier().VerifyPort, net.LookupIP}
|
return &configurator{
|
||||||
|
client: client,
|
||||||
|
fileManager: fileManager,
|
||||||
|
firewall: firewall,
|
||||||
|
logger: logger.WithPrefix("PIA configurator: "),
|
||||||
|
random: random.NewRandom(),
|
||||||
|
verifyPort: verification.NewVerifier().VerifyPort,
|
||||||
|
lookupIP: net.LookupIP}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) GetPortForward() (port uint16, err error) {
|
func (c *configurator) GetPortForward() (port uint16, err error) {
|
||||||
c.logger.Info("%s: Obtaining port to be forwarded", logPrefix)
|
c.logger.Info("Obtaining port to be forwarded")
|
||||||
b, err := c.random.GenerateRandomBytes(32)
|
b, err := c.random.GenerateRandomBytes(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -32,12 +32,12 @@ func (c *configurator) GetPortForward() (port uint16, err error) {
|
|||||||
if err := json.Unmarshal(content, &body); err != nil {
|
if err := json.Unmarshal(content, &body); err != nil {
|
||||||
return 0, fmt.Errorf("port forwarding response: %w", err)
|
return 0, fmt.Errorf("port forwarding response: %w", err)
|
||||||
}
|
}
|
||||||
c.logger.Info("%s: Port forwarded is %d", logPrefix, body.Port)
|
c.logger.Info("Port forwarded is %d", body.Port)
|
||||||
return body.Port, nil
|
return body.Port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error) {
|
func (c *configurator) WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error) {
|
||||||
c.logger.Info("%s: Writing forwarded port to %s", logPrefix, filepath)
|
c.logger.Info("Writing forwarded port to %s", filepath)
|
||||||
return c.fileManager.WriteLinesToFile(
|
return c.fileManager.WriteLinesToFile(
|
||||||
string(filepath),
|
string(filepath),
|
||||||
[]string{fmt.Sprintf("%d", port)},
|
[]string{fmt.Sprintf("%d", port)},
|
||||||
@@ -46,11 +46,11 @@ func (c *configurator) WritePortForward(filepath models.Filepath, port uint16, u
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) AllowPortForwardFirewall(device models.VPNDevice, port uint16) (err error) {
|
func (c *configurator) AllowPortForwardFirewall(device models.VPNDevice, port uint16) (err error) {
|
||||||
c.logger.Info("%s: Allowing forwarded port %d through firewall", logPrefix, port)
|
c.logger.Info("Allowing forwarded port %d through firewall", port)
|
||||||
return c.firewall.AllowInputTrafficOnPort(device, port)
|
return c.firewall.AllowInputTrafficOnPort(device, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *configurator) ClearPortForward(filepath models.Filepath, uid, gid int) (err error) {
|
func (c *configurator) ClearPortForward(filepath models.Filepath, uid, gid int) (err error) {
|
||||||
c.logger.Info("%s: Clearing forwarded port status file %s", logPrefix, filepath)
|
c.logger.Info("Clearing forwarded port status file %s", filepath)
|
||||||
return c.fileManager.WriteToFile(string(filepath), nil, files.Ownership(uid, gid), files.Permissions(400))
|
return c.fileManager.WriteToFile(string(filepath), nil, files.Ownership(uid, gid), files.Permissions(400))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, waitFn func() error, err error) {
|
func (c *configurator) Start(server string, port uint16, password string, log bool) (stdout io.ReadCloser, waitFn func() error, err error) {
|
||||||
c.logger.Info("%s: starting shadowsocks server", logPrefix)
|
c.logger.Info("starting shadowsocks server")
|
||||||
args := []string{
|
args := []string{
|
||||||
"-c", string(constants.ShadowsocksConf),
|
"-c", string(constants.ShadowsocksConf),
|
||||||
"-p", fmt.Sprintf("%d", port),
|
"-p", fmt.Sprintf("%d", port),
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) MakeConf(port uint16, password, method string, uid, gid int) (err error) {
|
func (c *configurator) MakeConf(port uint16, password, method string, uid, gid int) (err error) {
|
||||||
c.logger.Info("%s: generating configuration file", logPrefix)
|
c.logger.Info("generating configuration file")
|
||||||
data := generateConf(port, password, method)
|
data := generateConf(port, password, method)
|
||||||
return c.fileManager.WriteToFile(
|
return c.fileManager.WriteToFile(
|
||||||
string(constants.ShadowsocksConf),
|
string(constants.ShadowsocksConf),
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func Test_MakeConf(t *testing.T) {
|
|||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
logger := mock_logging.NewMockLogger(mockCtrl)
|
logger := mock_logging.NewMockLogger(mockCtrl)
|
||||||
logger.EXPECT().Info("%s: generating configuration file", logPrefix).Times(1)
|
logger.EXPECT().Info("generating configuration file").Times(1)
|
||||||
fileManager := mock_files.NewMockFileManager(mockCtrl)
|
fileManager := mock_files.NewMockFileManager(mockCtrl)
|
||||||
fileManager.EXPECT().WriteToFile(
|
fileManager.EXPECT().WriteToFile(
|
||||||
string(constants.ShadowsocksConf),
|
string(constants.ShadowsocksConf),
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"github.com/qdm12/golibs/logging"
|
"github.com/qdm12/golibs/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "shadowsocks configurator"
|
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
Version() (string, error)
|
Version() (string, error)
|
||||||
MakeConf(port uint16, password, method string, uid, gid int) (err error)
|
MakeConf(port uint16, password, method string, uid, gid int) (err error)
|
||||||
@@ -23,5 +21,8 @@ type configurator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
||||||
return &configurator{fileManager, logger, command.NewCommander()}
|
return &configurator{
|
||||||
|
fileManager: fileManager,
|
||||||
|
logger: logger.WithPrefix("shadowsocks configurator: "),
|
||||||
|
commander: command.NewCommander()}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
|
func (c *configurator) Start() (stdout io.ReadCloser, waitFn func() error, err error) {
|
||||||
c.logger.Info("%s: starting tinyproxy server", logPrefix)
|
c.logger.Info("starting tinyproxy server")
|
||||||
stdout, _, waitFn, err = c.commander.Start("tinyproxy", "-d")
|
stdout, _, waitFn, err = c.commander.Start("tinyproxy", "-d")
|
||||||
return stdout, waitFn, err
|
return stdout, waitFn, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *configurator) MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error {
|
func (c *configurator) MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error {
|
||||||
c.logger.Info("%s: generating tinyproxy configuration file", logPrefix)
|
c.logger.Info("generating tinyproxy configuration file")
|
||||||
lines := generateConf(logLevel, port, user, password, uid, gid)
|
lines := generateConf(logLevel, port, user, password, uid, gid)
|
||||||
return c.fileManager.WriteLinesToFile(string(constants.TinyProxyConf),
|
return c.fileManager.WriteLinesToFile(string(constants.TinyProxyConf),
|
||||||
lines,
|
lines,
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
"github.com/qdm12/private-internet-access-docker/internal/models"
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logPrefix = "tinyproxy configurator"
|
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
Version() (string, error)
|
Version() (string, error)
|
||||||
MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error
|
MakeConf(logLevel models.TinyProxyLogLevel, port uint16, user, password string, uid, gid int) error
|
||||||
@@ -24,5 +22,8 @@ type configurator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
func NewConfigurator(fileManager files.FileManager, logger logging.Logger) Configurator {
|
||||||
return &configurator{fileManager, logger, command.NewCommander()}
|
return &configurator{
|
||||||
|
fileManager: fileManager,
|
||||||
|
logger: logger.WithPrefix("tinyproxy configurator: "),
|
||||||
|
commander: command.NewCommander()}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user