mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
feat: update to go1.24 & support listening https (#1002)
* feat: support listening https * refactor * modernize * support snake case in config * more precise control of config fields * update goreleaser config * remove kubeyaml * fix: expose agent_secret * chore
This commit is contained in:
@@ -33,9 +33,7 @@ func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
||||
return nil, ErrGjsonWrongType
|
||||
}
|
||||
|
||||
return func(yield func(string, string) bool) {
|
||||
result.ForEach(func(k, v gjson.Result) bool {
|
||||
return yield(k.String(), v.String())
|
||||
})
|
||||
}, nil
|
||||
return ConvertSeq2(result.ForEach, func(k, v gjson.Result) (string, string) {
|
||||
return k.String(), v.String()
|
||||
}), nil
|
||||
}
|
||||
|
||||
71
pkg/utils/koanf.go
Normal file
71
pkg/utils/koanf.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"reflect"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
|
||||
// TextUnmarshalerHookFunc is a fixed version of mapstructure.TextUnmarshallerHookFunc.
|
||||
// This hook allows to additionally unmarshal text into custom string types that implement the encoding.Text(Un)Marshaler interface(s).
|
||||
func TextUnmarshalerHookFunc() mapstructure.DecodeHookFuncType {
|
||||
return func(
|
||||
f reflect.Type,
|
||||
t reflect.Type,
|
||||
data any,
|
||||
) (any, error) {
|
||||
if f.Kind() != reflect.String {
|
||||
return data, nil
|
||||
}
|
||||
result := reflect.New(t).Interface()
|
||||
unmarshaller, ok := result.(encoding.TextUnmarshaler)
|
||||
if !ok {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// default text representation is the actual value of the `from` string
|
||||
var (
|
||||
dataVal = reflect.ValueOf(data)
|
||||
text = []byte(dataVal.String())
|
||||
)
|
||||
if f.Kind() == t.Kind() {
|
||||
// source and target are of underlying type string
|
||||
var (
|
||||
err error
|
||||
ptrVal = reflect.New(dataVal.Type())
|
||||
)
|
||||
if !ptrVal.Elem().CanSet() {
|
||||
// cannot set, skip, this should not happen
|
||||
if err := unmarshaller.UnmarshalText(text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
ptrVal.Elem().Set(dataVal)
|
||||
|
||||
// We need to assert that both, the value type and the pointer type
|
||||
// do (not) implement the TextMarshaller interface before proceeding and simply
|
||||
// using the string value of the string type.
|
||||
// it might be the case that the internal string representation differs from
|
||||
// the (un)marshalled string.
|
||||
|
||||
for _, v := range []reflect.Value{dataVal, ptrVal} {
|
||||
if marshaller, ok := v.Interface().(encoding.TextMarshaler); ok {
|
||||
text, err = marshaller.MarshalText()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// text is either the source string's value or the source string type's marshaled value
|
||||
// which may differ from its internal string value.
|
||||
if err := unmarshaller.UnmarshalText(text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"iter"
|
||||
@@ -13,24 +14,19 @@ import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var (
|
||||
Json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
DNSServers = []string{"8.8.8.8:53", "8.8.4.4:53", "1.1.1.1:53", "1.0.0.1:53"}
|
||||
)
|
||||
|
||||
var ipv4Re = regexp.MustCompile(`(\d*\.).*(\.\d*)`)
|
||||
ipv4Re = regexp.MustCompile(`(\d*\.).*(\.\d*)`)
|
||||
ipv6Re = regexp.MustCompile(`(\w*:\w*:).*(:\w*:\w*)`)
|
||||
)
|
||||
|
||||
func ipv4Desensitize(ipv4Addr string) string {
|
||||
return ipv4Re.ReplaceAllString(ipv4Addr, "$1****$2")
|
||||
}
|
||||
|
||||
var ipv6Re = regexp.MustCompile(`(\w*:\w*:).*(:\w*:\w*)`)
|
||||
|
||||
func ipv6Desensitize(ipv6Addr string) string {
|
||||
return ipv6Re.ReplaceAllString(ipv6Addr, "$1****$2")
|
||||
}
|
||||
@@ -51,9 +47,11 @@ func IPStringToBinary(ip string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func BinaryToIPString(b []byte) string {
|
||||
var addr16 [16]byte
|
||||
copy(addr16[:], b)
|
||||
addr := netip.AddrFrom16(addr16)
|
||||
if len(b) < 16 {
|
||||
return "::"
|
||||
}
|
||||
|
||||
addr := netip.AddrFrom16([16]byte(b))
|
||||
return addr.Unmap().String()
|
||||
}
|
||||
|
||||
@@ -74,7 +72,7 @@ func GenerateRandomString(n int) (string, error) {
|
||||
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
lettersLength := big.NewInt(int64(len(letters)))
|
||||
ret := make([]byte, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
num, err := rand.Int(rand.Reader, lettersLength)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -117,22 +115,35 @@ func MapValuesToSlice[Map ~map[K]V, K comparable, V any](m Map) []V {
|
||||
return slices.AppendSeq(s, maps.Values(m))
|
||||
}
|
||||
|
||||
func Unique[T comparable](s []T) []T {
|
||||
m := make(map[T]struct{})
|
||||
ret := make([]T, 0, len(s))
|
||||
for _, v := range s {
|
||||
if _, ok := m[v]; !ok {
|
||||
m[v] = struct{}{}
|
||||
ret = append(ret, v)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
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 ConvertSeq[T, U any](seq iter.Seq[T], f func(e T) U) iter.Seq[U] {
|
||||
return func(yield func(U) bool) {
|
||||
for e := range seq {
|
||||
if !yield(f(e)) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestNotification(t *testing.T) {
|
||||
|
||||
func TestGenerGenerateRandomString(t *testing.T) {
|
||||
generatedString := make(map[string]bool)
|
||||
for i := 0; i < 100; i++ {
|
||||
for range 100 {
|
||||
str, err := GenerateRandomString(32)
|
||||
if err != nil {
|
||||
t.Fatalf("Error: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user