| 1 | /* |
| 2 | * Copyright 2012-2022 Li Kexian |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * A toolkit for Golang development |
| 17 | * https://www.likexian.com/ |
| 18 | */ |
| 19 | |
| 20 | package xipscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ |
| 21 | |
| 22 | import ( |
| 23 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/" |
| 24 | "encoding/hexscip-go gomod github.com/golang/go/src go1.18 `encoding/hex`/" |
| 25 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 26 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 27 | "netscip-go gomod github.com/golang/go/src go1.18 net/" |
| 28 | "strconvscip-go gomod github.com/golang/go/src go1.18 strconv/" |
| 29 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | // ErrInvalidIP ip value is invalid |
| 34 | ErrInvalidIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidIP. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("xip: not valid ip string") |
| 35 | // ErrInvalidMask ip mask value is invalid |
| 36 | ErrInvalidMaskscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidMask. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("xip: not valid ip mask") |
| 37 | // ErrInvalidHex hex string is invalid |
| 38 | ErrInvalidHexscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidHex. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("xip: not valid hex string") |
| 39 | ) |
| 40 | |
| 41 | // PrivateIPs is private ip |
| 42 | var PrivateIPsscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/PrivateIPs. = []string{ |
| 43 | "10.0.0.0/8", |
| 44 | "172.16.0.0/12", |
| 45 | "192.168.0.0/16", |
| 46 | "100.64.0.0/10", |
| 47 | "fc00::/7", |
| 48 | } |
| 49 | |
| 50 | // Version returns package version |
| 51 | func Versionscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/Version().() string { |
| 52 | return "0.5.0" |
| 53 | } |
| 54 | |
| 55 | // Author returns package author |
| 56 | func Authorscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/Author().() string { |
| 57 | return "[Li Kexian](https://www.likexian.com/)" |
| 58 | } |
| 59 | |
| 60 | // License returns package license |
| 61 | func Licensescip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/License().() string { |
| 62 | return "Licensed under the Apache License 2.0" |
| 63 | } |
| 64 | |
| 65 | // IsIP returns if string is a ip |
| 66 | func IsIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIP().(ip string) bool { |
| 67 | return netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(ip) != nil |
| 68 | } |
| 69 | |
| 70 | // IsIPv4 returns if string is a ipv4 |
| 71 | func IsIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv4().(ip string) bool { |
| 72 | if !stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Containsscip-go gomod github.com/golang/go/src go1.18 strings/Contains().(ip, ".") { |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | return IsIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIP().(ip) |
| 77 | } |
| 78 | |
| 79 | // IsIPv6 returns if string is a ipv6 |
| 80 | func IsIPv6scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv6().(ip string) bool { |
| 81 | if !stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Containsscip-go gomod github.com/golang/go/src go1.18 strings/Contains().(ip, ":") { |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | return IsIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIP().(ip) |
| 86 | } |
| 87 | |
| 88 | // IPv4ToLong returns uint32 of ip, -1 for error |
| 89 | func IPv4ToLongscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IPv4ToLong().(ip string) (uint32, error) { |
| 90 | if !IsIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv4().(ip) { |
| 91 | return 0, ErrInvalidIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidIP. |
| 92 | } |
| 93 | |
| 94 | return binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian..Uint32scip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/bigEndian#Uint32().(netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(ip).To4scip-go gomod github.com/golang/go/src go1.18 net/IP#To4().()), nil |
| 95 | } |
| 96 | |
| 97 | // LongToIPv4 returns string from uint32 of ip |
| 98 | func LongToIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/LongToIPv4().(ip uint32) string { |
| 99 | buf := make([]byte, 4) |
| 100 | |
| 101 | binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian..PutUint32scip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/bigEndian#PutUint32().(buf, ip) |
| 102 | s := netscip-go gomod github.com/golang/go/src go1.18 net/.IPscip-go gomod github.com/golang/go/src go1.18 net/IP#(buf) |
| 103 | |
| 104 | return s.Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().() |
| 105 | } |
| 106 | |
| 107 | // Uint32ToHex returns hex from uint32 |
| 108 | func Uint32ToHexscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/Uint32ToHex().(i uint32) string { |
| 109 | buf := make([]byte, 4) |
| 110 | binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian..PutUint32scip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/bigEndian#PutUint32().(buf, i) |
| 111 | return hexscip-go gomod github.com/golang/go/src go1.18 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/hex`/EncodeToString().(buf) |
| 112 | } |
| 113 | |
| 114 | // HexToUint32 returns uint32 from hex string |
| 115 | func HexToUint32scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/HexToUint32().(s string) (uint32, error) { |
| 116 | buf, err := hexscip-go gomod github.com/golang/go/src go1.18 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.18 `encoding/hex`/DecodeString().(s) |
| 117 | if err != nil { |
| 118 | return 0, ErrInvalidHexscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidHex. |
| 119 | } |
| 120 | return binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian..Uint32scip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/bigEndian#Uint32().(buf), nil |
| 121 | } |
| 122 | |
| 123 | // GetEthIPv4 returns all interface ipv4 without loopback |
| 124 | func GetEthIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/GetEthIPv4().() (ips []string, err error) { |
| 125 | addrs, err := netscip-go gomod github.com/golang/go/src go1.18 net/.InterfaceAddrsscip-go gomod github.com/golang/go/src go1.18 net/InterfaceAddrs().() |
| 126 | if err != nil { |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | for _, v := range addrs { |
| 131 | if ipnet, ok := v.(*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#); ok && !ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..IsLoopbackscip-go gomod github.com/golang/go/src go1.18 net/IP#IsLoopback().() && ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.18 net/IP#To4().() != nil { |
| 132 | if IsIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv4().(ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) { |
| 133 | ips = append(ips, ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | // GetEthIPv4ByInterface returns interface ipv4 by name |
| 142 | func GetEthIPv4ByInterfacescip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/GetEthIPv4ByInterface().(name string) (ips []string, err error) { |
| 143 | iface, err := netscip-go gomod github.com/golang/go/src go1.18 net/.InterfaceByNamescip-go gomod github.com/golang/go/src go1.18 net/InterfaceByName().(name) |
| 144 | if err != nil { |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | addrs, err := iface.Addrsscip-go gomod github.com/golang/go/src go1.18 net/Interface#Addrs().() |
| 149 | if err != nil { |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | for _, v := range addrs { |
| 154 | if ipnet, ok := v.(*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#); ok && ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.18 net/IP#To4().() != nil { |
| 155 | if IsIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv4().(ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) { |
| 156 | ips = append(ips, ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | // GetEthIPv6 returns all interface ipv6 without loopback |
| 165 | func GetEthIPv6scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/GetEthIPv6().() (ips []string, err error) { |
| 166 | addrs, err := netscip-go gomod github.com/golang/go/src go1.18 net/.InterfaceAddrsscip-go gomod github.com/golang/go/src go1.18 net/InterfaceAddrs().() |
| 167 | if err != nil { |
| 168 | return |
| 169 | } |
| 170 | |
| 171 | for _, v := range addrs { |
| 172 | if ipnet, ok := v.(*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#); ok && !ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..IsLoopbackscip-go gomod github.com/golang/go/src go1.18 net/IP#IsLoopback().() && ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..To16scip-go gomod github.com/golang/go/src go1.18 net/IP#To16().() != nil { |
| 173 | if IsIPv6scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv6().(ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) { |
| 174 | ips = append(ips, ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | // GetEthIPv6ByInterface returns interface ipv6 by name |
| 183 | func GetEthIPv6ByInterfacescip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/GetEthIPv6ByInterface().(name string) (ips []string, err error) { |
| 184 | iface, err := netscip-go gomod github.com/golang/go/src go1.18 net/.InterfaceByNamescip-go gomod github.com/golang/go/src go1.18 net/InterfaceByName().(name) |
| 185 | if err != nil { |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | addrs, err := iface.Addrsscip-go gomod github.com/golang/go/src go1.18 net/Interface#Addrs().() |
| 190 | if err != nil { |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | for _, v := range addrs { |
| 195 | if ipnet, ok := v.(*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#); ok && ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..To16scip-go gomod github.com/golang/go/src go1.18 net/IP#To16().() != nil { |
| 196 | if IsIPv6scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv6().(ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) { |
| 197 | ips = append(ips, ipnet.IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.18 net/IP#String().()) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | // IsContains returns if ip is in cidr |
| 206 | func IsContainsscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsContains().(cidr, ip string) bool { |
| 207 | _, ipnet, err := netscip-go gomod github.com/golang/go/src go1.18 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.18 net/ParseCIDR().(cidr) |
| 208 | if err != nil { |
| 209 | return false |
| 210 | } |
| 211 | |
| 212 | ipaddr := netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(ip) |
| 213 | if ipaddr == nil { |
| 214 | return false |
| 215 | } |
| 216 | |
| 217 | return ipnet.Containsscip-go gomod github.com/golang/go/src go1.18 net/IPNet#Contains().(ipaddr) |
| 218 | } |
| 219 | |
| 220 | // IsPrivate return ip is private |
| 221 | func IsPrivatescip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsPrivate().(ip string) bool { |
| 222 | ipaddr := netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(ip) |
| 223 | if ipaddr == nil { |
| 224 | return false |
| 225 | } |
| 226 | |
| 227 | if ipaddr.IsLoopbackscip-go gomod github.com/golang/go/src go1.18 net/IP#IsLoopback().() || ipaddr.IsLinkLocalMulticastscip-go gomod github.com/golang/go/src go1.18 net/IP#IsLinkLocalMulticast().() || ipaddr.IsLinkLocalUnicastscip-go gomod github.com/golang/go/src go1.18 net/IP#IsLinkLocalUnicast().() { |
| 228 | return true |
| 229 | } |
| 230 | |
| 231 | for _, v := range PrivateIPsscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/PrivateIPs. { |
| 232 | if IsContainsscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsContains().(v, ip) { |
| 233 | return true |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return false |
| 238 | } |
| 239 | |
| 240 | // FixSubnet fix ip with a subnet mask, for example: 1.2.3.4/24, 2001:700:300::/48 |
| 241 | func FixSubnetscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/FixSubnet().(ip string) (string, error) { |
| 242 | ips := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(ip, "/") |
| 243 | if len(ips) == 1 { |
| 244 | ips = append(ips, "-1") |
| 245 | } |
| 246 | |
| 247 | ips[1] = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(ips[1]) |
| 248 | if ips[1] == "" { |
| 249 | ips[1] = "-1" |
| 250 | } |
| 251 | |
| 252 | mask, err := strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.18 strconv/Atoi().(ips[1]) |
| 253 | if err != nil { |
| 254 | return "", ErrInvalidMaskscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidMask. |
| 255 | } |
| 256 | |
| 257 | ip = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(ips[0]) |
| 258 | if IsIPv4scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv4().(ip) { |
| 259 | if mask > 32 { |
| 260 | return "", ErrInvalidMaskscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidMask. |
| 261 | } |
| 262 | if mask < 0 { |
| 263 | mask = 24 |
| 264 | } |
| 265 | } else if IsIPv6scip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/IsIPv6().(ip) { |
| 266 | if mask > 128 { |
| 267 | return "", ErrInvalidMaskscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidMask. |
| 268 | } |
| 269 | if mask < 0 { |
| 270 | mask = 56 |
| 271 | } |
| 272 | } else { |
| 273 | return "", ErrInvalidIPscip-go gomod github.com/likexian/gokit . `github.com/likexian/gokit/xip`/ErrInvalidIP. |
| 274 | } |
| 275 | |
| 276 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%s/%d", ip, mask), nil |
| 277 | } |