-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdns.go
102 lines (86 loc) · 2.29 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package routinghelpers
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"net"
"net/http"
"strings"
cid "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/routing"
record_pb "github.com/libp2p/go-libp2p-record/pb"
)
//FIXME: TODO: specs and implementation 👌
//var _ routing.Routing = (*DNSRouter)(nil)
var _ routing.ValueStore = (*DNSValueStore)(nil)
// DNS uses domains for record storage
type DNSValueStore struct {
}
// Get value from FQDN's TXT
//TODO: opts handling
func (dnsRouter *DNSValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
txtFragments, err := net.LookupTXT(key + ".dns.ipns.dev")
if err != nil {
return nil, err
}
dec, err := base64.StdEncoding.DecodeString(strings.Join(txtFragments, ""))
if err != nil {
return nil, err
}
record := record_pb.Record{}
// defer func() { fmt.Printf("Record: %s\n", record.String()) }() // DBG
if err = record.Unmarshal(dec); err != nil {
//FIXME: error is unhandled
// test data from nameserver does not contain TimeReceived
fmt.Println("err:", err)
}
//FIXME: for some reason key value seems to be reversed?
//return record.Value, nil
return record.Key, nil
}
func (dnsRouter *DNSValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
nameChan := make(chan []byte)
go func() {
for range ctx.Done() {
value, err := dnsRouter.GetValue(ctx, key, opts...)
if err != nil {
return
}
nameChan <- value
}
}()
return nameChan, nil
}
// Put value to FQDN/nameserver for storage
func (dnsRouter *DNSValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
builder := cid.V1Builder{}
refCid, err := builder.Sum([]byte(key))
if err != nil {
return err
}
jsonStr := fmt.Sprintf(`{
key: %s,
record: %s,
subdomain: true,
}
`, refCid.String(),
base64.StdEncoding.EncodeToString(value))
req, err := http.NewRequest("PUT", "https://ipns.dev", bytes.NewBufferString(jsonStr))
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
fmt.Printf("DBG: %#v\n", resp.Status)
/* TODO: what status is expected?
if resp.Status != http.StatusOK {
return someError
}
*/
return nil
}