剪贴板:新增 web/src/utils/clipboard.ts,5 处调用点支持非 HTTPS 环境(对齐 1.0.4) 日志中心:新增「FRP 日志」Tab,解析 frpc 日志为 时间/级别/来源/消息(对齐 1.0.3) 通知:单通道密文解密失败不再拖垮整份配置(对齐 1.0.5);容器补 TZ + tzdata(对齐 1.0.4);Telegram 代理自动补 http:// 并校验 scheme(对齐 1.0.3) 反向代理:复制不再追加「-复制」(对齐 1.0.5);监听占用校验合并表单规则与手动 Nginx 配置两张表(迁移 024 + endpoints 解析器),冲突提示写明占用规则;改端口的并发冲突改为可读提示 FRP:frps 弹窗补「VPS 防火墙放行 TCP 远程端口」提示(对齐 1.0.3);反代配置弹窗补齐内容区内边距与来源说明 文档:新增 docs/upstream-sync-plan.md(逐版本对照 + 第 5 项详细整理);VERSION 与 CHANGELOG 记 v1.2.2
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package notify
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func SendTelegram(ctx context.Context, client *http.Client, cfg RuntimeConfig, content AlertContent) error {
|
|
token := strings.TrimSpace(cfg.TelegramToken)
|
|
if token == "" {
|
|
return fmt.Errorf("Telegram Bot Token 未配置")
|
|
}
|
|
chatID := strings.TrimSpace(cfg.Telegram.ChatID)
|
|
if chatID == "" {
|
|
return fmt.Errorf("Telegram Chat ID 未配置")
|
|
}
|
|
|
|
payload := map[string]string{
|
|
"chat_id": chatID,
|
|
"text": content.HTMLBody,
|
|
"parse_mode": "HTML",
|
|
}
|
|
body, _ := json.Marshal(payload)
|
|
endpoint := "https://api.telegram.org/bot" + token + "/sendMessage"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
httpClient := client
|
|
if httpClient == nil {
|
|
httpClient = &http.Client{Timeout: 15 * time.Second}
|
|
}
|
|
if proxyURL := strings.TrimSpace(cfg.Telegram.ProxyURL); proxyURL != "" {
|
|
// 常见误填:只写 host:port(无 scheme)。补全后再解析,并校验 scheme 是否受支持
|
|
if !strings.Contains(proxyURL, "://") {
|
|
proxyURL = "http://" + proxyURL
|
|
}
|
|
parsed, err := url.Parse(proxyURL)
|
|
if err != nil {
|
|
return fmt.Errorf("Telegram 代理地址无效:%s", proxyURL)
|
|
}
|
|
switch parsed.Scheme {
|
|
case "http", "https", "socks5", "socks5h":
|
|
default:
|
|
return fmt.Errorf("Telegram 代理仅支持 http / https / socks5,当前为 %q", parsed.Scheme)
|
|
}
|
|
httpClient = &http.Client{
|
|
Timeout: 15 * time.Second,
|
|
Transport: &http.Transport{
|
|
Proxy: http.ProxyURL(parsed),
|
|
},
|
|
}
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
|
if len(raw) > 0 {
|
|
return fmt.Errorf("Telegram 请求失败 (%d): %s", resp.StatusCode, strings.TrimSpace(string(raw)))
|
|
}
|
|
return fmt.Errorf("Telegram 请求失败 (%d)", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|