2022-06-15 14:49:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-15 15:36:17 +00:00
|
|
|
"crypto/ecdsa"
|
2022-06-15 14:49:31 +00:00
|
|
|
"crypto/tls"
|
2022-06-15 15:36:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PairingServer struct {
|
|
|
|
Server
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
pk *ecdsa.PrivateKey
|
|
|
|
mode Mode
|
|
|
|
payload *PayloadManager
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2022-06-15 15:36:17 +00:00
|
|
|
PK *ecdsa.PrivateKey
|
2022-06-15 14:49:31 +00:00
|
|
|
Cert *tls.Certificate
|
|
|
|
Hostname string
|
2022-06-15 15:36:17 +00:00
|
|
|
Mode Mode
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 23:08:57 +00:00
|
|
|
// NewPairingServer returns a *PairingServer init from the given *Config
|
2022-06-10 15:32:15 +00:00
|
|
|
func NewPairingServer(config *Config) (*PairingServer, error) {
|
2022-06-10 23:03:16 +00:00
|
|
|
pm, err := NewPayloadManager(config.PK)
|
2022-06-10 15:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
return &PairingServer{Server: NewServer(
|
|
|
|
config.Cert,
|
|
|
|
config.Hostname,
|
2022-06-15 15:36:17 +00:00
|
|
|
),
|
2022-06-10 15:32:15 +00:00
|
|
|
pk: config.PK,
|
|
|
|
mode: config.Mode,
|
2022-06-10 23:03:16 +00:00
|
|
|
payload: pm}, nil
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeConnectionParams generates a *ConnectionParams based on the Server's current state
|
|
|
|
func (s *PairingServer) MakeConnectionParams() (*ConnectionParams, error) {
|
|
|
|
switch {
|
|
|
|
case s.cert == nil:
|
|
|
|
return nil, fmt.Errorf("server has no cert set")
|
|
|
|
case s.cert.Leaf == nil:
|
|
|
|
return nil, fmt.Errorf("server cert has no Leaf set")
|
|
|
|
case s.cert.Leaf.NotBefore.IsZero():
|
|
|
|
return nil, fmt.Errorf("server cert Leaf has a zero value NotBefore")
|
|
|
|
}
|
|
|
|
|
|
|
|
netIP := net.ParseIP(s.hostname)
|
|
|
|
if netIP == nil {
|
|
|
|
return nil, fmt.Errorf("invalid ip address given '%s'", s.hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
netIP4 := netIP.To4()
|
|
|
|
if netIP4 != nil {
|
|
|
|
netIP = netIP4
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.port == 0 {
|
|
|
|
return nil, fmt.Errorf("port is 0, listener is not yet set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConnectionParams(netIP, s.port, s.pk, s.cert.Leaf.NotBefore, s.mode), nil
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
func (s *PairingServer) MountPayload(data []byte) error {
|
|
|
|
return s.payload.Mount(data)
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PairingServer) StartPairing() error {
|
|
|
|
switch s.mode {
|
|
|
|
case Receiving:
|
|
|
|
return s.startReceivingAccountData()
|
|
|
|
case Sending:
|
|
|
|
return s.startSendingAccountData()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid server mode '%d'", s.mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PairingServer) startReceivingAccountData() error {
|
|
|
|
s.SetHandlers(HandlerPatternMap{pairingReceive: handlePairingReceive(s)})
|
|
|
|
return s.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PairingServer) startSendingAccountData() error {
|
|
|
|
s.SetHandlers(HandlerPatternMap{pairingSend: handlePairingSend(s)})
|
|
|
|
return s.Start()
|
|
|
|
}
|