Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
committerMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
commitb73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 (patch)
treed95d2f5e903d97082e11eb9f9023c165b1bde388 /util
3x-ui
Diffstat (limited to 'util')
-rw-r--r--util/common/err.go29
-rw-r--r--util/common/format.go21
-rw-r--r--util/common/multi_error.go30
-rw-r--r--util/common/stringUtil.go9
-rw-r--r--util/context.go12
-rw-r--r--util/json_util/json.go24
-rw-r--r--util/random/random.go43
-rw-r--r--util/reflect_util/reflect.go21
-rw-r--r--util/sys/a.s0
-rw-r--r--util/sys/psutil.go8
-rw-r--r--util/sys/sys_darwin.go23
-rw-r--r--util/sys/sys_linux.go70
12 files changed, 290 insertions, 0 deletions
diff --git a/util/common/err.go b/util/common/err.go
new file mode 100644
index 00000000..c0ecbbb8
--- /dev/null
+++ b/util/common/err.go
@@ -0,0 +1,29 @@
+package common
+
+import (
+ "errors"
+ "fmt"
+ "x-ui/logger"
+)
+
+var CtxDone = errors.New("context done")
+
+func NewErrorf(format string, a ...interface{}) error {
+ msg := fmt.Sprintf(format, a...)
+ return errors.New(msg)
+}
+
+func NewError(a ...interface{}) error {
+ msg := fmt.Sprintln(a...)
+ return errors.New(msg)
+}
+
+func Recover(msg string) interface{} {
+ panicErr := recover()
+ if panicErr != nil {
+ if msg != "" {
+ logger.Error(msg, "panic:", panicErr)
+ }
+ }
+ return panicErr
+}
diff --git a/util/common/format.go b/util/common/format.go
new file mode 100644
index 00000000..1ea10877
--- /dev/null
+++ b/util/common/format.go
@@ -0,0 +1,21 @@
+package common
+
+import (
+ "fmt"
+)
+
+func FormatTraffic(trafficBytes int64) (size string) {
+ if trafficBytes < 1024 {
+ return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1))
+ } else if trafficBytes < (1024 * 1024) {
+ return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024))
+ } else if trafficBytes < (1024 * 1024 * 1024) {
+ return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024))
+ } else if trafficBytes < (1024 * 1024 * 1024 * 1024) {
+ return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024))
+ } else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) {
+ return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024))
+ } else {
+ return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024))
+ }
+}
diff --git a/util/common/multi_error.go b/util/common/multi_error.go
new file mode 100644
index 00000000..ff9ff628
--- /dev/null
+++ b/util/common/multi_error.go
@@ -0,0 +1,30 @@
+package common
+
+import (
+ "strings"
+)
+
+type multiError []error
+
+func (e multiError) Error() string {
+ var r strings.Builder
+ r.WriteString("multierr: ")
+ for _, err := range e {
+ r.WriteString(err.Error())
+ r.WriteString(" | ")
+ }
+ return r.String()
+}
+
+func Combine(maybeError ...error) error {
+ var errs multiError
+ for _, err := range maybeError {
+ if err != nil {
+ errs = append(errs, err)
+ }
+ }
+ if len(errs) == 0 {
+ return nil
+ }
+ return errs
+}
diff --git a/util/common/stringUtil.go b/util/common/stringUtil.go
new file mode 100644
index 00000000..5f1f93fd
--- /dev/null
+++ b/util/common/stringUtil.go
@@ -0,0 +1,9 @@
+package common
+
+import "sort"
+
+func IsSubString(target string, str_array []string) bool {
+ sort.Strings(str_array)
+ index := sort.SearchStrings(str_array, target)
+ return index < len(str_array) && str_array[index] == target
+}
diff --git a/util/context.go b/util/context.go
new file mode 100644
index 00000000..b768f05c
--- /dev/null
+++ b/util/context.go
@@ -0,0 +1,12 @@
+package util
+
+import "context"
+
+func IsDone(ctx context.Context) bool {
+ select {
+ case <-ctx.Done():
+ return true
+ default:
+ return false
+ }
+}
diff --git a/util/json_util/json.go b/util/json_util/json.go
new file mode 100644
index 00000000..65ad789e
--- /dev/null
+++ b/util/json_util/json.go
@@ -0,0 +1,24 @@
+package json_util
+
+import (
+ "errors"
+)
+
+type RawMessage []byte
+
+// MarshalJSON 自定义 json.RawMessage 默认行为
+func (m RawMessage) MarshalJSON() ([]byte, error) {
+ if len(m) == 0 {
+ return []byte("null"), nil
+ }
+ return m, nil
+}
+
+// UnmarshalJSON sets *m to a copy of data.
+func (m *RawMessage) UnmarshalJSON(data []byte) error {
+ if m == nil {
+ return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
+ }
+ *m = append((*m)[0:0], data...)
+ return nil
+}
diff --git a/util/random/random.go b/util/random/random.go
new file mode 100644
index 00000000..b1dd2e09
--- /dev/null
+++ b/util/random/random.go
@@ -0,0 +1,43 @@
+package random
+
+import (
+ "math/rand"
+ "time"
+)
+
+var numSeq [10]rune
+var lowerSeq [26]rune
+var upperSeq [26]rune
+var numLowerSeq [36]rune
+var numUpperSeq [36]rune
+var allSeq [62]rune
+
+func init() {
+ rand.Seed(time.Now().UnixNano())
+
+ for i := 0; i < 10; i++ {
+ numSeq[i] = rune('0' + i)
+ }
+ for i := 0; i < 26; i++ {
+ lowerSeq[i] = rune('a' + i)
+ upperSeq[i] = rune('A' + i)
+ }
+
+ copy(numLowerSeq[:], numSeq[:])
+ copy(numLowerSeq[len(numSeq):], lowerSeq[:])
+
+ copy(numUpperSeq[:], numSeq[:])
+ copy(numUpperSeq[len(numSeq):], upperSeq[:])
+
+ copy(allSeq[:], numSeq[:])
+ copy(allSeq[len(numSeq):], lowerSeq[:])
+ copy(allSeq[len(numSeq)+len(lowerSeq):], upperSeq[:])
+}
+
+func Seq(n int) string {
+ runes := make([]rune, n)
+ for i := 0; i < n; i++ {
+ runes[i] = allSeq[rand.Intn(len(allSeq))]
+ }
+ return string(runes)
+}
diff --git a/util/reflect_util/reflect.go b/util/reflect_util/reflect.go
new file mode 100644
index 00000000..1fdaec50
--- /dev/null
+++ b/util/reflect_util/reflect.go
@@ -0,0 +1,21 @@
+package reflect_util
+
+import "reflect"
+
+func GetFields(t reflect.Type) []reflect.StructField {
+ num := t.NumField()
+ fields := make([]reflect.StructField, 0, num)
+ for i := 0; i < num; i++ {
+ fields = append(fields, t.Field(i))
+ }
+ return fields
+}
+
+func GetFieldValues(v reflect.Value) []reflect.Value {
+ num := v.NumField()
+ fields := make([]reflect.Value, 0, num)
+ for i := 0; i < num; i++ {
+ fields = append(fields, v.Field(i))
+ }
+ return fields
+}
diff --git a/util/sys/a.s b/util/sys/a.s
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/util/sys/a.s
diff --git a/util/sys/psutil.go b/util/sys/psutil.go
new file mode 100644
index 00000000..645f839a
--- /dev/null
+++ b/util/sys/psutil.go
@@ -0,0 +1,8 @@
+package sys
+
+import (
+ _ "unsafe"
+)
+
+//go:linkname HostProc github.com/shirou/gopsutil/internal/common.HostProc
+func HostProc(combineWith ...string) string
diff --git a/util/sys/sys_darwin.go b/util/sys/sys_darwin.go
new file mode 100644
index 00000000..d61a38a2
--- /dev/null
+++ b/util/sys/sys_darwin.go
@@ -0,0 +1,23 @@
+// +build darwin
+
+package sys
+
+import (
+ "github.com/shirou/gopsutil/net"
+)
+
+func GetTCPCount() (int, error) {
+ stats, err := net.Connections("tcp")
+ if err != nil {
+ return 0, err
+ }
+ return len(stats), nil
+}
+
+func GetUDPCount() (int, error) {
+ stats, err := net.Connections("udp")
+ if err != nil {
+ return 0, err
+ }
+ return len(stats), nil
+}
diff --git a/util/sys/sys_linux.go b/util/sys/sys_linux.go
new file mode 100644
index 00000000..843d9b00
--- /dev/null
+++ b/util/sys/sys_linux.go
@@ -0,0 +1,70 @@
+// +build linux
+
+package sys
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "os"
+)
+
+func getLinesNum(filename string) (int, error) {
+ file, err := os.Open(filename)
+ if err != nil {
+ return 0, err
+ }
+ defer file.Close()
+
+ sum := 0
+ buf := make([]byte, 8192)
+ for {
+ n, err := file.Read(buf)
+
+ var buffPosition int
+ for {
+ i := bytes.IndexByte(buf[buffPosition:], '\n')
+ if i < 0 || n == buffPosition {
+ break
+ }
+ buffPosition += i + 1
+ sum++
+ }
+
+ if err == io.EOF {
+ return sum, nil
+ } else if err != nil {
+ return sum, err
+ }
+ }
+}
+
+func GetTCPCount() (int, error) {
+ root := HostProc()
+
+ tcp4, err := getLinesNum(fmt.Sprintf("%v/net/tcp", root))
+ if err != nil {
+ return tcp4, err
+ }
+ tcp6, err := getLinesNum(fmt.Sprintf("%v/net/tcp6", root))
+ if err != nil {
+ return tcp4 + tcp6, nil
+ }
+
+ return tcp4 + tcp6, nil
+}
+
+func GetUDPCount() (int, error) {
+ root := HostProc()
+
+ udp4, err := getLinesNum(fmt.Sprintf("%v/net/udp", root))
+ if err != nil {
+ return udp4, err
+ }
+ udp6, err := getLinesNum(fmt.Sprintf("%v/net/udp6", root))
+ if err != nil {
+ return udp4 + udp6, nil
+ }
+
+ return udp4 + udp6, nil
+}