Maint: package local narrow Logger interfaces
This commit is contained in:
@@ -431,7 +431,11 @@ type printVersionElement struct {
|
|||||||
getVersion func(ctx context.Context) (version string, err error)
|
getVersion func(ctx context.Context) (version string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printVersions(ctx context.Context, logger logging.Logger,
|
type infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printVersions(ctx context.Context, logger infoer,
|
||||||
elements []printVersionElement) (err error) {
|
elements []printVersionElement) (err error) {
|
||||||
const timeout = 5 * time.Second
|
const timeout = 5 * time.Second
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
|
|||||||
@@ -8,19 +8,18 @@ import (
|
|||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/gluetun/internal/healthcheck"
|
"github.com/qdm12/gluetun/internal/healthcheck"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/qdm12/golibs/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HealthChecker interface {
|
type HealthChecker interface {
|
||||||
HealthCheck(ctx context.Context, env params.Interface, logger logging.Logger) error
|
HealthCheck(ctx context.Context, env params.Interface, warner configuration.Warner) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CLI) HealthCheck(ctx context.Context, env params.Interface,
|
func (c *CLI) HealthCheck(ctx context.Context, env params.Interface,
|
||||||
logger logging.Logger) error {
|
warner configuration.Warner) error {
|
||||||
// Extract the health server port from the configuration.
|
// Extract the health server port from the configuration.
|
||||||
config := configuration.Health{}
|
config := configuration.Health{}
|
||||||
err := config.Read(env, logger)
|
err := config.Read(env, warner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,19 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/provider"
|
"github.com/qdm12/gluetun/internal/provider"
|
||||||
"github.com/qdm12/gluetun/internal/storage"
|
"github.com/qdm12/gluetun/internal/storage"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/qdm12/golibs/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OpenvpnConfigMaker interface {
|
type OpenvpnConfigMaker interface {
|
||||||
OpenvpnConfig(logger logging.Logger, env params.Interface) error
|
OpenvpnConfig(logger OpenvpnConfigLogger, env params.Interface) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CLI) OpenvpnConfig(logger logging.Logger, env params.Interface) error {
|
type OpenvpnConfigLogger interface {
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CLI) OpenvpnConfig(logger OpenvpnConfigLogger, env params.Interface) error {
|
||||||
storage, err := storage.New(logger, constants.ServersData)
|
storage, err := storage.New(logger, constants.ServersData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/storage"
|
"github.com/qdm12/gluetun/internal/storage"
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -26,10 +25,16 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Updater interface {
|
type Updater interface {
|
||||||
Update(ctx context.Context, args []string, logger logging.Logger) error
|
Update(ctx context.Context, args []string, logger UpdaterLogger) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CLI) Update(ctx context.Context, args []string, logger logging.Logger) error {
|
type UpdaterLogger interface {
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CLI) Update(ctx context.Context, args []string, logger UpdaterLogger) error {
|
||||||
options := configuration.Updater{CLI: true}
|
options := configuration.Updater{CLI: true}
|
||||||
var endUserMode, maintainerMode, updateAll bool
|
var endUserMode, maintainerMode, updateAll bool
|
||||||
flagSet := flag.NewFlagSet("update", flag.ExitOnError)
|
flagSet := flag.NewFlagSet("update", flag.ExitOnError)
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/qdm12/golibs/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,8 +35,8 @@ func (settings *Health) lines() (lines []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read is to be used for the healthcheck query mode.
|
// Read is to be used for the healthcheck query mode.
|
||||||
func (settings *Health) Read(env params.Interface, logger logging.Logger) (err error) {
|
func (settings *Health) Read(env params.Interface, warner Warner) (err error) {
|
||||||
reader := newReader(env, models.AllServers{}, logger) // note: no need for servers data
|
reader := newReader(env, models.AllServers{}, warner) // note: no need for servers data
|
||||||
return settings.read(reader)
|
return settings.read(reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +45,7 @@ func (settings *Health) read(r reader) (err error) {
|
|||||||
settings.ServerAddress, warning, err = r.env.ListeningAddress(
|
settings.ServerAddress, warning, err = r.env.ListeningAddress(
|
||||||
"HEALTH_SERVER_ADDRESS", params.Default("127.0.0.1:9999"))
|
"HEALTH_SERVER_ADDRESS", params.Default("127.0.0.1:9999"))
|
||||||
if warning != "" {
|
if warning != "" {
|
||||||
r.logger.Warn("environment variable HEALTH_SERVER_ADDRESS: " + warning)
|
r.warner.Warn("environment variable HEALTH_SERVER_ADDRESS: " + warning)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err)
|
return fmt.Errorf("environment variable HEALTH_SERVER_ADDRESS: %w", err)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/qdm12/golibs/logging/mock_logging"
|
|
||||||
"github.com/qdm12/golibs/params/mock_params"
|
"github.com/qdm12/golibs/params/mock_params"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -215,7 +214,7 @@ func Test_Health_read(t *testing.T) {
|
|||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
|
|
||||||
env := mock_params.NewMockInterface(ctrl)
|
env := mock_params.NewMockInterface(ctrl)
|
||||||
logger := mock_logging.NewMockLogger(ctrl)
|
warner := NewMockWarner(ctrl)
|
||||||
|
|
||||||
if testCase.serverAddress.call {
|
if testCase.serverAddress.call {
|
||||||
value := testCase.serverAddress.s
|
value := testCase.serverAddress.s
|
||||||
@@ -224,7 +223,7 @@ func Test_Health_read(t *testing.T) {
|
|||||||
env.EXPECT().ListeningAddress("HEALTH_SERVER_ADDRESS", gomock.Any()).
|
env.EXPECT().ListeningAddress("HEALTH_SERVER_ADDRESS", gomock.Any()).
|
||||||
Return(value, warning, err)
|
Return(value, warning, err)
|
||||||
if warning != "" {
|
if warning != "" {
|
||||||
logger.EXPECT().Warn("environment variable HEALTH_SERVER_ADDRESS: " + warning)
|
warner.EXPECT().Warn("environment variable HEALTH_SERVER_ADDRESS: " + warning)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +252,7 @@ func Test_Health_read(t *testing.T) {
|
|||||||
|
|
||||||
r := reader{
|
r := reader{
|
||||||
env: env,
|
env: env,
|
||||||
logger: logger,
|
warner: warner,
|
||||||
}
|
}
|
||||||
|
|
||||||
var health Health
|
var health Health
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ func (settings *HTTPProxy) read(r reader) (err error) {
|
|||||||
settings.Port, warning, err = r.env.ListeningPort("HTTPPROXY_PORT", params.Default("8888"),
|
settings.Port, warning, err = r.env.ListeningPort("HTTPPROXY_PORT", params.Default("8888"),
|
||||||
params.RetroKeys([]string{"TINYPROXY_PORT", "PROXY_PORT"}, r.onRetroActive))
|
params.RetroKeys([]string{"TINYPROXY_PORT", "PROXY_PORT"}, r.onRetroActive))
|
||||||
if len(warning) > 0 {
|
if len(warning) > 0 {
|
||||||
r.logger.Warn(warning)
|
r.warner.Warn(warning)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("environment variable HTTPPROXY_PORT (or TINYPROXY_PORT, PROXY_PORT): %w", err)
|
return fmt.Errorf("environment variable HTTPPROXY_PORT (or TINYPROXY_PORT, PROXY_PORT): %w", err)
|
||||||
|
|||||||
@@ -9,32 +9,37 @@ import (
|
|||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
ovpnextract "github.com/qdm12/gluetun/internal/openvpn/extract"
|
ovpnextract "github.com/qdm12/gluetun/internal/openvpn/extract"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/qdm12/golibs/params"
|
||||||
"github.com/qdm12/golibs/verification"
|
"github.com/qdm12/golibs/verification"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:generate mockgen -destination=warner_mock_test.go -package configuration . Warner
|
||||||
|
|
||||||
type reader struct {
|
type reader struct {
|
||||||
servers models.AllServers
|
servers models.AllServers
|
||||||
env params.Interface
|
env params.Interface
|
||||||
logger logging.Logger
|
warner Warner
|
||||||
regex verification.Regex
|
regex verification.Regex
|
||||||
ovpnExt ovpnextract.Interface
|
ovpnExt ovpnextract.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Warner interface {
|
||||||
|
Warn(s string)
|
||||||
|
}
|
||||||
|
|
||||||
func newReader(env params.Interface,
|
func newReader(env params.Interface,
|
||||||
servers models.AllServers, logger logging.Logger) reader {
|
servers models.AllServers, warner Warner) reader {
|
||||||
return reader{
|
return reader{
|
||||||
servers: servers,
|
servers: servers,
|
||||||
env: env,
|
env: env,
|
||||||
logger: logger,
|
warner: warner,
|
||||||
regex: verification.NewRegex(),
|
regex: verification.NewRegex(),
|
||||||
ovpnExt: ovpnextract.New(),
|
ovpnExt: ovpnextract.New(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *reader) onRetroActive(oldKey, newKey string) {
|
func (r *reader) onRetroActive(oldKey, newKey string) {
|
||||||
r.logger.Warn(
|
r.warner.Warn(
|
||||||
"You are using the old environment variable " + oldKey +
|
"You are using the old environment variable " + oldKey +
|
||||||
", please consider changing it to " + newKey)
|
", please consider changing it to " + newKey)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (settings *ControlServer) read(r reader) (err error) {
|
|||||||
settings.Port, warning, err = r.env.ListeningPort(
|
settings.Port, warning, err = r.env.ListeningPort(
|
||||||
"HTTP_CONTROL_SERVER_PORT", params.Default("8000"))
|
"HTTP_CONTROL_SERVER_PORT", params.Default("8000"))
|
||||||
if len(warning) > 0 {
|
if len(warning) > 0 {
|
||||||
r.logger.Warn(warning)
|
r.warner.Warn(warning)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("environment variable HTTP_CONTROL_SERVER_PORT: %w", err)
|
return fmt.Errorf("environment variable HTTP_CONTROL_SERVER_PORT: %w", err)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/qdm12/golibs/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,8 +65,8 @@ var (
|
|||||||
// Read obtains all configuration options for the program and returns an error as soon
|
// Read obtains all configuration options for the program and returns an error as soon
|
||||||
// as an error is encountered reading them.
|
// as an error is encountered reading them.
|
||||||
func (settings *Settings) Read(env params.Interface, servers models.AllServers,
|
func (settings *Settings) Read(env params.Interface, servers models.AllServers,
|
||||||
logger logging.Logger) (err error) {
|
warner Warner) (err error) {
|
||||||
r := newReader(env, servers, logger)
|
r := newReader(env, servers, warner)
|
||||||
|
|
||||||
settings.VersionInformation, err = r.env.OnOff("VERSION_INFORMATION", params.Default("on"))
|
settings.VersionInformation, err = r.env.OnOff("VERSION_INFORMATION", params.Default("on"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (settings *ShadowSocks) read(r reader) (err error) {
|
|||||||
|
|
||||||
warning, err := settings.getAddress(r.env)
|
warning, err := settings.getAddress(r.env)
|
||||||
if warning != "" {
|
if warning != "" {
|
||||||
r.logger.Warn(warning)
|
r.warner.Warn(warning)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
46
internal/configuration/warner_mock_test.go
Normal file
46
internal/configuration/warner_mock_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: github.com/qdm12/gluetun/internal/configuration (interfaces: Warner)
|
||||||
|
|
||||||
|
// Package configuration is a generated GoMock package.
|
||||||
|
package configuration
|
||||||
|
|
||||||
|
import (
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockWarner is a mock of Warner interface.
|
||||||
|
type MockWarner struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockWarnerMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockWarnerMockRecorder is the mock recorder for MockWarner.
|
||||||
|
type MockWarnerMockRecorder struct {
|
||||||
|
mock *MockWarner
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockWarner creates a new mock instance.
|
||||||
|
func NewMockWarner(ctrl *gomock.Controller) *MockWarner {
|
||||||
|
mock := &MockWarner{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockWarnerMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockWarner) EXPECT() *MockWarnerMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn mocks base method.
|
||||||
|
func (m *MockWarner) Warn(arg0 string) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
m.ctrl.Call(m, "Warn", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn indicates an expected call of Warn.
|
||||||
|
func (mr *MockWarnerMockRecorder) Warn(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockWarner)(nil).Warn), arg0)
|
||||||
|
}
|
||||||
8
internal/dns/logger.go
Normal file
8
internal/dns/logger.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(s string)
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/dns/state"
|
"github.com/qdm12/gluetun/internal/dns/state"
|
||||||
"github.com/qdm12/gluetun/internal/loopstate"
|
"github.com/qdm12/gluetun/internal/loopstate"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Looper = (*Loop)(nil)
|
var _ Looper = (*Loop)(nil)
|
||||||
@@ -33,7 +32,7 @@ type Loop struct {
|
|||||||
resolvConf string
|
resolvConf string
|
||||||
blockBuilder blacklist.Builder
|
blockBuilder blacklist.Builder
|
||||||
client *http.Client
|
client *http.Client
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
userTrigger bool
|
userTrigger bool
|
||||||
start <-chan struct{}
|
start <-chan struct{}
|
||||||
running chan<- models.LoopStatus
|
running chan<- models.LoopStatus
|
||||||
@@ -48,7 +47,7 @@ type Loop struct {
|
|||||||
const defaultBackoffTime = 10 * time.Second
|
const defaultBackoffTime = 10 * time.Second
|
||||||
|
|
||||||
func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http.Client,
|
func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http.Client,
|
||||||
logger logging.Logger) *Loop {
|
logger Logger) *Loop {
|
||||||
start := make(chan struct{})
|
start := make(chan struct{})
|
||||||
running := make(chan models.LoopStatus)
|
running := make(chan models.LoopStatus)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/routing"
|
"github.com/qdm12/gluetun/internal/routing"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Configurator = (*Config)(nil)
|
var _ Configurator = (*Config)(nil)
|
||||||
@@ -25,7 +24,7 @@ type Configurator interface {
|
|||||||
|
|
||||||
type Config struct { //nolint:maligned
|
type Config struct { //nolint:maligned
|
||||||
runner command.Runner
|
runner command.Runner
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
iptablesMutex sync.Mutex
|
iptablesMutex sync.Mutex
|
||||||
ip6tablesMutex sync.Mutex
|
ip6tablesMutex sync.Mutex
|
||||||
defaultInterface string
|
defaultInterface string
|
||||||
@@ -47,7 +46,7 @@ type Config struct { //nolint:maligned
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig creates a new Config instance.
|
// NewConfig creates a new Config instance.
|
||||||
func NewConfig(logger logging.Logger, runner command.Runner,
|
func NewConfig(logger Logger, runner command.Runner,
|
||||||
defaultInterface string, defaultGateway net.IP,
|
defaultInterface string, defaultGateway net.IP,
|
||||||
localNetworks []routing.LocalNetwork, localIP net.IP) *Config {
|
localNetworks []routing.LocalNetwork, localIP net.IP) *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
|
|||||||
7
internal/firewall/logger.go
Normal file
7
internal/firewall/logger.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package firewall
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(s string)
|
||||||
|
Info(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -4,21 +4,17 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
logger logging.Logger
|
|
||||||
healthErr error
|
healthErr error
|
||||||
healthErrMu sync.RWMutex
|
healthErrMu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var errHealthcheckNotRunYet = errors.New("healthcheck did not run yet")
|
var errHealthcheckNotRunYet = errors.New("healthcheck did not run yet")
|
||||||
|
|
||||||
func newHandler(logger logging.Logger) *handler {
|
func newHandler() *handler {
|
||||||
return &handler{
|
return &handler{
|
||||||
logger: logger,
|
|
||||||
healthErr: errHealthcheckNotRunYet,
|
healthErr: errHealthcheckNotRunYet,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
internal/healthcheck/logger.go
Normal file
6
internal/healthcheck/logger.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package healthcheck
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Info(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/gluetun/internal/vpn"
|
"github.com/qdm12/gluetun/internal/vpn"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ ServerRunner = (*Server)(nil)
|
var _ ServerRunner = (*Server)(nil)
|
||||||
@@ -15,7 +14,7 @@ type ServerRunner interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
handler *handler
|
handler *handler
|
||||||
pinger Pinger
|
pinger Pinger
|
||||||
config configuration.Health
|
config configuration.Health
|
||||||
@@ -23,10 +22,10 @@ type Server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(config configuration.Health,
|
func NewServer(config configuration.Health,
|
||||||
logger logging.Logger, vpnLooper vpn.Looper) *Server {
|
logger Logger, vpnLooper vpn.Looper) *Server {
|
||||||
return &Server{
|
return &Server{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
handler: newHandler(logger),
|
handler: newHandler(),
|
||||||
pinger: newPinger(config.AddressToPing),
|
pinger: newPinger(config.AddressToPing),
|
||||||
config: config,
|
config: config,
|
||||||
vpn: vpnHealth{
|
vpn: vpnHealth{
|
||||||
|
|||||||
@@ -5,11 +5,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHandler(ctx context.Context, wg *sync.WaitGroup, logger logging.Logger,
|
func newHandler(ctx context.Context, wg *sync.WaitGroup, logger Logger,
|
||||||
stealth, verbose bool, username, password string) http.Handler {
|
stealth, verbose bool, username, password string) http.Handler {
|
||||||
const httpTimeout = 24 * time.Hour
|
const httpTimeout = 24 * time.Hour
|
||||||
return &handler{
|
return &handler{
|
||||||
@@ -30,7 +28,7 @@ type handler struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
client *http.Client
|
client *http.Client
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
verbose, stealth bool
|
verbose, stealth bool
|
||||||
username, password string
|
username, password string
|
||||||
}
|
}
|
||||||
|
|||||||
21
internal/httpproxy/logger.go
Normal file
21
internal/httpproxy/logger.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package httpproxy
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(s string)
|
||||||
|
infoer
|
||||||
|
Warn(s string)
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoErrorer interface {
|
||||||
|
infoer
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorer interface {
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/httpproxy/state"
|
"github.com/qdm12/gluetun/internal/httpproxy/state"
|
||||||
"github.com/qdm12/gluetun/internal/loopstate"
|
"github.com/qdm12/gluetun/internal/loopstate"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Looper = (*Loop)(nil)
|
var _ Looper = (*Loop)(nil)
|
||||||
@@ -26,7 +25,7 @@ type Loop struct {
|
|||||||
statusManager loopstate.Manager
|
statusManager loopstate.Manager
|
||||||
state state.Manager
|
state state.Manager
|
||||||
// Other objects
|
// Other objects
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
// Internal channels and locks
|
// Internal channels and locks
|
||||||
running chan models.LoopStatus
|
running chan models.LoopStatus
|
||||||
stop, stopped chan struct{}
|
stop, stopped chan struct{}
|
||||||
@@ -37,7 +36,7 @@ type Loop struct {
|
|||||||
|
|
||||||
const defaultBackoffTime = 10 * time.Second
|
const defaultBackoffTime = 10 * time.Second
|
||||||
|
|
||||||
func NewLoop(logger logging.Logger, settings configuration.HTTPProxy) *Loop {
|
func NewLoop(logger Logger, settings configuration.HTTPProxy) *Loop {
|
||||||
start := make(chan struct{})
|
start := make(chan struct{})
|
||||||
running := make(chan models.LoopStatus)
|
running := make(chan models.LoopStatus)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
|
|||||||
@@ -5,18 +5,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
address string
|
address string
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
logger logging.Logger
|
logger infoErrorer
|
||||||
internalWG *sync.WaitGroup
|
internalWG *sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, address string, logger logging.Logger,
|
func New(ctx context.Context, address string, logger Logger,
|
||||||
stealth, verbose bool, username, password string) *Server {
|
stealth, verbose bool, username, password string) *Server {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
return &Server{
|
return &Server{
|
||||||
|
|||||||
12
internal/openvpn/logger.go
Normal file
12
internal/openvpn/logger.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package openvpn
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(s string)
|
||||||
|
Infoer
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package openvpn
|
|||||||
import (
|
import (
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Interface = (*Configurator)(nil)
|
var _ Interface = (*Configurator)(nil)
|
||||||
@@ -15,15 +14,15 @@ type Interface interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Configurator struct {
|
type Configurator struct {
|
||||||
logger logging.Logger
|
logger Infoer
|
||||||
cmder command.RunStarter
|
cmder command.RunStarter
|
||||||
configPath string
|
configPath string
|
||||||
authFilePath string
|
authFilePath string
|
||||||
puid, pgid int
|
puid, pgid int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(logger logging.Logger,
|
func New(logger Infoer, cmder command.RunStarter,
|
||||||
cmder command.RunStarter, puid, pgid int) *Configurator {
|
puid, pgid int) *Configurator {
|
||||||
return &Configurator{
|
return &Configurator{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cmder: cmder,
|
cmder: cmder,
|
||||||
|
|||||||
@@ -5,17 +5,16 @@ import (
|
|||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Runner struct {
|
type Runner struct {
|
||||||
settings configuration.OpenVPN
|
settings configuration.OpenVPN
|
||||||
starter command.Starter
|
starter command.Starter
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRunner(settings configuration.OpenVPN, starter command.Starter,
|
func NewRunner(settings configuration.OpenVPN, starter command.Starter,
|
||||||
logger logging.Logger) *Runner {
|
logger Logger) *Runner {
|
||||||
return &Runner{
|
return &Runner{
|
||||||
starter: starter,
|
starter: starter,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func streamLines(ctx context.Context, done chan<- struct{},
|
func streamLines(ctx context.Context, done chan<- struct{},
|
||||||
logger logging.Logger, stdout, stderr chan string,
|
logger Logger, stdout, stderr chan string,
|
||||||
tunnelReady chan<- struct{}) {
|
tunnelReady chan<- struct{}) {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
|
|||||||
7
internal/portforward/logger.go
Normal file
7
internal/portforward/logger.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package portforward
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/loopstate"
|
"github.com/qdm12/gluetun/internal/loopstate"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/portforward/state"
|
"github.com/qdm12/gluetun/internal/portforward/state"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Looper = (*Loop)(nil)
|
var _ Looper = (*Loop)(nil)
|
||||||
@@ -30,7 +29,7 @@ type Loop struct {
|
|||||||
// Objects
|
// Objects
|
||||||
client *http.Client
|
client *http.Client
|
||||||
portAllower firewall.PortAllower
|
portAllower firewall.PortAllower
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
// Internal channels and locks
|
// Internal channels and locks
|
||||||
start chan struct{}
|
start chan struct{}
|
||||||
running chan models.LoopStatus
|
running chan models.LoopStatus
|
||||||
@@ -45,7 +44,7 @@ const defaultBackoffTime = 5 * time.Second
|
|||||||
|
|
||||||
func NewLoop(settings configuration.PortForwarding,
|
func NewLoop(settings configuration.PortForwarding,
|
||||||
client *http.Client, portAllower firewall.PortAllower,
|
client *http.Client, portAllower firewall.PortAllower,
|
||||||
logger logging.Logger) *Loop {
|
logger Logger) *Loop {
|
||||||
start := make(chan struct{})
|
start := make(chan struct{})
|
||||||
running := make(chan models.LoopStatus)
|
running := make(chan models.LoopStatus)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
portCh <- port
|
portCh <- port
|
||||||
|
|
||||||
// Infinite loop
|
// Infinite loop
|
||||||
err = startData.PortForwarder.KeepPortForward(ctx, l.client, l.logger,
|
err = startData.PortForwarder.KeepPortForward(ctx, l.client,
|
||||||
port, startData.Gateway, startData.ServerName)
|
port, startData.Gateway, startData.ServerName)
|
||||||
errorCh <- err
|
errorCh <- err
|
||||||
}(pfCtx, startData)
|
}(pfCtx, startData)
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||||
"github.com/qdm12/golibs/format"
|
"github.com/qdm12/golibs/format"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -31,13 +31,13 @@ var (
|
|||||||
|
|
||||||
// PortForward obtains a VPN server side port forwarded from PIA.
|
// PortForward obtains a VPN server side port forwarded from PIA.
|
||||||
func (p *PIA) PortForward(ctx context.Context, client *http.Client,
|
func (p *PIA) PortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, gateway net.IP, serverName string) (
|
logger utils.Logger, gateway net.IP, serverName string) (
|
||||||
port uint16, err error) {
|
port uint16, err error) {
|
||||||
server := constants.PIAServerWhereName(p.servers, serverName)
|
server := constants.PIAServerWhereName(p.servers, serverName)
|
||||||
if !server.PortForward {
|
if !server.PortForward {
|
||||||
logger.Error("The server " + serverName +
|
logger.Error("The server " + serverName +
|
||||||
" (region " + server.Region + ") does not support port forwarding")
|
" (region " + server.Region + ") does not support port forwarding")
|
||||||
return
|
return 0, nil
|
||||||
}
|
}
|
||||||
if gateway == nil {
|
if gateway == nil {
|
||||||
return 0, ErrGatewayIPIsNil
|
return 0, ErrGatewayIPIsNil
|
||||||
@@ -92,8 +92,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (p *PIA) KeepPortForward(ctx context.Context, client *http.Client,
|
func (p *PIA) KeepPortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, port uint16, gateway net.IP, serverName string) (
|
port uint16, gateway net.IP, serverName string) (err error) {
|
||||||
err error) {
|
|
||||||
privateIPClient, err := newHTTPClient(serverName)
|
privateIPClient, err := newHTTPClient(serverName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w: %s", ErrCreateHTTPClient, err)
|
return fmt.Errorf("%w: %s", ErrCreateHTTPClient, err)
|
||||||
|
|||||||
@@ -26,11 +26,11 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/provider/purevpn"
|
"github.com/qdm12/gluetun/internal/provider/purevpn"
|
||||||
"github.com/qdm12/gluetun/internal/provider/surfshark"
|
"github.com/qdm12/gluetun/internal/provider/surfshark"
|
||||||
"github.com/qdm12/gluetun/internal/provider/torguard"
|
"github.com/qdm12/gluetun/internal/provider/torguard"
|
||||||
|
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||||
"github.com/qdm12/gluetun/internal/provider/vpnunlimited"
|
"github.com/qdm12/gluetun/internal/provider/vpnunlimited"
|
||||||
"github.com/qdm12/gluetun/internal/provider/vyprvpn"
|
"github.com/qdm12/gluetun/internal/provider/vyprvpn"
|
||||||
"github.com/qdm12/gluetun/internal/provider/wevpn"
|
"github.com/qdm12/gluetun/internal/provider/wevpn"
|
||||||
"github.com/qdm12/gluetun/internal/provider/windscribe"
|
"github.com/qdm12/gluetun/internal/provider/windscribe"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Provider contains methods to read and modify the openvpn configuration to connect as a client.
|
// Provider contains methods to read and modify the openvpn configuration to connect as a client.
|
||||||
@@ -42,11 +42,10 @@ type Provider interface {
|
|||||||
|
|
||||||
type PortForwarder interface {
|
type PortForwarder interface {
|
||||||
PortForward(ctx context.Context, client *http.Client,
|
PortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, gateway net.IP, serverName string) (
|
logger utils.Logger, gateway net.IP, serverName string) (
|
||||||
port uint16, err error)
|
port uint16, err error)
|
||||||
KeepPortForward(ctx context.Context, client *http.Client,
|
KeepPortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, port uint16, gateway net.IP, serverName string) (
|
port uint16, gateway net.IP, serverName string) (err error)
|
||||||
err error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(provider string, allServers models.AllServers, timeNow func() time.Time) Provider {
|
func New(provider string, allServers models.AllServers, timeNow func() time.Time) Provider {
|
||||||
|
|||||||
7
internal/provider/utils/logger.go
Normal file
7
internal/provider/utils/logger.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -6,17 +6,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type NoPortForwarder interface {
|
type NoPortForwarder interface {
|
||||||
PortForward(ctx context.Context, client *http.Client,
|
PortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, gateway net.IP, serverName string) (
|
logger Logger, gateway net.IP, serverName string) (
|
||||||
port uint16, err error)
|
port uint16, err error)
|
||||||
KeepPortForward(ctx context.Context, client *http.Client,
|
KeepPortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, port uint16, gateway net.IP, serverName string) (
|
port uint16, gateway net.IP, serverName string) (err error)
|
||||||
err error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type NoPortForwarding struct {
|
type NoPortForwarding struct {
|
||||||
@@ -32,13 +29,11 @@ func NewNoPortForwarding(providerName string) *NoPortForwarding {
|
|||||||
var ErrPortForwardingNotSupported = errors.New("custom port forwarding obtention is not supported")
|
var ErrPortForwardingNotSupported = errors.New("custom port forwarding obtention is not supported")
|
||||||
|
|
||||||
func (n *NoPortForwarding) PortForward(ctx context.Context, client *http.Client,
|
func (n *NoPortForwarding) PortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, gateway net.IP, serverName string) (
|
logger Logger, gateway net.IP, serverName string) (port uint16, err error) {
|
||||||
port uint16, err error) {
|
|
||||||
return 0, fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
|
return 0, fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NoPortForwarding) KeepPortForward(ctx context.Context, client *http.Client,
|
func (n *NoPortForwarding) KeepPortForward(ctx context.Context, client *http.Client,
|
||||||
logger logging.Logger, port uint16, gateway net.IP, serverName string) (
|
port uint16, gateway net.IP, serverName string) (err error) {
|
||||||
err error) {
|
|
||||||
return fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
|
return fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
|
||||||
}
|
}
|
||||||
|
|||||||
7
internal/publicip/logger.go
Normal file
7
internal/publicip/logger.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package publicip
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/loopstate"
|
"github.com/qdm12/gluetun/internal/loopstate"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/publicip/state"
|
"github.com/qdm12/gluetun/internal/publicip/state"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ Looper = (*Loop)(nil)
|
var _ Looper = (*Loop)(nil)
|
||||||
@@ -29,7 +28,7 @@ type Loop struct {
|
|||||||
// Objects
|
// Objects
|
||||||
fetcher Fetcher
|
fetcher Fetcher
|
||||||
client *http.Client
|
client *http.Client
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
// Fixed settings
|
// Fixed settings
|
||||||
puid int
|
puid int
|
||||||
pgid int
|
pgid int
|
||||||
@@ -47,7 +46,7 @@ type Loop struct {
|
|||||||
|
|
||||||
const defaultBackoffTime = 5 * time.Second
|
const defaultBackoffTime = 5 * time.Second
|
||||||
|
|
||||||
func NewLoop(client *http.Client, logger logging.Logger,
|
func NewLoop(client *http.Client, logger Logger,
|
||||||
settings configuration.PublicIP, puid, pgid int) *Loop {
|
settings configuration.PublicIP, puid, pgid int) *Loop {
|
||||||
start := make(chan struct{})
|
start := make(chan struct{})
|
||||||
running := make(chan models.LoopStatus)
|
running := make(chan models.LoopStatus)
|
||||||
|
|||||||
10
internal/routing/logger.go
Normal file
10
internal/routing/logger.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package routing
|
||||||
|
|
||||||
|
//go:generate mockgen -destination=logger_mock_test.go -package routing . Logger
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(s string)
|
||||||
|
Info(s string)
|
||||||
|
Warn(s string)
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
82
internal/routing/logger_mock_test.go
Normal file
82
internal/routing/logger_mock_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: github.com/qdm12/gluetun/internal/routing (interfaces: Logger)
|
||||||
|
|
||||||
|
// Package routing is a generated GoMock package.
|
||||||
|
package routing
|
||||||
|
|
||||||
|
import (
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockLogger is a mock of Logger interface.
|
||||||
|
type MockLogger struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockLoggerMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockLoggerMockRecorder is the mock recorder for MockLogger.
|
||||||
|
type MockLoggerMockRecorder struct {
|
||||||
|
mock *MockLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockLogger creates a new mock instance.
|
||||||
|
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
|
||||||
|
mock := &MockLogger{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockLoggerMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug mocks base method.
|
||||||
|
func (m *MockLogger) Debug(arg0 string) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
m.ctrl.Call(m, "Debug", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug indicates an expected call of Debug.
|
||||||
|
func (mr *MockLoggerMockRecorder) Debug(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockLogger)(nil).Debug), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error mocks base method.
|
||||||
|
func (m *MockLogger) Error(arg0 string) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
m.ctrl.Call(m, "Error", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error indicates an expected call of Error.
|
||||||
|
func (mr *MockLoggerMockRecorder) Error(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockLogger)(nil).Error), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info mocks base method.
|
||||||
|
func (m *MockLogger) Info(arg0 string) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
m.ctrl.Call(m, "Info", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info indicates an expected call of Info.
|
||||||
|
func (mr *MockLoggerMockRecorder) Info(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLogger)(nil).Info), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn mocks base method.
|
||||||
|
func (m *MockLogger) Warn(arg0 string) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
m.ctrl.Call(m, "Warn", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn indicates an expected call of Warn.
|
||||||
|
func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), arg0)
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/netlink"
|
"github.com/qdm12/gluetun/internal/netlink"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReadWriter interface {
|
type ReadWriter interface {
|
||||||
@@ -35,14 +34,13 @@ type Writer interface {
|
|||||||
|
|
||||||
type Routing struct {
|
type Routing struct {
|
||||||
netLinker netlink.NetLinker
|
netLinker netlink.NetLinker
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
outboundSubnets []net.IPNet
|
outboundSubnets []net.IPNet
|
||||||
stateMutex sync.RWMutex
|
stateMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new routing instance.
|
// New creates a new routing instance.
|
||||||
func New(netLinker netlink.NetLinker,
|
func New(netLinker netlink.NetLinker, logger Logger) *Routing {
|
||||||
logger logging.Logger) *Routing {
|
|
||||||
return &Routing{
|
return &Routing{
|
||||||
netLinker: netLinker,
|
netLinker: netLinker,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/qdm12/gluetun/internal/netlink"
|
"github.com/qdm12/gluetun/internal/netlink"
|
||||||
"github.com/qdm12/gluetun/internal/netlink/mock_netlink"
|
"github.com/qdm12/gluetun/internal/netlink/mock_netlink"
|
||||||
"github.com/qdm12/golibs/logging/mock_logging"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -116,7 +115,7 @@ func Test_Routing_addIPRule(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
|
|
||||||
logger := mock_logging.NewMockLogger(ctrl)
|
logger := NewMockLogger(ctrl)
|
||||||
logger.EXPECT().Debug(testCase.dbgMsg)
|
logger.EXPECT().Debug(testCase.dbgMsg)
|
||||||
|
|
||||||
netLinker := mock_netlink.NewMockNetLinker(ctrl)
|
netLinker := mock_netlink.NewMockNetLinker(ctrl)
|
||||||
@@ -234,7 +233,7 @@ func Test_Routing_deleteIPRule(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
|
|
||||||
logger := mock_logging.NewMockLogger(ctrl)
|
logger := NewMockLogger(ctrl)
|
||||||
logger.EXPECT().Debug(testCase.dbgMsg)
|
logger.EXPECT().Debug(testCase.dbgMsg)
|
||||||
|
|
||||||
netLinker := mock_netlink.NewMockNetLinker(ctrl)
|
netLinker := mock_netlink.NewMockNetLinker(ctrl)
|
||||||
|
|||||||
@@ -7,22 +7,21 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/dns"
|
"github.com/qdm12/gluetun/internal/dns"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newDNSHandler(ctx context.Context, looper dns.Looper,
|
func newDNSHandler(ctx context.Context, looper dns.Looper,
|
||||||
logger logging.Logger) http.Handler {
|
warner warner) http.Handler {
|
||||||
return &dnsHandler{
|
return &dnsHandler{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
looper: looper,
|
looper: looper,
|
||||||
logger: logger,
|
warner: warner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type dnsHandler struct {
|
type dnsHandler struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
looper dns.Looper
|
looper dns.Looper
|
||||||
logger logging.Logger
|
warner warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *dnsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *dnsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -47,7 +46,7 @@ func (h *dnsHandler) getStatus(w http.ResponseWriter) {
|
|||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
data := statusWrapper{Status: string(status)}
|
data := statusWrapper{Status: string(status)}
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -72,7 +71,7 @@ func (h *dnsHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,9 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/publicip"
|
"github.com/qdm12/gluetun/internal/publicip"
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
"github.com/qdm12/gluetun/internal/vpn"
|
"github.com/qdm12/gluetun/internal/vpn"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHandler(ctx context.Context, logger logging.Logger, logging bool,
|
func newHandler(ctx context.Context, logger infoWarner, logging bool,
|
||||||
buildInfo models.BuildInformation,
|
buildInfo models.BuildInformation,
|
||||||
vpnLooper vpn.Looper,
|
vpnLooper vpn.Looper,
|
||||||
pfGetter portforward.Getter,
|
pfGetter portforward.Getter,
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/dns"
|
"github.com/qdm12/gluetun/internal/dns"
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
"github.com/qdm12/gluetun/internal/vpn"
|
"github.com/qdm12/gluetun/internal/vpn"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHandlerV0(ctx context.Context, logger logging.Logger,
|
func newHandlerV0(ctx context.Context, logger infoWarner,
|
||||||
vpn vpn.Looper, dns dns.Looper, updater updater.Looper) http.Handler {
|
vpn vpn.Looper, dns dns.Looper, updater updater.Looper) http.Handler {
|
||||||
return &handlerV0{
|
return &handlerV0{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
@@ -24,7 +23,7 @@ func newHandlerV0(ctx context.Context, logger logging.Logger,
|
|||||||
|
|
||||||
type handlerV0 struct {
|
type handlerV0 struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
logger logging.Logger
|
logger infoWarner
|
||||||
vpn vpn.Looper
|
vpn vpn.Looper
|
||||||
dns dns.Looper
|
dns dns.Looper
|
||||||
updater updater.Looper
|
updater updater.Looper
|
||||||
|
|||||||
@@ -7,13 +7,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHandlerV1(logger logging.Logger, buildInfo models.BuildInformation,
|
func newHandlerV1(w warner, buildInfo models.BuildInformation,
|
||||||
openvpn, dns, updater, publicip http.Handler) http.Handler {
|
openvpn, dns, updater, publicip http.Handler) http.Handler {
|
||||||
return &handlerV1{
|
return &handlerV1{
|
||||||
logger: logger,
|
warner: w,
|
||||||
buildInfo: buildInfo,
|
buildInfo: buildInfo,
|
||||||
openvpn: openvpn,
|
openvpn: openvpn,
|
||||||
dns: dns,
|
dns: dns,
|
||||||
@@ -23,7 +22,7 @@ func newHandlerV1(logger logging.Logger, buildInfo models.BuildInformation,
|
|||||||
}
|
}
|
||||||
|
|
||||||
type handlerV1 struct {
|
type handlerV1 struct {
|
||||||
logger logging.Logger
|
warner warner
|
||||||
buildInfo models.BuildInformation
|
buildInfo models.BuildInformation
|
||||||
openvpn http.Handler
|
openvpn http.Handler
|
||||||
dns http.Handler
|
dns http.Handler
|
||||||
@@ -52,7 +51,7 @@ func (h *handlerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *handlerV1) getVersion(w http.ResponseWriter) {
|
func (h *handlerV1) getVersion(w http.ResponseWriter) {
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(h.buildInfo); err != nil {
|
if err := encoder.Encode(h.buildInfo); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func withLogMiddleware(childHandler http.Handler, logger logging.Logger, enabled bool) *logMiddleware {
|
func withLogMiddleware(childHandler http.Handler, logger infoer, enabled bool) *logMiddleware {
|
||||||
return &logMiddleware{
|
return &logMiddleware{
|
||||||
childHandler: childHandler,
|
childHandler: childHandler,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
@@ -20,7 +18,7 @@ func withLogMiddleware(childHandler http.Handler, logger logging.Logger, enabled
|
|||||||
|
|
||||||
type logMiddleware struct {
|
type logMiddleware struct {
|
||||||
childHandler http.Handler
|
childHandler http.Handler
|
||||||
logger logging.Logger
|
logger infoer
|
||||||
timeNow func() time.Time
|
timeNow func() time.Time
|
||||||
enabled bool
|
enabled bool
|
||||||
enabledMu sync.RWMutex
|
enabledMu sync.RWMutex
|
||||||
|
|||||||
29
internal/server/logger.go
Normal file
29
internal/server/logger.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
infoer
|
||||||
|
warner
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoErrorer interface {
|
||||||
|
infoer
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoWarner interface {
|
||||||
|
infoer
|
||||||
|
warner
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type warner interface {
|
||||||
|
Warn(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorer interface {
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -8,16 +8,15 @@ import (
|
|||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/portforward"
|
"github.com/qdm12/gluetun/internal/portforward"
|
||||||
"github.com/qdm12/gluetun/internal/vpn"
|
"github.com/qdm12/gluetun/internal/vpn"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
|
func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
|
||||||
pfGetter portforward.Getter, logger logging.Logger) http.Handler {
|
pfGetter portforward.Getter, w warner) http.Handler {
|
||||||
return &openvpnHandler{
|
return &openvpnHandler{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
looper: looper,
|
looper: looper,
|
||||||
pf: pfGetter,
|
pf: pfGetter,
|
||||||
logger: logger,
|
warner: w,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +24,7 @@ type openvpnHandler struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
looper vpn.Looper
|
looper vpn.Looper
|
||||||
pf portforward.Getter
|
pf portforward.Getter
|
||||||
logger logging.Logger
|
warner warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *openvpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *openvpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -64,7 +63,7 @@ func (h *openvpnHandler) getStatus(w http.ResponseWriter) {
|
|||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
data := statusWrapper{Status: string(status)}
|
data := statusWrapper{Status: string(status)}
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -89,7 +88,7 @@ func (h *openvpnHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -102,7 +101,7 @@ func (h *openvpnHandler) getSettings(w http.ResponseWriter) {
|
|||||||
settings.Password = "redacted"
|
settings.Password = "redacted"
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(settings); err != nil {
|
if err := encoder.Encode(settings); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -113,7 +112,7 @@ func (h *openvpnHandler) getPortForwarded(w http.ResponseWriter) {
|
|||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
data := portWrapper{Port: port}
|
data := portWrapper{Port: port}
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,21 +6,18 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/publicip"
|
"github.com/qdm12/gluetun/internal/publicip"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newPublicIPHandler(
|
func newPublicIPHandler(looper publicip.Looper, w warner) http.Handler {
|
||||||
looper publicip.Looper,
|
|
||||||
logger logging.Logger) http.Handler {
|
|
||||||
return &publicIPHandler{
|
return &publicIPHandler{
|
||||||
looper: looper,
|
looper: looper,
|
||||||
logger: logger,
|
warner: w,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type publicIPHandler struct {
|
type publicIPHandler struct {
|
||||||
looper publicip.Looper
|
looper publicip.Looper
|
||||||
logger logging.Logger
|
warner warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *publicIPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *publicIPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -42,7 +39,7 @@ func (h *publicIPHandler) getPublicIP(w http.ResponseWriter) {
|
|||||||
data := h.looper.GetData()
|
data := h.looper.GetData()
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/publicip"
|
"github.com/qdm12/gluetun/internal/publicip"
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
"github.com/qdm12/gluetun/internal/vpn"
|
"github.com/qdm12/gluetun/internal/vpn"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server interface {
|
type Server interface {
|
||||||
@@ -22,11 +21,11 @@ type Server interface {
|
|||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
address string
|
address string
|
||||||
logger logging.Logger
|
logger infoErrorer
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, address string, logEnabled bool, logger logging.Logger,
|
func New(ctx context.Context, address string, logEnabled bool, logger Logger,
|
||||||
buildInfo models.BuildInformation, openvpnLooper vpn.Looper,
|
buildInfo models.BuildInformation, openvpnLooper vpn.Looper,
|
||||||
pfGetter portforward.Getter, unboundLooper dns.Looper,
|
pfGetter portforward.Getter, unboundLooper dns.Looper,
|
||||||
updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server {
|
updaterLooper updater.Looper, publicIPLooper publicip.Looper) Server {
|
||||||
|
|||||||
@@ -7,24 +7,23 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newUpdaterHandler(
|
func newUpdaterHandler(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
looper updater.Looper,
|
looper updater.Looper,
|
||||||
logger logging.Logger) http.Handler {
|
warner warner) http.Handler {
|
||||||
return &updaterHandler{
|
return &updaterHandler{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
looper: looper,
|
looper: looper,
|
||||||
logger: logger,
|
warner: warner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type updaterHandler struct {
|
type updaterHandler struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
looper updater.Looper
|
looper updater.Looper
|
||||||
logger logging.Logger
|
warner warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *updaterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *updaterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -49,7 +48,7 @@ func (h *updaterHandler) getStatus(w http.ResponseWriter) {
|
|||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
data := statusWrapper{Status: string(status)}
|
data := statusWrapper{Status: string(status)}
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -74,7 +73,7 @@ func (h *updaterHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||||
h.logger.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
19
internal/shadowsocks/logger.go
Normal file
19
internal/shadowsocks/logger.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package shadowsocks
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
debuger
|
||||||
|
infoer
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type debuger interface {
|
||||||
|
Debug(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorer interface {
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
shadowsockslib "github.com/qdm12/ss-server/pkg/tcpudp"
|
shadowsockslib "github.com/qdm12/ss-server/pkg/tcpudp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +25,7 @@ type Looper interface {
|
|||||||
type looper struct {
|
type looper struct {
|
||||||
state state
|
state state
|
||||||
// Other objects
|
// Other objects
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
// Internal channels and locks
|
// Internal channels and locks
|
||||||
loopLock sync.Mutex
|
loopLock sync.Mutex
|
||||||
running chan models.LoopStatus
|
running chan models.LoopStatus
|
||||||
@@ -53,7 +52,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
|
|||||||
|
|
||||||
const defaultBackoffTime = 10 * time.Second
|
const defaultBackoffTime = 10 * time.Second
|
||||||
|
|
||||||
func NewLooper(settings configuration.ShadowSocks, logger logging.Logger) Looper {
|
func NewLooper(settings configuration.ShadowSocks, logger Logger) Looper {
|
||||||
return &looper{
|
return &looper{
|
||||||
state: state{
|
state: state{
|
||||||
status: constants.Stopped,
|
status: constants.Stopped,
|
||||||
|
|||||||
@@ -3,20 +3,23 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
mergedServers models.AllServers
|
mergedServers models.AllServers
|
||||||
hardcodedServers models.AllServers
|
hardcodedServers models.AllServers
|
||||||
logger logging.Logger
|
logger InfoErrorer
|
||||||
filepath string
|
filepath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type InfoErrorer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new storage and reads the servers from the
|
// New creates a new storage and reads the servers from the
|
||||||
// embedded servers file and the file on disk.
|
// embedded servers file and the file on disk.
|
||||||
// Passing an empty filepath disables writing servers to a file.
|
// Passing an empty filepath disables writing servers to a file.
|
||||||
func New(logger logging.Logger, filepath string) (storage *Storage, err error) {
|
func New(logger InfoErrorer, filepath string) (storage *Storage, err error) {
|
||||||
// error returned covered by unit test
|
// error returned covered by unit test
|
||||||
harcodedServers, _ := parseHardcodedServers()
|
harcodedServers, _ := parseHardcodedServers()
|
||||||
|
|
||||||
|
|||||||
24
internal/updater/logger.go
Normal file
24
internal/updater/logger.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package updater
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
infoer
|
||||||
|
warner
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoErrorer interface {
|
||||||
|
infoer
|
||||||
|
errorer
|
||||||
|
}
|
||||||
|
|
||||||
|
type infoer interface {
|
||||||
|
Info(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type warner interface {
|
||||||
|
Warn(s string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorer interface {
|
||||||
|
Error(s string)
|
||||||
|
}
|
||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/storage"
|
"github.com/qdm12/gluetun/internal/storage"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Looper interface {
|
type Looper interface {
|
||||||
@@ -29,7 +28,7 @@ type looper struct {
|
|||||||
updater Updater
|
updater Updater
|
||||||
flusher storage.Flusher
|
flusher storage.Flusher
|
||||||
setAllServers func(allServers models.AllServers)
|
setAllServers func(allServers models.AllServers)
|
||||||
logger logging.Logger
|
logger infoErrorer
|
||||||
// Internal channels and locks
|
// Internal channels and locks
|
||||||
loopLock sync.Mutex
|
loopLock sync.Mutex
|
||||||
start chan struct{}
|
start chan struct{}
|
||||||
@@ -47,7 +46,7 @@ const defaultBackoffTime = 5 * time.Second
|
|||||||
|
|
||||||
func NewLooper(settings configuration.Updater, currentServers models.AllServers,
|
func NewLooper(settings configuration.Updater, currentServers models.AllServers,
|
||||||
flusher storage.Flusher, setAllServers func(allServers models.AllServers),
|
flusher storage.Flusher, setAllServers func(allServers models.AllServers),
|
||||||
client *http.Client, logger logging.Logger) Looper {
|
client *http.Client, logger Logger) Looper {
|
||||||
return &looper{
|
return &looper{
|
||||||
state: state{
|
state: state{
|
||||||
status: constants.Stopped,
|
status: constants.Stopped,
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||||
"github.com/qdm12/gluetun/internal/updater/unzip"
|
"github.com/qdm12/gluetun/internal/updater/unzip"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Updater interface {
|
type Updater interface {
|
||||||
@@ -26,7 +25,7 @@ type updater struct {
|
|||||||
servers models.AllServers
|
servers models.AllServers
|
||||||
|
|
||||||
// Functions for tests
|
// Functions for tests
|
||||||
logger logging.Logger
|
logger Logger
|
||||||
timeNow func() time.Time
|
timeNow func() time.Time
|
||||||
presolver resolver.Parallel
|
presolver resolver.Parallel
|
||||||
client *http.Client
|
client *http.Client
|
||||||
@@ -34,7 +33,7 @@ type updater struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(settings configuration.Updater, httpClient *http.Client,
|
func New(settings configuration.Updater, httpClient *http.Client,
|
||||||
currentServers models.AllServers, logger logging.Logger) Updater {
|
currentServers models.AllServers, logger Logger) Updater {
|
||||||
if settings.DNSAddress == "" {
|
if settings.DNSAddress == "" {
|
||||||
settings.DNSAddress = "1.1.1.1"
|
settings.DNSAddress = "1.1.1.1"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/openvpn"
|
"github.com/qdm12/gluetun/internal/openvpn"
|
||||||
"github.com/qdm12/gluetun/internal/provider"
|
"github.com/qdm12/gluetun/internal/provider"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -25,7 +24,7 @@ var (
|
|||||||
// It returns a serverName for port forwarding (PIA) and an error if it fails.
|
// It returns a serverName for port forwarding (PIA) and an error if it fails.
|
||||||
func setupOpenVPN(ctx context.Context, fw firewall.VPNConnectionSetter,
|
func setupOpenVPN(ctx context.Context, fw firewall.VPNConnectionSetter,
|
||||||
openvpnConf openvpn.Interface, providerConf provider.Provider,
|
openvpnConf openvpn.Interface, providerConf provider.Provider,
|
||||||
settings configuration.VPN, starter command.Starter, logger logging.Logger) (
|
settings configuration.VPN, starter command.Starter, logger openvpn.Logger) (
|
||||||
runner vpnRunner, serverName string, err error) {
|
runner vpnRunner, serverName string, err error) {
|
||||||
connection, err := providerConf.GetConnection(settings.Provider.ServerSelection)
|
connection, err := providerConf.GetConnection(settings.Provider.ServerSelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user