forked from Trisia/gotlcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (65 loc) · 1.78 KB
/
main.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
package main
import (
"fmt"
"gitee.com/Trisia/gotlcp/tlcp"
"github.com/emmansun/gmsm/smx509"
)
// 客户端握手重用
func main() {
loadCert()
// 构造根证书列表
pool := smx509.NewCertPool()
pool.AddCert(rootCert)
config := &tlcp.Config{
RootCAs: pool,
SessionCache: tlcp.NewLRUSessionCache(32),
}
// 进行完整握手,并缓存会话
conn, err := tlcp.Dial("tcp", "127.0.0.1:8448", config)
if err != nil {
panic(err)
}
err = conn.Handshake()
if err != nil {
panic(err)
}
_ = conn.Close()
// 通过同一个配置对象创建新的TLCP 触发重用握手
conn, err = tlcp.Dial("tcp", "127.0.0.1:8448", config)
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Write([]byte("Hello Go TLCP!"))
if err != nil {
panic(err)
}
buff := make([]byte, 512)
n, err := conn.Read(buff)
if err != nil {
panic(err)
}
fmt.Printf("<< %s\n", buff[:n])
}
const ROOT_CERT_PEM = `-----BEGIN CERTIFICATE-----
MIIB3jCCAYOgAwIBAgIIAs4MAPwpIBcwCgYIKoEcz1UBg3UwQjELMAkGA1UEBhMC
Q04xDzANBgNVBAgMBua1meaxnzEPMA0GA1UEBwwG5p2t5beeMREwDwYDVQQKDAjm
tYvor5VDQTAeFw0yMTEyMjMwODQ4MzNaFw0zMTEyMjMwODQ4MzNaMEIxCzAJBgNV
BAYTAkNOMQ8wDQYDVQQIDAbmtZnmsZ8xDzANBgNVBAcMBuadreW3njERMA8GA1UE
CgwI5rWL6K+VQ0EwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAARKs6B5ZBy753Os
ZSeIfv8zScbiiXkLjB+Plw+YWvoesRkqYGe/Mqjr8rrmThq6iEWubYK6ZiQQV54k
Klcva3Hto2MwYTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zAdBgNV
HQ4EFgQUNpPjFOdFCfrV7+ovEi3ToZY8wqQwHwYDVR0jBBgwFoAUNpPjFOdFCfrV
7+ovEi3ToZY8wqQwCgYIKoEcz1UBg3UDSQAwRgIhALDhtLKVziUhXbTedDovRANS
Cdu6CJ0MAw7Wbl3vAWGOAiEAzCXLcF32DM5Aze9MqpUfQfYPaRTLYkNwSXlw/LUY
E6E=
-----END CERTIFICATE-----
`
var rootCert *smx509.Certificate
func loadCert() {
var err error
rootCert, err = smx509.ParseCertificatePEM([]byte(ROOT_CERT_PEM))
if err != nil {
panic(err)
}
}