-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(gateway): IPNS record response format (IPIP-351) #9399
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package commands | |
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
@@ -135,6 +134,7 @@ const ( | |
) | ||
|
||
var provideRefRoutingCmd = &cmds.Command{ | ||
Status: cmds.Experimental, | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Announce to the network that you are providing given values.", | ||
}, | ||
|
@@ -346,6 +346,7 @@ var findPeerRoutingCmd = &cmds.Command{ | |
} | ||
|
||
var getValueRoutingCmd = &cmds.Command{ | ||
Status: cmds.Experimental, | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Given a key, query the routing system for its best value.", | ||
ShortDescription: ` | ||
|
@@ -362,78 +363,30 @@ Different key types can specify other 'best' rules. | |
Arguments: []cmds.Argument{ | ||
cmds.StringArg("key", true, true, "The key to find a value for."), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.BoolOption(dhtVerboseOptionName, "v", "Print extra information."), | ||
}, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
nd, err := cmdenv.GetNode(env) | ||
api, err := cmdenv.GetApi(env, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !nd.IsOnline { | ||
return ErrNotOnline | ||
} | ||
|
||
dhtkey, err := escapeDhtKey(req.Arguments[0]) | ||
r, err := api.Routing().Get(req.Context, req.Arguments[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx, cancel := context.WithCancel(req.Context) | ||
ctx, events := routing.RegisterForQueryEvents(ctx) | ||
|
||
var getErr error | ||
go func() { | ||
defer cancel() | ||
var val []byte | ||
val, getErr = nd.Routing.GetValue(ctx, dhtkey) | ||
if getErr != nil { | ||
routing.PublishQueryEvent(ctx, &routing.QueryEvent{ | ||
Type: routing.QueryError, | ||
Extra: getErr.Error(), | ||
}) | ||
} else { | ||
routing.PublishQueryEvent(ctx, &routing.QueryEvent{ | ||
Type: routing.Value, | ||
Extra: base64.StdEncoding.EncodeToString(val), | ||
}) | ||
} | ||
}() | ||
|
||
for e := range events { | ||
if err := res.Emit(e); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return getErr | ||
return res.Emit(r) | ||
}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *routing.QueryEvent) error { | ||
pfm := pfuncMap{ | ||
routing.Value: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error { | ||
if verbose { | ||
_, err := fmt.Fprintf(out, "got value: '%s'\n", obj.Extra) | ||
return err | ||
} | ||
res, err := base64.StdEncoding.DecodeString(obj.Extra) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.Write(res) | ||
return err | ||
}, | ||
} | ||
|
||
verbose, _ := req.Options[dhtVerboseOptionName].(bool) | ||
return printEvent(out, w, verbose, pfm) | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error { | ||
_, err := w.Write(out) | ||
return err | ||
}), | ||
}, | ||
Type: routing.QueryEvent{}, | ||
Type: []byte{}, | ||
} | ||
|
||
var putValueRoutingCmd = &cmds.Command{ | ||
Status: cmds.Experimental, | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Write a key/value pair to the routing system.", | ||
ShortDescription: ` | ||
|
@@ -459,20 +412,8 @@ identified by QmFoo. | |
cmds.StringArg("key", true, false, "The key to store the value at."), | ||
cmds.FileArg("value-file", true, false, "A path to a file containing the value to store.").EnableStdin(), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.BoolOption(dhtVerboseOptionName, "v", "Print extra information."), | ||
}, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
nd, err := cmdenv.GetNode(env) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !nd.IsOnline { | ||
return ErrNotOnline | ||
} | ||
|
||
key, err := escapeDhtKey(req.Arguments[0]) | ||
api, err := cmdenv.GetApi(env, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -488,50 +429,20 @@ identified by QmFoo. | |
return err | ||
} | ||
|
||
ctx, cancel := context.WithCancel(req.Context) | ||
ctx, events := routing.RegisterForQueryEvents(ctx) | ||
|
||
var putErr error | ||
go func() { | ||
defer cancel() | ||
putErr = nd.Routing.PutValue(ctx, key, []byte(data)) | ||
if putErr != nil { | ||
routing.PublishQueryEvent(ctx, &routing.QueryEvent{ | ||
Type: routing.QueryError, | ||
Extra: putErr.Error(), | ||
}) | ||
} | ||
}() | ||
|
||
for e := range events { | ||
if err := res.Emit(e); err != nil { | ||
return err | ||
} | ||
err = api.Routing().Put(req.Context, req.Arguments[0], data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return putErr | ||
return res.Emit([]byte(fmt.Sprintf("%s added", req.Arguments[0]))) | ||
}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *routing.QueryEvent) error { | ||
pfm := pfuncMap{ | ||
routing.FinalPeer: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error { | ||
if verbose { | ||
fmt.Fprintf(out, "* closest peer %s\n", obj.ID) | ||
} | ||
return nil | ||
}, | ||
routing.Value: func(obj *routing.QueryEvent, out io.Writer, verbose bool) error { | ||
fmt.Fprintf(out, "%s\n", obj.ID.Pretty()) | ||
return nil | ||
}, | ||
} | ||
|
||
verbose, _ := req.Options[dhtVerboseOptionName].(bool) | ||
|
||
return printEvent(out, w, verbose, pfm) | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out []byte) error { | ||
_, err := w.Write(out) | ||
return err | ||
}), | ||
}, | ||
Type: routing.QueryEvent{}, | ||
Type: []byte{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💭 This changes both -{
- "Extra": "<string>",
- "ID": "<peer-id>",
- "Responses": [
- {
- "Addrs": [
- "<multiaddr-string>"
- ],
- "ID": "peer-id"
- }
- ],
- "Type": "<int>"
-}
+"<base64-string>" and breaks I've removed flag and marked |
||
} | ||
|
||
type printFunc func(obj *routing.QueryEvent, out io.Writer, verbose bool) error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package coreapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ipfs/go-path" | ||
coreiface "github.com/ipfs/interface-go-ipfs-core" | ||
peer "github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
type RoutingAPI CoreAPI | ||
|
||
func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { | ||
if !r.nd.IsOnline { | ||
return nil, coreiface.ErrOffline | ||
} | ||
|
||
dhtKey, err := normalizeKey(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return r.routing.GetValue(ctx, dhtKey) | ||
} | ||
|
||
func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error { | ||
if !r.nd.IsOnline { | ||
return coreiface.ErrOffline | ||
} | ||
|
||
dhtKey, err := normalizeKey(key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return r.routing.PutValue(ctx, dhtKey, value) | ||
} | ||
|
||
func normalizeKey(s string) (string, error) { | ||
parts := path.SplitList(s) | ||
if len(parts) != 3 || | ||
parts[0] != "" || | ||
!(parts[1] == "ipns" || parts[1] == "pk") { | ||
return "", errors.New("invalid key") | ||
} | ||
|
||
k, err := peer.Decode(parts[2]) | ||
if err != nil { | ||
return "", err | ||
} | ||
return path.Join(append(parts[:2], string(k))), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
ipns_pb "github.com/ipfs/go-ipns/pb" | ||
path "github.com/ipfs/go-path" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (i *gatewayHandler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) { | ||
if contentPath.Namespace() != "ipns" { | ||
err := fmt.Errorf("%s is not an IPNS link", contentPath.String()) | ||
webError(w, err.Error(), err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
key := contentPath.String() | ||
key = strings.TrimSuffix(key, "/") | ||
if strings.Count(key, "/") > 2 { | ||
err := errors.New("cannot find ipns key for subpath") | ||
webError(w, err.Error(), err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
rawRecord, err := i.api.Routing().Get(ctx, key) | ||
if err != nil { | ||
webError(w, err.Error(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var record ipns_pb.IpnsEntry | ||
err = proto.Unmarshal(rawRecord, &record) | ||
if err != nil { | ||
webError(w, err.Error(), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Set cache control headers based on the TTL set in the IPNS record. If the | ||
// TTL is not present, we use the Last-Modified tag. We are tracking IPNS | ||
// caching on: https://github.com/ipfs/kubo/issues/1818. | ||
// TODO: use addCacheControlHeaders once #1818 is fixed. | ||
w.Header().Set("Etag", getEtag(r, resolvedPath.Cid())) | ||
if record.Ttl != nil { | ||
seconds := int(time.Duration(*record.Ttl).Seconds()) | ||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", seconds)) | ||
} else { | ||
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) | ||
} | ||
|
||
// Set Content-Disposition | ||
var name string | ||
if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" { | ||
name = urlFilename | ||
} else { | ||
name = path.SplitList(key)[2] + ".ipns-record" | ||
} | ||
setContentDispositionHeader(w, name, "attachment") | ||
|
||
w.Header().Set("Content-Type", "application/vnd.ipfs.ipns-record") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
|
||
_, _ = w.Write(rawRecord) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 This changes both
/api/v0/dht/get
and/api/v0/routing/get
(https://docs.ipfs.tech/reference/kubo/rpc/):and breaks
--verbose
flag in both.I've removed flag and marked
routing/get
as Experimental (dht/get
was already deprecated)