@cryptotaxi247 / kubo / commits / d01ee1111

better parsing logic for dht keys containing prefixes

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Sep 22, 2015 at 17:31 UTC d01ee1111f55f2296b8354ac661f9d76d51e3faf
1 file changed +32 -3
core/commands/dht.go
+32 -3
@@ -5,6 +5,7 @@ import (
5 "errors"
6 "fmt"
7 "io"
8 + "strings"
9 "time"
10
11 key "github.com/ipfs/go-ipfs/blocks/key"
@@ -396,6 +397,12 @@ GetValue will return the value stored in the dht at the given key.
397 events := make(chan *notif.QueryEvent)
398 ctx := notif.RegisterForQueryEvents(req.Context(), events)
399
400 + dhtkey, err := escapeDhtKey(req.Arguments()[0])
401 + if err != nil {
402 + res.SetError(err, cmds.ErrNormal)
403 + return
404 + }
405 +
406 go func() {
407 defer close(outChan)
408 for e := range events {
@@ -405,7 +412,7 @@ GetValue will return the value stored in the dht at the given key.
412
413 go func() {
414 defer close(events)
408 - val, err := dht.GetValue(ctx, key.B58KeyDecode(req.Arguments()[0]))
415 + val, err := dht.GetValue(ctx, dhtkey)
416 if err != nil {
417 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
418 Type: notif.QueryError,
@@ -452,7 +459,11 @@ GetValue will return the value stored in the dht at the given key.
459 fmt.Fprintf(buf, "* querying %s\n", obj.ID)
460 }
461 case notif.Value:
455 - fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra)
462 + if verbose {
463 + fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra)
464 + } else {
465 + buf.WriteString(obj.Extra)
466 + }
467 case notif.QueryError:
468 fmt.Fprintf(buf, "error: %s\n", obj.Extra)
469 default:
@@ -505,7 +516,12 @@ PutValue will store the given key value pair in the dht.
516 events := make(chan *notif.QueryEvent)
517 ctx := notif.RegisterForQueryEvents(req.Context(), events)
518
508 - key := key.B58KeyDecode(req.Arguments()[0])
519 + key, err := escapeDhtKey(req.Arguments()[0])
520 + if err != nil {
521 + res.SetError(err, cmds.ErrNormal)
522 + return
523 + }
524 +
525 data := req.Arguments()[1]
526
527 go func() {
@@ -581,3 +597,16 @@ PutValue will store the given key value pair in the dht.
597 },
598 Type: notif.QueryEvent{},
599 }
600 +
601 +func escapeDhtKey(s string) (key.Key, error) {
602 + parts := strings.Split(s, "/")
603 + switch len(parts) {
604 + case 1:
605 + return key.B58KeyDecode(s), nil
606 + case 3:
607 + k := key.B58KeyDecode(parts[2])
608 + return key.Key(strings.Join(append(parts[:2], string(k)), "/")), nil
609 + default:
610 + return "", errors.New("invalid key")
611 + }
612 +}