aboutsummaryrefslogtreecommitdiffstats
path: root/handshake/send.go
blob: 95c8f48608968bcb5db2b06d6284250b63525234 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package handshake

import (
	"context"
	"fmt"
	"net"
	"time"

	"git.samanthony.xyz/hose/key"
	"git.samanthony.xyz/hose/util"
)

// send sends the local public box (encryption) key to a remote host.
func send(rhost string) error {
	// Load keys from disc.
	boxPubKey, sigPubKey, err := loadKeys()
	if err != nil {
		return err
	}
	// Send them to the remote host.
	return sendKeys(rhost, boxPubKey, sigPubKey)
}

func loadKeys() (key.BoxPublicKey, key.SigPublicKey, error) {
	boxPubKey, err := key.LoadBoxPublicKey()
	if err != nil {
		return key.BoxPublicKey{}, key.SigPublicKey{}, err
	}
	sigPubKey, err := key.LoadSigPublicKey()
	return boxPubKey, sigPubKey, err
}

func sendKeys(rhost string, boxPubKey key.BoxPublicKey, sigPubKey key.SigPublicKey) error {
	raddr := net.JoinHostPort(rhost, fmt.Sprintf("%d", port))
	util.Logf("connecting to %s...", raddr)
	conn, err := dialWithTimeout(network, raddr, timeout)
	if err != nil {
		return err
	}
	defer conn.Close()
	util.Logf("connected to %s", raddr)

	if _, err := conn.Write(boxPubKey[:]); err != nil {
		return err
	}
	if _, err := conn.Write(sigPubKey[:]); err != nil {
		return err
	}
	util.Logf("sent public keys to %s", rhost)

	return nil
}

func dialWithTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	for {
		select {
		case <-ctx.Done(): // timeout.
			return nil, fmt.Errorf("dial %s %s: connection refused", network, address)
		default:
		}
		conn, err := net.Dial(network, address)
		if err == nil {
			return conn, nil
		}
		time.Sleep(retryInterval)
	}
}