-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
69 lines (61 loc) · 1.76 KB
/
conn.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package protocol
import (
"crypto/tls"
"fmt"
"github.com/hashicorp/nodeenrollment"
"google.golang.org/protobuf/types/known/structpb"
)
// Conn embeds a *tls.Conn and allows us to add extra bits into it
type Conn struct {
*tls.Conn
clientNextProtos []string
clientState *structpb.Struct
}
// NewConn constructs a conn from a base TLS connection and possibly client next
// protos.
//
// Supported options: WithExtraAlpnProtos (used to set clientNextProtos),
// WithState (storing client state information)
func NewConn(base *tls.Conn, opt ...nodeenrollment.Option) (*Conn, error) {
const op = "nodeenrollment.protocol.NewConn"
opts, err := nodeenrollment.GetOpts(opt...)
if err != nil {
return nil, fmt.Errorf("(%s) error parsing options: %w", op, err)
}
conn := &Conn{
Conn: base,
clientState: opts.WithState,
}
switch {
case opts.WithExtraAlpnProtos == nil:
case len(opts.WithExtraAlpnProtos) == 0:
conn.clientNextProtos = make([]string, 0)
default:
conn.clientNextProtos = make([]string, len(opts.WithExtraAlpnProtos))
copy(conn.clientNextProtos, opts.WithExtraAlpnProtos)
}
return conn, nil
}
// ClientNextProtos returns the value of NextProtos originally presented by the
// client at connection time
func (c *Conn) ClientNextProtos() []string {
switch {
case c == nil:
return nil
case c.clientNextProtos == nil:
return nil
case len(c.clientNextProtos) == 0:
return []string{}
default:
ret := make([]string, len(c.clientNextProtos))
copy(ret, c.clientNextProtos)
return ret
}
}
// ClientState returns the value of the state embedded into the original client
// request, which may be nil
func (c *Conn) ClientState() *structpb.Struct {
return c.clientState
}