-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (41 loc) · 1.27 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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
// Package main implements a DTLS client using self-signed certificates.
package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"time"
"github.com/adrian38/dtls/v2"
"github.com/adrian38/dtls/v2/examples/util"
"github.com/adrian38/dtls/v2/pkg/crypto/selfsign"
)
func main() {
// Prepare the IP to connect to
addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4444}
// Generate a certificate and private key to secure the connection
certificate, genErr := selfsign.GenerateSelfSigned()
util.Check(genErr)
//
// Everything below is the pion-DTLS API! Thanks for using it ❤️.
//
// Prepare the configuration of the DTLS connection
config := &dtls.Config{
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: true,
ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
}
// Connect to a DTLS server
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
dtlsConn, err := dtls.DialWithContext(ctx, "udp", addr, config)
util.Check(err)
defer func() {
util.Check(dtlsConn.Close())
}()
fmt.Println("Connected; type 'exit' to shutdown gracefully")
// Simulate a chat session
util.Chat(dtlsConn)
}