Add filter for self when routing to node

This commit is contained in:
Philip Laine 2023-01-31 18:22:02 +01:00
parent 77f349dd9b
commit 60b02b80cf
2 changed files with 27 additions and 12 deletions

View File

@ -175,7 +175,9 @@ func (r *RegistryHandler) handleMirror(c *gin.Context, remoteRegistry, registryP
if key == "" { if key == "" {
key = ref.String() key = ref.String()
} }
ip, ok, err := r.router.Resolve(c, key) timeoutCtx, cancel := context.WithTimeout(c, 5*time.Second)
defer cancel()
ip, ok, err := r.router.Resolve(timeoutCtx, key)
if err != nil { if err != nil {
c.AbortWithError(http.StatusNotFound, err) c.AbortWithError(http.StatusNotFound, err)
return return

View File

@ -117,24 +117,37 @@ func (r *P2PRouter) Resolve(ctx context.Context, key string) (string, bool, erro
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
ch := r.rd.FindProvidersAsync(ctx, c, 1) cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
ch := r.rd.FindProvidersAsync(cancelCtx, c, 0)
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return "", false, ctx.Err() return "", false, ctx.Err()
case info := <-ch: case info, ok := <-ch:
if len(info.Addrs) == 0 { // Channel is closed means no provider is found.
return "", false, fmt.Errorf("invalid node found with empty address list") if !ok {
return "", false, fmt.Errorf("key not found %s", key)
} }
addr := info.Addrs[0] // Ignore responses that come from self.
v, err := addr.ValueForProtocol(multiaddr.P_IP4) if info.ID == r.host.ID() {
if err != nil { continue
return "", false, err
} }
if v == "" { for _, addr := range info.Addrs {
return "", false, fmt.Errorf("unexpected empty ip address") v, err := addr.ValueForProtocol(multiaddr.P_IP4)
if err != nil {
return "", false, err
}
// Protect against empty values beeing returned.
if v == "" {
continue
}
// There are two IPs being advertised, one is localhost which is not useful.
if v == "127.0.0.1" {
continue
}
return v, true, nil
} }
return v, true, nil
} }
} }
} }