-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.go
66 lines (60 loc) · 1.42 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"log"
"os"
"time"
piondtls "github.com/pion/dtls/v2"
"github.com/plgd-dev/go-coap/v2/dtls"
"github.com/plgd-dev/go-coap/v2/examples/dtls/pki"
)
func main() {
config, err := createClientConfig(context.Background())
if err != nil {
log.Fatalln(err)
return
}
co, err := dtls.Dial("localhost:5688", config)
if err != nil {
log.Fatalf("Error dialing: %v", err)
}
path := "/a"
if len(os.Args) > 1 {
path = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := co.Get(ctx, path)
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
log.Printf("Response payload: %+v", resp)
}
func createClientConfig(ctx context.Context) (*piondtls.Config, error) {
// root cert
ca, rootBytes, _, caPriv, err := pki.GenerateCA()
if err != nil {
return nil, err
}
// client cert
certBytes, keyBytes, err := pki.GenerateCertificate(ca, caPriv, "client@test.com")
if err != nil {
return nil, err
}
certificate, err := pki.LoadKeyAndCertificate(keyBytes, certBytes)
if err != nil {
return nil, err
}
// cert pool
certPool, err := pki.LoadCertPool(rootBytes)
if err != nil {
return nil, err
}
return &piondtls.Config{
Certificates: []tls.Certificate{*certificate},
ExtendedMasterSecret: piondtls.RequireExtendedMasterSecret,
RootCAs: certPool,
InsecureSkipVerify: true,
}, nil
}