|
| 1 | +package proxycontrol |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/clovapi/switcher/internal/profile" |
| 11 | +) |
| 12 | + |
| 13 | +// PauseIfRunning stops the local clovapi proxy when it is healthy. |
| 14 | +// Returns true when a running proxy was stopped (caller may restart later). |
| 15 | +func PauseIfRunning() bool { |
| 16 | + s, err := profile.Load() |
| 17 | + if err != nil || !s.Proxy.Enabled { |
| 18 | + return false |
| 19 | + } |
| 20 | + cfg := s.Proxy |
| 21 | + ok, err := probeHealth(cfg) |
| 22 | + if err != nil || !ok { |
| 23 | + return false |
| 24 | + } |
| 25 | + _ = shutdownViaHTTP(cfg) |
| 26 | + _ = waitDown(cfg, 5*time.Second) |
| 27 | + return true |
| 28 | +} |
| 29 | + |
| 30 | +func probeHealth(cfg profile.ProxyConfig) (bool, error) { |
| 31 | + client := http.Client{Timeout: 2 * time.Second} |
| 32 | + resp, err := client.Get(healthURL(cfg)) |
| 33 | + if err != nil { |
| 34 | + return false, nil |
| 35 | + } |
| 36 | + defer resp.Body.Close() |
| 37 | + if resp.StatusCode != http.StatusOK { |
| 38 | + return false, nil |
| 39 | + } |
| 40 | + var body map[string]any |
| 41 | + if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
| 42 | + return false, nil |
| 43 | + } |
| 44 | + ok, _ := body["ok"].(bool) |
| 45 | + service, _ := body["service"].(string) |
| 46 | + return ok && strings.Contains(service, "clovapi-core-proxy"), nil |
| 47 | +} |
| 48 | + |
| 49 | +func shutdownViaHTTP(cfg profile.ProxyConfig) bool { |
| 50 | + req, err := http.NewRequest(http.MethodPost, baseURL(cfg)+"/__debug/shutdown", nil) |
| 51 | + if err != nil { |
| 52 | + return false |
| 53 | + } |
| 54 | + client := http.Client{Timeout: 2 * time.Second} |
| 55 | + resp, err := client.Do(req) |
| 56 | + if err != nil { |
| 57 | + return false |
| 58 | + } |
| 59 | + defer resp.Body.Close() |
| 60 | + return resp.StatusCode == http.StatusOK |
| 61 | +} |
| 62 | + |
| 63 | +func waitDown(cfg profile.ProxyConfig, deadline time.Duration) error { |
| 64 | + deadlineAt := time.Now().Add(deadline) |
| 65 | + for time.Now().Before(deadlineAt) { |
| 66 | + ok, err := probeHealth(cfg) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if !ok { |
| 71 | + return nil |
| 72 | + } |
| 73 | + time.Sleep(100 * time.Millisecond) |
| 74 | + } |
| 75 | + return fmt.Errorf("proxy still healthy at %s", healthURL(cfg)) |
| 76 | +} |
| 77 | + |
| 78 | +func healthClientHost(bindHost string) string { |
| 79 | + host := strings.TrimSpace(bindHost) |
| 80 | + if host == "" { |
| 81 | + return "127.0.0.1" |
| 82 | + } |
| 83 | + switch strings.ToLower(host) { |
| 84 | + case "0.0.0.0", "::", "::ffff:0.0.0.0": |
| 85 | + return "127.0.0.1" |
| 86 | + default: |
| 87 | + return host |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func healthURL(cfg profile.ProxyConfig) string { |
| 92 | + host := healthClientHost(cfg.Host) |
| 93 | + return fmt.Sprintf("http://%s:%d/health", host, cfg.Port) |
| 94 | +} |
| 95 | + |
| 96 | +func baseURL(cfg profile.ProxyConfig) string { |
| 97 | + host := healthClientHost(cfg.Host) |
| 98 | + return fmt.Sprintf("http://%s:%d", host, cfg.Port) |
| 99 | +} |
0 commit comments