74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func applySingBoxRuntime(client TransportClient, restart bool) error {
|
|
backend := selectTransportBackend(client)
|
|
now := time.Now().UTC()
|
|
|
|
prov := backend.Provision(client)
|
|
if !prov.OK {
|
|
transportMu.Lock()
|
|
st := loadTransportClientsState()
|
|
if idx := findTransportClientIndex(st.Items, client.ID); idx >= 0 {
|
|
it := st.Items[idx]
|
|
applyTransportProvisionResult(&it, now, backend.ID(), prov)
|
|
st.Items[idx] = it
|
|
_ = saveTransportClientsState(st)
|
|
}
|
|
transportMu.Unlock()
|
|
msg := strings.TrimSpace(prov.Message)
|
|
if msg == "" {
|
|
msg = "transport provision failed"
|
|
}
|
|
return errors.New(msg)
|
|
}
|
|
|
|
action := "restart"
|
|
if !restart {
|
|
action = "start"
|
|
}
|
|
act := backend.Action(client, action)
|
|
if !act.OK {
|
|
transportMu.Lock()
|
|
st := loadTransportClientsState()
|
|
if idx := findTransportClientIndex(st.Items, client.ID); idx >= 0 {
|
|
it := st.Items[idx]
|
|
applyTransportLifecycleFailure(&it, action, now, backend.ID(), act)
|
|
st.Items[idx] = it
|
|
_ = saveTransportClientsState(st)
|
|
}
|
|
transportMu.Unlock()
|
|
msg := strings.TrimSpace(act.Message)
|
|
if msg == "" {
|
|
msg = "transport action failed"
|
|
}
|
|
return errors.New(msg)
|
|
}
|
|
|
|
probe := backend.Health(client)
|
|
transportMu.Lock()
|
|
st := loadTransportClientsState()
|
|
if idx := findTransportClientIndex(st.Items, client.ID); idx >= 0 {
|
|
it := st.Items[idx]
|
|
applyTransportLifecycleAction(&it, action, now)
|
|
it = applyTransportHealthProbeSnapshot(it, backend.ID(), probe, now)
|
|
st.Items[idx] = it
|
|
_ = saveTransportClientsState(st)
|
|
}
|
|
transportMu.Unlock()
|
|
|
|
if !probe.OK && normalizeTransportStatus(probe.Status) == TransportClientDown {
|
|
msg := strings.TrimSpace(probe.Message)
|
|
if msg == "" {
|
|
msg = "transport health check failed"
|
|
}
|
|
return errors.New(msg)
|
|
}
|
|
return nil
|
|
}
|