Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

refactor: clarify tls mode #1095

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions pkg/apis/logger.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
package apis

import (
"bufio"
"bytes"
stdlog "log"
"strings"

"github.com/seal-io/seal/utils/log"
)

func newStdLogger(delegate log.Logger) *stdlog.Logger {
return stdlog.New(logWriter{logger: delegate}, "", stdlog.Lshortfile)
func newStdErrorLogger(delegate log.Logger) *stdlog.Logger {
return stdlog.New(logWriter{logger: delegate}, "", 0)
}

type logWriter struct {
logger log.Logger
}

func (l logWriter) Write(p []byte) (int, error) {
s := bufio.NewScanner(bytes.NewReader(p))
for s.Scan() {
if strings.HasSuffix(s.Text(), "tls: unknown certificate") {
continue
s := string(p)

ok := true

switch {
case strings.HasPrefix(s, "http: TLS handshake error from"):
switch {
case strings.HasSuffix(s, "tls: unknown certificate\n"):
// Ignore self-generated certificate errors from client.
ok = false
case strings.HasSuffix(s, "connection reset by peer\n"):
// Reset TLS handshake errors from client.
ok = false
}
case strings.Contains(s, "broken pipe"):
// Terminate by client.
ok = false
}

l.logger.Info(s.Text())
if ok {
l.logger.Warn(s)
}

return len(p), s.Err()
return len(p), nil
}
18 changes: 15 additions & 3 deletions pkg/apis/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ func (s *Server) Serve(c context.Context, opts ServeOptions) error {
// Serve https.
g.Go(func(ctx context.Context) error {
if opts.TlsMode == TlsModeDisabled {
s.logger.Info("serving in HTTP")

httpHandler <- handler

return nil
}

h := handler
lg := newStdLogger(s.logger.WithName("https"))
lg := newStdErrorLogger(s.logger.WithName("https"))

ls, err := newTcpListener(ctx, opts.BindAddress, 443)
if err != nil {
Expand All @@ -84,13 +87,20 @@ func (s *Server) Serve(c context.Context, opts ServeOptions) error {

switch opts.TlsMode {
default: // TlsModeSelfGenerated.
s.logger.Info("serving in HTTPs with self-generated keypair",
"cache", opts.TlsCertDir)

mgr := &dynacert.Manager{
Cache: dynacert.DirCache(opts.TlsCertDir),
}
tlsConfig.GetCertificate = mgr.GetCertificate
ls = tls.NewListener(ls, tlsConfig)
httpHandler <- http.HandlerFunc(redirectHandler)
case TlsModeAutoGenerated:
s.logger.InfoS("serving in HTTPs with auto-generated keypair",
"domains", opts.TlsAutoCertDomains,
"cache", opts.TlsCertDir)

mgr := &autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(opts.TlsCertDir),
Expand All @@ -109,8 +119,10 @@ func (s *Server) Serve(c context.Context, opts ServeOptions) error {
return mgr.GetCertificate(i)
}
ls = tls.NewListener(ls, tlsConfig)
httpHandler <- mgr.HTTPHandler(nil)
httpHandler <- mgr.HTTPHandler(http.HandlerFunc(redirectHandler))
case TlsModeCustomized:
s.logger.Info("serving in HTTPs with custom keypair")

cert, err := tls.LoadX509KeyPair(opts.TlsCertFile, opts.TlsPrivateKeyFile)
if err != nil {
return err
Expand All @@ -128,7 +140,7 @@ func (s *Server) Serve(c context.Context, opts ServeOptions) error {
// Serve http.
g.Go(func(ctx context.Context) error {
h := <-httpHandler
lg := newStdLogger(s.logger.WithName("http"))
lg := newStdErrorLogger(s.logger.WithName("http"))

ls, err := newTcpListener(ctx, opts.BindAddress, 80)
if err != nil {
Expand Down