2020-07-08 13:14:39 +00:00
|
|
|
package openvpn
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-10-12 10:55:08 -04:00
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2020-07-08 13:14:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/firewall"
|
2021-07-23 20:41:45 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/loopstate"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2021-07-23 20:41:45 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/openvpn/state"
|
2020-10-12 10:55:08 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/routing"
|
2020-07-08 13:14:39 +00:00
|
|
|
"github.com/qdm12/golibs/logging"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Looper interface {
|
2021-05-11 22:24:32 +00:00
|
|
|
Run(ctx context.Context, done chan<- struct{})
|
2020-12-19 20:10:34 -05:00
|
|
|
GetStatus() (status models.LoopStatus)
|
2021-07-16 21:20:34 +00:00
|
|
|
ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
2021-07-16 00:49:59 +00:00
|
|
|
outcome string, err error)
|
2021-02-06 11:05:50 -05:00
|
|
|
GetSettings() (settings configuration.OpenVPN)
|
2021-07-16 00:49:59 +00:00
|
|
|
SetSettings(ctx context.Context, settings configuration.OpenVPN) (
|
|
|
|
|
outcome string)
|
2020-12-19 20:10:34 -05:00
|
|
|
GetServers() (servers models.AllServers)
|
|
|
|
|
SetServers(servers models.AllServers)
|
|
|
|
|
GetPortForwarded() (port uint16)
|
|
|
|
|
PortForward(vpnGatewayIP net.IP)
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type looper struct {
|
2021-07-23 20:41:45 +00:00
|
|
|
statusManager loopstate.Manager
|
|
|
|
|
state state.Manager
|
2020-07-11 21:03:55 +00:00
|
|
|
// Fixed parameters
|
2021-07-23 16:06:19 +00:00
|
|
|
username string
|
|
|
|
|
puid int
|
|
|
|
|
pgid int
|
|
|
|
|
targetConfPath string
|
2020-07-11 21:03:55 +00:00
|
|
|
// Configurators
|
2020-10-12 10:55:08 -04:00
|
|
|
conf Configurator
|
|
|
|
|
fw firewall.Configurator
|
|
|
|
|
routing routing.Routing
|
2020-07-11 21:03:55 +00:00
|
|
|
// Other objects
|
2020-10-12 10:55:08 -04:00
|
|
|
logger, pfLogger logging.Logger
|
|
|
|
|
client *http.Client
|
2021-01-26 04:17:22 +00:00
|
|
|
tunnelReady chan<- struct{}
|
2021-07-16 21:20:34 +00:00
|
|
|
// Internal channels and values
|
|
|
|
|
stop <-chan struct{}
|
|
|
|
|
stopped chan<- struct{}
|
|
|
|
|
start <-chan struct{}
|
|
|
|
|
running chan<- models.LoopStatus
|
2020-10-12 10:55:08 -04:00
|
|
|
portForwardSignals chan net.IP
|
2021-07-16 21:20:34 +00:00
|
|
|
userTrigger bool
|
|
|
|
|
// Internal constant values
|
2021-07-18 03:17:48 +00:00
|
|
|
backoffTime time.Duration
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 15:36:12 -04:00
|
|
|
const (
|
2021-07-18 03:17:48 +00:00
|
|
|
defaultBackoffTime = 15 * time.Second
|
2021-05-04 15:36:12 -04:00
|
|
|
)
|
2020-12-30 17:22:54 +00:00
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
func NewLooper(settings configuration.OpenVPN,
|
2020-12-29 16:44:35 +00:00
|
|
|
username string, puid, pgid int, allServers models.AllServers,
|
2020-10-12 10:55:08 -04:00
|
|
|
conf Configurator, fw firewall.Configurator, routing routing.Routing,
|
2021-07-23 16:06:19 +00:00
|
|
|
logger logging.ParentLogger, client *http.Client,
|
2021-07-18 03:17:48 +00:00
|
|
|
tunnelReady chan<- struct{}) Looper {
|
2021-07-16 21:20:34 +00:00
|
|
|
start := make(chan struct{})
|
|
|
|
|
running := make(chan models.LoopStatus)
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
|
|
2021-07-23 20:41:45 +00:00
|
|
|
statusManager := loopstate.New(constants.Stopped, start, running, stop, stopped)
|
|
|
|
|
state := state.New(statusManager, settings, allServers)
|
2021-07-16 21:20:34 +00:00
|
|
|
|
2020-07-08 13:14:39 +00:00
|
|
|
return &looper{
|
2021-07-23 20:41:45 +00:00
|
|
|
statusManager: statusManager,
|
2021-07-16 21:20:34 +00:00
|
|
|
state: state,
|
2020-12-27 00:36:39 +00:00
|
|
|
username: username,
|
2020-12-29 16:44:35 +00:00
|
|
|
puid: puid,
|
|
|
|
|
pgid: pgid,
|
2021-07-23 16:06:19 +00:00
|
|
|
targetConfPath: constants.OpenVPNConf,
|
2020-07-15 01:34:46 +00:00
|
|
|
conf: conf,
|
|
|
|
|
fw: fw,
|
2020-10-12 10:55:08 -04:00
|
|
|
routing: routing,
|
2021-05-12 22:57:15 +00:00
|
|
|
logger: logger.NewChild(logging.Settings{Prefix: "openvpn: "}),
|
|
|
|
|
pfLogger: logger.NewChild(logging.Settings{Prefix: "port forwarding: "}),
|
2020-07-15 01:34:46 +00:00
|
|
|
client: client,
|
2021-01-26 04:17:22 +00:00
|
|
|
tunnelReady: tunnelReady,
|
2021-07-16 21:20:34 +00:00
|
|
|
start: start,
|
|
|
|
|
running: running,
|
|
|
|
|
stop: stop,
|
|
|
|
|
stopped: stopped,
|
2020-10-12 10:55:08 -04:00
|
|
|
portForwardSignals: make(chan net.IP),
|
2021-07-16 21:20:34 +00:00
|
|
|
userTrigger: true,
|
2020-12-30 17:22:54 +00:00
|
|
|
backoffTime: defaultBackoffTime,
|
2020-07-08 13:14:39 +00:00
|
|
|
}
|
|
|
|
|
}
|