Files
gluetun/internal/openvpn/loop.go

97 lines
2.6 KiB
Go
Raw Normal View History

package openvpn
import (
"net"
"net/http"
"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"
"github.com/qdm12/gluetun/internal/loopstate"
2020-07-26 12:07:06 +00:00
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/state"
"github.com/qdm12/gluetun/internal/routing"
"github.com/qdm12/golibs/logging"
)
var _ Looper = (*Loop)(nil)
type Looper interface {
Runner
loopstate.Getter
loopstate.Applier
SettingsGetSetter
ServersGetterSetter
PortForwadedGetter
PortForwader
}
type Loop struct {
statusManager loopstate.Manager
state state.Manager
// Fixed parameters
username string
puid int
pgid int
targetConfPath string
// Configurators
conf StarterAuthWriter
fw firewall.Configurator
routing routing.Routing
// Other objects
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
portForwardSignals chan net.IP
2021-07-16 21:20:34 +00:00
userTrigger bool
// Internal constant values
backoffTime time.Duration
}
2021-05-04 15:36:12 -04:00
const (
defaultBackoffTime = 15 * time.Second
2021-05-04 15:36:12 -04:00
)
func NewLoop(settings configuration.OpenVPN,
2020-12-29 16:44:35 +00:00
username string, puid, pgid int, allServers models.AllServers,
conf Configurator, fw firewall.Configurator, routing routing.Routing,
logger logging.ParentLogger, client *http.Client,
tunnelReady chan<- struct{}) *Loop {
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{})
statusManager := loopstate.New(constants.Stopped, start, running, stop, stopped)
state := state.New(statusManager, settings, allServers)
2021-07-16 21:20:34 +00:00
return &Loop{
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,
targetConfPath: constants.OpenVPNConf,
conf: conf,
fw: fw,
routing: routing,
logger: logger.NewChild(logging.Settings{Prefix: "openvpn: "}),
pfLogger: logger.NewChild(logging.Settings{Prefix: "port forwarding: "}),
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,
portForwardSignals: make(chan net.IP),
2021-07-16 21:20:34 +00:00
userTrigger: true,
backoffTime: defaultBackoffTime,
}
}