Refactor and tie in of signal events

This commit is contained in:
Samuel Hawksby-Robinson 2022-08-31 15:31:28 +01:00
parent 67088b1ab1
commit 1d5e19cf96
6 changed files with 40 additions and 69 deletions

View File

@ -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))

View File

@ -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 {

View File

@ -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"`
}

View File

@ -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})
}
}

View File

@ -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
}

View File

@ -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
}