mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 20:54:03 +08:00
添加 github.com/pion/dtls 代码
This commit is contained in:
47
dtls-2.0.9/examples/dial/selfsign/main.go
Normal file
47
dtls-2.0.9/examples/dial/selfsign/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pion/dtls/v2"
|
||||
"github.com/pion/dtls/v2/examples/util"
|
||||
"github.com/pion/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)
|
||||
}
|
Reference in New Issue
Block a user