mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-24 12:08:30 +00:00
Renamed tor to onion and added embedded port field
This commit is contained in:
parent
ed310561bd
commit
0e7ae0d2ee
34
codec.go
34
codec.go
@ -166,16 +166,36 @@ func addressStringToBytes(p Protocol, s string) ([]byte, error) {
|
|||||||
binary.BigEndian.PutUint16(b, uint16(i))
|
binary.BigEndian.PutUint16(b, uint16(i))
|
||||||
return b, nil
|
return b, nil
|
||||||
|
|
||||||
case P_TOR:
|
case P_ONION:
|
||||||
fields := strings.Split(s, ".onion")
|
addr := strings.Split(s, ":")
|
||||||
if len(fields) != 2 {
|
if len(addr) != 2 {
|
||||||
return nil, fmt.Errorf("failed to parse ipfs addr: %s not a Tor .onion address.", s)
|
return nil, fmt.Errorf("failed to parse %s addr: %s does not contain a port number.", p.Name, s)
|
||||||
}
|
}
|
||||||
b, err := base32.StdEncoding.DecodeString(strings.ToUpper(fields[0]))
|
|
||||||
|
// onion address without the ".onion" substring
|
||||||
|
if len(addr[0]) != 16 {
|
||||||
|
return nil, fmt.Errorf("failed to parse %s addr: %s not a Tor onion address.", p.Name, s)
|
||||||
|
}
|
||||||
|
onionHostBytes, err := base32.StdEncoding.DecodeString(strings.ToUpper(addr[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
|
return nil, fmt.Errorf("failed to decode base32 %s addr: %s %s", p.Name, s, err)
|
||||||
}
|
}
|
||||||
return b, nil
|
|
||||||
|
// onion port number
|
||||||
|
i, err := strconv.Atoi(addr[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, err)
|
||||||
|
}
|
||||||
|
if i >= 65536 {
|
||||||
|
return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, "greater than 65536")
|
||||||
|
}
|
||||||
|
onionPortBytes := make([]byte, 2)
|
||||||
|
binary.BigEndian.PutUint16(onionPortBytes, uint16(i))
|
||||||
|
bytes := []byte{}
|
||||||
|
bytes = append(bytes, onionHostBytes...)
|
||||||
|
bytes = append(bytes, onionPortBytes...)
|
||||||
|
return bytes, nil
|
||||||
|
|
||||||
case P_IPFS: // ipfs
|
case P_IPFS: // ipfs
|
||||||
// the address is a varint prefixed multihash string representation
|
// the address is a varint prefixed multihash string representation
|
||||||
m, err := mh.FromB58String(s)
|
m, err := mh.FromB58String(s)
|
||||||
|
@ -25,8 +25,9 @@ func TestConstructFails(t *testing.T) {
|
|||||||
"/sctp",
|
"/sctp",
|
||||||
"/udp/65536",
|
"/udp/65536",
|
||||||
"/tcp/65536",
|
"/tcp/65536",
|
||||||
"/tor/9imaq4ygg2iegci7.onion",
|
"/onion/9imaq4ygg2iegci7:80",
|
||||||
"/tor/aaimaq4ygg2iegci7.onion",
|
"/onion/aaimaq4ygg2iegci7:80",
|
||||||
|
"/onion/timaq4ygg2iegci7",
|
||||||
"/udp/1234/sctp",
|
"/udp/1234/sctp",
|
||||||
"/udp/1234/udt/1234",
|
"/udp/1234/udt/1234",
|
||||||
"/udp/1234/utp/1234",
|
"/udp/1234/utp/1234",
|
||||||
@ -51,9 +52,8 @@ func TestConstructSucceeds(t *testing.T) {
|
|||||||
"/ip4/0.0.0.0",
|
"/ip4/0.0.0.0",
|
||||||
"/ip6/::1",
|
"/ip6/::1",
|
||||||
"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
|
"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
|
||||||
"/tor/timaq4ygg2iegci7.onion",
|
"/onion/timaq4ygg2iegci7:1234",
|
||||||
"/tor/timaq4ygg2iegci7.onion/tcp/1234",
|
"/onion/timaq4ygg2iegci7:80/http",
|
||||||
"/tor/timaq4ygg2iegci7.onion/tcp/80/http",
|
|
||||||
"/udp/0",
|
"/udp/0",
|
||||||
"/tcp/0",
|
"/tcp/0",
|
||||||
"/sctp/0",
|
"/sctp/0",
|
||||||
|
@ -5,9 +5,9 @@ code size name
|
|||||||
33 16 dccp
|
33 16 dccp
|
||||||
41 128 ip6
|
41 128 ip6
|
||||||
132 16 sctp
|
132 16 sctp
|
||||||
133 10 tor
|
|
||||||
301 0 udt
|
301 0 udt
|
||||||
302 0 utp
|
302 0 utp
|
||||||
421 V ipfs
|
421 V ipfs
|
||||||
480 0 http
|
480 0 http
|
||||||
443 0 https
|
443 0 https
|
||||||
|
444 10 onion
|
|
@ -25,12 +25,12 @@ const (
|
|||||||
P_DCCP = 33
|
P_DCCP = 33
|
||||||
P_IP6 = 41
|
P_IP6 = 41
|
||||||
P_SCTP = 132
|
P_SCTP = 132
|
||||||
P_TOR = 133
|
|
||||||
P_UTP = 301
|
P_UTP = 301
|
||||||
P_UDT = 302
|
P_UDT = 302
|
||||||
P_IPFS = 421
|
P_IPFS = 421
|
||||||
P_HTTP = 480
|
P_HTTP = 480
|
||||||
P_HTTPS = 443
|
P_HTTPS = 443
|
||||||
|
P_ONION = 444
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are special sizes
|
// These are special sizes
|
||||||
@ -47,7 +47,7 @@ var Protocols = []Protocol{
|
|||||||
Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
|
Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
|
||||||
// these require varint:
|
// these require varint:
|
||||||
Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
|
Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
|
||||||
Protocol{P_TOR, 10, "tor", CodeToVarint(P_TOR)},
|
Protocol{P_ONION, 10, "onion", CodeToVarint(P_ONION)},
|
||||||
Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
|
Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
|
||||||
Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
|
Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
|
||||||
Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)},
|
Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user