From 94c7970fd64592c0729a67f40c2a44f864bd4387 Mon Sep 17 00:00:00 2001
From: yiguous <147401898+yiguous@users.noreply.github.com>
Date: Wed, 12 Feb 2025 22:55:16 +0800
Subject: [PATCH] Config: Correctly marshal PortList and NameServerConfig to
 JSON (#4386)

---
 infra/conf/common.go | 18 ++++++++++++++++++
 infra/conf/dns.go    | 14 +++++++-------
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/infra/conf/common.go b/infra/conf/common.go
index 1cda2459..fa48edea 100644
--- a/infra/conf/common.go
+++ b/infra/conf/common.go
@@ -203,6 +203,24 @@ func (list *PortList) Build() *net.PortList {
 	return portList
 }
 
+func (v PortList) MarshalJSON() ([]byte, error) {
+	return json.Marshal(v.String())
+}
+
+func (v PortList) String() string {
+	ports := []string{}
+	for _, port := range v.Range {
+		if port.From == port.To {
+			p := strconv.Itoa(int(port.From))
+			ports = append(ports, p)
+		} else {
+			p := fmt.Sprintf("%d-%d", port.From, port.To)
+			ports = append(ports, p)
+		}
+	}
+	return strings.Join(ports, ",")
+}
+
 // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
 func (list *PortList) UnmarshalJSON(data []byte) error {
 	var listStr string
diff --git a/infra/conf/dns.go b/infra/conf/dns.go
index d9ed9ab5..65964f3d 100644
--- a/infra/conf/dns.go
+++ b/infra/conf/dns.go
@@ -12,13 +12,13 @@ import (
 )
 
 type NameServerConfig struct {
-	Address       *Address
-	ClientIP      *Address
-	Port          uint16
-	SkipFallback  bool
-	Domains       []string
-	ExpectIPs     StringList
-	QueryStrategy string
+	Address       *Address   `json:"address"`
+	ClientIP      *Address   `json:"clientIp"`
+	Port          uint16     `json:"port"`
+	SkipFallback  bool       `json:"skipFallback"`
+	Domains       []string   `json:"domains"`
+	ExpectIPs     StringList `json:"expectIps"`
+	QueryStrategy string     `json:"queryStrategy"`
 }
 
 func (c *NameServerConfig) UnmarshalJSON(data []byte) error {