-
Notifications
You must be signed in to change notification settings - Fork 6
/
sanity.go
84 lines (73 loc) · 2.44 KB
/
sanity.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
// Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org>
//
// This file is part of tordam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tordam
import (
"encoding/base32"
"errors"
"fmt"
"strconv"
"strings"
)
// ValidateOnionAddress checks if the given string is a valid Tor v3 Hidden
// service address. Returns error if not.
func ValidateOnionAddress(addr string) error {
aupp := strings.ToUpper(strings.TrimSuffix(addr, ".onion"))
if len(aupp) != 56 {
return fmt.Errorf("invalid v3 onion address (len != 56)")
}
if _, err := base32.StdEncoding.DecodeString(aupp); err != nil {
return fmt.Errorf("invalid v3 onion address: %s", err)
}
return nil
}
// ValidateOnionInternal takes someunlikelyname.onion:port as a parameter
// and validates its format.
func ValidateOnionInternal(onionaddr string) error {
splitOnion := strings.Split(onionaddr, ":")
if len(splitOnion) != 2 {
return errors.New("onion address doesn't contain a port")
}
p, err := strconv.Atoi(splitOnion[1])
if err != nil {
return errors.New("onion port is invalid (not a number)")
}
if p < 1 || p > 65535 {
return errors.New("onion port is invalid (!= 0 < port < 65536)")
}
return ValidateOnionAddress(splitOnion[0])
}
// ValidatePortmap checks if the given []string holds valid portmaps in the
// form of port:port (e.g. 1234:48372). Returns error if any of the found
// portmaps are invalid.
func ValidatePortmap(pm []string) error {
for _, pmap := range pm {
ports := strings.Split(pmap, ":")
if len(ports) != 2 {
return fmt.Errorf("invalid portmap: %s (len != 2)", pmap)
}
for i := 0; i < 2; i++ {
p, err := strconv.Atoi(ports[i])
if err != nil {
return fmt.Errorf("invalid port: %s (%s)", ports[i], err)
}
if p < 1 || p > 65535 {
return fmt.Errorf("invalid port: %d (!= 0 < %d < 65536)", p, p)
}
}
}
return nil
}