2018-08-17 02:15:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-04-21 08:59:30 +08:00
|
|
|
func versionPrint() error {
|
|
|
|
logger.Infof("starting Commento %s", version)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-17 02:15:59 +08:00
|
|
|
func versionCheckStart() error {
|
|
|
|
go func() {
|
|
|
|
printedError := false
|
|
|
|
errorCount := 0
|
2018-12-29 00:47:54 +08:00
|
|
|
latestSeen := ""
|
2018-08-17 02:15:59 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
|
|
|
|
data := url.Values{
|
|
|
|
"version": {version},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Post("https://version.commento.io/api/check", "application/x-www-form-urlencoded", bytes.NewBufferString(data.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
errorCount++
|
|
|
|
// print the error only once; we don't want to spam the logs with this
|
|
|
|
// every five minutes
|
|
|
|
if !printedError && errorCount > 5 {
|
|
|
|
logger.Errorf("error checking version: %v", err)
|
|
|
|
printedError = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorCount++
|
|
|
|
if !printedError && errorCount > 5 {
|
|
|
|
logger.Errorf("error reading body: %s", err)
|
|
|
|
printedError = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Latest string `json:"latest"`
|
|
|
|
NewUpdate bool `json:"newUpdate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r := response{}
|
|
|
|
json.Unmarshal(body, &r)
|
|
|
|
if r.Success == false {
|
|
|
|
errorCount++
|
|
|
|
if !printedError && errorCount > 5 {
|
|
|
|
logger.Errorf("error checking version: %s", r.Message)
|
|
|
|
printedError = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-29 00:47:54 +08:00
|
|
|
if r.NewUpdate && r.Latest != latestSeen {
|
2018-08-17 02:15:59 +08:00
|
|
|
logger.Infof("New update available! Latest version: %s", r.Latest)
|
2018-12-29 00:47:54 +08:00
|
|
|
latestSeen = r.Latest
|
2018-08-17 02:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errorCount = 0
|
|
|
|
printedError = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|