Forráskód Böngészése

更新 HTTPS 和 HTTP 配置默认值和检查逻辑

移除了不必要的导入包,并简化了 `HttpsConfig` 和 `HttpConfig` 的默认值设置和检查逻辑。现在,只有在 `Address` 不为空时才会设置其他默认值并进行相关检查。
SongZihuan 3 hónapja
szülő
commit
810a8ce05c
2 módosított fájl, 22 hozzáadás és 41 törlés
  1. 1 10
      src/config/httpconfig.go
  2. 21 31
      src/config/httpsconfig.go

+ 1 - 10
src/config/httpconfig.go

@@ -1,9 +1,7 @@
 package config
 
 import (
-	"fmt"
 	"github.com/SongZihuan/huan-proxy/src/config/configerr"
-	"net/url"
 )
 
 type HttpConfig struct {
@@ -13,7 +11,7 @@ type HttpConfig struct {
 
 func (h *HttpConfig) SetDefault() {
 	if h.Address == "" {
-		return
+		h.Address = ":4022"
 	}
 
 	if h.StopWaitSecond <= 0 {
@@ -22,12 +20,5 @@ func (h *HttpConfig) SetDefault() {
 }
 
 func (h *HttpConfig) Check() configerr.ConfigError {
-	if h.Address == "" {
-		return nil
-	}
-
-	if _, err := url.Parse(h.Address); err != nil {
-		return configerr.NewConfigError(fmt.Sprintf("http address error: %s", err.Error()))
-	}
 	return nil
 }

+ 21 - 31
src/config/httpsconfig.go

@@ -1,9 +1,7 @@
 package config
 
 import (
-	"fmt"
 	"github.com/SongZihuan/huan-proxy/src/config/configerr"
-	"net/url"
 	"os"
 )
 
@@ -23,24 +21,22 @@ type HttpsConfig struct {
 }
 
 func (h *HttpsConfig) SetDefault() {
-	if h.Address == "" {
-		return
-	}
-
-	if h.SSLEmail == "" {
-		h.SSLEmail = "no-reply@example.com"
-	}
+	if h.Address != "" {
+		if h.SSLEmail == "" {
+			h.SSLEmail = "no-reply@example.com"
+		}
 
-	if h.SSLCertDir == "" {
-		h.SSLCertDir = "./ssl-certs"
-	}
+		if h.SSLCertDir == "" {
+			h.SSLCertDir = "./ssl-certs"
+		}
 
-	if h.AliyunDNSAccessKey == "" {
-		h.AliyunDNSAccessKey = os.Getenv(EnvAliyunKey)
-	}
+		if h.AliyunDNSAccessKey == "" {
+			h.AliyunDNSAccessKey = os.Getenv(EnvAliyunKey)
+		}
 
-	if h.AliyunDNSAccessSecret == "" {
-		h.AliyunDNSAccessKey = os.Getenv(EnvAliyunSecret)
+		if h.AliyunDNSAccessSecret == "" {
+			h.AliyunDNSAccessKey = os.Getenv(EnvAliyunSecret)
+		}
 	}
 
 	if h.StopWaitSecond <= 0 {
@@ -49,20 +45,14 @@ func (h *HttpsConfig) SetDefault() {
 }
 
 func (h *HttpsConfig) Check() configerr.ConfigError {
-	if h.Address == "" {
-		return nil
-	}
-
-	if _, err := url.Parse(h.Address); err != nil {
-		return configerr.NewConfigError(fmt.Sprintf("http address error: %s", err.Error()))
-	}
-
-	if h.SSLDomain == "" {
-		return configerr.NewConfigError("http ssl must has a domain")
-	}
-
-	if h.AliyunDNSAccessKey == "" || h.AliyunDNSAccessSecret == "" {
-		return configerr.NewConfigError("http ssl must has a aliyun access key or secret")
+	if h.Address != "" {
+		if h.SSLDomain == "" {
+			return configerr.NewConfigError("http ssl must has a domain")
+		}
+
+		if h.AliyunDNSAccessKey == "" || h.AliyunDNSAccessSecret == "" {
+			return configerr.NewConfigError("http ssl must has a aliyun access key or secret")
+		}
 	}
 
 	return nil