3x-ui/web/controller/setting.go

186 lines
5.7 KiB
Go
Raw Normal View History

2023-02-09 19:18:06 +00:00
package controller
import (
"errors"
feat: Add Gemini AI integration for intelligent Telegram bot PRODUCTION-READY IMPLEMENTATION Features: - Natural language processing for Telegram bot commands - Gemini AI-powered intent detection and parameter extraction - Smart fallback to traditional commands on AI failure - Rate limiting (20 req/min per user) and response caching (5 min) - Admin-only access with comprehensive security measures New Components: - AIService: Core AI service layer with Gemini SDK integration - Enhanced Tgbot: AI message handling and action execution - API Endpoints: /api/setting/ai/update and /api/setting/ai/status - Database Settings: aiEnabled, aiApiKey, aiMaxTokens, aiTemperature Files Created (5): - web/service/ai_service.go (420 lines) - docs/AI_INTEGRATION.md (comprehensive documentation) - docs/AI_QUICKSTART.md (5-minute setup guide) - docs/AI_MIGRATION.md (migration guide) - docs/IMPLEMENTATION_SUMMARY.md (technical details) Files Modified (6): - web/service/tgbot.go: Added AI service integration - web/service/setting.go: Added AI settings management - web/controller/setting.go: Added AI configuration endpoints - web/translation/translate.en_US.toml: Added AI messages - go.mod: Added Gemini SDK dependencies - README.md: Added AI feature announcement Usage Examples: Instead of: /status Now works: "show me server status", "what's the CPU?", "check health" Instead of: /usage user@example.com Now works: "how much traffic has user@example.com used?" Setup: 1. Get API key from https://makersuite.google.com/app/apikey 2. Enable in Settings > Telegram Bot > AI Integration 3. Paste API key and save 4. Restart bot 5. Chat naturally! Technical Details: - Model: gemini-1.5-flash (fast, cost-effective) - Architecture: Service layer with dependency injection - Concurrency: Worker pool (max 10 concurrent) - Error Handling: Comprehensive with graceful degradation - Security: Admin-only, rate limited, API key encrypted - Cost: FREE for typical usage (15 req/min free tier) Testing: - No compilation errors - Backward compatible (no breaking changes) - Fallback mechanism tested - Documentation comprehensive Status: Production Ready
2026-02-02 16:45:04 +00:00
"fmt"
2023-02-09 19:18:06 +00:00
"time"
2025-09-19 08:05:43 +00:00
"github.com/mhsanaei/3x-ui/v2/util/crypto"
"github.com/mhsanaei/3x-ui/v2/web/entity"
"github.com/mhsanaei/3x-ui/v2/web/service"
"github.com/mhsanaei/3x-ui/v2/web/session"
2023-03-17 16:07:49 +00:00
"github.com/gin-gonic/gin"
2023-02-09 19:18:06 +00:00
)
2025-09-20 07:35:50 +00:00
// updateUserForm represents the form for updating user credentials.
2023-02-09 19:18:06 +00:00
type updateUserForm struct {
OldUsername string `json:"oldUsername" form:"oldUsername"`
OldPassword string `json:"oldPassword" form:"oldPassword"`
NewUsername string `json:"newUsername" form:"newUsername"`
NewPassword string `json:"newPassword" form:"newPassword"`
}
2025-09-20 07:35:50 +00:00
// SettingController handles settings and user management operations.
2023-02-09 19:18:06 +00:00
type SettingController struct {
settingService service.SettingService
userService service.UserService
panelService service.PanelService
}
2025-09-20 07:35:50 +00:00
// NewSettingController creates a new SettingController and initializes its routes.
2023-02-09 19:18:06 +00:00
func NewSettingController(g *gin.RouterGroup) *SettingController {
a := &SettingController{}
a.initRouter(g)
return a
}
2025-09-20 07:35:50 +00:00
// initRouter sets up the routes for settings management.
2023-02-09 19:18:06 +00:00
func (a *SettingController) initRouter(g *gin.RouterGroup) {
g = g.Group("/setting")
g.POST("/all", a.getAllSetting)
g.POST("/defaultSettings", a.getDefaultSettings)
2023-02-09 19:18:06 +00:00
g.POST("/update", a.updateSetting)
g.POST("/updateUser", a.updateUser)
g.POST("/restartPanel", a.restartPanel)
2023-12-04 18:20:46 +00:00
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
feat: Add Gemini AI integration for intelligent Telegram bot PRODUCTION-READY IMPLEMENTATION Features: - Natural language processing for Telegram bot commands - Gemini AI-powered intent detection and parameter extraction - Smart fallback to traditional commands on AI failure - Rate limiting (20 req/min per user) and response caching (5 min) - Admin-only access with comprehensive security measures New Components: - AIService: Core AI service layer with Gemini SDK integration - Enhanced Tgbot: AI message handling and action execution - API Endpoints: /api/setting/ai/update and /api/setting/ai/status - Database Settings: aiEnabled, aiApiKey, aiMaxTokens, aiTemperature Files Created (5): - web/service/ai_service.go (420 lines) - docs/AI_INTEGRATION.md (comprehensive documentation) - docs/AI_QUICKSTART.md (5-minute setup guide) - docs/AI_MIGRATION.md (migration guide) - docs/IMPLEMENTATION_SUMMARY.md (technical details) Files Modified (6): - web/service/tgbot.go: Added AI service integration - web/service/setting.go: Added AI settings management - web/controller/setting.go: Added AI configuration endpoints - web/translation/translate.en_US.toml: Added AI messages - go.mod: Added Gemini SDK dependencies - README.md: Added AI feature announcement Usage Examples: Instead of: /status Now works: "show me server status", "what's the CPU?", "check health" Instead of: /usage user@example.com Now works: "how much traffic has user@example.com used?" Setup: 1. Get API key from https://makersuite.google.com/app/apikey 2. Enable in Settings > Telegram Bot > AI Integration 3. Paste API key and save 4. Restart bot 5. Chat naturally! Technical Details: - Model: gemini-1.5-flash (fast, cost-effective) - Architecture: Service layer with dependency injection - Concurrency: Worker pool (max 10 concurrent) - Error Handling: Comprehensive with graceful degradation - Security: Admin-only, rate limited, API key encrypted - Cost: FREE for typical usage (15 req/min free tier) Testing: - No compilation errors - Backward compatible (no breaking changes) - Fallback mechanism tested - Documentation comprehensive Status: Production Ready
2026-02-02 16:45:04 +00:00
g.POST("/ai/update", a.updateAISetting)
g.GET("/ai/status", a.getAIStatus)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// getAllSetting retrieves all current settings.
2023-02-09 19:18:06 +00:00
func (a *SettingController) getAllSetting(c *gin.Context) {
allSetting, err := a.settingService.GetAllSetting()
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
2023-02-09 19:18:06 +00:00
return
}
jsonObj(c, allSetting, nil)
}
2025-09-20 07:35:50 +00:00
// getDefaultSettings retrieves the default settings based on the host.
func (a *SettingController) getDefaultSettings(c *gin.Context) {
2023-12-08 16:18:51 +00:00
result, err := a.settingService.GetDefaultSettings(c.Request.Host)
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
jsonObj(c, result, nil)
}
2025-09-20 07:35:50 +00:00
// updateSetting updates all settings with the provided data.
2023-02-09 19:18:06 +00:00
func (a *SettingController) updateSetting(c *gin.Context) {
allSetting := &entity.AllSetting{}
err := c.ShouldBind(allSetting)
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
return
}
err = a.settingService.UpdateAllSetting(allSetting)
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// updateUser updates the current user's username and password.
2023-02-09 19:18:06 +00:00
func (a *SettingController) updateUser(c *gin.Context) {
form := &updateUserForm{}
err := c.ShouldBind(form)
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
return
}
user := session.GetLoginUser(c)
if user.Username != form.OldUsername || !crypto.CheckPasswordHash(user.Password, form.OldPassword) {
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
2023-02-09 19:18:06 +00:00
return
}
if form.NewUsername == "" || form.NewPassword == "" {
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
2023-02-09 19:18:06 +00:00
return
}
err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
if err == nil {
user.Username = form.NewUsername
user.Password, _ = crypto.HashPasswordAsBcrypt(form.NewPassword)
2023-02-09 19:18:06 +00:00
session.SetLoginUser(c, user)
}
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// restartPanel restarts the panel service after a delay.
2023-02-09 19:18:06 +00:00
func (a *SettingController) restartPanel(c *gin.Context) {
err := a.panelService.RestartPanel(time.Second * 3)
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.restartPanelSuccess"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// getDefaultXrayConfig retrieves the default Xray configuration.
2023-12-04 18:20:46 +00:00
func (a *SettingController) getDefaultXrayConfig(c *gin.Context) {
defaultJsonConfig, err := a.settingService.GetDefaultXrayConfig()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
jsonObj(c, defaultJsonConfig, nil)
}
feat: Add Gemini AI integration for intelligent Telegram bot PRODUCTION-READY IMPLEMENTATION Features: - Natural language processing for Telegram bot commands - Gemini AI-powered intent detection and parameter extraction - Smart fallback to traditional commands on AI failure - Rate limiting (20 req/min per user) and response caching (5 min) - Admin-only access with comprehensive security measures New Components: - AIService: Core AI service layer with Gemini SDK integration - Enhanced Tgbot: AI message handling and action execution - API Endpoints: /api/setting/ai/update and /api/setting/ai/status - Database Settings: aiEnabled, aiApiKey, aiMaxTokens, aiTemperature Files Created (5): - web/service/ai_service.go (420 lines) - docs/AI_INTEGRATION.md (comprehensive documentation) - docs/AI_QUICKSTART.md (5-minute setup guide) - docs/AI_MIGRATION.md (migration guide) - docs/IMPLEMENTATION_SUMMARY.md (technical details) Files Modified (6): - web/service/tgbot.go: Added AI service integration - web/service/setting.go: Added AI settings management - web/controller/setting.go: Added AI configuration endpoints - web/translation/translate.en_US.toml: Added AI messages - go.mod: Added Gemini SDK dependencies - README.md: Added AI feature announcement Usage Examples: Instead of: /status Now works: "show me server status", "what's the CPU?", "check health" Instead of: /usage user@example.com Now works: "how much traffic has user@example.com used?" Setup: 1. Get API key from https://makersuite.google.com/app/apikey 2. Enable in Settings > Telegram Bot > AI Integration 3. Paste API key and save 4. Restart bot 5. Chat naturally! Technical Details: - Model: gemini-1.5-flash (fast, cost-effective) - Architecture: Service layer with dependency injection - Concurrency: Worker pool (max 10 concurrent) - Error Handling: Comprehensive with graceful degradation - Security: Admin-only, rate limited, API key encrypted - Cost: FREE for typical usage (15 req/min free tier) Testing: - No compilation errors - Backward compatible (no breaking changes) - Fallback mechanism tested - Documentation comprehensive Status: Production Ready
2026-02-02 16:45:04 +00:00
// updateAISetting updates AI configuration settings
func (a *SettingController) updateAISetting(c *gin.Context) {
var req struct {
Enabled bool `json:"enabled"`
ApiKey string `json:"apiKey"`
MaxTokens int `json:"maxTokens"`
Temperature float64 `json:"temperature"`
}
if err := c.ShouldBindJSON(&req); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), err)
return
}
// Update settings
if err := a.settingService.SetAIEnabled(req.Enabled); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), err)
return
}
if req.ApiKey != "" {
if err := a.settingService.SetAIApiKey(req.ApiKey); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), err)
return
}
}
if req.MaxTokens > 0 {
if err := a.settingService.SetAIMaxTokens(req.MaxTokens); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), err)
return
}
}
if req.Temperature > 0 {
tempStr := fmt.Sprintf("%.1f", req.Temperature)
if err := a.settingService.SetAISetting("aiTemperature", tempStr); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), err)
return
}
}
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.updateSettings"), nil)
}
// getAIStatus returns the current AI service status
func (a *SettingController) getAIStatus(c *gin.Context) {
enabled, _ := a.settingService.GetAIEnabled()
hasApiKey := false
if apiKey, err := a.settingService.GetAIApiKey(); err == nil && apiKey != "" {
hasApiKey = true
}
jsonObj(c, gin.H{
"enabled": enabled,
"hasApiKey": hasApiKey,
"ready": enabled && hasApiKey,
}, nil)
}