Files
elmprodvpn/selective-vpn-api/app/routes_handlers_policy_fix.go

55 lines
1.2 KiB
Go

package app
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
func handleFixPolicyRoute(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
data, err := os.ReadFile(statusFilePath)
if err != nil {
http.Error(w, "status.json missing", http.StatusBadRequest)
return
}
var st Status
if err := json.Unmarshal(data, &st); err != nil {
http.Error(w, "invalid status.json", http.StatusBadRequest)
return
}
iface := strings.TrimSpace(st.Iface)
table := strings.TrimSpace(st.Table)
if iface == "" || iface == "-" || table == "" || table == "-" {
http.Error(w, "iface/table unknown in status.json", http.StatusBadRequest)
return
}
stdout, stderr, exitCode, err := runCommand(
"ip", "-4", "route", "replace",
"default", "dev", iface, "table", table, "mtu", policyRouteMTU,
)
ok := err == nil && exitCode == 0
res := cmdResult{
OK: ok,
ExitCode: exitCode,
Stdout: stdout,
Stderr: stderr,
}
if ok {
res.Message = fmt.Sprintf("policy route fixed: default dev %s table %s", iface, table)
} else if err != nil {
res.Message = err.Error()
}
writeJSON(w, http.StatusOK, res)
}