main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "net"
  7. "time"
  8. "github.com/pion/dtls/v2"
  9. "github.com/pion/dtls/v2/examples/util"
  10. "github.com/pion/dtls/v2/pkg/crypto/selfsign"
  11. )
  12. func main() {
  13. // Prepare the IP to connect to
  14. addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4444}
  15. // Generate a certificate and private key to secure the connection
  16. certificate, genErr := selfsign.GenerateSelfSigned()
  17. util.Check(genErr)
  18. //
  19. // Everything below is the pion-DTLS API! Thanks for using it ❤️.
  20. //
  21. // Prepare the configuration of the DTLS connection
  22. config := &dtls.Config{
  23. Certificates: []tls.Certificate{certificate},
  24. InsecureSkipVerify: true,
  25. ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
  26. }
  27. // Connect to a DTLS server
  28. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  29. defer cancel()
  30. dtlsConn, err := dtls.DialWithContext(ctx, "udp", addr, config)
  31. util.Check(err)
  32. defer func() {
  33. util.Check(dtlsConn.Close())
  34. }()
  35. fmt.Println("Connected; type 'exit' to shutdown gracefully")
  36. // Simulate a chat session
  37. util.Chat(dtlsConn)
  38. }