mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-18 21:24:37 +03:00
Some checks failed
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
https://github.com/XTLS/Xray-core/pull/4968#issuecomment-3146592323
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package version
|
|
|
|
import (
|
|
"context"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Version struct {
|
|
config *Config
|
|
ctx context.Context
|
|
}
|
|
|
|
func New(ctx context.Context, config *Config) (*Version, error) {
|
|
if config.MinVersion != "" {
|
|
result, err := compareVersions(config.MinVersion, config.CoreVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result > 0 {
|
|
return nil, errors.New("this config must be run on version ", config.MinVersion, " or higher")
|
|
}
|
|
}
|
|
if config.MaxVersion != "" {
|
|
result, err := compareVersions(config.MaxVersion, config.CoreVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result < 0 {
|
|
return nil, errors.New("this config should be run on version ", config.MaxVersion, " or lower")
|
|
}
|
|
}
|
|
return &Version{config: config, ctx: ctx}, nil
|
|
}
|
|
|
|
func compareVersions(v1, v2 string) (int, error) {
|
|
// Split version strings into components
|
|
v1Parts := strings.Split(v1, ".")
|
|
v2Parts := strings.Split(v2, ".")
|
|
|
|
// Pad shorter versions with zeros
|
|
for len(v1Parts) < len(v2Parts) {
|
|
v1Parts = append(v1Parts, "0")
|
|
}
|
|
for len(v2Parts) < len(v1Parts) {
|
|
v2Parts = append(v2Parts, "0")
|
|
}
|
|
|
|
// Compare each part
|
|
for i := 0; i < len(v1Parts); i++ {
|
|
// Convert parts to integers
|
|
n1, err := strconv.Atoi(v1Parts[i])
|
|
if err != nil {
|
|
return 0, errors.New("invalid version component ", v1Parts[i], " in ", v1)
|
|
}
|
|
n2, err := strconv.Atoi(v2Parts[i])
|
|
if err != nil {
|
|
return 0, errors.New("invalid version component ", v2Parts[i], " in ", v2)
|
|
}
|
|
|
|
if n1 < n2 {
|
|
return -1, nil // v1 < v2
|
|
}
|
|
if n1 > n2 {
|
|
return 1, nil // v1 > v2
|
|
}
|
|
}
|
|
return 0, nil // v1 == v2
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
return New(ctx, config.(*Config))
|
|
}))
|
|
}
|