浏览代码

修复日志格式并增强配置检查

修正了日志输出格式错误,并增强了HTTPS配置中的邮箱验证。同时,将部分标准输出替换为日志记录以提高可维护性。
SongZihuan 2 月之前
父节点
当前提交
7b357059bd

+ 2 - 1
src/certssl/applycert/main.go

@@ -3,6 +3,7 @@ package applycert
 import (
 	"fmt"
 	"github.com/SongZihuan/huan-proxy/src/certssl/account"
+	"github.com/SongZihuan/huan-proxy/src/logger"
 	"github.com/SongZihuan/huan-proxy/src/utils"
 	"github.com/go-acme/lego/v4/certcrypto"
 	"github.com/go-acme/lego/v4/certificate"
@@ -21,7 +22,7 @@ func ApplyCert(basedir string, email string, aliyunAccessKey string, aliyunAcces
 
 	user, err := account.LoadAccount(basedir, email)
 	if err != nil {
-		fmt.Printf("load local account failed, register a new on for %s: %s\n", email, err.Error())
+		logger.Infof("load local account failed, register a new on for %s: %s\n", email, err.Error())
 		user, err = account.NewAccount(basedir, email)
 		if err != nil {
 			return nil, fmt.Errorf("generate new user failed: %s", err.Error())

+ 2 - 2
src/certssl/main.go

@@ -13,7 +13,7 @@ const CertDefaultNewApplyTime = 5 * 24 * time.Hour
 
 func GetCertificateAndPrivateKey(basedir string, email string, aliyunAccessKey string, aliyunAccessSecret string, domain string) (crypto.PrivateKey, *x509.Certificate, *x509.Certificate, error) {
 	if email == "" {
-		email = "no-reply@example.com"
+		return nil, nil, nil, fmt.Errorf("email is empty")
 	}
 
 	if !utils.IsValidEmail(email) {
@@ -92,7 +92,7 @@ func WatchCertificate(dir string, email string, aliyunAccessKey string, aliyunAc
 
 func watchCertificate(dir string, email string, aliyunAccessKey string, aliyunAccessSecret string, domain string, oldCert *x509.Certificate) (crypto.PrivateKey, *x509.Certificate, *x509.Certificate, error) {
 	if email == "" {
-		email = "no-reply@example.com"
+		return nil, nil, nil, fmt.Errorf("email is empty")
 	}
 
 	if !utils.IsValidEmail(email) {

+ 5 - 4
src/config/httpsconfig.go

@@ -2,6 +2,7 @@ package config
 
 import (
 	"github.com/SongZihuan/huan-proxy/src/config/configerr"
+	"github.com/SongZihuan/huan-proxy/src/utils"
 	"os"
 )
 
@@ -22,10 +23,6 @@ type HttpsConfig struct {
 
 func (h *HttpsConfig) SetDefault() {
 	if h.Address != "" {
-		if h.SSLEmail == "" {
-			h.SSLEmail = "no-reply@example.com"
-		}
-
 		if h.SSLCertDir == "" {
 			h.SSLCertDir = "./ssl-certs"
 		}
@@ -46,6 +43,10 @@ func (h *HttpsConfig) SetDefault() {
 
 func (h *HttpsConfig) Check() configerr.ConfigError {
 	if h.Address != "" {
+		if h.SSLEmail == "" || !utils.IsValidEmail(h.SSLEmail) {
+			return configerr.NewConfigError("http ssl must has a valid email")
+		}
+
 		if h.SSLDomain == "" {
 			return configerr.NewConfigError("http ssl must has a domain")
 		}

+ 1 - 1
src/logger/logger.go

@@ -96,7 +96,7 @@ func (l *Logger) Executablef(format string, args ...interface{}) string {
 	if str == "" {
 		_, _ = fmt.Fprintf(l.warnWriter, "[Executable]: %s\n", l.args0)
 	} else {
-		_, _ = fmt.Fprintf(l.warnWriter, "{Executable %s]: %s\n", l.args0, str)
+		_, _ = fmt.Fprintf(l.warnWriter, "[Executable %s]: %s\n", l.args0, str)
 	}
 	return l.args0
 }

+ 4 - 4
src/server/httpsserver/server.go

@@ -126,7 +126,7 @@ func (s *HTTPSServer) watchCertificate(stopchan chan bool) {
 	go func() {
 		err := certssl.WatchCertificate(s.cfg.SSLCertDir, s.cfg.SSLEmail, s.cfg.AliyunDNSAccessKey, s.cfg.AliyunDNSAccessSecret, s.cfg.SSLDomain, s.cert, stopchan, newchan)
 		if err != nil {
-			fmt.Printf("watch https cert server error: %s", err.Error())
+			logger.Errorf("watch https cert server error: %s", err.Error())
 		}
 	}()
 
@@ -137,7 +137,7 @@ func (s *HTTPSServer) watchCertificate(stopchan chan bool) {
 				close(newchan)
 				return
 			} else if res.Error != nil {
-				fmt.Printf("https cert reload server error: %s", res.Error.Error())
+				logger.Errorf("https cert reload server error: %s", res.Error.Error())
 			} else if res.PrivateKey != nil && res.Certificate != nil && res.IssuerCertificate != nil {
 				func() {
 					s.reloadMutex.Lock()
@@ -148,7 +148,7 @@ func (s *HTTPSServer) watchCertificate(stopchan chan bool) {
 
 					err := s.server.Shutdown(ctx)
 					if err != nil {
-						fmt.Printf("https server reload shutdown error: %s", err.Error())
+						logger.Errorf("https server reload shutdown error: %s", err.Error())
 					}
 
 					s.key = res.PrivateKey
@@ -157,7 +157,7 @@ func (s *HTTPSServer) watchCertificate(stopchan chan bool) {
 
 					err = s.reloadHttps()
 					if err != nil {
-						fmt.Printf("https server reload init error: %s", err.Error())
+						logger.Errorf("https server reload init error: %s", err.Error())
 					}
 				}()
 			}

+ 2 - 3
src/server/responsewriter/writer.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
+	"github.com/SongZihuan/huan-proxy/src/logger"
 	"net/http"
 )
 
@@ -60,7 +61,6 @@ func (r *ResponseWriter) WriteHeader(statusCode int) {
 	}
 
 	r.status = statusCode
-	fmt.Printf("Set Status is: %d\n", r.status)
 }
 
 func (r *ResponseWriter) ServerError() {
@@ -73,7 +73,7 @@ func (r *ResponseWriter) ServerError() {
 	r.writtenStatus = true
 	r.written = true
 
-	fmt.Printf("Server Error Status is: %d\n", r.status)
+	logger.Errorf("Server Error Status is: %d\n", r.status)
 }
 
 func (r *ResponseWriter) Header() http.Header {
@@ -106,7 +106,6 @@ func (r *ResponseWriter) WriteToResponse() error {
 	if !r.writtenStatus {
 		r.writer.WriteHeader(r.status)
 		r.writtenStatus = true
-		fmt.Printf("Write Status is: %d\n", r.status)
 	}
 
 	if !r.writtenHeader {