forked from pion/dtls
/
util.go
96 lines (78 loc) · 1.94 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Package util provides auxiliary utilities used in examples
package util
import (
"bufio"
"crypto/tls"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
)
const bufSize = 8192
var (
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) {
var netError net.Error
if errors.As(err, &netError) && netError.Temporary() { //nolint:staticcheck
fmt.Printf("Warning: %v\n", err)
} else if err != nil {
fmt.Printf("error: %v\n", err)
panic(err)
}
}
// LoadKeyAndCertificate reads certificates or key from file
func LoadKeyAndCertificate(keyPath string, certificatePath string) (tls.Certificate, error) {
return tls.LoadX509KeyPair(certificatePath, keyPath)
}
// 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
}