New features: TG Telegram robot terminal [One-click configuration] and [Subscription conversion]

This commit is contained in:
心隨緣動 2025-10-08 00:03:03 +08:00 committed by GitHub
parent 41152d745e
commit a9b814278c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,6 +19,17 @@ import (
type InboundService struct {
xrayApi xray.XrayAPI
tgService TelegramService
}
// 【新增方法】: 用于从外部注入 XrayAPI 实例
func (s *InboundService) SetXrayAPI(api xray.XrayAPI) {
s.xrayApi = api
}
// 【新增方法】: 用于从外部注入 TelegramService 实例
func (s *InboundService) SetTelegramService(tgService TelegramService) {
s.tgService = tgService
}
func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
@ -259,6 +270,34 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo
tx := db.Begin()
defer func() {
if err == nil {
// 【修改位置】: 在事务提交前,执行我们的新逻辑
// =================================================================
// 〔中文注释〕: 新增逻辑 - 判断是否为【一键配置】创建的入站
// 我们通过备注remark是否为一个8位的随机字符串来做一个简单识别
// 只有来自【一键配置】的添加入站操作,才会触发 TG 通知
// =================================================================
isOneClick := false
if len(inbound.Remark) == 8 {
// 假设8位随机备注大概率是一键配置生成的
isOneClick = true
}
if isOneClick && s.tgService != nil && s.tgService.IsRunning() {
// 〔中文注释〕: 使用 goroutine 异步发送通知,避免阻塞正常的 API 返回
go func(ib *model.Inbound) {
// 等待5秒确保事务已经完全提交
time.Sleep(5 * time.Second)
logger.Infof("检测到一键配置创建成功 (备注: %s),准备发送 TG 通知...", ib.Remark)
// 【修改位置】: 将 global.TgBot 替换为 s.tgService
// 〔中文注释〕: 调用注入的服务实例的方法来发送消息。
err := s.tgService.SendOneClickConfig(ib, true, 0)
if err != nil {
logger.Warningf("发送【一键配置】TG 通知失败: %v", err)
}
}(inbound) // 将 inbound 对象的副本传入 goroutine
}
// 中文注释:如果没有错误,提交事务
tx.Commit()
} else {