mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-01-15 10:09:34 +00:00
Compare commits
No commits in common. "de6e0df5d0cdf750d4fe312739b84e2559a82637" and "ae3e95b2f73abc883bf6e5bbc41bf0e76b7313bf" have entirely different histories.
de6e0df5d0
...
ae3e95b2f7
7 changed files with 44 additions and 73 deletions
|
|
@ -109,7 +109,7 @@ func GetLogFolder() string {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return filepath.Join(".", "log")
|
return filepath.Join(".", "log")
|
||||||
}
|
}
|
||||||
return "/var/log/x-ui"
|
return "/var/log"
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyFile(src, dst string) error {
|
func copyFile(src, dst string) error {
|
||||||
|
|
|
||||||
|
|
@ -641,7 +641,6 @@ install_x-ui() {
|
||||||
# Update x-ui cli and se set permission
|
# Update x-ui cli and se set permission
|
||||||
mv -f /usr/bin/x-ui-temp /usr/bin/x-ui
|
mv -f /usr/bin/x-ui-temp /usr/bin/x-ui
|
||||||
chmod +x /usr/bin/x-ui
|
chmod +x /usr/bin/x-ui
|
||||||
mkdir -p /var/log/x-ui
|
|
||||||
config_after_install
|
config_after_install
|
||||||
|
|
||||||
if [[ $release == "alpine" ]]; then
|
if [[ $release == "alpine" ]]; then
|
||||||
|
|
|
||||||
|
|
@ -285,7 +285,6 @@ update_x-ui() {
|
||||||
|
|
||||||
chmod +x /usr/local/x-ui/x-ui.sh >/dev/null 2>&1
|
chmod +x /usr/local/x-ui/x-ui.sh >/dev/null 2>&1
|
||||||
chmod +x /usr/bin/x-ui >/dev/null 2>&1
|
chmod +x /usr/bin/x-ui >/dev/null 2>&1
|
||||||
mkdir -p /var/log/x-ui >/dev/null 2>&1
|
|
||||||
|
|
||||||
echo -e "${green}Changing owner...${plain}"
|
echo -e "${green}Changing owner...${plain}"
|
||||||
chown -R root:root /usr/local/x-ui >/dev/null 2>&1
|
chown -R root:root /usr/local/x-ui >/dev/null 2>&1
|
||||||
|
|
|
||||||
2
web/assets/css/custom.min.css
vendored
2
web/assets/css/custom.min.css
vendored
File diff suppressed because one or more lines are too long
|
|
@ -529,18 +529,6 @@ func (s *ServerService) GetXrayVersions() ([]string, error) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Check HTTP status code - GitHub API returns object instead of array on error
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
||||||
var errorResponse struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
if json.Unmarshal(bodyBytes, &errorResponse) == nil && errorResponse.Message != "" {
|
|
||||||
return nil, fmt.Errorf("GitHub API error: %s", errorResponse.Message)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("GitHub API returned status %d: %s", resp.StatusCode, resp.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := bytes.NewBuffer(make([]byte, bufferSize))
|
buffer := bytes.NewBuffer(make([]byte, bufferSize))
|
||||||
buffer.Reset()
|
buffer.Reset()
|
||||||
if _, err := buffer.ReadFrom(resp.Body); err != nil {
|
if _, err := buffer.ReadFrom(resp.Body); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -174,10 +174,6 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Start is called again (e.g. during reload), ensure any previous long-polling
|
|
||||||
// loop is stopped before creating a new bot / receiver.
|
|
||||||
StopBot()
|
|
||||||
|
|
||||||
// Initialize hash storage to store callback queries
|
// Initialize hash storage to store callback queries
|
||||||
hashStorage = global.NewHashStorage(20 * time.Minute)
|
hashStorage = global.NewHashStorage(20 * time.Minute)
|
||||||
|
|
||||||
|
|
@ -211,7 +207,6 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedAdminIds := make([]int64, 0)
|
|
||||||
// Parse admin IDs from comma-separated string
|
// Parse admin IDs from comma-separated string
|
||||||
if tgBotID != "" {
|
if tgBotID != "" {
|
||||||
for _, adminID := range strings.Split(tgBotID, ",") {
|
for _, adminID := range strings.Split(tgBotID, ",") {
|
||||||
|
|
@ -220,12 +215,9 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
logger.Warning("Failed to parse admin ID from Telegram bot chat ID:", err)
|
logger.Warning("Failed to parse admin ID from Telegram bot chat ID:", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
parsedAdminIds = append(parsedAdminIds, int64(id))
|
adminIds = append(adminIds, int64(id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tgBotMutex.Lock()
|
|
||||||
adminIds = parsedAdminIds
|
|
||||||
tgBotMutex.Unlock()
|
|
||||||
|
|
||||||
// Get Telegram bot proxy URL
|
// Get Telegram bot proxy URL
|
||||||
tgBotProxy, err := t.settingService.GetTgBotProxy()
|
tgBotProxy, err := t.settingService.GetTgBotProxy()
|
||||||
|
|
@ -260,12 +252,10 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start receiving Telegram bot messages
|
// Start receiving Telegram bot messages
|
||||||
tgBotMutex.Lock()
|
if !isRunning {
|
||||||
alreadyRunning := isRunning || botCancel != nil
|
|
||||||
tgBotMutex.Unlock()
|
|
||||||
if !alreadyRunning {
|
|
||||||
logger.Info("Telegram bot receiver started")
|
logger.Info("Telegram bot receiver started")
|
||||||
go t.OnReceive()
|
go t.OnReceive()
|
||||||
|
isRunning = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -310,8 +300,6 @@ func (t *Tgbot) NewBot(token string, proxyUrl string, apiServerUrl string) (*tel
|
||||||
|
|
||||||
// IsRunning checks if the Telegram bot is currently running.
|
// IsRunning checks if the Telegram bot is currently running.
|
||||||
func (t *Tgbot) IsRunning() bool {
|
func (t *Tgbot) IsRunning() bool {
|
||||||
tgBotMutex.Lock()
|
|
||||||
defer tgBotMutex.Unlock()
|
|
||||||
return isRunning
|
return isRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -329,34 +317,34 @@ func (t *Tgbot) SetHostname() {
|
||||||
// Stop safely stops the Telegram bot's Long Polling operation.
|
// Stop safely stops the Telegram bot's Long Polling operation.
|
||||||
// This method now calls the global StopBot function and cleans up other resources.
|
// This method now calls the global StopBot function and cleans up other resources.
|
||||||
func (t *Tgbot) Stop() {
|
func (t *Tgbot) Stop() {
|
||||||
|
// Call the global StopBot function to gracefully shut down Long Polling
|
||||||
StopBot()
|
StopBot()
|
||||||
|
|
||||||
|
// Stop the bot handler (in case the goroutine hasn't exited yet)
|
||||||
|
if botHandler != nil {
|
||||||
|
botHandler.Stop()
|
||||||
|
}
|
||||||
logger.Info("Stop Telegram receiver ...")
|
logger.Info("Stop Telegram receiver ...")
|
||||||
tgBotMutex.Lock()
|
isRunning = false
|
||||||
adminIds = nil
|
adminIds = nil
|
||||||
tgBotMutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopBot safely stops the Telegram bot's Long Polling operation by cancelling its context.
|
// StopBot safely stops the Telegram bot's Long Polling operation by cancelling its context.
|
||||||
// This is the global function called from main.go's signal handler and t.Stop().
|
// This is the global function called from main.go's signal handler and t.Stop().
|
||||||
func StopBot() {
|
func StopBot() {
|
||||||
// Don't hold the mutex while cancelling/waiting.
|
|
||||||
tgBotMutex.Lock()
|
tgBotMutex.Lock()
|
||||||
cancel := botCancel
|
defer tgBotMutex.Unlock()
|
||||||
botCancel = nil
|
|
||||||
handler := botHandler
|
|
||||||
botHandler = nil
|
|
||||||
isRunning = false
|
|
||||||
tgBotMutex.Unlock()
|
|
||||||
|
|
||||||
if handler != nil {
|
if botCancel != nil {
|
||||||
handler.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
if cancel != nil {
|
|
||||||
logger.Info("Sending cancellation signal to Telegram bot...")
|
logger.Info("Sending cancellation signal to Telegram bot...")
|
||||||
// Cancels the context passed to UpdatesViaLongPolling; this closes updates channel
|
|
||||||
// and lets botHandler.Start() exit cleanly.
|
// Calling botCancel() cancels the context passed to UpdatesViaLongPolling,
|
||||||
cancel()
|
// which stops the Long Polling operation and closes the updates channel,
|
||||||
|
// allowing the th.Start() goroutine to exit cleanly.
|
||||||
|
botCancel()
|
||||||
|
|
||||||
|
botCancel = nil
|
||||||
|
// Giving the goroutine a small delay to exit cleanly.
|
||||||
botWG.Wait()
|
botWG.Wait()
|
||||||
logger.Info("Telegram bot successfully stopped.")
|
logger.Info("Telegram bot successfully stopped.")
|
||||||
}
|
}
|
||||||
|
|
@ -391,38 +379,36 @@ func (t *Tgbot) OnReceive() {
|
||||||
params := telego.GetUpdatesParams{
|
params := telego.GetUpdatesParams{
|
||||||
Timeout: 30, // Increased timeout to reduce API calls
|
Timeout: 30, // Increased timeout to reduce API calls
|
||||||
}
|
}
|
||||||
// Strict singleton: never start a second long-polling loop.
|
// --- GRACEFUL SHUTDOWN FIX: Context creation ---
|
||||||
tgBotMutex.Lock()
|
tgBotMutex.Lock()
|
||||||
if botCancel != nil || isRunning {
|
|
||||||
tgBotMutex.Unlock()
|
// Create a context with cancellation and store the cancel function.
|
||||||
logger.Warning("TgBot OnReceive called while already running; ignoring.")
|
var ctx context.Context
|
||||||
return
|
|
||||||
|
// Check if botCancel is already set (to prevent race condition overwrite and goroutine leak)
|
||||||
|
if botCancel == nil {
|
||||||
|
ctx, botCancel = context.WithCancel(context.Background())
|
||||||
|
} else {
|
||||||
|
// If botCancel is already set, use a non-cancellable context for this redundant call.
|
||||||
|
// This prevents overwriting the active botCancel and causing a goroutine leak from the previous call.
|
||||||
|
logger.Warning("TgBot OnReceive called concurrently. Using background context for redundant call.")
|
||||||
|
ctx = context.Background() // <<< ИЗМЕНЕНИЕ
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
botCancel = cancel
|
|
||||||
isRunning = true
|
|
||||||
// Add to WaitGroup before releasing the lock so StopBot() can't return
|
|
||||||
// before this receiver goroutine is accounted for.
|
|
||||||
botWG.Add(1)
|
|
||||||
tgBotMutex.Unlock()
|
tgBotMutex.Unlock()
|
||||||
|
|
||||||
// Get updates channel using the context.
|
// Get updates channel using the context.
|
||||||
updates, _ := bot.UpdatesViaLongPolling(ctx, ¶ms)
|
updates, _ := bot.UpdatesViaLongPolling(ctx, ¶ms)
|
||||||
go func() {
|
botWG.Go(func() {
|
||||||
defer botWG.Done()
|
|
||||||
h, _ := th.NewBotHandler(bot, updates)
|
|
||||||
tgBotMutex.Lock()
|
|
||||||
botHandler = h
|
|
||||||
tgBotMutex.Unlock()
|
|
||||||
|
|
||||||
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
botHandler, _ = th.NewBotHandler(bot, updates)
|
||||||
|
botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
||||||
delete(userStates, message.Chat.ID)
|
delete(userStates, message.Chat.ID)
|
||||||
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
|
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
|
||||||
return nil
|
return nil
|
||||||
}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
|
}, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
|
||||||
|
|
||||||
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
||||||
// Use goroutine with worker pool for concurrent command processing
|
// Use goroutine with worker pool for concurrent command processing
|
||||||
go func() {
|
go func() {
|
||||||
messageWorkerPool <- struct{}{} // Acquire worker
|
messageWorkerPool <- struct{}{} // Acquire worker
|
||||||
|
|
@ -434,7 +420,7 @@ func (t *Tgbot) OnReceive() {
|
||||||
return nil
|
return nil
|
||||||
}, th.AnyCommand())
|
}, th.AnyCommand())
|
||||||
|
|
||||||
h.HandleCallbackQuery(func(ctx *th.Context, query telego.CallbackQuery) error {
|
botHandler.HandleCallbackQuery(func(ctx *th.Context, query telego.CallbackQuery) error {
|
||||||
// Use goroutine with worker pool for concurrent callback processing
|
// Use goroutine with worker pool for concurrent callback processing
|
||||||
go func() {
|
go func() {
|
||||||
messageWorkerPool <- struct{}{} // Acquire worker
|
messageWorkerPool <- struct{}{} // Acquire worker
|
||||||
|
|
@ -446,7 +432,7 @@ func (t *Tgbot) OnReceive() {
|
||||||
return nil
|
return nil
|
||||||
}, th.AnyCallbackQueryWithMessage())
|
}, th.AnyCallbackQueryWithMessage())
|
||||||
|
|
||||||
h.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
botHandler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
||||||
if userState, exists := userStates[message.Chat.ID]; exists {
|
if userState, exists := userStates[message.Chat.ID]; exists {
|
||||||
switch userState {
|
switch userState {
|
||||||
case "awaiting_id":
|
case "awaiting_id":
|
||||||
|
|
@ -592,8 +578,8 @@ func (t *Tgbot) OnReceive() {
|
||||||
return nil
|
return nil
|
||||||
}, th.AnyMessage())
|
}, th.AnyMessage())
|
||||||
|
|
||||||
h.Start()
|
botHandler.Start()
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// answerCommand processes incoming command messages from Telegram users.
|
// answerCommand processes incoming command messages from Telegram users.
|
||||||
|
|
|
||||||
3
x-ui.sh
3
x-ui.sh
|
|
@ -53,8 +53,7 @@ os_version=""
|
||||||
os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.')
|
os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.')
|
||||||
|
|
||||||
# Declare Variables
|
# Declare Variables
|
||||||
log_folder="${XUI_LOG_FOLDER:=/var/log/x-ui}"
|
log_folder="${XUI_LOG_FOLDER:=/var/log}"
|
||||||
mkdir -p "${log_folder}"
|
|
||||||
iplimit_log_path="${log_folder}/3xipl.log"
|
iplimit_log_path="${log_folder}/3xipl.log"
|
||||||
iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
|
iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue