gotlcp

Go TLCP

Github CI Documentation GitHub go.mod Go version GitHub tag (latest SemVer)

在使用 GoTLCP 前,请务必悉知 《Go TLCP 免责声明》!

GoTLCP 采用 Go 语言实现的国密传输层密码协议套件,同时支持以下两个协议标准:

协议关系

GoTLCP 实现了记录层协议、握手协议族以及密钥计算,支持完整握手、会话重用、传输保护、单向身份认证(认证服务端)和双向身份认证。

全面支持国产商用密码全系列密码套件

兼容适配各类Web框架,包括Gin、Fiber、go http。

若 clone 和文档预览存在困难,请移步 https://gitee.com/Trisia/gotlcp

若您想快速实现TLCP(国密)HTTPS代理,可以考虑使用 tlcpchan工具 . https://github.com/Trisia/tlcpchan

安装

为了安装使用 GoTLCP,您需要首先安装 Go 并且设置您的 Go 环境,GoTLCP 至少需要您的 Go 版本在 1.25 及以上。

通过下面命令就可以安装 GoTLCP:

go get -u gitee.com/Trisia/gotlcp

GoTLCP 将持续保证 API 的向下兼容,您可以放心的升级 GoTLCP 库至最新版本。

TLCP 快速开始

客户端

package main

import (
	"fmt"
	"gitee.com/Trisia/gotlcp/tlcp"
)

func main() {
	conn, err := tlcp.Dial("tcp", "127.0.0.1:8443", &tlcp.Config{InsecureSkipVerify: true})
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	buff := make([]byte, 516)
	n, err := conn.Read(buff)
	if err != nil {
		panic(err)
	}
	fmt.Printf(">> %s\n", buff[:n])
}

服务端

package main

import (
	"gitee.com/Trisia/gotlcp/tlcp"
	"net"
)

func main() {
	config := &tlcp.Config{
		Certificates: []tlcp.Certificate{sigCert, encCert},
	}
	listen, err := tlcp.Listen("tcp", ":8443", config)
	if err != nil {
		panic(err)
	}
	var conn net.Conn
	for {
		conn, err = listen.Accept()
		if err != nil {
			panic(err)
		}
		_, _ = conn.Write([]byte("Hello Go TLCP!"))
		_ = conn.Close()
	}
}

若您需要同时支持 TLCP/TLS 协议,请参考 GoTLCP 协议适配器 相关内容。

若您需要使用基于 SM9 的标识密码(IBC/IBSDH)密码套件,请参考 IBC 快速入门 与 IBC 配置与使用指南。

DTLCP 快速开始

客户端

package main

import (
	"fmt"
	"gitee.com/Trisia/gotlcp/dtlcp"
)

func main() {
	conn, err := dtlcp.Dial("udp", "127.0.0.1:8443", &dtlcp.Config{InsecureSkipVerify: true})
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	_, err = conn.WriteTo([]byte("Hello DTLCP Server!"), conn.RemoteAddr())
	if err != nil {
		panic(err)
	}
	buff := make([]byte, 516)
	n, addr, err := conn.ReadFrom(buff)
	if err != nil {
		panic(err)
	}
	fmt.Printf(">> %s (来自 %s)\n", buff[:n], addr)
}

服务端

package main

import (
	"gitee.com/Trisia/gotlcp/dtlcp"
)

func main() {
	config := &dtlcp.Config{
		Certificates: []dtlcp.Certificate{sigCert, encCert},
	}
	ln, err := dtlcp.Listen("udp", ":8443", config)
	if err != nil {
		panic(err)
	}
	defer ln.Close()

	for {
		conn, err := ln.Accept()
		if err != nil {
			panic(err)
		}
		go func() {
			defer conn.Close()
			buf := make([]byte, 1024)
			n, addr, _ := conn.(*dtlcp.Conn).ReadFrom(buf)
			conn.(*dtlcp.Conn).WriteTo([]byte("Hello DTLCP Client!"), addr)
			_ = buf[:n]
		}()
	}
}

文档

TLCP

IBC(标识密码)

DTLCP

其他

致谢