2022-09-09 10:45:10 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-02-18 06:55:47 +00:00
|
|
|
STDConfig = tls.Config
|
|
|
|
STDConn = tls.Conn
|
|
|
|
ConnectionState = tls.ConnectionState
|
2022-09-09 10:45:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config interface {
|
2022-11-09 03:43:03 +00:00
|
|
|
ServerName() string
|
|
|
|
SetServerName(serverName string)
|
2022-09-11 02:22:52 +00:00
|
|
|
NextProtos() []string
|
|
|
|
SetNextProtos(nextProto []string)
|
2022-09-09 10:45:10 +00:00
|
|
|
Config() (*STDConfig, error)
|
2023-02-25 08:24:08 +00:00
|
|
|
Client(conn net.Conn) (Conn, error)
|
2022-11-09 03:43:03 +00:00
|
|
|
Clone() Config
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 06:08:13 +00:00
|
|
|
type ConfigWithSessionIDGenerator interface {
|
|
|
|
SetSessionIDGenerator(generator func(clientHello []byte, sessionID []byte) error)
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:45:10 +00:00
|
|
|
type ServerConfig interface {
|
|
|
|
Config
|
|
|
|
adapter.Service
|
2023-02-25 08:24:08 +00:00
|
|
|
Server(conn net.Conn) (Conn, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerConfigCompat interface {
|
|
|
|
ServerConfig
|
|
|
|
ServerHandshake(ctx context.Context, conn net.Conn) (Conn, error)
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Conn interface {
|
|
|
|
net.Conn
|
|
|
|
HandshakeContext(ctx context.Context) error
|
2023-02-18 06:55:47 +00:00
|
|
|
ConnectionState() ConnectionState
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseTLSVersion(version string) (uint16, error) {
|
|
|
|
switch version {
|
|
|
|
case "1.0":
|
|
|
|
return tls.VersionTLS10, nil
|
|
|
|
case "1.1":
|
|
|
|
return tls.VersionTLS11, nil
|
|
|
|
case "1.2":
|
|
|
|
return tls.VersionTLS12, nil
|
|
|
|
case "1.3":
|
|
|
|
return tls.VersionTLS13, nil
|
|
|
|
default:
|
|
|
|
return 0, E.New("unknown tls version:", version)
|
|
|
|
}
|
|
|
|
}
|