Better onConnected logic
- First port forward after 5 seconds - Public IP obtained ASAP - Logging in main only - Allow port forward firewall with 1 second timeout local context
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/qdm12/golibs/crypto/random"
|
||||
"github.com/qdm12/golibs/files"
|
||||
"github.com/qdm12/golibs/logging"
|
||||
"github.com/qdm12/golibs/network"
|
||||
"github.com/qdm12/golibs/verification"
|
||||
"github.com/qdm12/private-internet-access-docker/internal/firewall"
|
||||
@@ -20,7 +19,6 @@ type Configurator interface {
|
||||
BuildConf(connections []models.OpenVPNConnection, encryption models.PIAEncryption, verbosity, uid, gid int, root bool, cipher, auth string) (err error)
|
||||
GetPortForward() (port uint16, err error)
|
||||
WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error)
|
||||
ClearPortForward(filepath models.Filepath, uid, gid int) (err error)
|
||||
AllowPortForwardFirewall(ctx context.Context, device models.VPNDevice, port uint16) (err error)
|
||||
}
|
||||
|
||||
@@ -28,19 +26,17 @@ type configurator struct {
|
||||
client network.Client
|
||||
fileManager files.FileManager
|
||||
firewall firewall.Configurator
|
||||
logger logging.Logger
|
||||
random random.Random
|
||||
verifyPort func(port string) error
|
||||
lookupIP func(host string) ([]net.IP, error)
|
||||
}
|
||||
|
||||
// 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) Configurator {
|
||||
return &configurator{
|
||||
client: client,
|
||||
fileManager: fileManager,
|
||||
firewall: firewall,
|
||||
logger: logger.WithPrefix("PIA configurator: "),
|
||||
random: random.NewRandom(),
|
||||
verifyPort: verification.NewVerifier().VerifyPort,
|
||||
lookupIP: net.LookupIP}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
)
|
||||
|
||||
func (c *configurator) GetPortForward() (port uint16, err error) {
|
||||
c.logger.Info("Obtaining port to be forwarded")
|
||||
b, err := c.random.GenerateRandomBytes(32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -35,12 +34,10 @@ func (c *configurator) GetPortForward() (port uint16, err error) {
|
||||
if err := json.Unmarshal(content, &body); err != nil {
|
||||
return 0, fmt.Errorf("port forwarding response: %w", err)
|
||||
}
|
||||
c.logger.Info("Port forwarded is %d", body.Port)
|
||||
return body.Port, nil
|
||||
}
|
||||
|
||||
func (c *configurator) WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error) {
|
||||
c.logger.Info("Writing forwarded port to %s", filepath)
|
||||
return c.fileManager.WriteLinesToFile(
|
||||
string(filepath),
|
||||
[]string{fmt.Sprintf("%d", port)},
|
||||
@@ -49,11 +46,5 @@ func (c *configurator) WritePortForward(filepath models.Filepath, port uint16, u
|
||||
}
|
||||
|
||||
func (c *configurator) AllowPortForwardFirewall(ctx context.Context, device models.VPNDevice, port uint16) (err error) {
|
||||
c.logger.Info("Allowing forwarded port %d through firewall", port)
|
||||
return c.firewall.AllowInputTrafficOnPort(ctx, device, port)
|
||||
}
|
||||
|
||||
func (c *configurator) ClearPortForward(filepath models.Filepath, uid, gid int) (err error) {
|
||||
c.logger.Info("Clearing forwarded port status file %s", filepath)
|
||||
return c.fileManager.Remove(string(filepath))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user