-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
69 lines (57 loc) · 1.56 KB
/
client.go
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
// SPDX-FileCopyrightText: 2021 The Secretstream Authors
//
// SPDX-License-Identifier: MIT
package secretstream // import "github.com/ssbc/go-secretstream"
import (
"fmt"
"net"
"time"
"github.com/ssbc/go-secretstream/boxstream"
"github.com/ssbc/go-secretstream/secrethandshake"
"github.com/ssbc/go-netwrap"
)
// Client can dial secret-handshake server endpoints
type Client struct {
appKey []byte
kp secrethandshake.EdKeyPair
}
// NewClient creates a new Client with the passed keyPair and appKey
func NewClient(kp secrethandshake.EdKeyPair, appKey []byte) (*Client, error) {
// TODO: consistancy check?!..
return &Client{
appKey: appKey,
kp: kp,
}, nil
}
// ConnWrapper returns a connection wrapper for the client.
func (c *Client) ConnWrapper(pubKey []byte) netwrap.ConnWrapper {
return func(conn net.Conn) (net.Conn, error) {
state, err := secrethandshake.NewClientState(c.appKey, c.kp, pubKey)
if err != nil {
return nil, err
}
errc := make(chan error)
go func() {
errc <- secrethandshake.Client(state, conn)
close(errc)
}()
select {
case err := <-errc:
if err != nil {
return nil, err
}
case <-time.After(30 * time.Second):
return nil, fmt.Errorf("secretstream: handshake timeout")
}
enKey, enNonce := state.GetBoxstreamEncKeys()
deKey, deNonce := state.GetBoxstreamDecKeys()
boxed := &Conn{
boxer: boxstream.NewBoxer(conn, &enNonce, &enKey),
unboxer: boxstream.NewUnboxer(conn, &deNonce, &deKey),
conn: conn,
local: c.kp.Public[:],
remote: state.Remote(),
}
return boxed, nil
}
}