- 原包名 http 与标准库 net/http 严重混淆, 实为按首部字节探测协议, 更名 httpprobe; spmux 将基于此做单端口分流, 附测试、例程与 README
27 lines
872 B
Go
27 lines
872 B
Go
// httpprobe 包示例:按报文首部字节识别流量是 HTTP 还是 HTTPS(常用于网关/代理分流)
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.zeroonesoft.cn/golib/zogo/httpprobe"
|
|
)
|
|
|
|
func main() {
|
|
// HTTP 请求报文(以请求方法开头)
|
|
httpData := []byte("GET /index.html HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
|
fmt.Println("IsHttp(HTTP报文):", httpprobe.IsHttp(httpData))
|
|
|
|
// TLS 握手报文(首字节 0x16 0x03)
|
|
httpsData := make([]byte, 32)
|
|
httpsData[0] = 0x16
|
|
httpsData[1] = 0x03
|
|
httpsData[3] = 0x01
|
|
copy(httpsData[8:], "http") // ClientHello 的 SNI 中会出现 host 字符
|
|
fmt.Println("IsHttps(TLS报文):", httpprobe.IsHttps(httpsData))
|
|
|
|
// 普通数据两者都不是
|
|
fmt.Println("IsHttp(普通数据):", httpprobe.IsHttp([]byte("hello world")))
|
|
fmt.Println("IsHttps(普通数据):", httpprobe.IsHttps([]byte("hello world")))
|
|
}
|