-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
131 lines (108 loc) · 2.57 KB
/
client.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package hkp
import (
"bytes"
"fmt"
"net"
"net/http"
"net/url"
"path"
"github.com/ProtonMail/go-crypto/openpgp"
)
type Client struct {
Host string
Insecure bool
}
func (c *Client) hostURL() (*url.URL, error) {
if u, err := url.Parse(c.Host); err == nil {
if !c.Insecure && u.Scheme != "https" {
return nil, fmt.Errorf("hkp: refusing to connect to non-HTTPS keyserver")
}
return u, nil
}
host := c.Host
_, addrs, err := net.LookupSRV("hkp", "tcp", host)
if dnsErr, ok := err.(*net.DNSError); ok {
if dnsErr.IsTemporary {
return nil, err
}
} else if err != nil {
return nil, err
}
if len(addrs) > 0 {
addr := addrs[0]
host = fmt.Sprintf("%v:%v", addr.Target, addr.Port)
}
scheme := "https"
if c.Insecure {
scheme = "http"
}
return &url.URL{Scheme: scheme, Host: host}, nil
}
func (c *Client) url(p string) (*url.URL, error) {
u, err := c.hostURL()
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, p)
return u, nil
}
func (c *Client) lookup(op string, req *LookupRequest) (*http.Response, error) {
u, err := c.url(lookupPath)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("op", op)
q.Set("search", req.Search)
q.Set("options", req.Options.format())
if req.Exact {
q.Set("exact", "on")
}
q.Set("fingerprint", "on") // implicit
u.RawQuery = q.Encode()
return http.Get(u.String())
}
func (c *Client) Index(req *LookupRequest) ([]IndexKey, error) {
resp, err := c.lookup("index", req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("hkp: failed to get index: %v %v", resp.StatusCode, resp.Status)
}
return readIndex(resp.Body)
}
func (c *Client) Get(req *LookupRequest) (openpgp.EntityList, error) {
resp, err := c.lookup("get", req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("hkp: failed to get key: %v %v", resp.StatusCode, resp.Status)
}
return openpgp.ReadArmoredKeyRing(resp.Body)
}
func (c *Client) Add(el openpgp.EntityList) error {
u, err := c.url(addPath)
if err != nil {
return err
}
var b bytes.Buffer
if err := serializeArmoredKeyRing(&b, el); err != nil {
return err
}
v := url.Values{}
v.Set("keytext", b.String())
resp, err := http.PostForm(u.String(), v)
if err != nil {
return err
} else if resp.StatusCode/100 != 2 {
return fmt.Errorf("hkp: failed to add key: %v %v", resp.StatusCode, resp.Status)
}
defer resp.Body.Close()
return nil
}