bitsealws

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildHandshakeRequest

func BuildHandshakeRequest(clientPriv *ec.PrivateKey, serverPub *ec.PublicKey, salt string, nonce string) (body string, headers map[string]string, err error)

BuildHandshakeRequest constructs body+headers like TS side.

func CreateToken

func CreateToken(payload map[string]any, priv *ec.PrivateKey, expSec int64) (string, error)

CreateToken builds payload JSON, adds iat/exp, signs SHA256(payload) with secp256k1 ECDSA. Returns base64url(payload) + "." + base64url(signatureDER)

func VerifyHandshakeRequest

func VerifyHandshakeRequest(body, method, uriPath string, headers map[string]string, serverPriv *ec.PrivateKey) (*ec.PublicKey, string, string, error)

VerifyHandshakeRequest validates and returns client pubkey & salt

func VerifyToken

func VerifyToken(token string, pub *ec.PublicKey) (map[string]any, error)

VerifyToken parses token, verifies signature, returns payload claims.

Types

type BitSealWSConn

type BitSealWSConn struct {
	Conn    *websocket.Conn
	Session *rtc.Session

	// OnMessage 若非 nil,则 Serve/ServeAsync 解包明文后调用;
	// 返回值非 nil ⇒ 自动 Encode + 发送;
	OnMessage func(sess *rtc.Session, plain []byte) ([]byte, error)
}

BitSealWSConn 封装了 x/net/websocket.Conn,并在读写时自动进行 BST2 编解码。 发送方需先 EncodeRecord,接收方需 DecodeRecord,本结构体内部自动处理。

func ConnectBitSealWS

func ConnectBitSealWS(clientPriv *ec.PrivateKey, serverPub *ec.PublicKey, wsURL string) (*BitSealWSConn, error)

ConnectBitSealWS 完成客户端两步握手并建立 BST2 会话,返回包装后的连接。

  1. HTTP POST /ws/handshake – BitSeal-WEB 签名请求
  2. WebSocket Upgrade /ws/socket – 子协议携带 SimpleToken

wsURL 形如 wss://host/ws/socket

func (*BitSealWSConn) Close

func (c *BitSealWSConn) Close() error

Close 关闭底层 websocket 连接。

func (*BitSealWSConn) Read

func (c *BitSealWSConn) Read() ([]byte, error)

Read 接收并解密下一帧,返回明文。

func (*BitSealWSConn) Serve added in v0.2.1

func (c *BitSealWSConn) Serve()

Serve 在当前 goroutine 中持续读取并分发消息,直到 Read 返回错误或连接关闭。 若设置了 OnMessage,则自动调用并根据返回值决定是否回复。

func (*BitSealWSConn) ServeAsync added in v0.2.1

func (c *BitSealWSConn) ServeAsync()

ServeAsync 在新 goroutine 中调用 Serve。

func (*BitSealWSConn) Write

func (c *BitSealWSConn) Write(plain []byte) error

Write 加密并发送明文数据。

type Server

type Server struct {

	// OnMessage 为业务回调;若不为 nil,则在收到每条消息后调用以生成响应明文。
	// 回调返回的明文会再次加密后发回客户端;若返回 nil 则表示不需要回复。
	// 若 OnMessage 本身为 nil,则 Server 默认回显收到的明文(兼容旧逻辑)。
	OnMessage func(sess *rtc.Session, plain []byte) ([]byte, error)
	// contains filtered or unexported fields
}

Server bundles server private key and an in-memory map for pending sessions.

func NewServer

func NewServer(priv *ec.PrivateKey) *Server

NewServer creates a new BitSeal-WS server with its own ServeMux.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler so Server can be passed to http.ListenAndServe.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL