-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service for checking if there are new versions available (#…
- Loading branch information
1 parent
09ba88c
commit 30f8bd6
Showing
15 changed files
with
1,217 additions
and
2 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
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
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,99 @@ | ||
package updates | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-version" | ||
"go.uber.org/zap" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/status-im/status-go/services/ens" | ||
"github.com/status-im/status-go/signal" | ||
) | ||
|
||
func NewAPI(ensService *ens.Service) *API { | ||
return &API{ | ||
ensService: ensService, | ||
httpClient: &http.Client{Timeout: time.Minute}, | ||
} | ||
} | ||
|
||
type API struct { | ||
ensService *ens.Service | ||
httpClient *http.Client | ||
} | ||
|
||
func (api *API) Check(ctx context.Context, chainID uint64, ens string, currentVersion string) { | ||
go func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) | ||
defer cancel() | ||
|
||
current, err := version.NewVersion(currentVersion) | ||
if err != nil { | ||
log.Error("invalid current version", "err", err) | ||
return | ||
} | ||
|
||
uri, err := api.ensService.API().ResourceURL(ctx, chainID, ens) | ||
if err != nil || uri.Host == "" { | ||
log.Error("can't get obtain the updates content hash url", "ens", ens) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
url := uri.Scheme + "://" + uri.Host + uri.Path | ||
versionURL := url + "VERSION" | ||
response, err := api.httpClient.Get(versionURL) | ||
if err != nil { | ||
log.Error("can't get content", zap.String("any", versionURL)) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
defer response.Body.Close() | ||
if response.StatusCode != http.StatusOK { | ||
log.Error(fmt.Sprintf("version verification response status error: %v", response.StatusCode)) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
data, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
log.Error("version verification body err", "err", err) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
c := make(map[string]interface{}) | ||
err = json.Unmarshal(data, &c) | ||
if err != nil { | ||
log.Error("invalid json", "err", err) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
latestStr := "" | ||
switch c["version"].(type) { | ||
case string: | ||
latestStr = c["version"].(string) | ||
default: | ||
log.Error("invalid latest version", "val", c["version"]) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
latest, err := version.NewVersion(latestStr) | ||
if err != nil { | ||
log.Error("invalid latest version", "err", err) | ||
signal.SendUpdateAvailable(false, "", "") | ||
return | ||
} | ||
|
||
signal.SendUpdateAvailable(latest.GreaterThan(current), latestStr, url) | ||
}() | ||
} |
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,42 @@ | ||
package updates | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/p2p" | ||
ethRpc "github.com/ethereum/go-ethereum/rpc" | ||
"github.com/status-im/status-go/services/ens" | ||
) | ||
|
||
// NewService initializes service instance. | ||
func NewService(ensService *ens.Service) *Service { | ||
return &Service{ensService} | ||
} | ||
|
||
type Service struct { | ||
ensService *ens.Service | ||
} | ||
|
||
// Start a service. | ||
func (s *Service) Start() error { | ||
return nil | ||
} | ||
|
||
// Stop a service. | ||
func (s *Service) Stop() error { | ||
return nil | ||
} | ||
|
||
// APIs returns list of available RPC APIs. | ||
func (s *Service) APIs() []ethRpc.API { | ||
return []ethRpc.API{ | ||
{ | ||
Namespace: "updates", | ||
Version: "0.1.0", | ||
Service: NewAPI(s.ensService), | ||
}, | ||
} | ||
} | ||
|
||
// Protocols returns list of p2p protocols. | ||
func (s *Service) Protocols() []p2p.Protocol { | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.