forked from filecoin-project/go-legs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
147 lines (130 loc) · 3.17 KB
/
convert.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package multiaddr
import (
"bytes"
"fmt"
"net"
"net/url"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
// register an 'httpath' component:
var transcodePath = multiaddr.NewTranscoderFromFunctions(pathStB, pathBtS, pathVal)
func pathVal(b []byte) error {
if bytes.IndexByte(b, '/') >= 0 {
return fmt.Errorf("encoded path '%s' contains a slash", string(b))
}
return nil
}
func pathStB(s string) ([]byte, error) {
return []byte(s), nil
}
func pathBtS(b []byte) (string, error) {
return string(b), nil
}
func init() {
_ = multiaddr.AddProtocol(protoHTTPath)
}
var protoHTTPath = multiaddr.Protocol{
Name: "httpath",
Code: 0x300200,
VCode: multiaddr.CodeToVarint(0x300200),
Size: multiaddr.LengthPrefixedVarSize,
Transcoder: transcodePath,
}
// ToURL takes a multiaddr of the form:
// /dns/thing.com/http/urlescape<path/to/root>
// /ip/192.168.0.1/tcp/80/http
func ToURL(ma multiaddr.Multiaddr) (*url.URL, error) {
// host should be either the dns name or the IP
_, host, err := manet.DialArgs(ma)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
if !ip.To4().Equal(ip) {
// raw v6 IPs need `[ip]` encapsulation.
host = fmt.Sprintf("[%s]", host)
}
}
protos := ma.Protocols()
pm := make(map[int]string, len(protos))
for _, p := range protos {
v, err := ma.ValueForProtocol(p.Code)
if err == nil {
pm[p.Code] = v
}
}
scheme := "http"
if _, ok := pm[multiaddr.P_HTTPS]; ok {
scheme = "https"
} else if _, ok = pm[multiaddr.P_HTTP]; ok {
// /tls/http == /https
if _, ok = pm[multiaddr.P_TLS]; ok {
scheme = "https"
}
} else if _, ok = pm[multiaddr.P_WSS]; ok {
scheme = "wss"
} else if _, ok = pm[multiaddr.P_WS]; ok {
scheme = "ws"
// /tls/ws == /wss
if _, ok = pm[multiaddr.P_TLS]; ok {
scheme = "wss"
}
}
path := ""
if pb, ok := pm[protoHTTPath.Code]; ok {
path, err = url.PathUnescape(pb)
if err != nil {
path = ""
}
}
out := url.URL{
Scheme: scheme,
Host: host,
Path: path,
}
return &out, nil
}
// ToMultiaddr takes a url and converts it into a multiaddr.
// converts scheme://host:port/path -> /ip/host/tcp/port/scheme/urlescape{path}
func ToMultiaddr(u *url.URL) (multiaddr.Multiaddr, error) {
h := u.Hostname()
var addr *multiaddr.Multiaddr
if n := net.ParseIP(h); n != nil {
ipAddr, err := manet.FromIP(n)
if err != nil {
return nil, err
}
addr = &ipAddr
} else {
// domain name
ma, err := multiaddr.NewComponent(multiaddr.ProtocolWithCode(multiaddr.P_DNS).Name, h)
if err != nil {
return nil, err
}
mab := multiaddr.Cast(ma.Bytes())
addr = &mab
}
pv := u.Port()
if pv != "" {
port, err := multiaddr.NewComponent(multiaddr.ProtocolWithCode(multiaddr.P_TCP).Name, pv)
if err != nil {
return nil, err
}
wport := multiaddr.Join(*addr, port)
addr = &wport
}
http, err := multiaddr.NewComponent(u.Scheme, "")
if err != nil {
return nil, err
}
joint := multiaddr.Join(*addr, http)
if u.Path != "" {
httpath, err := multiaddr.NewComponent(protoHTTPath.Name, url.PathEscape(u.Path))
if err != nil {
return nil, err
}
joint = multiaddr.Join(joint, httpath)
}
return joint, nil
}