mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 15:18:31 +08:00
添加 github.com/pion/dtls 代码
This commit is contained in:
80
dtls-2.0.9/examples/util/hub.go
Normal file
80
dtls-2.0.9/examples/util/hub.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Hub is a helper to handle one to many chat
|
||||
type Hub struct {
|
||||
conns map[string]net.Conn
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// NewHub builds a new hub
|
||||
func NewHub() *Hub {
|
||||
return &Hub{conns: make(map[string]net.Conn)}
|
||||
}
|
||||
|
||||
// Register adds a new conn to the Hub
|
||||
func (h *Hub) Register(conn net.Conn) {
|
||||
fmt.Printf("Connected to %s\n", conn.RemoteAddr())
|
||||
h.lock.Lock()
|
||||
defer h.lock.Unlock()
|
||||
|
||||
h.conns[conn.RemoteAddr().String()] = conn
|
||||
|
||||
go h.readLoop(conn)
|
||||
}
|
||||
|
||||
func (h *Hub) readLoop(conn net.Conn) {
|
||||
b := make([]byte, bufSize)
|
||||
for {
|
||||
n, err := conn.Read(b)
|
||||
if err != nil {
|
||||
h.unregister(conn)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Got message: %s\n", string(b[:n]))
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) unregister(conn net.Conn) {
|
||||
h.lock.Lock()
|
||||
defer h.lock.Unlock()
|
||||
delete(h.conns, conn.RemoteAddr().String())
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
fmt.Println("Failed to disconnect", conn.RemoteAddr(), err)
|
||||
} else {
|
||||
fmt.Println("Disconnected ", conn.RemoteAddr())
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) broadcast(msg []byte) {
|
||||
h.lock.RLock()
|
||||
defer h.lock.RUnlock()
|
||||
for _, conn := range h.conns {
|
||||
_, err := conn.Write(msg)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to write message to %s: %v\n", conn.RemoteAddr(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Chat starts the stdin readloop to dispatch messages to the hub
|
||||
func (h *Hub) Chat() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
msg, err := reader.ReadString('\n')
|
||||
Check(err)
|
||||
if strings.TrimSpace(msg) == "exit" {
|
||||
return
|
||||
}
|
||||
h.broadcast([]byte(msg))
|
||||
}
|
||||
}
|
154
dtls-2.0.9/examples/util/util.go
Normal file
154
dtls-2.0.9/examples/util/util.go
Normal file
@@ -0,0 +1,154 @@
|
||||
// Package util provides auxiliary utilities used in examples
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const bufSize = 8192
|
||||
|
||||
var (
|
||||
errBlockIsNotPrivateKey = errors.New("block is not a private key, unable to load key")
|
||||
errUnknownKeyTime = errors.New("unknown key time in PKCS#8 wrapping, unable to load key")
|
||||
errNoPrivateKeyFound = errors.New("no private key found, unable to load key")
|
||||
errBlockIsNotCertificate = errors.New("block is not a certificate, unable to load certificates")
|
||||
errNoCertificateFound = errors.New("no certificate found, unable to load certificates")
|
||||
)
|
||||
|
||||
// Chat simulates a simple text chat session over the connection
|
||||
func Chat(conn io.ReadWriter) {
|
||||
go func() {
|
||||
b := make([]byte, bufSize)
|
||||
|
||||
for {
|
||||
n, err := conn.Read(b)
|
||||
Check(err)
|
||||
fmt.Printf("Got message: %s\n", string(b[:n]))
|
||||
}
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
for {
|
||||
text, err := reader.ReadString('\n')
|
||||
Check(err)
|
||||
|
||||
if strings.TrimSpace(text) == "exit" {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = conn.Write([]byte(text))
|
||||
Check(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Check is a helper to throw errors in the examples
|
||||
func Check(err error) {
|
||||
switch e := err.(type) {
|
||||
case nil:
|
||||
case (net.Error):
|
||||
if e.Temporary() {
|
||||
fmt.Printf("Warning: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("net.Error: %v\n", err)
|
||||
panic(err)
|
||||
default:
|
||||
fmt.Printf("error: %v\n", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadKeyAndCertificate reads certificates or key from file
|
||||
func LoadKeyAndCertificate(keyPath string, certificatePath string) (*tls.Certificate, error) {
|
||||
privateKey, err := LoadKey(keyPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
certificate, err := LoadCertificate(certificatePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
certificate.PrivateKey = privateKey
|
||||
|
||||
return certificate, nil
|
||||
}
|
||||
|
||||
// LoadKey Load/read key from file
|
||||
func LoadKey(path string) (crypto.PrivateKey, error) {
|
||||
rawData, err := ioutil.ReadFile(filepath.Clean(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, _ := pem.Decode(rawData)
|
||||
if block == nil || !strings.HasSuffix(block.Type, "PRIVATE KEY") {
|
||||
return nil, errBlockIsNotPrivateKey
|
||||
}
|
||||
|
||||
if key, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil {
|
||||
return key, nil
|
||||
}
|
||||
|
||||
if key, err := x509.ParsePKCS8PrivateKey(block.Bytes); err == nil {
|
||||
switch key := key.(type) {
|
||||
case *rsa.PrivateKey, *ecdsa.PrivateKey:
|
||||
return key, nil
|
||||
default:
|
||||
return nil, errUnknownKeyTime
|
||||
}
|
||||
}
|
||||
|
||||
if key, err := x509.ParseECPrivateKey(block.Bytes); err == nil {
|
||||
return key, nil
|
||||
}
|
||||
|
||||
return nil, errNoPrivateKeyFound
|
||||
}
|
||||
|
||||
// LoadCertificate Load/read certificate(s) from file
|
||||
func LoadCertificate(path string) (*tls.Certificate, error) {
|
||||
rawData, err := ioutil.ReadFile(filepath.Clean(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var certificate tls.Certificate
|
||||
|
||||
for {
|
||||
block, rest := pem.Decode(rawData)
|
||||
if block == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if block.Type != "CERTIFICATE" {
|
||||
return nil, errBlockIsNotCertificate
|
||||
}
|
||||
|
||||
certificate.Certificate = append(certificate.Certificate, block.Bytes)
|
||||
rawData = rest
|
||||
}
|
||||
|
||||
if len(certificate.Certificate) == 0 {
|
||||
return nil, errNoCertificateFound
|
||||
}
|
||||
|
||||
return &certificate, nil
|
||||
}
|
Reference in New Issue
Block a user