@@ -5,10 +5,15 @@ import (
55 "NodePassDash/internal/endpoint"
66 log "NodePassDash/internal/log"
77 "NodePassDash/internal/models"
8+ "NodePassDash/internal/nodepass"
89 "NodePassDash/internal/sse"
910 "NodePassDash/internal/tunnel"
1011 "encoding/json"
12+ "fmt"
13+ "net"
1114 "net/http"
15+ "net/url"
16+ "strings"
1217 "time"
1318
1419 "github.com/gin-gonic/gin"
@@ -32,6 +37,73 @@ func NewDataHandler(db *gorm.DB, mgr *sse.Manager, endpointService *endpoint.Ser
3237 }
3338}
3439
40+ // extractIPFromURL 从URL中提取IP地址(IPv4或IPv6)
41+ func extractIPFromURL (urlStr string ) string {
42+ // 尝试解析URL
43+ parsedURL , err := url .Parse (urlStr )
44+ if err != nil {
45+ // 如果URL解析失败,尝试手动提取
46+ return extractIPFromString (urlStr )
47+ }
48+
49+ // 从解析后的URL中提取主机名
50+ host := parsedURL .Hostname ()
51+ if host == "" {
52+ return ""
53+ }
54+
55+ // 检查是否为有效的IP地址
56+ if ip := net .ParseIP (host ); ip != nil {
57+ return ip .String ()
58+ }
59+
60+ // 如果不是IP地址,返回空字符串
61+ return ""
62+ }
63+
64+ // extractIPFromString 从字符串中手动提取IP地址(备用方法)
65+ func extractIPFromString (input string ) string {
66+ // 去除协议部分
67+ if idx := strings .Index (input , "://" ); idx != - 1 {
68+ input = input [idx + 3 :]
69+ }
70+
71+ // 去除用户认证信息
72+ if atIdx := strings .Index (input , "@" ); atIdx != - 1 {
73+ input = input [atIdx + 1 :]
74+ }
75+
76+ // 去除路径部分
77+ if slashIdx := strings .Index (input , "/" ); slashIdx != - 1 {
78+ input = input [:slashIdx ]
79+ }
80+
81+ // 处理IPv6地址(方括号包围的地址)
82+ if strings .HasPrefix (input , "[" ) {
83+ if end := strings .Index (input , "]" ); end != - 1 {
84+ // 提取方括号内的IPv6地址
85+ ipv6Addr := input [1 :end ]
86+ // 检查是否为有效的IPv6地址
87+ if ip := net .ParseIP (ipv6Addr ); ip != nil {
88+ return ip .String ()
89+ }
90+ }
91+ return ""
92+ }
93+
94+ // 去除端口部分(IPv4)
95+ if colonIdx := strings .Index (input , ":" ); colonIdx != - 1 {
96+ input = input [:colonIdx ]
97+ }
98+
99+ // 检查是否为有效的IP地址
100+ if ip := net .ParseIP (input ); ip != nil {
101+ return ip .String ()
102+ }
103+
104+ return ""
105+ }
106+
35107func SetupDataRoutes (rg * gin.RouterGroup , db * gorm.DB , sseManager * sse.Manager , endpointService * endpoint.Service , tunnelService * tunnel.Service ) {
36108 // 创建DataHandler实例
37109 dataHandler := NewDataHandler (db , sseManager , endpointService , tunnelService )
@@ -203,9 +275,13 @@ func (h *DataHandler) handleImportV1(c *gin.Context, baseData struct {
203275 }
204276 }
205277
278+ // 从URL中提取IP地址
279+ extractedIP := extractIPFromURL (ep .URL )
280+
206281 newEndpoint := models.Endpoint {
207282 Name : ep .Name ,
208283 URL : ep .URL ,
284+ IP : extractedIP , // 填充提取的IP地址
209285 APIPath : ep .APIPath ,
210286 APIKey : ep .APIKey ,
211287 Status : status ,
@@ -218,6 +294,9 @@ func (h *DataHandler) handleImportV1(c *gin.Context, baseData struct {
218294 continue
219295 }
220296
297+ // 添加到缓存
298+ nodepass .GetCache ().Set (fmt .Sprintf ("%d" , newEndpoint .ID ), newEndpoint .URL + newEndpoint .APIPath , newEndpoint .APIKey )
299+
221300 // 保存端点信息用于后续启动SSE
222301 newEndpoints = append (newEndpoints , struct {
223302 ID int64
@@ -401,10 +480,14 @@ func (h *DataHandler) handleImportV2(c *gin.Context, baseData struct {
401480 continue
402481 }
403482
483+ // 从URL中提取IP地址
484+ extractedIP := extractIPFromURL (ep .URL )
485+
404486 // 插入端点,设置默认状态为 OFFLINE
405487 newEndpoint := models.Endpoint {
406488 Name : ep .Name ,
407489 URL : ep .URL ,
490+ IP : extractedIP , // 填充提取的IP地址
408491 APIPath : ep .APIPath ,
409492 APIKey : ep .APIKey ,
410493 Status : models .EndpointStatusOffline ,
@@ -422,6 +505,9 @@ func (h *DataHandler) handleImportV2(c *gin.Context, baseData struct {
422505 continue
423506 }
424507
508+ // 添加到缓存
509+ nodepass .GetCache ().Set (fmt .Sprintf ("%d" , newEndpoint .ID ), newEndpoint .URL + newEndpoint .APIPath , newEndpoint .APIKey )
510+
425511 // 保存端点信息用于后续启动SSE
426512 newEndpoints = append (newEndpoints , struct {
427513 ID int64
0 commit comments