|
| 1 | +package xray |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/netip" |
| 10 | + "sort" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/pasarguard/node/common" |
| 14 | +) |
| 15 | + |
| 16 | +type observatoryEntry struct { |
| 17 | + Alive bool `json:"alive"` |
| 18 | + Delay int64 `json:"delay"` |
| 19 | + OutboundTag string `json:"outbound_tag"` |
| 20 | + LastSeenTime int64 `json:"last_seen_time"` |
| 21 | + LastTryTime int64 `json:"last_try_time"` |
| 22 | +} |
| 23 | + |
| 24 | +type debugVarsResponse struct { |
| 25 | + Observatory map[string]observatoryEntry `json:"observatory"` |
| 26 | +} |
| 27 | + |
| 28 | +func (x *Xray) GetOutboundsLatency(ctx context.Context, request *common.LatencyRequest) (*common.LatencyResponse, error) { |
| 29 | + x.mu.RLock() |
| 30 | + started := x.core != nil && x.core.Started() |
| 31 | + metricPort := x.metricPort |
| 32 | + timeoutSeconds := x.cfg.LatencyTimeoutSeconds |
| 33 | + x.mu.RUnlock() |
| 34 | + |
| 35 | + if !started { |
| 36 | + return nil, errors.New("xray not started") |
| 37 | + } |
| 38 | + |
| 39 | + client := &http.Client{Timeout: time.Duration(timeoutSeconds) * time.Second} |
| 40 | + addr := netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), uint16(metricPort)) |
| 41 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+addr.String()+"/debug/vars", nil) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + resp, err := client.Do(req) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("read xray observatory metrics: %w", err) |
| 49 | + } |
| 50 | + defer resp.Body.Close() |
| 51 | + |
| 52 | + if resp.StatusCode != http.StatusOK { |
| 53 | + return nil, fmt.Errorf("read xray observatory metrics: unexpected status %d", resp.StatusCode) |
| 54 | + } |
| 55 | + |
| 56 | + var vars debugVarsResponse |
| 57 | + if err := json.NewDecoder(resp.Body).Decode(&vars); err != nil { |
| 58 | + return nil, fmt.Errorf("decode xray observatory metrics: %w", err) |
| 59 | + } |
| 60 | + if len(vars.Observatory) == 0 { |
| 61 | + return nil, errors.New("xray outbound latency is not available") |
| 62 | + } |
| 63 | + |
| 64 | + name := request.GetName() |
| 65 | + keys := make([]string, 0, len(vars.Observatory)) |
| 66 | + for key := range vars.Observatory { |
| 67 | + if name != "" && key != name { |
| 68 | + continue |
| 69 | + } |
| 70 | + keys = append(keys, key) |
| 71 | + } |
| 72 | + sort.Strings(keys) |
| 73 | + |
| 74 | + latencies := make([]*common.Latency, 0, len(keys)) |
| 75 | + for _, key := range keys { |
| 76 | + entry := vars.Observatory[key] |
| 77 | + linkName := entry.OutboundTag |
| 78 | + if linkName == "" { |
| 79 | + linkName = key |
| 80 | + } |
| 81 | + latencies = append(latencies, &common.Latency{ |
| 82 | + Name: key, |
| 83 | + Alive: entry.Alive, |
| 84 | + Delay: entry.Delay, |
| 85 | + Link: linkName, |
| 86 | + LastSeenTime: entry.LastSeenTime, |
| 87 | + LastTryTime: entry.LastTryTime, |
| 88 | + Source: "xray-observatory", |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + return &common.LatencyResponse{Latencies: latencies}, nil |
| 93 | +} |
0 commit comments