package app import ( "net/http" "os" "syscall" ) func handleTrafficInterfaces(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } st := loadTrafficModeState() active, reason := resolveTrafficIface(st.PreferredIface) resp := TrafficInterfacesResponse{ Interfaces: listSelectableIfaces(st.PreferredIface), PreferredIface: normalizePreferredIface(st.PreferredIface), ActiveIface: active, IfaceReason: reason, } writeJSON(w, http.StatusOK, resp) } func handleTrafficModeTest(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet && r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } st := loadTrafficModeState() writeJSON(w, http.StatusOK, evaluateTrafficMode(st)) } func acquireTrafficApplyLock() (*os.File, *TrafficModeStatusResponse) { lock, err := os.OpenFile(lockFile, os.O_CREATE|os.O_RDWR, 0o644) if err != nil { msg := evaluateTrafficMode(loadTrafficModeState()) msg.Message = "traffic lock open failed: " + err.Error() return nil, &msg } if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { _ = lock.Close() msg := evaluateTrafficMode(loadTrafficModeState()) msg.Message = "traffic apply skipped: routes operation already running" return nil, &msg } return lock, nil } func handleTrafficMode(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: st := loadTrafficModeState() writeJSON(w, http.StatusOK, evaluateTrafficMode(st)) case http.MethodPost: handleTrafficModePost(w, r) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } }