feat(server): patch VPN settings
- `PUT` at `/v1/vpn/settings` - Undocumented, experimental for now
This commit is contained in:
@@ -15,10 +15,11 @@ func newHandler(ctx context.Context, logger infoWarner, logging bool,
|
||||
unboundLooper DNSLoop,
|
||||
updaterLooper UpdaterLooper,
|
||||
publicIPLooper PublicIPLoop,
|
||||
storage Storage,
|
||||
) http.Handler {
|
||||
handler := &handler{}
|
||||
|
||||
vpn := newVPNHandler(ctx, vpnLooper, logger)
|
||||
vpn := newVPNHandler(ctx, vpnLooper, storage, logger)
|
||||
openvpn := newOpenvpnHandler(ctx, vpnLooper, pfGetter, logger)
|
||||
dns := newDNSHandler(ctx, unboundLooper, logger)
|
||||
updater := newUpdaterHandler(ctx, updaterLooper, logger)
|
||||
|
||||
@@ -12,6 +12,7 @@ type VPNLooper interface {
|
||||
ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
||||
outcome string, err error)
|
||||
GetSettings() (settings settings.VPN)
|
||||
SetSettings(ctx context.Context, settings settings.VPN) (outcome string)
|
||||
}
|
||||
|
||||
type DNSLoop interface {
|
||||
@@ -27,3 +28,7 @@ type PortForwardedGetter interface {
|
||||
type PublicIPLoop interface {
|
||||
GetData() (data models.PublicIP)
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
GetFilterChoices(provider string) models.FilterChoices
|
||||
}
|
||||
|
||||
@@ -11,9 +11,10 @@ import (
|
||||
func New(ctx context.Context, address string, logEnabled bool, logger Logger,
|
||||
buildInfo models.BuildInformation, openvpnLooper VPNLooper,
|
||||
pfGetter PortForwardedGetter, unboundLooper DNSLoop,
|
||||
updaterLooper UpdaterLooper, publicIPLooper PublicIPLoop) (server *httpserver.Server, err error) {
|
||||
updaterLooper UpdaterLooper, publicIPLooper PublicIPLoop, storage Storage) (
|
||||
server *httpserver.Server, err error) {
|
||||
handler := newHandler(ctx, logger, logEnabled, buildInfo,
|
||||
openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper)
|
||||
openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper, storage)
|
||||
|
||||
httpServerSettings := httpserver.Settings{
|
||||
Address: address,
|
||||
|
||||
@@ -5,21 +5,25 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
)
|
||||
|
||||
func newVPNHandler(ctx context.Context, looper VPNLooper,
|
||||
w warner) http.Handler {
|
||||
storage Storage, w warner) http.Handler {
|
||||
return &vpnHandler{
|
||||
ctx: ctx,
|
||||
looper: looper,
|
||||
warner: w,
|
||||
ctx: ctx,
|
||||
looper: looper,
|
||||
storage: storage,
|
||||
warner: w,
|
||||
}
|
||||
}
|
||||
|
||||
type vpnHandler struct {
|
||||
ctx context.Context //nolint:containedctx
|
||||
looper VPNLooper
|
||||
warner warner
|
||||
ctx context.Context //nolint:containedctx
|
||||
looper VPNLooper
|
||||
storage Storage
|
||||
warner warner
|
||||
}
|
||||
|
||||
func (h *vpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -38,6 +42,8 @@ func (h *vpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
h.getSettings(w)
|
||||
case http.MethodPut:
|
||||
h.patchSettings(w, r)
|
||||
default:
|
||||
http.Error(w, "method "+r.Method+" not supported", http.StatusBadRequest)
|
||||
}
|
||||
@@ -91,3 +97,32 @@ func (h *vpnHandler) getSettings(w http.ResponseWriter) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *vpnHandler) patchSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var overrideSettings settings.VPN
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&overrideSettings)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = r.Body.Close()
|
||||
if err != nil {
|
||||
h.warner.Warn("closing body: " + err.Error())
|
||||
}
|
||||
|
||||
updatedSettings := h.looper.GetSettings() // already copied
|
||||
updatedSettings.OverrideWith(overrideSettings)
|
||||
err = updatedSettings.Validate(h.storage)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
outcome := h.looper.SetSettings(h.ctx, updatedSettings)
|
||||
_, err = w.Write([]byte(outcome))
|
||||
if err != nil {
|
||||
h.warner.Warn("writing response: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user