|
| 1 | +// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// Package main implements an example DTLS client using a pre-shared key. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/pion/dtls/v2" |
| 14 | + "github.com/pion/dtls/v2/examples/util" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + // Prepare the IP to connect to |
| 19 | + addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4444} |
| 20 | + |
| 21 | + // |
| 22 | + // Everything below is the pion-DTLS API! Thanks for using it ❤️. |
| 23 | + // |
| 24 | + |
| 25 | + // Prepare the configuration of the DTLS connection |
| 26 | + config := &dtls.Config{ |
| 27 | + PSK: func(hint []byte) ([]byte, error) { |
| 28 | + fmt.Printf("Server's hint: %s \n", hint) |
| 29 | + return []byte{0xAB, 0xC1, 0x23}, nil |
| 30 | + }, |
| 31 | + PSKIdentityHint: []byte("Pion DTLS Server"), |
| 32 | + CipherSuites: []dtls.CipherSuiteID{dtls.TLS_PSK_WITH_AES_128_CCM_8}, |
| 33 | + ExtendedMasterSecret: dtls.RequireExtendedMasterSecret, |
| 34 | + ConnectionIDGenerator: dtls.OnlySendCIDGenerator(), |
| 35 | + } |
| 36 | + |
| 37 | + // Connect to a DTLS server |
| 38 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 39 | + defer cancel() |
| 40 | + dtlsConn, err := dtls.DialWithContext(ctx, "udp", addr, config) |
| 41 | + util.Check(err) |
| 42 | + defer func() { |
| 43 | + util.Check(dtlsConn.Close()) |
| 44 | + }() |
| 45 | + |
| 46 | + fmt.Println("Connected; type 'exit' to shutdown gracefully") |
| 47 | + |
| 48 | + // Simulate a chat session |
| 49 | + util.Chat(dtlsConn) |
| 50 | +} |
0 commit comments