mirror of https://github.com/bjdgyc/anylink.git
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/pion/dtls/v2"
|
|
"github.com/pion/dtls/v2/examples/util"
|
|
)
|
|
|
|
func main() {
|
|
// Prepare the IP to connect to
|
|
addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4444}
|
|
|
|
//
|
|
// Everything below is the pion-DTLS API! Thanks for using it ❤️.
|
|
//
|
|
|
|
certificate, err := util.LoadKeyAndCertificate("examples/certificates/client.pem",
|
|
"examples/certificates/client.pub.pem")
|
|
util.Check(err)
|
|
|
|
rootCertificate, err := util.LoadCertificate("examples/certificates/server.pub.pem")
|
|
util.Check(err)
|
|
certPool := x509.NewCertPool()
|
|
cert, err := x509.ParseCertificate(rootCertificate.Certificate[0])
|
|
util.Check(err)
|
|
certPool.AddCert(cert)
|
|
|
|
// Prepare the configuration of the DTLS connection
|
|
config := &dtls.Config{
|
|
Certificates: []tls.Certificate{*certificate},
|
|
ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
|
|
RootCAs: certPool,
|
|
}
|
|
|
|
// 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)
|
|
}
|