feat(netproxy): add network proxy module to provide unified network proxy connection.

feat(internal/controller): make email, ntft, webhook, qq_napcat able to use network proxy.
This commit is contained in:
2026-08-12 22:53:23 +08:00
parent 105fa47811
commit 56cc3485a7
7 changed files with 243 additions and 55 deletions
+8
View File
@@ -6,6 +6,7 @@ import (
gomail "gopkg.in/mail.v2"
"nukumizu-backend/config"
"nukumizu-backend/internal/netproxy"
"nukumizu-backend/internal/node"
"nukumizu-backend/internal/template"
"nukumizu-backend/postLog"
@@ -18,6 +19,13 @@ type EmailController struct {
// NewEmailController creates a new Email controller.
func NewEmailController(cfg config.EmailConfig) *EmailController {
if cfg.NetworkUseProxy {
// Route SMTP through the HTTP CONNECT proxy. NetDialTimeout is
// gomail's documented hook for overriding how the SMTP connection is
// dialed. There is a single global email channel, so overriding it
// unconditionally when the flag is set is safe.
gomail.NetDialTimeout = netproxy.DialWithTimeout(true)
}
return &EmailController{cfg: cfg}
}
+3 -4
View File
@@ -7,6 +7,7 @@ import (
"time"
"nukumizu-backend/config"
"nukumizu-backend/internal/netproxy"
"nukumizu-backend/internal/node"
"nukumizu-backend/internal/template"
"nukumizu-backend/postLog"
@@ -21,10 +22,8 @@ type NtfyController struct {
// NewNtfyController creates a new Ntfy controller.
func NewNtfyController(cfg config.NtfyConfig) *NtfyController {
return &NtfyController{
cfg: cfg,
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
cfg: cfg,
httpClient: netproxy.HTTPClient(cfg.NetworkUseProxy, 10*time.Second),
}
}
+18 -12
View File
@@ -10,9 +10,11 @@ import (
"sync"
"time"
"nukumizu-backend/postLog"
"nukumizu-backend/config"
"github.com/gorilla/websocket"
"nukumizu-backend/config"
"nukumizu-backend/internal/netproxy"
"nukumizu-backend/postLog"
)
// APIResponse mirrors NapCat's HTTP API response envelope.
@@ -30,6 +32,7 @@ type Client struct {
addr string
port string
token string
useProxy bool
httpClient *http.Client
connMu sync.Mutex
@@ -38,16 +41,17 @@ type Client struct {
stopOnce sync.Once
}
// NewClient creates a NapCat client for the given host/port/token.
func NewClient(addr, port, token string) *Client {
// NewClient creates a NapCat client for the given host/port/token. When
// useProxy is set, HTTP API calls and the WebSocket connection are routed
// through the system-wide network proxy.
func NewClient(addr, port, token string, useProxy bool) *Client {
return &Client{
addr: addr,
port: port,
token: token,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
stopCh: make(chan struct{}),
addr: addr,
port: port,
token: token,
useProxy: useProxy,
httpClient: netproxy.HTTPClient(useProxy, 30*time.Second),
stopCh: make(chan struct{}),
}
}
@@ -265,7 +269,9 @@ func (c *Client) listenOnce(onEvent func(raw []byte)) {
header.Set("Authorization", "Bearer "+c.token)
}
conn, _, err := websocket.DefaultDialer.Dial(wsURL, header)
dialer := *websocket.DefaultDialer
dialer.Proxy = netproxy.ProxyFunc(c.useProxy)
conn, _, err := dialer.Dial(wsURL, header)
if err != nil {
postLog.Error(fmt.Sprintf("[Napcat] Failed to connect to NapCat WebSocket: %v", err))
return
+1 -1
View File
@@ -39,7 +39,7 @@ type QQController struct {
func NewQQController(cfg config.QQConfig) *QQController {
q := &QQController{cfg: cfg}
if cfg.Enabled {
q.napcatClient = NewClient(cfg.NapcatAddr, cfg.NapcatPort, cfg.NapcatToken)
q.napcatClient = NewClient(cfg.NapcatAddr, cfg.NapcatPort, cfg.NapcatToken, cfg.NetworkUseProxy)
}
return q
}
+3 -4
View File
@@ -8,6 +8,7 @@ import (
"time"
"nukumizu-backend/config"
"nukumizu-backend/internal/netproxy"
"nukumizu-backend/internal/node"
"nukumizu-backend/internal/template"
"nukumizu-backend/postLog"
@@ -22,10 +23,8 @@ type WebhookController struct {
// NewWebhookController creates a new Webhook controller.
func NewWebhookController(cfg config.WebhookConfig) *WebhookController {
return &WebhookController{
cfg: cfg,
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
cfg: cfg,
httpClient: netproxy.HTTPClient(cfg.NetworkUseProxy, 10*time.Second),
}
}