2022-09-23 16:33:55 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-09-26 16:32:40 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
mathRand "math/rand"
|
2022-09-27 16:57:23 +08:00
|
|
|
"net"
|
2024-04-06 22:32:10 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-12-18 23:00:24 +08:00
|
|
|
"reflect"
|
2022-09-23 16:33:55 +08:00
|
|
|
"regexp"
|
2022-10-03 17:35:39 +08:00
|
|
|
"sort"
|
2022-09-23 16:33:55 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-05-04 12:00:37 +08:00
|
|
|
"time"
|
2023-12-28 16:49:38 +08:00
|
|
|
"unicode"
|
2023-05-31 14:03:04 +08:00
|
|
|
|
2024-02-22 15:32:07 +08:00
|
|
|
"golang.org/x/net/idna"
|
|
|
|
|
2023-05-31 14:03:04 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-09-23 16:33:55 +08:00
|
|
|
)
|
|
|
|
|
2023-12-14 11:20:11 +08:00
|
|
|
func CompareVersion(version1, version2 string) bool {
|
|
|
|
v1s := extractNumbers(version1)
|
|
|
|
v2s := extractNumbers(version2)
|
|
|
|
|
|
|
|
maxLen := max(len(v1s), len(v2s))
|
|
|
|
v1s = append(v1s, make([]string, maxLen-len(v1s))...)
|
|
|
|
v2s = append(v2s, make([]string, maxLen-len(v2s))...)
|
2022-09-23 16:33:55 +08:00
|
|
|
|
2023-12-14 11:20:11 +08:00
|
|
|
for i := 0; i < maxLen; i++ {
|
|
|
|
v1, err1 := strconv.Atoi(v1s[i])
|
|
|
|
v2, err2 := strconv.Atoi(v2s[i])
|
|
|
|
if err1 != nil {
|
|
|
|
v1 = 0
|
2023-09-16 16:30:16 +08:00
|
|
|
}
|
2023-12-14 11:20:11 +08:00
|
|
|
if err2 != nil {
|
|
|
|
v2 = 0
|
|
|
|
}
|
|
|
|
if v1 != v2 {
|
|
|
|
return v1 > v2
|
2023-09-16 16:30:16 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 11:20:11 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-09-16 16:30:16 +08:00
|
|
|
|
2024-04-12 11:04:06 +08:00
|
|
|
func ComparePanelVersion(version1, version2 string) bool {
|
|
|
|
if version1 == version2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
version1s := strings.Split(version1, ".")
|
|
|
|
version2s := strings.Split(version2, ".")
|
|
|
|
|
|
|
|
if len(version2s) > len(version1s) {
|
|
|
|
for i := 0; i < len(version2s)-len(version1s); i++ {
|
|
|
|
version1s = append(version1s, "0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(version1s) > len(version2s) {
|
|
|
|
for i := 0; i < len(version1s)-len(version2s); i++ {
|
|
|
|
version2s = append(version2s, "0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := min(len(version1s), len(version2s))
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if version1s[i] == version2s[i] {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return version1s[i] > version2s[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-12-14 11:20:11 +08:00
|
|
|
func extractNumbers(version string) []string {
|
|
|
|
var numbers []string
|
|
|
|
start := -1
|
|
|
|
for i, r := range version {
|
|
|
|
if isDigit(r) {
|
|
|
|
if start == -1 {
|
|
|
|
start = i
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
} else {
|
2023-12-14 11:20:11 +08:00
|
|
|
if start != -1 {
|
|
|
|
numbers = append(numbers, version[start:i])
|
|
|
|
start = -1
|
|
|
|
}
|
2022-09-23 16:33:55 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 11:20:11 +08:00
|
|
|
if start != -1 {
|
|
|
|
numbers = append(numbers, version[start:])
|
|
|
|
}
|
|
|
|
return numbers
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDigit(r rune) bool {
|
|
|
|
return r >= '0' && r <= '9'
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(x, y int) int {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
2022-09-23 16:33:55 +08:00
|
|
|
}
|
|
|
|
|
2022-10-03 17:35:39 +08:00
|
|
|
func GetSortedVersions(versions []string) []string {
|
|
|
|
sort.Slice(versions, func(i, j int) bool {
|
|
|
|
return CompareVersion(versions[i], versions[j])
|
|
|
|
})
|
|
|
|
return versions
|
|
|
|
}
|
|
|
|
|
2024-04-06 22:32:10 +08:00
|
|
|
func CopyFile(src, dst string) error {
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
if path.Base(src) != path.Base(dst) {
|
|
|
|
dst = path.Join(dst, path.Base(src))
|
|
|
|
}
|
2024-04-07 22:09:46 +08:00
|
|
|
target, err := os.OpenFile(dst+"_temp", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
2024-04-06 22:32:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-07 22:09:46 +08:00
|
|
|
defer target.Close()
|
2024-04-06 22:32:10 +08:00
|
|
|
|
2024-04-07 22:09:46 +08:00
|
|
|
if _, err = io.Copy(target, source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = os.Rename(dst+"_temp", dst); err != nil {
|
2024-04-06 22:32:10 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:35:39 +08:00
|
|
|
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-26 16:32:40 +08: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 16:57:23 +08:00
|
|
|
|
2023-05-13 12:20:29 +08:00
|
|
|
func RandStrAndNum(n int) string {
|
2023-11-01 13:49:30 +08:00
|
|
|
source := mathRand.NewSource(time.Now().UnixNano())
|
|
|
|
randGen := mathRand.New(source)
|
2023-05-13 12:20:29 +08:00
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
2023-11-01 13:49:30 +08:00
|
|
|
b := make([]byte, n)
|
2023-05-13 12:20:29 +08:00
|
|
|
for i := range b {
|
2023-11-01 13:49:30 +08:00
|
|
|
b[i] = charset[randGen.Intn(len(charset)-1)]
|
2023-05-13 12:20:29 +08:00
|
|
|
}
|
|
|
|
return (string(b))
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func ScanPort(port int) bool {
|
|
|
|
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
2022-09-27 16:57:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
return false
|
|
|
|
}
|
2022-09-29 18:16:56 +08:00
|
|
|
|
2023-04-12 11:22:29 +08:00
|
|
|
func ScanUDPPort(port int) bool {
|
|
|
|
ln, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port})
|
2023-04-01 00:51:25 +08:00
|
|
|
if err != nil {
|
2023-04-12 11:22:29 +08:00
|
|
|
return true
|
2023-04-01 00:51:25 +08:00
|
|
|
}
|
2023-04-12 11:22:29 +08:00
|
|
|
defer ln.Close()
|
2023-04-01 00:51:25 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-04 15:56:12 +08:00
|
|
|
func ScanPortWithProto(port int, proto string) bool {
|
|
|
|
if proto == "udp" {
|
|
|
|
return ScanUDPPort(port)
|
|
|
|
}
|
|
|
|
return ScanPort(port)
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:06:49 +08:00
|
|
|
func IsNum(s string) bool {
|
|
|
|
_, err := strconv.ParseFloat(s, 64)
|
|
|
|
return err == nil
|
|
|
|
}
|
2022-12-18 23:00:24 +08: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 18:00:19 +08:00
|
|
|
|
|
|
|
func LoadSizeUnit(value float64) string {
|
2023-09-15 15:28:15 +08:00
|
|
|
if value > 1048576 {
|
|
|
|
return fmt.Sprintf("%vM", value/1048576)
|
|
|
|
}
|
|
|
|
if value > 1024 {
|
|
|
|
return fmt.Sprintf("%vK", value/1024)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadSizeUnit2F(value float64) string {
|
2023-09-08 23:12:13 +08:00
|
|
|
if value > 1073741824 {
|
2023-09-14 10:54:17 +08:00
|
|
|
return fmt.Sprintf("%.2fG", value/1073741824)
|
2023-09-08 23:12:13 +08:00
|
|
|
}
|
2022-12-23 18:00:19 +08:00
|
|
|
if value > 1048576 {
|
2023-09-08 23:12:13 +08:00
|
|
|
return fmt.Sprintf("%.2fM", value/1048576)
|
2022-12-23 18:00:19 +08:00
|
|
|
}
|
|
|
|
if value > 1024 {
|
2023-09-08 23:12:13 +08:00
|
|
|
return fmt.Sprintf("%.2fK", value/1024)
|
2022-12-23 18:00:19 +08:00
|
|
|
}
|
2023-09-08 23:12:13 +08:00
|
|
|
return fmt.Sprintf("%.2f", value)
|
2022-12-23 18:00:19 +08:00
|
|
|
}
|
2023-05-04 12:00:37 +08:00
|
|
|
|
|
|
|
func LoadTimeZone() string {
|
|
|
|
loc := time.Now().Location()
|
|
|
|
if _, err := time.LoadLocation(loc.String()); err != nil {
|
|
|
|
return "Asia/Shanghai"
|
|
|
|
}
|
|
|
|
return loc.String()
|
|
|
|
}
|
2023-05-31 14:03:04 +08:00
|
|
|
func LoadTimeZoneByCmd() string {
|
|
|
|
loc := time.Now().Location().String()
|
|
|
|
if _, err := time.LoadLocation(loc); err != nil {
|
|
|
|
loc = "Asia/Shanghai"
|
|
|
|
}
|
|
|
|
std, err := cmd.Exec("timedatectl | grep 'Time zone'")
|
|
|
|
if err != nil {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
fields := strings.Fields(string(std))
|
|
|
|
if len(fields) != 5 {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
if _, err := time.LoadLocation(fields[2]); err != nil {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
return fields[2]
|
|
|
|
}
|
2023-09-07 15:16:11 +08:00
|
|
|
|
2023-11-02 18:36:55 +08:00
|
|
|
func IsValidDomain(domain string) bool {
|
2023-11-08 10:35:29 +08:00
|
|
|
pattern := `^([\w\p{Han}\-\*]{1,100}\.){1,10}([\w\p{Han}\-]{1,24}|[\w\p{Han}\-]{1,24}\.[\w\p{Han}\-]{1,24})(:\d{1,5})?$`
|
|
|
|
match, err := regexp.MatchString(pattern, domain)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-11-02 18:36:55 +08:00
|
|
|
return match
|
|
|
|
}
|
2023-12-28 16:49:38 +08:00
|
|
|
|
|
|
|
func ContainsChinese(text string) bool {
|
|
|
|
for _, char := range text {
|
|
|
|
if unicode.Is(unicode.Han, char) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func PunycodeEncode(text string) (string, error) {
|
|
|
|
encoder := idna.New()
|
|
|
|
ascii, err := encoder.ToASCII(text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ascii, nil
|
|
|
|
}
|