-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clash api: download clash-dashboard if external-ui directory is empty
- Loading branch information
1 parent
f98cfdf
commit eb57cbc
Showing
10 changed files
with
380 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package badversion | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
F "github.com/sagernet/sing/common/format" | ||
) | ||
|
||
type Version struct { | ||
Major int | ||
Minor int | ||
Patch int | ||
PreReleaseIdentifier string | ||
PreReleaseVersion int | ||
} | ||
|
||
func (v Version) After(anotherVersion Version) bool { | ||
if v.Major > anotherVersion.Major { | ||
return true | ||
} else if v.Major < anotherVersion.Major { | ||
return false | ||
} | ||
if v.Minor > anotherVersion.Minor { | ||
return true | ||
} else if v.Minor < anotherVersion.Minor { | ||
return false | ||
} | ||
if v.Patch > anotherVersion.Patch { | ||
return true | ||
} else if v.Patch < anotherVersion.Patch { | ||
return false | ||
} | ||
if v.PreReleaseIdentifier == "" && anotherVersion.PreReleaseIdentifier != "" { | ||
return true | ||
} else if v.PreReleaseIdentifier != "" && anotherVersion.PreReleaseIdentifier == "" { | ||
return false | ||
} | ||
if v.PreReleaseIdentifier != "" && anotherVersion.PreReleaseIdentifier != "" { | ||
if v.PreReleaseIdentifier == "beta" && anotherVersion.PreReleaseIdentifier == "alpha" { | ||
return true | ||
} else if v.PreReleaseIdentifier == "alpha" && anotherVersion.PreReleaseIdentifier == "beta" { | ||
return false | ||
} | ||
if v.PreReleaseVersion > anotherVersion.PreReleaseVersion { | ||
return true | ||
} else if v.PreReleaseVersion < anotherVersion.PreReleaseVersion { | ||
return false | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (v Version) String() string { | ||
version := F.ToString(v.Major, ".", v.Minor, ".", v.Patch) | ||
if v.PreReleaseIdentifier != "" { | ||
version = F.ToString(version, "-", v.PreReleaseIdentifier, ".", v.PreReleaseVersion) | ||
} | ||
return version | ||
} | ||
|
||
func (v Version) BadString() string { | ||
version := F.ToString(v.Major, ".", v.Minor) | ||
if v.Patch > 0 { | ||
version = F.ToString(version, ".", v.Patch) | ||
} | ||
if v.PreReleaseIdentifier != "" { | ||
version = F.ToString(version, "-", v.PreReleaseIdentifier) | ||
if v.PreReleaseVersion > 0 { | ||
version = F.ToString(version, v.PreReleaseVersion) | ||
} | ||
} | ||
return version | ||
} | ||
|
||
func Parse(versionName string) (version Version) { | ||
if strings.HasPrefix(versionName, "v") { | ||
versionName = versionName[1:] | ||
} | ||
if strings.Contains(versionName, "-") { | ||
parts := strings.Split(versionName, "-") | ||
versionName = parts[0] | ||
identifier := parts[1] | ||
if strings.Contains(identifier, ".") { | ||
identifierParts := strings.Split(identifier, ".") | ||
version.PreReleaseIdentifier = identifierParts[0] | ||
if len(identifierParts) >= 2 { | ||
version.PreReleaseVersion, _ = strconv.Atoi(identifierParts[1]) | ||
} | ||
} else { | ||
if strings.HasPrefix(identifier, "alpha") { | ||
version.PreReleaseIdentifier = "alpha" | ||
version.PreReleaseVersion, _ = strconv.Atoi(identifier[5:]) | ||
} else if strings.HasPrefix(identifier, "beta") { | ||
version.PreReleaseIdentifier = "beta" | ||
version.PreReleaseVersion, _ = strconv.Atoi(identifier[4:]) | ||
} else { | ||
version.PreReleaseIdentifier = identifier | ||
} | ||
} | ||
} | ||
versionElements := strings.Split(versionName, ".") | ||
versionLen := len(versionElements) | ||
if versionLen >= 1 { | ||
version.Major, _ = strconv.Atoi(versionElements[0]) | ||
} | ||
if versionLen >= 2 { | ||
version.Minor, _ = strconv.Atoi(versionElements[1]) | ||
} | ||
if versionLen >= 3 { | ||
version.Patch, _ = strconv.Atoi(versionElements[2]) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package badversion | ||
|
||
import "github.com/sagernet/sing-box/common/json" | ||
|
||
func (v Version) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(v.String()) | ||
} | ||
|
||
func (v *Version) UnmarshalJSON(data []byte) error { | ||
var version string | ||
err := json.Unmarshal(data, &version) | ||
if err != nil { | ||
return err | ||
} | ||
*v = Parse(version) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package badversion | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCompareVersion(t *testing.T) { | ||
t.Parallel() | ||
require.Equal(t, "1.3.0-beta.1", Parse("v1.3.0-beta1").String()) | ||
require.Equal(t, "1.3-beta1", Parse("v1.3.0-beta.1").BadString()) | ||
require.True(t, Parse("1.3.0").After(Parse("1.3-beta1"))) | ||
require.True(t, Parse("1.3.0").After(Parse("1.3.0-beta1"))) | ||
require.True(t, Parse("1.3.0-beta1").After(Parse("1.3.0-alpha1"))) | ||
require.True(t, Parse("1.3.1").After(Parse("1.3.0"))) | ||
require.True(t, Parse("1.4").After(Parse("1.3"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.