8 Star 33 Fork 14

Cliven/gotlcp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 4.04 KB
一键复制 编辑 原始数据 按行查看 历史
Cliven 提交于 2022-08-23 22:50 . 更新:
package main
import (
"gitee.com/Trisia/gotlcp/tlcp"
"github.com/emmansun/gmsm/smx509"
"io"
"net"
)
// 服务端双向身份认证
func main() {
loadCert()
pool := smx509.NewCertPool()
pool.AddCert(rootCert)
config := &tlcp.Config{
Certificates: []tlcp.Certificate{sigCertKey, encCertKey},
ClientAuth: tlcp.RequireAndVerifyClientCert,
ClientCAs: pool,
CipherSuites: []uint16{
tlcp.ECDHE_SM4_GCM_SM3, // 最高优先级
tlcp.ECC_SM4_CBC_SM3, // 最低优先级
},
}
listen, err := tlcp.Listen("tcp", ":8450", config)
if err != nil {
panic(err)
}
for {
conn, err := listen.Accept()
if err != nil {
panic(err)
}
go echo(conn)
}
}
func echo(conn net.Conn) {
defer conn.Close()
_, _ = io.Copy(conn, conn)
}
func loadCert() {
var err error
rootCert, err = smx509.ParseCertificatePEM([]byte(ROOT_CERT_PEM))
if err != nil {
panic(err)
}
sigCertKey, err = tlcp.X509KeyPair([]byte(SIG_CERT_PEM), []byte(SIG_KEY_PEM))
if err != nil {
panic(err)
}
encCertKey, err = tlcp.X509KeyPair([]byte(ENC_CERT_PEM), []byte(ENC_KEY_PEM))
if err != nil {
panic(err)
}
}
var (
sigCertKey tlcp.Certificate
encCertKey tlcp.Certificate
rootCert *smx509.Certificate
)
const (
ROOT_CERT_PEM = `-----BEGIN CERTIFICATE-----
MIIB3jCCAYOgAwIBAgIIAs4MAPwpIBcwCgYIKoEcz1UBg3UwQjELMAkGA1UEBhMC
Q04xDzANBgNVBAgMBua1meaxnzEPMA0GA1UEBwwG5p2t5beeMREwDwYDVQQKDAjm
tYvor5VDQTAeFw0yMTEyMjMwODQ4MzNaFw0zMTEyMjMwODQ4MzNaMEIxCzAJBgNV
B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QQgC1Sw2Ptopr75mxS/
R+lT45og/55WuueomJKSXqTmAfKgCgYIKoEcz1UBgi2hRANCAASVy6EufNqqMxsI
YECDpBlMDqIwKxyT3STCHg0rSuj5djRfDNhoPk9CrtV6Fy5wca+tQvZUrZ36/XqL
UnZoP43l
-----END PRIVATE KEY-----
`
ENC_KEY_PEM = `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgpcgOKHIjr+jDTNjc
mfeSZuYZlwi344P7s7bz1ofThjigCgYIKoEcz1UBgi2hRANCAAR6IMq/LhqYYxJT
qVKaiFTBwjihEBUZJbqS0va/eaqmzwn0kMR8yySScRVaN+svs5P4RpibSs7u2/Hq
gmquZR3m
-----END PRIVATE KEY-----
`
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/Trisia/gotlcp.git
git@gitee.com:Trisia/gotlcp.git
Trisia
gotlcp
gotlcp
v1.2.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385