|
@@ -1,8 +1,9 @@
|
|
-package inbound
|
|
|
|
|
|
+package tls
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
|
|
+ "net"
|
|
"os"
|
|
"os"
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/adapter"
|
|
@@ -14,9 +15,7 @@ import (
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/fsnotify/fsnotify"
|
|
)
|
|
)
|
|
|
|
|
|
-var _ adapter.Service = (*TLSConfig)(nil)
|
|
|
|
-
|
|
|
|
-type TLSConfig struct {
|
|
|
|
|
|
+type STDServerConfig struct {
|
|
config *tls.Config
|
|
config *tls.Config
|
|
logger log.Logger
|
|
logger log.Logger
|
|
acmeService adapter.Service
|
|
acmeService adapter.Service
|
|
@@ -27,11 +26,110 @@ type TLSConfig struct {
|
|
watcher *fsnotify.Watcher
|
|
watcher *fsnotify.Watcher
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) Config() *tls.Config {
|
|
|
|
- return c.config
|
|
|
|
|
|
+func NewServer(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (ServerConfig, error) {
|
|
|
|
+ if !options.Enabled {
|
|
|
|
+ return nil, nil
|
|
|
|
+ }
|
|
|
|
+ var tlsConfig *tls.Config
|
|
|
|
+ var acmeService adapter.Service
|
|
|
|
+ var err error
|
|
|
|
+ if options.ACME != nil && len(options.ACME.Domain) > 0 {
|
|
|
|
+ tlsConfig, acmeService, err = startACME(ctx, common.PtrValueOrDefault(options.ACME))
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ tlsConfig = &tls.Config{}
|
|
|
|
+ }
|
|
|
|
+ if options.ServerName != "" {
|
|
|
|
+ tlsConfig.ServerName = options.ServerName
|
|
|
|
+ }
|
|
|
|
+ if len(options.ALPN) > 0 {
|
|
|
|
+ tlsConfig.NextProtos = append(tlsConfig.NextProtos, options.ALPN...)
|
|
|
|
+ }
|
|
|
|
+ if options.MinVersion != "" {
|
|
|
|
+ minVersion, err := ParseTLSVersion(options.MinVersion)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, E.Cause(err, "parse min_version")
|
|
|
|
+ }
|
|
|
|
+ tlsConfig.MinVersion = minVersion
|
|
|
|
+ }
|
|
|
|
+ if options.MaxVersion != "" {
|
|
|
|
+ maxVersion, err := ParseTLSVersion(options.MaxVersion)
|
|
|
|
+ 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
|
|
|
|
+ var key []byte
|
|
|
|
+ if acmeService == nil {
|
|
|
|
+ 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 options.Key != "" {
|
|
|
|
+ key = []byte(options.Key)
|
|
|
|
+ } else if options.KeyPath != "" {
|
|
|
|
+ content, err := os.ReadFile(options.KeyPath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, E.Cause(err, "read key")
|
|
|
|
+ }
|
|
|
|
+ key = content
|
|
|
|
+ }
|
|
|
|
+ if certificate == nil {
|
|
|
|
+ return nil, E.New("missing certificate")
|
|
|
|
+ }
|
|
|
|
+ if key == nil {
|
|
|
|
+ return nil, E.New("missing key")
|
|
|
|
+ }
|
|
|
|
+ keyPair, err := tls.X509KeyPair(certificate, key)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, E.Cause(err, "parse x509 key pair")
|
|
|
|
+ }
|
|
|
|
+ tlsConfig.Certificates = []tls.Certificate{keyPair}
|
|
|
|
+ }
|
|
|
|
+ return &STDServerConfig{
|
|
|
|
+ config: tlsConfig,
|
|
|
|
+ logger: logger,
|
|
|
|
+ acmeService: acmeService,
|
|
|
|
+ certificate: certificate,
|
|
|
|
+ key: key,
|
|
|
|
+ certificatePath: options.CertificatePath,
|
|
|
|
+ keyPath: options.KeyPath,
|
|
|
|
+ }, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (c *STDServerConfig) Config() (*STDConfig, error) {
|
|
|
|
+ return c.config, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (c *STDServerConfig) Client(conn net.Conn) Conn {
|
|
|
|
+ return tls.Client(conn, c.config)
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) Start() error {
|
|
|
|
|
|
+func (c *STDServerConfig) Server(conn net.Conn) Conn {
|
|
|
|
+ return tls.Server(conn, c.config)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (c *STDServerConfig) Start() error {
|
|
if c.acmeService != nil {
|
|
if c.acmeService != nil {
|
|
return c.acmeService.Start()
|
|
return c.acmeService.Start()
|
|
} else {
|
|
} else {
|
|
@@ -46,7 +144,7 @@ func (c *TLSConfig) Start() error {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) startWatcher() error {
|
|
|
|
|
|
+func (c *STDServerConfig) startWatcher() error {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -68,7 +166,7 @@ func (c *TLSConfig) startWatcher() error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) loopUpdate() {
|
|
|
|
|
|
+func (c *STDServerConfig) loopUpdate() {
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|
|
case event, ok := <-c.watcher.Events:
|
|
case event, ok := <-c.watcher.Events:
|
|
@@ -91,7 +189,7 @@ func (c *TLSConfig) loopUpdate() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) reloadKeyPair() error {
|
|
|
|
|
|
+func (c *STDServerConfig) reloadKeyPair() error {
|
|
if c.certificatePath != "" {
|
|
if c.certificatePath != "" {
|
|
certificate, err := os.ReadFile(c.certificatePath)
|
|
certificate, err := os.ReadFile(c.certificatePath)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -115,7 +213,7 @@ func (c *TLSConfig) reloadKeyPair() error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *TLSConfig) Close() error {
|
|
|
|
|
|
+func (c *STDServerConfig) Close() error {
|
|
if c.acmeService != nil {
|
|
if c.acmeService != nil {
|
|
return c.acmeService.Close()
|
|
return c.acmeService.Close()
|
|
}
|
|
}
|
|
@@ -124,94 +222,3 @@ func (c *TLSConfig) Close() error {
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
-
|
|
|
|
-func NewTLSConfig(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (*TLSConfig, error) {
|
|
|
|
- if !options.Enabled {
|
|
|
|
- return nil, nil
|
|
|
|
- }
|
|
|
|
- var tlsConfig *tls.Config
|
|
|
|
- var acmeService adapter.Service
|
|
|
|
- var err error
|
|
|
|
- if options.ACME != nil && len(options.ACME.Domain) > 0 {
|
|
|
|
- tlsConfig, acmeService, err = startACME(ctx, common.PtrValueOrDefault(options.ACME))
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- tlsConfig = &tls.Config{}
|
|
|
|
- }
|
|
|
|
- if options.ServerName != "" {
|
|
|
|
- tlsConfig.ServerName = options.ServerName
|
|
|
|
- }
|
|
|
|
- if len(options.ALPN) > 0 {
|
|
|
|
- tlsConfig.NextProtos = append(tlsConfig.NextProtos, options.ALPN...)
|
|
|
|
- }
|
|
|
|
- if options.MinVersion != "" {
|
|
|
|
- minVersion, err := option.ParseTLSVersion(options.MinVersion)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, E.Cause(err, "parse min_version")
|
|
|
|
- }
|
|
|
|
- tlsConfig.MinVersion = minVersion
|
|
|
|
- }
|
|
|
|
- if options.MaxVersion != "" {
|
|
|
|
- maxVersion, err := option.ParseTLSVersion(options.MaxVersion)
|
|
|
|
- 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
|
|
|
|
- var key []byte
|
|
|
|
- if acmeService == nil {
|
|
|
|
- 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 options.Key != "" {
|
|
|
|
- key = []byte(options.Key)
|
|
|
|
- } else if options.KeyPath != "" {
|
|
|
|
- content, err := os.ReadFile(options.KeyPath)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, E.Cause(err, "read key")
|
|
|
|
- }
|
|
|
|
- key = content
|
|
|
|
- }
|
|
|
|
- if certificate == nil {
|
|
|
|
- return nil, E.New("missing certificate")
|
|
|
|
- }
|
|
|
|
- if key == nil {
|
|
|
|
- return nil, E.New("missing key")
|
|
|
|
- }
|
|
|
|
- keyPair, err := tls.X509KeyPair(certificate, key)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, E.Cause(err, "parse x509 key pair")
|
|
|
|
- }
|
|
|
|
- tlsConfig.Certificates = []tls.Certificate{keyPair}
|
|
|
|
- }
|
|
|
|
- return &TLSConfig{
|
|
|
|
- config: tlsConfig,
|
|
|
|
- logger: logger,
|
|
|
|
- acmeService: acmeService,
|
|
|
|
- certificate: certificate,
|
|
|
|
- key: key,
|
|
|
|
- certificatePath: options.CertificatePath,
|
|
|
|
- keyPath: options.KeyPath,
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|