|
@@ -15,6 +15,8 @@ import (
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
)
|
|
|
|
|
|
+var errInsecureUnused = E.New("tls: insecure unused")
|
|
|
+
|
|
|
type STDServerConfig struct {
|
|
|
config *tls.Config
|
|
|
logger log.Logger
|
|
@@ -26,6 +28,14 @@ type STDServerConfig struct {
|
|
|
watcher *fsnotify.Watcher
|
|
|
}
|
|
|
|
|
|
+func (c *STDServerConfig) ServerName() string {
|
|
|
+ return c.config.ServerName
|
|
|
+}
|
|
|
+
|
|
|
+func (c *STDServerConfig) SetServerName(serverName string) {
|
|
|
+ c.config.ServerName = serverName
|
|
|
+}
|
|
|
+
|
|
|
func (c *STDServerConfig) NextProtos() []string {
|
|
|
return c.config.NextProtos
|
|
|
}
|
|
@@ -34,109 +44,6 @@ func (c *STDServerConfig) SetNextProtos(nextProto []string) {
|
|
|
c.config.NextProtos = nextProto
|
|
|
}
|
|
|
|
|
|
-var errInsecureUnused = E.New("tls: insecure unused")
|
|
|
-
|
|
|
-func newSTDServer(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))
|
|
|
- //nolint:staticcheck
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- if options.Insecure {
|
|
|
- return nil, errInsecureUnused
|
|
|
- }
|
|
|
- } 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 && key == nil && options.Insecure {
|
|
|
- tlsConfig.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
- return GenerateKeyPair(info.ServerName)
|
|
|
- }
|
|
|
- } else {
|
|
|
- if certificate == nil {
|
|
|
- return nil, E.New("missing certificate")
|
|
|
- } else 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
|
|
|
}
|
|
@@ -149,6 +56,12 @@ func (c *STDServerConfig) Server(conn net.Conn) Conn {
|
|
|
return tls.Server(conn, c.config)
|
|
|
}
|
|
|
|
|
|
+func (c *STDServerConfig) Clone() Config {
|
|
|
+ return &STDServerConfig{
|
|
|
+ config: c.config.Clone(),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (c *STDServerConfig) Start() error {
|
|
|
if c.acmeService != nil {
|
|
|
return c.acmeService.Start()
|
|
@@ -242,3 +155,104 @@ func (c *STDServerConfig) Close() error {
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func NewSTDServer(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))
|
|
|
+ //nolint:staticcheck
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if options.Insecure {
|
|
|
+ return nil, errInsecureUnused
|
|
|
+ }
|
|
|
+ } 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 && key == nil && options.Insecure {
|
|
|
+ tlsConfig.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
+ return GenerateKeyPair(info.ServerName)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if certificate == nil {
|
|
|
+ return nil, E.New("missing certificate")
|
|
|
+ } else 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
|
|
|
+}
|