2022-09-23 08:33:55 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-09-26 08:32:40 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
mathRand "math/rand"
|
2022-09-27 08:57:23 +00:00
|
|
|
"net"
|
2022-12-18 15:00:24 +00:00
|
|
|
"reflect"
|
2022-09-23 08:33:55 +00:00
|
|
|
"regexp"
|
2022-10-03 09:35:39 +00:00
|
|
|
"sort"
|
2022-09-23 08:33:55 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-03-31 16:51:25 +00:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-09-23 08:33:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CompareVersion(version1 string, version2 string) bool {
|
2022-10-13 10:56:53 +00:00
|
|
|
if version1 == version2 {
|
|
|
|
return false
|
|
|
|
}
|
2022-09-23 08:33:55 +00:00
|
|
|
version1s := strings.Split(version1, ".")
|
|
|
|
version2s := strings.Split(version2, ".")
|
|
|
|
|
|
|
|
n := min(len(version1s), len(version2s))
|
|
|
|
re := regexp.MustCompile("[0-9]+")
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
sVersion1s := re.FindAllString(version1s[i], -1)
|
|
|
|
sVersion2s := re.FindAllString(version2s[i], -1)
|
|
|
|
if len(sVersion1s) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(sVersion2s) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v1num, _ := strconv.Atoi(sVersion1s[0])
|
|
|
|
v2num, _ := strconv.Atoi(sVersion2s[0])
|
|
|
|
if v1num == v2num {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return v1num > v2num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:35:39 +00:00
|
|
|
func GetSortedVersions(versions []string) []string {
|
|
|
|
sort.Slice(versions, func(i, j int) bool {
|
|
|
|
return CompareVersion(versions[i], versions[j])
|
|
|
|
})
|
|
|
|
return versions
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsCrossVersion(version1, version2 string) bool {
|
|
|
|
version1s := strings.Split(version1, ".")
|
|
|
|
version2s := strings.Split(version2, ".")
|
|
|
|
v1num, _ := strconv.Atoi(version1s[0])
|
|
|
|
v2num, _ := strconv.Atoi(version2s[0])
|
|
|
|
return v2num > v1num
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:33:55 +00:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2022-09-26 08:32:40 +00:00
|
|
|
|
|
|
|
func GetUuid() string {
|
|
|
|
b := make([]byte, 16)
|
|
|
|
_, _ = io.ReadFull(rand.Reader, b)
|
|
|
|
b[6] = (b[6] & 0x0f) | 0x40
|
|
|
|
b[8] = (b[8] & 0x3f) | 0x80
|
|
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
|
|
|
}
|
|
|
|
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
|
|
|
|
|
|
|
func RandStr(n int) string {
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[mathRand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2022-09-27 08:57:23 +00:00
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
func ScanPort(port int) bool {
|
2022-09-27 08:57:23 +00:00
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
2022-09-27 08:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
return false
|
|
|
|
}
|
2022-09-29 10:16:56 +00:00
|
|
|
|
2023-03-31 16:51:25 +00:00
|
|
|
func ScanPortWithProtocol(port, Protocol string) bool {
|
|
|
|
command := "netstat -ntpl"
|
|
|
|
if Protocol == "udp" {
|
|
|
|
command = "netstat -nupl"
|
|
|
|
}
|
|
|
|
stdout, err := cmd.Execf("%s | awk '{print $4}' ", command)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
if len(lines) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
if strings.HasSuffix(line, ":"+port) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:16:56 +00:00
|
|
|
func ExistWithStrArray(str string, arr []string) bool {
|
|
|
|
for _, a := range arr {
|
|
|
|
if strings.Contains(a, str) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-10-24 15:06:49 +00:00
|
|
|
|
|
|
|
func IsNum(s string) bool {
|
|
|
|
_, err := strconv.ParseFloat(s, 64)
|
|
|
|
return err == nil
|
|
|
|
}
|
2022-12-18 15:00:24 +00:00
|
|
|
|
|
|
|
func RemoveRepeatElement(a interface{}) (ret []interface{}) {
|
|
|
|
va := reflect.ValueOf(a)
|
|
|
|
for i := 0; i < va.Len(); i++ {
|
|
|
|
if i > 0 && reflect.DeepEqual(va.Index(i-1).Interface(), va.Index(i).Interface()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, va.Index(i).Interface())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2022-12-23 10:00:19 +00:00
|
|
|
|
|
|
|
func LoadSizeUnit(value float64) string {
|
|
|
|
if value > 1048576 {
|
|
|
|
return fmt.Sprintf("%vM", value/1048576)
|
|
|
|
}
|
|
|
|
if value > 1024 {
|
|
|
|
return fmt.Sprintf("%vK", value/1024)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", value)
|
|
|
|
}
|