@@ -77,34 +77,47 @@ func parseInstanceURL(raw, mode string) parsedURL {
7777 hostPart = raw
7878 }
7979
80- // 解析 hostPart -> tunnelAddress:tunnelPort
81- if hostPart != "" {
82- if strings .Contains (hostPart , ":" ) {
83- parts := strings .SplitN (hostPart , ":" , 2 )
84- res .TunnelAddress = parts [0 ]
85- res .TunnelPort = parts [1 ]
80+ // 内部工具函数: 解析 "addr:port" 片段 (兼容 IPv6 字面量,如 [::1]:8080)
81+ parsePart := func (part string ) (addr , port string ) {
82+ part = strings .TrimSpace (part )
83+ if part == "" {
84+ return "" , ""
85+ }
86+ // IPv6 Literals
87+ if strings .HasPrefix (part , "[" ) {
88+ if end := strings .Index (part , "]" ); end != - 1 {
89+ addr = part [:end + 1 ]
90+ if len (part ) > end + 1 && part [end + 1 ] == ':' {
91+ port = part [end + 2 :]
92+ }
93+ return
94+ }
95+ }
96+ if strings .Contains (part , ":" ) {
97+ pieces := strings .SplitN (part , ":" , 2 )
98+ addr , port = pieces [0 ], pieces [1 ]
8699 } else {
87- if _ , err := strconv .Atoi (hostPart ); err == nil {
88- res . TunnelPort = hostPart
100+ if _ , err := strconv .Atoi (part ); err == nil {
101+ port = part
89102 } else {
90- res . TunnelAddress = hostPart
103+ addr = part
91104 }
92105 }
106+ return
107+ }
108+
109+ // 解析 hostPart -> tunnelAddress:tunnelPort (兼容 IPv6)
110+ if hostPart != "" {
111+ addr , port := parsePart (hostPart )
112+ res .TunnelAddress = addr
113+ res .TunnelPort = port
93114 }
94115
95- // 解析 pathPart -> targetAddress:targetPort
116+ // 解析 pathPart -> targetAddress:targetPort (兼容 IPv6)
96117 if pathPart != "" {
97- if strings .Contains (pathPart , ":" ) {
98- parts := strings .SplitN (pathPart , ":" , 2 )
99- res .TargetAddress = parts [0 ]
100- res .TargetPort = parts [1 ]
101- } else {
102- if _ , err := strconv .Atoi (pathPart ); err == nil {
103- res .TargetPort = pathPart
104- } else {
105- res .TargetAddress = pathPart
106- }
107- }
118+ addr , port := parsePart (pathPart )
119+ res .TargetAddress = addr
120+ res .TargetPort = port
108121 }
109122
110123 // 解析查询参数
@@ -336,6 +349,8 @@ func (s *Service) CreateTunnel(req CreateTunnelRequest) (*Tunnel, error) {
336349 npClient := nodepass .NewClient (endpointURL , endpointAPIPath , endpointAPIKey , nil )
337350 instanceID , remoteStatus , err := npClient .CreateInstance (commandLine )
338351 if err != nil {
352+ // 记录 NodePass API 错误,包含关键上下文信息
353+ log .Errorf ("[NodePass] 创建实例失败 endpoint=%d cmd=%s err=%v" , req .EndpointID , commandLine , err )
339354 return nil , err
340355 }
341356
0 commit comments