package runtime import ( "bytes" "context" "errors" "fmt" "net/http" "github.com/mhsanaei/3x-ui/v3/internal/util/json_util" ) type inboundCatalogFlight struct { done chan struct{} cancel context.CancelFunc waiters int options []RemoteInboundOption err error } // loadInboundCatalog shares a refresh across callers. A lookup rechecks its cache // under the same lock used to register the flight. func (r *Remote) loadInboundCatalog(ctx context.Context, tag string) ([]RemoteInboundOption, error) { if err := ctx.Err(); err != nil { return nil, err } r.mu.Lock() if tag != "" { if _, ok := lookupRemoteTag(r.remoteIDByTag, tag, nodeInboundTagPrefix(r.node.Id)); ok { r.mu.Unlock() return nil, nil } } flight := r.catalogFlight if flight == nil { // The RPC belongs to the group. Each caller waits on its own context. requestCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), remoteHTTPTimeout) flight = &inboundCatalogFlight{done: make(chan struct{}), cancel: cancel} r.catalogFlight = flight go r.refreshInboundCatalog(requestCtx, flight, r.catalogVersion) } flight.waiters++ r.mu.Unlock() select { case <-flight.done: if err := ctx.Err(); err != nil { return nil, err } return flight.options, flight.err case <-ctx.Done(): r.mu.Lock() flight.waiters-- if r.catalogFlight == flight && flight.waiters == 0 { r.catalogFlight = nil flight.cancel() } r.mu.Unlock() return nil, ctx.Err() } } func (r *Remote) refreshInboundCatalog(ctx context.Context, flight *inboundCatalogFlight, version uint64) { defer flight.cancel() defer close(flight.done) for attempt := 0; ; attempt++ { options, ids, err := r.fetchInboundCatalog(ctx) r.mu.Lock() if r.catalogFlight != flight { r.mu.Unlock() return } if err == nil && version != r.catalogVersion { // An inbound mutation completed during the read. Retry once within // the same budget, so a stale response cannot revive an old ID. if attempt == 0 { version = r.catalogVersion r.mu.Unlock() continue } err = errors.New("remote inbound catalog changed during refresh") } if err == nil { r.installInboundCatalog(ids) flight.options = options } flight.err = err r.catalogFlight = nil r.mu.Unlock() return } } func (r *Remote) fetchInboundCatalog(ctx context.Context) (list []RemoteInboundOption, ids map[string]int, err error) { // Fetching runs on the shared worker, outside each caller's panic boundary. defer func() { if cause := recover(); cause != nil { list, ids, err = nil, nil, fmt.Errorf("fetch inbound catalog: panic: %v", cause) } }() env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/catalog", nil) var status *remoteHTTPError legacy := errors.As(err, &status) && (status.code == http.StatusNotFound || status.code == http.StatusMethodNotAllowed) if legacy { env, err = r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil) } if err != nil { return nil, nil, err } if !legacy && !bytes.HasPrefix(bytes.TrimSpace(env.Obj), []byte("[")) { return nil, nil, errors.New("decode inbound list: catalog must be an array") } list, err = json_util.DecodeCappedList[RemoteInboundOption](env.Obj, maxNodeInbounds) if err != nil { return nil, nil, fmt.Errorf("decode inbound list: %w", err) } ids = make(map[string]int, len(list)) for _, ib := range list { if ib.Id <= 0 || ib.Tag == "" { return nil, nil, errors.New("decode inbound list: entry missing a valid ID or tag") } if _, exists := ids[ib.Tag]; exists { return nil, nil, errors.New("decode inbound list: duplicate tag") } ids[ib.Tag] = ib.Id } return list, ids, nil } // installInboundCatalog runs with mu held. Explicit node tags outrank inferred aliases. func (r *Remote) installInboundCatalog(ids map[string]int) { prefix := nodeInboundTagPrefix(r.node.Id) aliases := make(map[string]int, len(r.adoptedAliases)) for central, nodeTag := range r.adoptedAliases { if _, exists := lookupRemoteTag(ids, central, prefix); exists { continue } if id, ok := lookupRemoteTag(ids, nodeTag, prefix); ok { aliases[central] = id } } for tag, id := range aliases { ids[tag] = id } for tag := range r.pushedFP { oldID, known := lookupRemoteTag(r.remoteIDByTag, tag, prefix) nextID, present := lookupRemoteTag(ids, tag, prefix) if !present || known && nextID != oldID { delete(r.pushedFP, tag) } } r.remoteIDByTag = ids }