Files
v2ray_simple/tlsLayer/server.go
2022-12-22 10:41:13 +08:00

67 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tlsLayer
import (
"crypto/tls"
"net"
"unsafe"
"github.com/e1732a364fed/v2ray_simple/utils"
"golang.org/x/exp/slices"
)
type Server struct {
tlsConfig *tls.Config
tlstype int
}
// 如 certFile, keyFile 有一项没给出,则会自动生成随机证书
func NewServer(conf Conf) (*Server, error) {
//服务端必须给出 http/1.1 等否则不会协商出这个alpn而我们为了回落是需要协商出所有可能需要的 alpn的。
//而且我们如果不提供 h1 和 h2 的alpn的话很容易被审查者察觉的。
if conf.AlpnList == nil {
conf.AlpnList = []string{"http/1.1", "h2"}
} else {
if !slices.Contains(conf.AlpnList, "http/1.1") {
conf.AlpnList = append(conf.AlpnList, "http/1.1")
}
if !slices.Contains(conf.AlpnList, "h2") {
conf.AlpnList = append(conf.AlpnList, "h2")
}
}
s := &Server{
tlsConfig: GetTlsConfig(true, conf),
tlstype: conf.Tls_type,
}
return s, nil
}
func (s *Server) Handshake(clientConn net.Conn) (tlsConn *Conn, err error) {
if s.tlstype == shadowTls_t {
return shadowTls1(s.tlsConfig.ServerName, clientConn)
}
rawTlsConn := tls.Server(clientConn, s.tlsConfig)
err = rawTlsConn.Handshake()
if err != nil {
err = utils.ErrInErr{ErrDesc: "Failed in Tls handshake", ErrDetail: err}
return
}
tlsConn = &Conn{
Conn: rawTlsConn,
ptr: unsafe.Pointer(rawTlsConn),
}
return
}