starter.go 2.98 KB
Newer Older
1
2
3
package sign

import (
Caro Axel's avatar
Caro Axel committed
4
5
	"crypto/rsa"
	"crypto/x509"
6
7
8
9
	"errors"
	"fmt"
	"strconv"

Caro Axel's avatar
Caro Axel committed
10
11
	"dfss"
	cAPI "dfss/dfssc/api"
12
	"dfss/dfssc/security"
Caro Axel's avatar
Caro Axel committed
13
	pAPI "dfss/dfssp/api"
14
15
16
	"dfss/dfssp/contract"
	"dfss/net"
	"golang.org/x/net/context"
Caro Axel's avatar
Caro Axel committed
17
	"google.golang.org/grpc"
18
19
20
21
22
23
24
)

// SignatureManager handles the signature of a contract.
type SignatureManager struct {
	auth      *security.AuthContainer
	localPort int
	contract  *contract.JSON
Caro Axel's avatar
Caro Axel committed
25
26
27
28
29
	platform  pAPI.PlatformClient
	peers     map[string]*cAPI.ClientClient
	cServer   *grpc.Server
	cert, ca  *x509.Certificate
	key       *rsa.PrivateKey
30
31
32
33
34
35
36
37
38
}

// NewSignatureManager populates a SignatureManager and connects to the platform.
func NewSignatureManager(fileCA, fileCert, fileKey, addrPort, passphrase string, port int, c *contract.JSON) (*SignatureManager, error) {
	m := &SignatureManager{
		auth:      security.NewAuthContainer(fileCA, fileCert, fileKey, addrPort, passphrase),
		localPort: port,
		contract:  c,
	}
Caro Axel's avatar
Caro Axel committed
39
40
	var err error
	m.ca, m.cert, m.key, err = m.auth.LoadFiles()
41
42
43
44
	if err != nil {
		return nil, err
	}

Caro Axel's avatar
Caro Axel committed
45
46
47
48
	m.cServer = m.GetServer()
	go func() { _ = net.Listen("0.0.0.0:"+strconv.Itoa(port), m.cServer) }()

	conn, err := net.Connect(m.auth.AddrPort, m.cert, m.key, m.ca)
49
50
51
52
	if err != nil {
		return nil, err
	}

Caro Axel's avatar
Caro Axel committed
53
	m.platform = pAPI.NewPlatformClient(conn)
54

Caro Axel's avatar
Caro Axel committed
55
	m.peers = make(map[string]*cAPI.ClientClient)
56
57
58
59
60
61
62
63
64
	for _, u := range c.Signers {
		m.peers[u.Email] = nil
	}

	return m, nil
}

// ConnectToPeers tries to fetch the list of users for this contract, and tries to establish a connection to each peer.
func (m *SignatureManager) ConnectToPeers() error {
Caro Axel's avatar
Caro Axel committed
65
	stream, err := m.platform.JoinSignature(context.Background(), &pAPI.JoinSignatureRequest{
66
67
68
69
70
71
72
73
74
75
76
77
78
		ContractUuid: m.contract.UUID,
		Port:         uint32(m.localPort),
	})
	if err != nil {
		return err
	}

	for {
		userConnected, err := stream.Recv()
		if err != nil {
			return err
		}
		errorCode := userConnected.GetErrorCode()
Caro Axel's avatar
Caro Axel committed
79
		if errorCode.Code != pAPI.ErrorCode_SUCCESS {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
			return errors.New(errorCode.Message)
		}
		ready, err := m.addPeer(userConnected.User)
		if err != nil {
			return err
		}
		if ready {
			break
		}
	}

	return nil
}

// addPeer stores a peer from the platform and tries to establish a connection to this peer.
Caro Axel's avatar
Caro Axel committed
95
func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
96
97
98
99
100
	if user == nil {
		err = errors.New("unexpected user format")
		return
	}

Caro Axel's avatar
Caro Axel committed
101
102
	addrPort := user.Ip + ":" + strconv.Itoa(int(user.Port))
	fmt.Println("Trying to connect with", user.Email, "/", addrPort)
103

Caro Axel's avatar
Caro Axel committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	conn, err := net.Connect(addrPort, m.cert, m.key, m.ca)
	if err != nil {
		return false, err
	}

	// Sending Hello message
	client := cAPI.NewClientClient(conn)
	m.peers[user.Email] = &client
	msg, err := client.Discover(context.Background(), &cAPI.Hello{Version: dfss.Version})
	if err != nil {
		return false, err
	}
	// Printing answer: application version
	fmt.Println("Recieved:", msg.Version)
118
119
120
121

	// Check if we have any other peer to connect to
	for _, u := range m.peers {
		if u == nil {
Caro Axel's avatar
Caro Axel committed
122
			return false, nil
123
124
125
		}
	}

Caro Axel's avatar
Caro Axel committed
126
	return true, nil
127
}