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:
@@ -5,6 +5,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newOpenvpnHandler(ctx context.Context, looper VPNLooper,
|
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) {
|
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)
|
encoder := json.NewEncoder(w)
|
||||||
data := statusWrapper{Status: string(status)}
|
data := statusWrapper{Status: string(openVPNStatus)}
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
h.warner.Warn(err.Error())
|
h.warner.Warn(err.Error())
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
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)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
outcome, err := h.looper.ApplyStatus(h.ctx, status)
|
|
||||||
if err != nil {
|
var outcome string
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
loopSettings := h.looper.GetSettings()
|
||||||
return
|
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)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user