Skip to content

Commit 409b61f

Browse files
holisticodefjl
authored andcommitted
swarm/api: better name resolver handling (#3754)
Fixes #3608
1 parent d5d910e commit 409b61f

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

cmd/swarm/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
358358
if err != nil {
359359
utils.Fatalf("Can't connect: %v", err)
360360
}
361+
} else {
362+
swapEnabled = false
361363
}
362364
return swarm.NewSwarm(ctx, client, bzzconfig, swapEnabled, syncEnabled, cors)
363365
}

swarm/api/api.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package api
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"io"
2322
"net/http"
@@ -84,25 +83,28 @@ type ErrResolve error
8483
// DNS Resolver
8584
func (self *Api) Resolve(uri *URI) (storage.Key, error) {
8685
log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr))
86+
87+
var err error
88+
if !uri.Immutable() {
89+
if self.dns != nil {
90+
resolved, err := self.dns.Resolve(uri.Addr)
91+
if err == nil {
92+
return resolved[:], nil
93+
}
94+
} else {
95+
err = fmt.Errorf("no DNS to resolve name")
96+
}
97+
}
8798
if hashMatcher.MatchString(uri.Addr) {
88-
log.Trace(fmt.Sprintf("addr is a hash: %q", uri.Addr))
8999
return storage.Key(common.Hex2Bytes(uri.Addr)), nil
90100
}
91-
if uri.Immutable() {
92-
return nil, errors.New("refusing to resolve immutable address")
93-
}
94-
if self.dns == nil {
95-
return nil, fmt.Errorf("unable to resolve addr %q, resolver not configured", uri.Addr)
96-
}
97-
hash, err := self.dns.Resolve(uri.Addr)
98101
if err != nil {
99-
log.Warn(fmt.Sprintf("DNS error resolving addr %q: %s", uri.Addr, err))
100-
return nil, ErrResolve(err)
102+
return nil, fmt.Errorf("'%s' does not resolve: %v but is not a content hash", uri.Addr, err)
101103
}
102-
log.Trace(fmt.Sprintf("addr lookup: %v -> %v", uri.Addr, hash))
103-
return hash[:], nil
104+
return nil, fmt.Errorf("'%s' is not a content hash", uri.Addr)
104105
}
105106

107+
106108
// Put provides singleton manifest creation on top of dpa store
107109
func (self *Api) Put(content, contentType string) (storage.Key, error) {
108110
r := strings.NewReader(content)

swarm/api/http/server_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,32 @@ func TestBzzrGetPath(t *testing.T) {
9999
}
100100
}
101101

102+
nonhashtests := []string{
103+
srv.URL + "/bzz:/name",
104+
srv.URL + "/bzzi:/nonhash",
105+
srv.URL + "/bzzr:/nonhash",
106+
}
107+
108+
nonhashresponses := []string{
109+
"error resolving name: 'name' does not resolve: no DNS to resolve name but is not a content hash\n",
110+
"error resolving nonhash: 'nonhash' is not a content hash\n",
111+
"error resolving nonhash: 'nonhash' does not resolve: no DNS to resolve name but is not a content hash\n",
112+
}
113+
114+
for i, url := range nonhashtests {
115+
var resp *http.Response
116+
var respbody []byte
117+
118+
resp, err = http.Get(url)
119+
120+
if err != nil {
121+
t.Fatalf("Request failed: %v", err)
122+
}
123+
defer resp.Body.Close()
124+
respbody, err = ioutil.ReadAll(resp.Body)
125+
if string(respbody) != nonhashresponses[i] {
126+
t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
127+
}
128+
}
129+
102130
}

swarm/swarm.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ethereum/go-ethereum/contracts/chequebook"
2828
"github.com/ethereum/go-ethereum/contracts/ens"
2929
"github.com/ethereum/go-ethereum/crypto"
30+
"github.com/ethereum/go-ethereum/ethclient"
3031
"github.com/ethereum/go-ethereum/log"
3132
"github.com/ethereum/go-ethereum/node"
3233
"github.com/ethereum/go-ethereum/p2p"
@@ -134,9 +135,13 @@ func NewSwarm(ctx *node.ServiceContext, backend chequebook.Backend, config *api.
134135
// set up high level api
135136
transactOpts := bind.NewKeyedTransactor(self.privateKey)
136137

137-
self.dns, err = ens.NewENS(transactOpts, config.EnsRoot, self.backend)
138-
if err != nil {
139-
return nil, err
138+
if backend == (*ethclient.Client)(nil) {
139+
log.Warn("No ENS, please specify non-empty --ethapi to use domain name resolution")
140+
} else {
141+
self.dns, err = ens.NewENS(transactOpts, config.EnsRoot, self.backend)
142+
if err != nil {
143+
return nil, err
144+
}
140145
}
141146
log.Debug(fmt.Sprintf("-> Swarm Domain Name Registrar @ address %v", config.EnsRoot.Hex()))
142147

0 commit comments

Comments
 (0)