2022-05-03 14:50:40 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-10 15:32:15 +00:00
|
|
|
"bytes"
|
2022-05-10 09:34:53 +00:00
|
|
|
"crypto/ecdsa"
|
2022-05-03 14:50:40 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2022-06-10 15:32:15 +00:00
|
|
|
"io/ioutil"
|
2022-05-03 14:50:40 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
type PairingClient struct {
|
2022-05-03 14:50:40 +00:00
|
|
|
*http.Client
|
|
|
|
|
|
|
|
baseAddress *url.URL
|
2022-05-10 09:34:53 +00:00
|
|
|
certPEM []byte
|
|
|
|
privateKey *ecdsa.PrivateKey
|
2022-06-10 15:32:15 +00:00
|
|
|
serverMode Mode
|
|
|
|
payload *PayloadManager
|
2022-05-03 14:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
func NewPairingClient(c *ConnectionParams) (*PairingClient, error) {
|
2022-05-03 14:50:40 +00:00
|
|
|
u, certPem, err := c.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := rootCAs.AppendCertsFromPEM(certPem); !ok {
|
|
|
|
return nil, fmt.Errorf("failed to append certPem to rootCAs")
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
InsecureSkipVerify: false, // MUST BE FALSE
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
pm, err := NewPayloadManager(c.privateKey)
|
2022-06-10 15:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PairingClient{
|
2022-05-03 14:50:40 +00:00
|
|
|
Client: &http.Client{Transport: tr},
|
|
|
|
baseAddress: u,
|
|
|
|
certPEM: certPem,
|
2022-05-10 09:34:53 +00:00
|
|
|
privateKey: c.privateKey,
|
2022-06-10 15:32:15 +00:00
|
|
|
serverMode: c.serverMode,
|
2022-06-10 23:03:16 +00:00
|
|
|
payload: pm,
|
2022-05-03 14:50:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
func (c *PairingClient) MountPayload(data []byte) error {
|
|
|
|
return c.payload.Mount(data)
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PairingClient) PairAccount() error {
|
|
|
|
switch c.serverMode {
|
|
|
|
case Receiving:
|
|
|
|
return c.sendAccountData()
|
|
|
|
case Sending:
|
|
|
|
return c.receiveAccountData()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognised server mode '%d'", c.serverMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PairingClient) sendAccountData() error {
|
|
|
|
c.baseAddress.Path = pairingReceive
|
|
|
|
_, err := c.Post(c.baseAddress.String(), "application/octet-stream", bytes.NewBuffer(c.payload.ToSend()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PairingClient) receiveAccountData() error {
|
|
|
|
c.baseAddress.Path = pairingSend
|
|
|
|
resp, err := c.Get(c.baseAddress.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
payload, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
return c.payload.Receive(payload)
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|