Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use New Routing V1 #17

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
someguy
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-bullseye
FROM golang:1.21-bullseye

WORKDIR /app

Expand Down
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
# someguy

> ⚠️ Reframe has been deprecated in favour of the [Routing V1 Specification](https://specs.ipfs.tech/routing/http-routing-v1/).
A [Delegated Routing V1](https://specs.ipfs.tech/routing/http-routing-v1/) server and client for all your routing needs. Ask `someguy` for directions.

A Reframe server you can delegate routing requests to. Ask someguy for directions.
## Install

```bash
go install github.com/ipfs-shipyard/someguy@latest
```


## Build

```bash
go build -o someguy
```

## Usage

`someguy start` runs a [Reframe](https://github.com/ipfs/specs/blob/6bdb7b2751038e1d0212a40494ea8fd4018f384c/REFRAME.md) server
that proxies requests to the IPFS Public DHT and an Indexer node (planned to be upgraded to querying the indexer network more broadly).
You can use `someguy` as a client or server.

If you don't feel like running any routing code yourself, that's ok just ask `someguy` to do it for you.
`someguy start` runs a Delegated Routing V1 server that proxies requests to the [IPFS Amino DHT](https://blog.ipfs.tech/2023-09-amino-refactoring/) and the [cid.contact](https://cid.contact) indexer node.

If you're looking for an implementation of a Reframe client already packaged up and ready to use check out https://github.com/ipfs/go-delegated-routing.
If you don't want to run a server yourself, but want to query some other server, you can run `someguy ask` and choose any of the subcommands and ask for a provider, a peer, or even an IPNS record.

If you're missing tooling in your language you can take a look at the spec to write an HTTP client yourself,
or if you're up to it add codegeneration for your language into https://github.com/ipld/edelweiss/ to make it easier to maintain
your implementation if/when more methods are added to Reframe.
For more details run `someguy --help`.
229 changes: 108 additions & 121 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,177 +1,164 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"io"
"os"
"time"

pbuf "github.com/gogo/protobuf/proto"

"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/types"
"github.com/ipfs/boxo/routing/http/types/iter"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-delegated-routing/client"
"github.com/ipfs/go-ipns"
ipns_pb "github.com/ipfs/go-ipns/pb"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"

drp "github.com/ipfs/go-delegated-routing/gen/proto"
"github.com/libp2p/go-libp2p/core/peer"
)

func identify(ctx context.Context, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
func findProviders(ctx context.Context, key cid.Cid, endpoint string, prettyOutput bool) error {
drc, err := client.New(endpoint)
if err != nil {
return err
}

respCh, err := ic.Identify_Async(ctx, &drp.DelegatedRouting_IdentifyArg{})
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}

if !prettyOutput {
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
}
fmt.Println(buf.String())
} else {
var methods []string
for _, m := range r.Resp.Methods {
methods = append(methods, string(m))
}
fmt.Println(strings.Join(methods, ","))
}
recordsIter, err := drc.FindProviders(ctx, key)
if err != nil {
return err
}
return nil
defer recordsIter.Close()

return printIter(os.Stdout, prettyOutput, recordsIter)
}

func findprovs(ctx context.Context, c cid.Cid, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
func findPeers(ctx context.Context, pid peer.ID, endpoint string, prettyOutput bool) error {
drc, err := client.New(endpoint)
if err != nil {
return err
}

respCh, err := ic.FindProviders_Async(ctx, &drp.FindProvidersRequest{
Key: drp.LinkToAny(c),
})
recordsIter, err := drc.FindPeers(ctx, pid)
if err != nil {
return err
}
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
defer recordsIter.Close()

return printIter(os.Stdout, prettyOutput, recordsIter)
}

func printIter(w io.Writer, prettyOutput bool, iter iter.ResultIter[types.Record]) error {
for iter.Next() {
res := iter.Val()

// Check for error, but do not complain if we exceeded the timeout. We are
// expecting that to happen: we explicitly defined a timeout.
if res.Err != nil {
if !errors.Is(res.Err, context.DeadlineExceeded) {
return res.Err
}

return nil
}

if !prettyOutput {
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
if prettyOutput {
switch res.Val.GetSchema() {
case types.SchemaPeer:
record := res.Val.(*types.PeerRecord)
fmt.Fprintln(w, record.ID)
fmt.Fprintln(w, "\tProtocols:", record.Protocols)
fmt.Fprintln(w, "\tAddresses:", record.Addrs)

case types.SchemaBitswap:
record := res.Val.(*types.BitswapRecord)
fmt.Fprintln(w, record.ID)
fmt.Fprintln(w, "\tProtocol:", record.Protocol)
fmt.Fprintln(w, "\tAddresses:", record.Addrs)

default:
// This is an unknown schema. Let's just print it raw.
err := json.NewEncoder(w).Encode(res.Val)
if err != nil {
return err
}
}
fmt.Println(buf.String())

fmt.Fprintln(w)
} else {
for _, prov := range r.Resp.Providers {
if prov.ProviderNode.Peer != nil {
ai := &peer.AddrInfo{}
ai.ID = peer.ID(prov.ProviderNode.Peer.ID)
for _, bma := range prov.ProviderNode.Peer.Multiaddresses {
ma, err := multiaddr.NewMultiaddrBytes(bma)
if err != nil {
return err
}
ai.Addrs = append(ai.Addrs, ma)
}
fmt.Println(ai)
}
for _, proto := range prov.ProviderProto {
if proto.Bitswap != nil {
fmt.Println("\t Bitswap")
} else if proto.GraphSyncFILv1 != nil {
fmt.Println("\t GraphSyncFILv1")
var buf bytes.Buffer
if err := dagjson.Encode(proto.GraphSyncFILv1, &buf); err != nil {
return err
}
fmt.Println("\t\t" + buf.String())
} else {
var buf bytes.Buffer
if err := dagjson.Encode(proto, &buf); err != nil {
return err
}
fmt.Println("\t" + buf.String())
}
}
err := json.NewEncoder(w).Encode(res.Val)
if err != nil {
return err
}
}
}

return nil
}

func getIPNS(ctx context.Context, p peer.ID, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
func getIPNS(ctx context.Context, name ipns.Name, endpoint string, prettyOutput bool) error {
drc, err := client.New(endpoint)
if err != nil {
return err
}

rec, err := drc.GetIPNS(ctx, name)
if err != nil {
return err
}

if prettyOutput {
c := client.NewClient(ic)
respCh, err := c.GetIPNSAsync(ctx, []byte(p))
v, err := rec.Value()
if err != nil {
return err
}
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}
rec := new(ipns_pb.IpnsEntry)
if err := pbuf.Unmarshal(r.Record, rec); err != nil {
return err
}
seqno := rec.GetSequence()
ttl := time.Duration(rec.GetTtl())
eol, err := ipns.GetEOL(rec)
if err != nil {
return err
}
value := string(rec.GetValue())
fmt.Printf("Sequence: %d, TTL: %v, EOL: %v, Value: %s\n", seqno, ttl, eol, value)

seq, err := rec.Sequence()
if err != nil {
return err
}

eol, err := rec.Validity()
if err != nil {
return err
}

fmt.Printf("/ipns/%s\n", name)

// Since [client.Client.GetIPNS] verifies if the retrieved record is valid, we
// do not need to verify it again. However, if you were not using this specific
// client, but using some other tool, you should always validate the IPNS Record
// using the [ipns.Validate] or [ipns.ValidateWithName] functions.
fmt.Println("\tSignature Validated")
fmt.Println("\tValue:", v.String())
fmt.Println("\tSequence:", seq)
fmt.Println("\tValidityType : EOL/End-of-Life")
fmt.Println("\tValidity:", eol.Format(time.RFC3339))
hacdias marked this conversation as resolved.
Show resolved Hide resolved
if ttl, err := rec.TTL(); err == nil {
fmt.Println("\tTTL:", ttl.String())
}

return nil
}

respCh, err := ic.GetIPNS_Async(ctx, &drp.GetIPNSRequest{
ID: []byte(p),
})
raw, err := ipns.MarshalRecord(rec)
if err != nil {
return err
}
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
}
fmt.Println(buf.String())
}
return nil

_, err = os.Stdout.Write(raw)
return err
}

func putIPNS(ctx context.Context, key peer.ID, record []byte, endpoint string) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
func putIPNS(ctx context.Context, name ipns.Name, record []byte, endpoint string) error {
drc, err := client.New(endpoint)
if err != nil {
return err
}

rec, err := ipns.UnmarshalRecord(record)
if err != nil {
return err
}

c := client.NewClient(ic)
return c.PutIPNS(ctx, []byte(key), record)
return drc.PutIPNS(ctx, name, rec)
}
Loading