-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.go
50 lines (37 loc) · 1.24 KB
/
utils.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
package tor
import (
"fmt"
"net"
"strings"
ma "github.com/multiformats/go-multiaddr"
)
// nopMaddr is an empty maddr used as filler when needed.
const NopMaddr2Str = "/onion/aaaaaaaaaaaaaaaa:1"
var NopMaddr2, _ = ma.NewMultiaddr(NopMaddr2Str)
const NopMaddr3Str = "/onion3/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1"
var NopMaddr3, _ = ma.NewMultiaddr(NopMaddr3Str)
// maddrToNetAddr is unsafe and need to be called with caution.
func maddrToNetAddr(m ma.Multiaddr) addr {
return maddrToNetAddrP0(m, m.Protocols()[0].Code)
}
func maddrToNetAddrP0(m ma.Multiaddr, p0 int) addr {
v, err := m.ValueForProtocol(p0)
checkError(err)
vs := strings.SplitN(v, ":", 2)
return addr(vs[0] + ".onion:" + vs[1])
}
// Used to build bine friendly tor net.Addr.
type addr string
var _ net.Addr = addr("")
func (_ addr) Network() string {
return "tcp"
}
func (a addr) String() string {
return string(a)
}
// checkError is used to check if an error ocured during the build of multiaddr, if this fail that mean you have a problem with your packages.
func checkError(err error) {
if err != nil {
panic(fmt.Sprintf("Was not able to create onion multiaddr, this shouldn't fail, check your multiaddr package or report to maintainers ! (%s)", err))
}
}