-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
60 lines (51 loc) · 1.54 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
package justworks
import (
"context"
"fmt"
"github.com/pion/dtls/v2"
"github.com/plgd-dev/device/client/core/otm/just-works/cipher"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/schema/doxm"
kitNet "github.com/plgd-dev/kit/v2/net"
)
type Client struct {
dialDTLS DialDTLS
}
type DialDTLS = func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error)
type OptionFunc func(Client) Client
func WithDialDTLS(dial DialDTLS) OptionFunc {
return func(cfg Client) Client {
if dial != nil {
cfg.dialDTLS = dial
}
return cfg
}
}
func NewClient(opts ...OptionFunc) *Client {
c := Client{
dialDTLS: coap.DialUDPSecure,
}
for _, o := range opts {
c = o(c)
}
return &c
}
func (*Client) Type() doxm.OwnerTransferMethod {
return doxm.JustWorks
}
func (c *Client) Dial(ctx context.Context, addr kitNet.Addr, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
if schema.Scheme(addr.GetScheme()) == schema.UDPSecureScheme {
tlsConfig := dtls.Config{
CustomCipherSuites: func() []dtls.CipherSuite {
return []dtls.CipherSuite{cipher.NewTLSAecdhAes128Sha256(dtls.CipherSuiteID(0xff00))}
},
CipherSuites: []dtls.CipherSuiteID{},
ConnectContextMaker: func() (context.Context, func()) {
return context.WithCancel(ctx)
},
}
return c.dialDTLS(ctx, addr.String(), &tlsConfig, opts...)
}
return nil, fmt.Errorf("cannot dial to url %v: scheme %v not supported", addr.URL(), addr.GetScheme())
}