2020-05-15 09:39:16 +00:00
|
|
|
package cwversion
|
|
|
|
|
|
|
|
import (
|
2020-09-01 12:32:45 +00:00
|
|
|
"encoding/json"
|
2020-05-15 09:39:16 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-09-01 12:32:45 +00:00
|
|
|
"net/http"
|
2022-03-14 10:29:34 +00:00
|
|
|
"runtime"
|
2021-03-11 10:18:09 +00:00
|
|
|
"strings"
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2023-05-23 08:52:47 +00:00
|
|
|
goversion "github.com/hashicorp/go-version"
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/version"
|
2020-05-15 09:39:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-14 08:16:38 +00:00
|
|
|
Codename string // = "SoumSoum"
|
|
|
|
System = runtime.GOOS // = "linux"
|
|
|
|
Libre2 = "WebAssembly"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-12-14 15:34:51 +00:00
|
|
|
Constraint_parser = ">= 1.0, <= 3.0"
|
|
|
|
Constraint_scenario = ">= 1.0, <= 3.0"
|
2020-05-15 09:39:16 +00:00
|
|
|
Constraint_api = "v1"
|
|
|
|
Constraint_acquis = ">= 1.0, < 2.0"
|
|
|
|
)
|
|
|
|
|
2023-12-14 08:16:38 +00:00
|
|
|
func versionWithTag() string {
|
|
|
|
ret := version.Version
|
|
|
|
|
|
|
|
if !strings.HasSuffix(ret, version.Tag) {
|
|
|
|
ret += fmt.Sprintf("-%s", version.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func ShowStr() string {
|
2023-12-14 08:16:38 +00:00
|
|
|
ret := fmt.Sprintf("version: %s", versionWithTag())
|
2020-11-30 09:37:17 +00:00
|
|
|
ret += fmt.Sprintf("Codename: %s\n", Codename)
|
2023-05-23 08:52:47 +00:00
|
|
|
ret += fmt.Sprintf("BuildDate: %s\n", version.BuildDate)
|
|
|
|
ret += fmt.Sprintf("GoVersion: %s\n", version.GoVersion)
|
2021-04-23 13:23:46 +00:00
|
|
|
ret += fmt.Sprintf("Platform: %s\n", System)
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func Show() {
|
2023-12-14 08:16:38 +00:00
|
|
|
log.Printf("version: %s", versionWithTag())
|
2020-05-15 09:39:16 +00:00
|
|
|
log.Printf("Codename: %s", Codename)
|
2023-05-23 08:52:47 +00:00
|
|
|
log.Printf("BuildDate: %s", version.BuildDate)
|
|
|
|
log.Printf("GoVersion: %s", version.GoVersion)
|
2022-02-04 12:02:45 +00:00
|
|
|
log.Printf("Platform: %s\n", System)
|
2023-06-06 13:46:25 +00:00
|
|
|
log.Printf("libre2: %s\n", Libre2)
|
2020-05-15 09:39:16 +00:00
|
|
|
log.Printf("Constraint_parser: %s", Constraint_parser)
|
|
|
|
log.Printf("Constraint_scenario: %s", Constraint_scenario)
|
|
|
|
log.Printf("Constraint_api: %s", Constraint_api)
|
|
|
|
log.Printf("Constraint_acquis: %s", Constraint_acquis)
|
|
|
|
}
|
|
|
|
|
|
|
|
func VersionStr() string {
|
2023-05-23 08:52:47 +00:00
|
|
|
return fmt.Sprintf("%s-%s-%s", version.Version, System, version.Tag)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 10:18:09 +00:00
|
|
|
func VersionStrip() string {
|
2023-05-23 08:52:47 +00:00
|
|
|
version := strings.Split(version.Version, "~")
|
2023-05-16 23:13:55 +00:00
|
|
|
version = strings.Split(version[0], "-")
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2021-03-11 10:18:09 +00:00
|
|
|
return version[0]
|
|
|
|
}
|
|
|
|
|
2023-05-23 08:52:47 +00:00
|
|
|
func Satisfies(strvers string, constraint string) (bool, error) {
|
|
|
|
vers, err := goversion.NewVersion(strvers)
|
2020-05-15 09:39:16 +00:00
|
|
|
if err != nil {
|
2020-05-19 19:31:06 +00:00
|
|
|
return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2023-05-23 08:52:47 +00:00
|
|
|
constraints, err := goversion.NewConstraint(constraint)
|
2020-05-15 09:39:16 +00:00
|
|
|
if err != nil {
|
2020-05-19 19:31:06 +00:00
|
|
|
return false, fmt.Errorf("failed to parse constraint '%s'", constraint)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
if !constraints.Check(vers) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2020-09-01 12:32:45 +00:00
|
|
|
|
|
|
|
// Latest return latest crowdsec version based on github
|
|
|
|
func Latest() (string, error) {
|
|
|
|
latest := make(map[string]interface{})
|
|
|
|
|
2021-09-02 13:17:37 +00:00
|
|
|
resp, err := http.Get("https://version.crowdsec.net/latest")
|
2020-09-01 12:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&latest)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-12-14 08:16:38 +00:00
|
|
|
|
2020-09-01 12:32:45 +00:00
|
|
|
if _, ok := latest["name"]; !ok {
|
2020-11-30 09:37:17 +00:00
|
|
|
return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest)
|
2020-09-01 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return latest["name"].(string), nil
|
|
|
|
}
|