-
Notifications
You must be signed in to change notification settings - Fork 123
/
ipdualstack.go
115 lines (105 loc) · 2.77 KB
/
ipdualstack.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
// Copyright (C) 2022 mieru authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
import (
"net"
"os"
"runtime"
"strconv"
"strings"
)
type IPVersion uint8
const (
IPVersionUnknown IPVersion = iota
IPVersion4
IPVersion6
)
func (v IPVersion) String() string {
switch v {
case IPVersionUnknown:
return "UNKNOWN"
case IPVersion4:
return "IPV4"
case IPVersion6:
return "IPV6"
default:
return "UNSPECIFIED"
}
}
// IsIPDualStack returns true if an IPv6 socket is able to send and receive
// both IPv4 and IPv6 packets.
//
// This function only supports Linux. It always returns false if running other
// operating systems.
func IsIPDualStack() bool {
if runtime.GOOS == "linux" {
v, err := os.ReadFile("/proc/sys/net/ipv6/bindv6only")
if err != nil {
return false
}
s := string(v)
s = strings.Trim(s, "\n")
i, err := strconv.Atoi(s)
if err != nil {
return false
}
if i == 0 {
return true
}
}
return false
}
// AllIPAddr returns a catch-all IP address to bind. If the machine supports
// IP dual stack, "::" is returned. Otherwise "0.0.0.0" is returned.
func AllIPAddr() string {
if IsIPDualStack() {
return "::"
}
return "0.0.0.0"
}
// LocalIPAddr returns the localhost IP address.
func LocalIPAddr() string {
// If IP dual stack is supported, bind to "::1" will also bind to
// "127.0.0.1". This may cause an error if the program is running
// inside a container. Generally, we believe "127.0.0.1" is available
// on every machine, so just use this.
return "127.0.0.1"
}
// GetIPVersion returns the IP version of the given network address.
func GetIPVersion(addr string) IPVersion {
host, _, err := net.SplitHostPort(addr)
if err != nil {
// Assume there is no port.
host = addr
}
ip := net.ParseIP(host)
if ip == nil {
return IPVersionUnknown
}
ip4 := ip.To4()
if ip4 != nil {
return IPVersion4
}
return IPVersion6
}
// MaybeDecorateIPv6 adds [ and ] before and after an IPv6 address. If the
// input string is a IPv4 address or not a valid IP address (e.g. is a domain),
// the same string is returned.
func MaybeDecorateIPv6(addr string) string {
if GetIPVersion(addr) == IPVersion6 {
return "[" + addr + "]"
}
return addr
}