File size: 3,943 Bytes
5c24871 | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | package utils
import (
"cmp"
"crypto/rand"
"errors"
"fmt"
"iter"
"maps"
"math/big"
"net/netip"
"regexp"
"slices"
"strconv"
"strings"
"golang.org/x/exp/constraints"
)
var (
DNSServersV4 = []string{"8.8.8.8:53", "8.8.4.4:53", "1.1.1.1:53", "1.0.0.1:53"}
DNSServersV6 = []string{"[2001:4860:4860::8888]:53", "[2001:4860:4860::8844]:53", "[2606:4700:4700::1111]:53", "[2606:4700:4700::1001]:53"}
DNSServers = append(DNSServersV4, DNSServersV6...)
ipv4Re = regexp.MustCompile(`(\d*\.).*(\.\d*)`)
ipv6Re = regexp.MustCompile(`(\w*:\w*:).*(:\w*:\w*)`)
)
func ipv4Desensitize(ipv4Addr string) string {
return ipv4Re.ReplaceAllString(ipv4Addr, "$1****$2")
}
func ipv6Desensitize(ipv6Addr string) string {
return ipv6Re.ReplaceAllString(ipv6Addr, "$1****$2")
}
func IPDesensitize(ipAddr string) string {
ipAddr = ipv4Desensitize(ipAddr)
ipAddr = ipv6Desensitize(ipAddr)
return ipAddr
}
func IPStringToBinary(ip string) ([]byte, error) {
addr, err := netip.ParseAddr(ip)
if err != nil {
return nil, err
}
b := addr.As16()
return b[:], nil
}
func BinaryToIPString(b []byte) string {
if len(b) < 16 {
return "::"
}
addr := netip.AddrFrom16([16]byte(b))
return addr.Unmap().String()
}
func GetIPFromHeader(headerValue string) (string, error) {
a := strings.Split(headerValue, ",")
h := strings.TrimSpace(a[len(a)-1])
ip, err := netip.ParseAddr(h)
if err != nil {
return "", err
}
if !ip.IsValid() {
return "", errors.New("invalid ip")
}
return ip.String(), nil
}
func GenerateRandomString(n int) (string, error) {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
lettersLength := big.NewInt(int64(len(letters)))
ret := make([]byte, n)
for i := range n {
num, err := rand.Int(rand.Reader, lettersLength)
if err != nil {
return "", err
}
ret[i] = letters[num.Int64()]
}
return string(ret), nil
}
func MustGenerateRandomString(n int) string {
str, err := GenerateRandomString(n)
if err != nil {
panic(fmt.Errorf("MustGenerateRandomString: %v", err))
}
return str
}
func IfOr[T any](a bool, x, y T) T {
if a {
return x
}
return y
}
func Itoa[T constraints.Integer](i T) string {
switch any(i).(type) {
case int, int8, int16, int32, int64:
return strconv.FormatInt(int64(i), 10)
case uint, uint8, uint16, uint32, uint64:
return strconv.FormatUint(uint64(i), 10)
default:
return ""
}
}
func MapValuesToSlice[Map ~map[K]V, K comparable, V any](m Map) []V {
s := make([]V, 0, len(m))
return slices.AppendSeq(s, maps.Values(m))
}
func MapKeysToSlice[Map ~map[K]V, K comparable, V any](m Map) []K {
s := make([]K, 0, len(m))
return slices.AppendSeq(s, maps.Keys(m))
}
func Unique[S ~[]E, E cmp.Ordered](list S) S {
if list == nil {
return nil
}
out := make([]E, len(list))
copy(out, list)
slices.Sort(out)
return slices.Compact(out)
}
func ConvertSeq[In, Out any](seq iter.Seq[In], f func(In) Out) iter.Seq[Out] {
return func(yield func(Out) bool) {
for in := range seq {
if !yield(f(in)) {
return
}
}
}
}
func ConvertSeq2[KIn, VIn, KOut, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, VIn) (KOut, VOut)) iter.Seq2[KOut, VOut] {
return func(yield func(KOut, VOut) bool) {
for k, v := range seq {
if !yield(f(k, v)) {
return
}
}
}
}
func Seq2To1[K, V any](seq iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range seq {
if !yield(v) {
return
}
}
}
}
type WrapError struct {
err, errIn error
}
func NewWrapError(err, errIn error) error {
return &WrapError{err, errIn}
}
func (e *WrapError) Error() string {
return e.err.Error()
}
func (e *WrapError) Unwrap() error {
return e.errIn
}
func FirstError(errorer ...func() error) error {
for _, fn := range errorer {
if err := fn(); err != nil {
return err
}
}
return nil
}
func SubUintChecked[T constraints.Unsigned](a, b T) T {
if a < b {
return 0
}
return a - b
}
|