From 29b8dda4ac2de647f0b80c0f906804b06e2b52cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 20 Jul 2025 00:43:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(#878):=20=E5=AE=89=E8=A3=85=E6=97=B6?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E9=BB=98=E8=AE=A4=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/cli.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/service/cli.go b/internal/service/cli.go index 172c485a..68399b7d 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "math/rand/v2" stdos "os" "path/filepath" "strings" @@ -977,6 +978,24 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error { config.App.Key = str.Random(32) config.HTTP.Entrance = "/" + str.Random(6) + // 随机默认端口 +checkPort: + port := uint(rand.IntN(50000) + 10000) // 10000-60000 + if os.TCPPortInUse(port) { + goto checkPort + } + config.HTTP.Port = port + + // 放行端口 + fw := firewall.NewFirewall() + _ = fw.Port(firewall.FireInfo{ + Type: firewall.TypeNormal, + PortStart: config.HTTP.Port, + PortEnd: config.HTTP.Port, + Direction: firewall.DirectionIn, + Strategy: firewall.StrategyAccept, + }, firewall.OperationAdd) + encoded, err := yaml.Marshal(config) if err != nil { return err