2025-07-24 05:06:14 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-22 19:09:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-09-25 13:37:50 +00:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/database"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/database/model"
|
2025-07-24 05:06:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MultiServerService struct{}
|
|
|
|
|
|
|
|
|
|
func (s *MultiServerService) GetServers() ([]*model.Server, error) {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
var servers []*model.Server
|
|
|
|
|
err := db.Find(&servers).Error
|
|
|
|
|
return servers, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *MultiServerService) GetServer(id int) (*model.Server, error) {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
var server model.Server
|
|
|
|
|
err := db.First(&server, id).Error
|
|
|
|
|
return &server, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 19:09:55 +00:00
|
|
|
// GetOnlineClients
|
|
|
|
|
func (s *MultiServerService) GetOnlineClients() (map[int][]string, error) {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
var servers []*model.Server
|
|
|
|
|
err := db.Find(&servers).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clients map[int][]string
|
|
|
|
|
for _, server := range servers {
|
|
|
|
|
var onlineResp struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
|
Obj []string `json:"obj"`
|
|
|
|
|
}
|
|
|
|
|
url := fmt.Sprintf("http://%s:%d%spanel/api/inbounds/onlines", server.Address, server.Port, server.SecretWebPath)
|
|
|
|
|
resp, err := http.Post(url, "application/json", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&onlineResp); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decode online: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if !onlineResp.Success {
|
|
|
|
|
return nil, fmt.Errorf("failed to get online list at %s", server.Address)
|
|
|
|
|
}
|
|
|
|
|
clients[server.Id] = onlineResp.Obj
|
|
|
|
|
}
|
|
|
|
|
return clients, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 05:06:14 +00:00
|
|
|
func (s *MultiServerService) AddServer(server *model.Server) error {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
return db.Create(server).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *MultiServerService) UpdateServer(server *model.Server) error {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
return db.Save(server).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *MultiServerService) DeleteServer(id int) error {
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
return db.Delete(&model.Server{}, id).Error
|
|
|
|
|
}
|