|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/ipfs/boxo/ipns" |
| 12 | + "github.com/ipfs/boxo/routing/http/client" |
| 13 | + "github.com/ipfs/boxo/routing/http/types" |
| 14 | + "github.com/ipfs/boxo/routing/http/types/iter" |
| 15 | + "github.com/ipfs/go-cid" |
| 16 | + "github.com/libp2p/go-libp2p/core/peer" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + gatewayUrlPtr := flag.String("e", "", "routing v1 endpoint to use") |
| 21 | + timeoutPtr := flag.Int("t", 10, "timeout in seconds for lookup") |
| 22 | + cidPtr := flag.String("c", "", "cid to find") |
| 23 | + pidPtr := flag.String("p", "", "peer to find") |
| 24 | + namePtr := flag.String("n", "", "ipns name to retrieve record for") |
| 25 | + flag.Parse() |
| 26 | + |
| 27 | + // Creates a new Delegated Routing V1 client. |
| 28 | + client, err := client.New(*gatewayUrlPtr) |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + |
| 33 | + timeout := time.Duration((*timeoutPtr)) * time.Second |
| 34 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + if *cidPtr != "" { |
| 38 | + err = findProviders(ctx, client, *cidPtr) |
| 39 | + } else if *pidPtr != "" { |
| 40 | + err = findPeers(ctx, client, *pidPtr) |
| 41 | + } else if *namePtr != "" { |
| 42 | + err = findIPNS(ctx, client, *namePtr) |
| 43 | + } else { |
| 44 | + err = errors.New("cid or peer must be provided") |
| 45 | + } |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + log.Fatal(err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func findProviders(ctx context.Context, client *client.Client, cidStr string) error { |
| 53 | + // Parses the given CID to lookup the providers for. |
| 54 | + contentCid, err := cid.Parse(cidStr) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + // Ask for providers providing the given content CID. |
| 60 | + recordsIter, err := client.FindProviders(ctx, contentCid) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + defer recordsIter.Close() |
| 65 | + return printIter(recordsIter) |
| 66 | +} |
| 67 | + |
| 68 | +func findPeers(ctx context.Context, client *client.Client, pidStr string) error { |
| 69 | + // Parses the given Peer ID to lookup the information for. |
| 70 | + pid, err := peer.Decode(pidStr) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + // Ask for information about the peer with the given peer ID. |
| 76 | + recordsIter, err := client.FindPeers(ctx, pid) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + defer recordsIter.Close() |
| 81 | + return printIter(recordsIter) |
| 82 | +} |
| 83 | + |
| 84 | +func printIter(iter iter.ResultIter[types.Record]) error { |
| 85 | + // The response is streamed. Alternatively, you could use [iter.ReadAll] |
| 86 | + // to fetch all the results all at once, instead of iterating as they are |
| 87 | + // streamed. |
| 88 | + for iter.Next() { |
| 89 | + res := iter.Val() |
| 90 | + |
| 91 | + // Check for error, but do not complain if we exceeded the timeout. We are |
| 92 | + // expecting that to happen: we explicitly defined a timeout. |
| 93 | + if res.Err != nil { |
| 94 | + if !errors.Is(res.Err, context.DeadlineExceeded) { |
| 95 | + return res.Err |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + switch res.Val.GetSchema() { |
| 102 | + case types.SchemaPeer: |
| 103 | + record := res.Val.(*types.PeerRecord) |
| 104 | + fmt.Println(record.ID) |
| 105 | + fmt.Println("\tProtocols:", record.Protocols) |
| 106 | + fmt.Println("\tAddresses:", record.Addrs) |
| 107 | + default: |
| 108 | + // You may not want to fail here, it's up to you. You can just handle |
| 109 | + // the schemas you want, or that you know, but not fail. |
| 110 | + log.Printf("unrecognized schema: %s", res.Val.GetSchema()) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func findIPNS(ctx context.Context, client *client.Client, nameStr string) error { |
| 118 | + // Parses the given name string to get a record for. |
| 119 | + name, err := ipns.NameFromString(nameStr) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + // Ask for a record. [client.GetIPNS] validates the record for us. |
| 125 | + record, err := client.GetIPNS(ctx, name) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + v, err := record.Value() |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Printf("/ipns/%s\n", name) |
| 136 | + fmt.Println("\tValue:", v.String()) |
| 137 | + return nil |
| 138 | +} |
0 commit comments