2022-09-09 18:45:10 +08:00
|
|
|
package tls
|
2022-07-18 20:40:14 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
|
2023-02-21 14:53:00 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-18 20:40:14 +08:00
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
2022-11-09 11:43:03 +08:00
|
|
|
type STDClientConfig struct {
|
2022-07-18 20:40:14 +08:00
|
|
|
config *tls.Config
|
|
|
|
}
|
|
|
|
|
2022-11-09 11:43:03 +08:00
|
|
|
func (s *STDClientConfig) ServerName() string {
|
|
|
|
return s.config.ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *STDClientConfig) SetServerName(serverName string) {
|
|
|
|
s.config.ServerName = serverName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *STDClientConfig) NextProtos() []string {
|
|
|
|
return s.config.NextProtos
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *STDClientConfig) SetNextProtos(nextProto []string) {
|
|
|
|
s.config.NextProtos = nextProto
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *STDClientConfig) Config() (*STDConfig, error) {
|
|
|
|
return s.config, nil
|
|
|
|
}
|
|
|
|
|
2023-02-25 16:24:08 +08:00
|
|
|
func (s *STDClientConfig) Client(conn net.Conn) (Conn, error) {
|
|
|
|
return tls.Client(conn, s.config), nil
|
2022-11-09 11:43:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *STDClientConfig) Clone() Config {
|
|
|
|
return &STDClientConfig{s.config.Clone()}
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:53:00 +08:00
|
|
|
func NewSTDClient(router adapter.Router, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
|
2022-07-18 20:40:14 +08:00
|
|
|
var serverName string
|
|
|
|
if options.ServerName != "" {
|
|
|
|
serverName = options.ServerName
|
|
|
|
} else if serverAddress != "" {
|
2022-09-09 18:45:10 +08:00
|
|
|
if _, err := netip.ParseAddr(serverName); err != nil {
|
2022-07-18 20:40:14 +08:00
|
|
|
serverName = serverAddress
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 16:11:41 +08:00
|
|
|
if serverName == "" && !options.Insecure {
|
2022-07-18 20:40:14 +08:00
|
|
|
return nil, E.New("missing server_name or insecure=true")
|
|
|
|
}
|
|
|
|
|
|
|
|
var tlsConfig tls.Config
|
2023-02-21 14:53:00 +08:00
|
|
|
tlsConfig.Time = router.TimeFunc()
|
2022-07-18 20:40:14 +08:00
|
|
|
if options.DisableSNI {
|
|
|
|
tlsConfig.ServerName = "127.0.0.1"
|
|
|
|
} else {
|
|
|
|
tlsConfig.ServerName = serverName
|
|
|
|
}
|
|
|
|
if options.Insecure {
|
|
|
|
tlsConfig.InsecureSkipVerify = options.Insecure
|
|
|
|
} else if options.DisableSNI {
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
|
|
|
|
verifyOptions := x509.VerifyOptions{
|
|
|
|
DNSName: serverName,
|
|
|
|
Intermediates: x509.NewCertPool(),
|
|
|
|
}
|
|
|
|
for _, cert := range state.PeerCertificates[1:] {
|
|
|
|
verifyOptions.Intermediates.AddCert(cert)
|
|
|
|
}
|
|
|
|
_, err := state.PeerCertificates[0].Verify(verifyOptions)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 08:14:09 +08:00
|
|
|
if len(options.ALPN) > 0 {
|
|
|
|
tlsConfig.NextProtos = options.ALPN
|
|
|
|
}
|
|
|
|
if options.MinVersion != "" {
|
2022-09-09 18:45:10 +08:00
|
|
|
minVersion, err := ParseTLSVersion(options.MinVersion)
|
2022-07-25 08:14:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse min_version")
|
|
|
|
}
|
|
|
|
tlsConfig.MinVersion = minVersion
|
|
|
|
}
|
|
|
|
if options.MaxVersion != "" {
|
2022-09-09 18:45:10 +08:00
|
|
|
maxVersion, err := ParseTLSVersion(options.MaxVersion)
|
2022-07-25 08:14:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse max_version")
|
|
|
|
}
|
|
|
|
tlsConfig.MaxVersion = maxVersion
|
|
|
|
}
|
|
|
|
if options.CipherSuites != nil {
|
|
|
|
find:
|
|
|
|
for _, cipherSuite := range options.CipherSuites {
|
|
|
|
for _, tlsCipherSuite := range tls.CipherSuites() {
|
|
|
|
if cipherSuite == tlsCipherSuite.Name {
|
|
|
|
tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
|
|
|
|
continue find
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, E.New("unknown cipher_suite: ", cipherSuite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var certificate []byte
|
|
|
|
if options.Certificate != "" {
|
|
|
|
certificate = []byte(options.Certificate)
|
|
|
|
} else if options.CertificatePath != "" {
|
|
|
|
content, err := os.ReadFile(options.CertificatePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "read certificate")
|
|
|
|
}
|
|
|
|
certificate = content
|
|
|
|
}
|
|
|
|
if len(certificate) > 0 {
|
2022-07-27 12:03:07 +08:00
|
|
|
certPool := x509.NewCertPool()
|
2022-08-08 08:56:04 +08:00
|
|
|
if !certPool.AppendCertsFromPEM(certificate) {
|
|
|
|
return nil, E.New("failed to parse certificate:\n\n", certificate)
|
2022-07-25 08:14:09 +08:00
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = certPool
|
|
|
|
}
|
2022-11-09 11:43:03 +08:00
|
|
|
return &STDClientConfig{&tlsConfig}, nil
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|