Skip to content

Commit

Permalink
feat: add service for checking if there are new versions available (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Jun 8, 2022
1 parent 09ba88c commit 30f8bd6
Show file tree
Hide file tree
Showing 15 changed files with 1,217 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.2.0
github.com/imdario/mergo v0.3.12
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-ds-sql v0.3.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
2 changes: 2 additions & 0 deletions node/get_status_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/status-im/status-go/services/status"
"github.com/status-im/status-go/services/stickers"
"github.com/status-im/status-go/services/subscriptions"
"github.com/status-im/status-go/services/updates"
"github.com/status-im/status-go/services/wakuext"
"github.com/status-im/status-go/services/wakuv2ext"
"github.com/status-im/status-go/services/wallet"
Expand Down Expand Up @@ -122,6 +123,7 @@ type StatusNode struct {
gifSrvc *gif.Service
stickersSrvc *stickers.Service
chatSrvc *chat.Service
updatesSrvc *updates.Service
}

// New makes new instance of StatusNode.
Expand Down
10 changes: 10 additions & 0 deletions node/status_node_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/status-im/status-go/services/status"
"github.com/status-im/status-go/services/stickers"
"github.com/status-im/status-go/services/subscriptions"
"github.com/status-im/status-go/services/updates"
"github.com/status-im/status-go/services/wakuext"
"github.com/status-im/status-go/services/wakuv2ext"
"github.com/status-im/status-go/services/wallet"
Expand Down Expand Up @@ -75,6 +76,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig) error {
services = append(services, b.statusPublicService())
services = append(services, b.ensService())
services = append(services, b.stickersService(accDB))
services = append(services, b.updatesService())
services = appendIf(config.EnableNTPSync, services, b.timeSource())
services = appendIf(b.appDB != nil && b.multiaccountsDB != nil, services, b.accountsService(accountsFeed, accDB))
services = appendIf(config.BrowsersConfig.Enabled, services, b.browsersService())
Expand Down Expand Up @@ -397,6 +399,14 @@ func (b *StatusNode) stickersService(accountDB *accounts.Database) *stickers.Ser
return b.stickersSrvc
}

func (b *StatusNode) updatesService() *updates.Service {
if b.updatesSrvc == nil {
b.updatesSrvc = updates.NewService(b.ensService())
}

return b.updatesSrvc
}

func (b *StatusNode) gifService(accountsDB *accounts.Database) *gif.Service {
if b.gifSrvc == nil {
b.gifSrvc = gif.NewService(accountsDB)
Expand Down
15 changes: 13 additions & 2 deletions services/ens/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import (

// NewService initializes service instance.
func NewService(rpcClient *rpc.Client, accountsManager *account.GethManager, rpcFiltersSrvc *rpcfilters.Service, config *params.NodeConfig) *Service {
return &Service{rpcClient, accountsManager, rpcFiltersSrvc, config}
return &Service{
rpcClient,
accountsManager,
rpcFiltersSrvc,
config,
NewAPI(rpcClient, accountsManager, rpcFiltersSrvc, config),
}
}

// Service is a browsers service.
Expand All @@ -20,6 +26,7 @@ type Service struct {
accountsManager *account.GethManager
rpcFiltersSrvc *rpcfilters.Service
config *params.NodeConfig
api *API
}

// Start a service.
Expand All @@ -32,13 +39,17 @@ func (s *Service) Stop() error {
return nil
}

func (s *Service) API() *API {
return s.api
}

// APIs returns list of available RPC APIs.
func (s *Service) APIs() []ethRpc.API {
return []ethRpc.API{
{
Namespace: "ens",
Version: "0.1.0",
Service: NewAPI(s.rpcClient, s.accountsManager, s.rpcFiltersSrvc, s.config),
Service: s.api,
},
}
}
Expand Down
99 changes: 99 additions & 0 deletions services/updates/api.go
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)
}()
}
42 changes: 42 additions & 0 deletions services/updates/service.go
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
}
13 changes: 13 additions & 0 deletions signal/events_shhext.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const (

// EventMailserverNotWorking is triggered when the mailserver has failed to connect or failed to respond to requests
EventMailserverNotWorking = "mailserver.not.working"

// EventUpdateAvailable is triggered after a update verification is performed
EventUpdateAvailable = "update.available"
)

// EnvelopeSignal includes hash of the envelope.
Expand All @@ -81,6 +84,12 @@ type HistoryMessagesSignal struct {
ErrorMsg string `json:"errorMessage,omitempty"`
}

type UpdateAvailableSignal struct {
Available bool `json:"available"`
Version string `json:"version"`
URL string `json:"url"`
}

// DecryptMessageFailedSignal holds the sender of the message that could not be decrypted
type DecryptMessageFailedSignal struct {
Sender string `json:"sender"`
Expand Down Expand Up @@ -154,6 +163,10 @@ func SendHistoricMessagesRequestCompleted(requestID string) {
send(EventHistoryRequestCompleted, HistoryMessagesSignal{RequestID: requestID})
}

func SendUpdateAvailable(available bool, latestVersion string, url string) {
send(EventUpdateAvailable, UpdateAvailableSignal{Available: available, Version: latestVersion, URL: url})
}

// SendMailServerRequestCompleted triggered when mail server response has been received
func SendMailServerRequestCompleted(requestID types.Hash, lastEnvelopeHash types.Hash, cursor []byte, err error) {
errorMsg := ""
Expand Down
13 changes: 13 additions & 0 deletions vendor/github.com/hashicorp/go-version/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 30f8bd6

Please sign in to comment.