fix(server): /openvpn route status get and put

- get status return stopped if running wireguard
- put status changes vpn type if running wireguard
This commit is contained in:
Quentin McGaw
2024-04-08 07:25:31 +00:00
parent b3ceece779
commit c47858fdda

View File

@@ -5,6 +5,9 @@ import (
"encoding/json"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/vpn"
)
func newOpenvpnHandler(ctx context.Context, looper VPNLooper,
@@ -56,9 +59,16 @@ func (h *openvpnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func (h *openvpnHandler) getStatus(w http.ResponseWriter) {
status := h.looper.GetStatus()
vpnStatus := h.looper.GetStatus()
openVPNStatus := vpnStatus
if vpnStatus != constants.Stopped {
vpnSettings := h.looper.GetSettings()
if vpnSettings.Type != vpn.OpenVPN {
openVPNStatus = constants.Stopped
}
}
encoder := json.NewEncoder(w)
data := statusWrapper{Status: string(status)}
data := statusWrapper{Status: string(openVPNStatus)}
if err := encoder.Encode(data); err != nil {
h.warner.Warn(err.Error())
w.WriteHeader(http.StatusInternalServerError)
@@ -78,10 +88,20 @@ func (h *openvpnHandler) setStatus(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
outcome, err := h.looper.ApplyStatus(h.ctx, status)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
var outcome string
loopSettings := h.looper.GetSettings()
if status == constants.Running && loopSettings.Type != vpn.OpenVPN {
// Stop Wireguard if it was the selected type and we want to start OpenVPN
loopSettings.Type = vpn.OpenVPN
outcome = h.looper.SetSettings(h.ctx, loopSettings)
} else {
// Only update status of OpenVPN
outcome, err = h.looper.ApplyStatus(h.ctx, status)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {