platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
107
selective-vpn-api/app/vpn_handlers_auth.go
Normal file
107
selective-vpn-api/app/vpn_handlers_auth.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func handleVPNLoginState(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
state := VPNLoginState{
|
||||
State: "no_login",
|
||||
Msg: "login state file not found",
|
||||
Text: "AdGuard VPN: (no login data)",
|
||||
Color: "gray30",
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(loginStatePath)
|
||||
if err == nil {
|
||||
var fileState VPNLoginState
|
||||
if err := json.Unmarshal(data, &fileState); err == nil {
|
||||
if fileState.State != "" {
|
||||
state.State = fileState.State
|
||||
}
|
||||
if fileState.Email != "" {
|
||||
state.Email = fileState.Email
|
||||
}
|
||||
if fileState.Msg != "" {
|
||||
state.Msg = fileState.Msg
|
||||
}
|
||||
} else {
|
||||
state.State = "error"
|
||||
state.Msg = "invalid adguard-login.json: " + err.Error()
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
state.State = "error"
|
||||
state.Msg = err.Error()
|
||||
}
|
||||
|
||||
// text/color для GUI
|
||||
switch state.State {
|
||||
case "ok":
|
||||
if state.Email != "" {
|
||||
state.Text = fmt.Sprintf("AdGuard VPN: logged in as %s", state.Email)
|
||||
} else {
|
||||
state.Text = "AdGuard VPN: logged in"
|
||||
}
|
||||
state.Color = "green4"
|
||||
case "no_login":
|
||||
state.Text = "AdGuard VPN: (no login data)"
|
||||
state.Color = "gray30"
|
||||
default:
|
||||
state.Text = "AdGuard VPN: " + state.State
|
||||
state.Color = "orange3"
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, state)
|
||||
}
|
||||
|
||||
func handleVPNLogout(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
appendTraceLine("login", "logout")
|
||||
stdout, stderr, exitCode, err := runCommand(adgvpnCLI, "logout")
|
||||
res := cmdResult{
|
||||
OK: err == nil && exitCode == 0,
|
||||
ExitCode: exitCode,
|
||||
Stdout: stdout,
|
||||
Stderr: stderr,
|
||||
}
|
||||
if err != nil {
|
||||
res.Message = err.Error()
|
||||
} else {
|
||||
res.Message = "logout done"
|
||||
}
|
||||
|
||||
// refresh login state
|
||||
_, _, _, _ = runCommand("systemctl", "restart", adgvpnUnit)
|
||||
|
||||
writeJSON(w, http.StatusOK, res)
|
||||
}
|
||||
|
||||
func handleSystemdState(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
unit := strings.TrimSpace(r.URL.Query().Get("unit"))
|
||||
if unit == "" {
|
||||
http.Error(w, "unit required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
stdout, _, _, err := runCommand("systemctl", "is-active", unit)
|
||||
st := strings.TrimSpace(stdout)
|
||||
if err != nil || st == "" {
|
||||
st = "unknown"
|
||||
}
|
||||
writeJSON(w, http.StatusOK, SystemdState{State: st})
|
||||
}
|
||||
Reference in New Issue
Block a user