From 1d5e19cf96658e89fc88df037447ad359a1f961b Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 31 Aug 2022 15:31:28 +0100 Subject: [PATCH] Refactor and tie in of signal events --- server/certs.go | 6 ++++ server/client.go | 18 ++++++++++- {services/local_pairing => server}/events.go | 11 +++---- server/handlers.go | 13 +++++++- services/local_pairing/api.go | 29 ------------------ services/local_pairing/service.go | 32 -------------------- 6 files changed, 40 insertions(+), 69 deletions(-) rename {services/local_pairing => server}/events.go (63%) delete mode 100644 services/local_pairing/api.go delete mode 100644 services/local_pairing/service.go diff --git a/server/certs.go b/server/certs.go index 853a365f6..6add4b664 100644 --- a/server/certs.go +++ b/server/certs.go @@ -15,6 +15,8 @@ import ( "net" "net/url" "time" + + "github.com/status-im/status-go/signal" ) var globalCertificate *tls.Certificate = nil @@ -212,10 +214,14 @@ func getServerCert(URL *url.URL) (*x509.Certificate, error) { conn, err := tls.Dial("tcp", URL.Host, conf) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventConnectionError, Error: err}) return nil, err } defer conn.Close() + // No error on the dial out then the URL.Host is accessible + signal.SendLocalPairingEvent(Event{Type: EventConnectionSuccess}) + certs := conn.ConnectionState().PeerCertificates if len(certs) != 1 { return nil, fmt.Errorf("expected 1 TLS certificate, received '%d'", len(certs)) diff --git a/server/client.go b/server/client.go index 41ff3115c..84deb7e5c 100644 --- a/server/client.go +++ b/server/client.go @@ -16,6 +16,7 @@ import ( "github.com/btcsuite/btcutil/base58" "github.com/status-im/status-go/multiaccounts" + "github.com/status-im/status-go/signal" ) type PairingClient struct { @@ -107,13 +108,16 @@ func (c *PairingClient) sendAccountData() error { c.baseAddress.Path = pairingReceive resp, err := c.Post(c.baseAddress.String(), "application/octet-stream", bytes.NewBuffer(c.PayloadManager.ToSend())) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) return err } if resp.StatusCode != http.StatusOK { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) return fmt.Errorf("status not ok, received '%s'", resp.Status) } + signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess}) return nil } @@ -139,15 +143,24 @@ func (c *PairingClient) receiveAccountData() error { } if resp.StatusCode != http.StatusOK { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) return fmt.Errorf("status not ok, received '%s'", resp.Status) } payload, err := ioutil.ReadAll(resp.Body) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) return err } + signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess}) - return c.PayloadManager.Receive(payload) + err = c.PayloadManager.Receive(payload) + if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventProcessError, Error: err}) + return err + } + signal.SendLocalPairingEvent(Event{Type: EventProcessSuccess}) + return nil } func (c *PairingClient) getChallenge() error { @@ -174,6 +187,9 @@ func StartUpPairingClient(db *multiaccounts.Database, cs, configJSON string) err ccp := new(ConnectionParams) err = ccp.FromString(cs) + if err != nil { + return err + } c, err := NewPairingClient(ccp, &PairingPayloadManagerConfig{db, conf}) if err != nil { diff --git a/services/local_pairing/events.go b/server/events.go similarity index 63% rename from services/local_pairing/events.go rename to server/events.go index a66c84ffa..db113f509 100644 --- a/services/local_pairing/events.go +++ b/server/events.go @@ -1,4 +1,4 @@ -package local_pairing +package server // EventType type for event types. type EventType string @@ -12,14 +12,13 @@ const ( EventTransferSuccess EventType = "transfer-success" - EventDecryptionError EventType = "decryption-error" + EventProcessSuccess EventType = "process-success" - EventInstallationError EventType = "installation-error" - - EventSuccess EventType = "success" + EventProcessError EventType = "process-error" ) // Event is a type for transfer events. type Event struct { - Type EventType `json:"type"` + Type EventType `json:"type"` + Error error `json:"error,omitempty"` } diff --git a/server/handlers.go b/server/handlers.go index f74ed35c1..cb6c54f82 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -15,6 +15,7 @@ import ( "github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/identity/identicon" "github.com/status-im/status-go/protocol/images" + "github.com/status-im/status-go/signal" ) const ( @@ -150,27 +151,37 @@ func handleIPFS(downloader *ipfs.Downloader, logger *zap.Logger) http.HandlerFun } func handlePairingReceive(ps *PairingServer) http.HandlerFunc { + signal.SendLocalPairingEvent(Event{Type: EventConnectionSuccess}) + return func(w http.ResponseWriter, r *http.Request) { payload, err := ioutil.ReadAll(r.Body) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) ps.logger.Error("ioutil.ReadAll(r.Body)", zap.Error(err)) } + signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess}) err = ps.PayloadManager.Receive(payload) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventProcessError, Error: err}) ps.logger.Error("ps.PayloadManager.Receive(payload)", zap.Error(err)) } + signal.SendLocalPairingEvent(Event{Type: EventProcessSuccess}) } } func handlePairingSend(ps *PairingServer) http.HandlerFunc { - // TODO lock sending after one successful transfer + signal.SendLocalPairingEvent(Event{Type: EventConnectionSuccess}) + + // TODO lock sending after one successful transfer, perhaps perform the lock on the PayloadManager level return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/octet-stream") _, err := w.Write(ps.PayloadManager.ToSend()) if err != nil { + signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err}) ps.logger.Error("w.Write(ps.PayloadManager.ToSend())", zap.Error(err)) } + signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess}) } } diff --git a/services/local_pairing/api.go b/services/local_pairing/api.go deleted file mode 100644 index 12b4b351d..000000000 --- a/services/local_pairing/api.go +++ /dev/null @@ -1,29 +0,0 @@ -package local_pairing - -import ( - "time" - - "github.com/status-im/status-go/signal" -) - -func NewAPI() *API { - return &API{} -} - -type API struct {} - -func (a *API) StartSendingServer(password string) (string, error){ - - go func() { - time.Sleep(time.Second) - signal.SendLocalPairingEvent(Event{Type: EventConnectionSuccess}) - - time.Sleep(time.Second) - signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess}) - - time.Sleep(time.Second) - signal.SendLocalPairingEvent(Event{Type: EventSuccess}) - }() - - return password, nil -} \ No newline at end of file diff --git a/services/local_pairing/service.go b/services/local_pairing/service.go deleted file mode 100644 index 6ee93db4f..000000000 --- a/services/local_pairing/service.go +++ /dev/null @@ -1,32 +0,0 @@ -package local_pairing - -import ( - "github.com/ethereum/go-ethereum/p2p" - "github.com/ethereum/go-ethereum/rpc" -) - -type Service struct {} - -func (s *Service) Start() error { - return nil -} - -func (s *Service) Stop() error { - return nil -} - -func (s *Service) APIs() []rpc.API { - return []rpc.API{ - { - Namespace: "localPairing", - Version: "0.1.0", - Service: NewAPI(), - Public: true, - }, - } -} - -func (s *Service) Protocols() []p2p.Protocol { - return nil -} -