123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040 |
- package service
- import (
- "encoding/json"
- "errors"
- "fmt"
- "regexp"
- "strconv"
- "strings"
- "time"
- "x-ui/database"
- "x-ui/database/model"
- "x-ui/logger"
- "x-ui/util/common"
- "x-ui/xray"
- "gorm.io/gorm"
- )
- type InboundService struct {
- xrayApi xray.XrayAPI
- }
- func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
- db := database.GetDB()
- var inbounds []*model.Inbound
- err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Find(&inbounds).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, err
- }
- return inbounds, nil
- }
- func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
- db := database.GetDB()
- var inbounds []*model.Inbound
- err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, err
- }
- return inbounds, nil
- }
- func (s *InboundService) checkPortExist(listen string, port int, ignoreId int) (bool, error) {
- db := database.GetDB()
- if listen == "" || listen == "0.0.0.0" || listen == "::" || listen == "::0" {
- db = db.Model(model.Inbound{}).Where("port = ?", port)
- } else {
- db = db.Model(model.Inbound{}).
- Where("port = ?", port).
- Where(
- db.Model(model.Inbound{}).Where(
- "listen = ?", listen,
- ).Or(
- "listen = \"\"",
- ).Or(
- "listen = \"0.0.0.0\"",
- ).Or(
- "listen = \"::\"",
- ).Or(
- "listen = \"::0\""))
- }
- if ignoreId > 0 {
- db = db.Where("id != ?", ignoreId)
- }
- var count int64
- err := db.Count(&count).Error
- if err != nil {
- return false, err
- }
- return count > 0, nil
- }
- func (s *InboundService) GetClients(inbound *model.Inbound) ([]model.Client, error) {
- settings := map[string][]model.Client{}
- json.Unmarshal([]byte(inbound.Settings), &settings)
- if settings == nil {
- return nil, fmt.Errorf("setting is null")
- }
- clients := settings["clients"]
- if clients == nil {
- return nil, nil
- }
- return clients, nil
- }
- func (s *InboundService) getAllEmails() ([]string, error) {
- db := database.GetDB()
- var emails []string
- err := db.Raw(`
- SELECT JSON_EXTRACT(client.value, '$.email')
- FROM inbounds,
- JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
- `).Scan(&emails).Error
- if err != nil {
- return nil, err
- }
- return emails, nil
- }
- func (s *InboundService) contains(slice []string, str string) bool {
- for _, s := range slice {
- if s == str {
- return true
- }
- }
- return false
- }
- func (s *InboundService) checkEmailsExistForClients(clients []model.Client) (string, error) {
- allEmails, err := s.getAllEmails()
- if err != nil {
- return "", err
- }
- var emails []string
- for _, client := range clients {
- if client.Email != "" {
- if s.contains(emails, client.Email) {
- return client.Email, nil
- }
- if s.contains(allEmails, client.Email) {
- return client.Email, nil
- }
- emails = append(emails, client.Email)
- }
- }
- return "", nil
- }
- func (s *InboundService) checkEmailExistForInbound(inbound *model.Inbound) (string, error) {
- clients, err := s.GetClients(inbound)
- if err != nil {
- return "", err
- }
- allEmails, err := s.getAllEmails()
- if err != nil {
- return "", err
- }
- var emails []string
- for _, client := range clients {
- if client.Email != "" {
- if s.contains(emails, client.Email) {
- return client.Email, nil
- }
- if s.contains(allEmails, client.Email) {
- return client.Email, nil
- }
- emails = append(emails, client.Email)
- }
- }
- return "", nil
- }
- func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
- exist, err := s.checkPortExist(inbound.Listen, inbound.Port, 0)
- if err != nil {
- return inbound, false, err
- }
- if exist {
- return inbound, false, common.NewError("Port already exists:", inbound.Port)
- }
- existEmail, err := s.checkEmailExistForInbound(inbound)
- if err != nil {
- return inbound, false, err
- }
- if existEmail != "" {
- return inbound, false, common.NewError("Duplicate email:", existEmail)
- }
- clients, err := s.GetClients(inbound)
- if err != nil {
- return inbound, false, err
- }
- // Secure client ID
- for _, client := range clients {
- if inbound.Protocol == "trojan" {
- if client.Password == "" {
- return inbound, false, common.NewError("empty client ID")
- }
- } else if inbound.Protocol == "shadowsocks" {
- if client.Email == "" {
- return inbound, false, common.NewError("empty client ID")
- }
- } else {
- if client.ID == "" {
- return inbound, false, common.NewError("empty client ID")
- }
- }
- }
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err == nil {
- tx.Commit()
- } else {
- tx.Rollback()
- }
- }()
- err = tx.Save(inbound).Error
- if err == nil {
- if len(inbound.ClientStats) == 0 {
- for _, client := range clients {
- s.AddClientStat(tx, inbound.Id, &client)
- }
- }
- } else {
- return inbound, false, err
- }
- needRestart := false
- if inbound.Enable {
- s.xrayApi.Init(p.GetAPIPort())
- inboundJson, err1 := json.MarshalIndent(inbound.GenXrayInboundConfig(), "", " ")
- if err1 != nil {
- logger.Debug("Unable to marshal inbound config:", err1)
- }
- err1 = s.xrayApi.AddInbound(inboundJson)
- if err1 == nil {
- logger.Debug("New inbound added by api:", inbound.Tag)
- } else {
- logger.Debug("Unable to add inbound by api:", err1)
- needRestart = true
- }
- s.xrayApi.Close()
- }
- return inbound, needRestart, err
- }
- func (s *InboundService) DelInbound(id int) (bool, error) {
- db := database.GetDB()
- var tag string
- needRestart := false
- result := db.Model(model.Inbound{}).Select("tag").Where("id = ? and enable = ?", id, true).First(&tag)
- if result.Error == nil {
- s.xrayApi.Init(p.GetAPIPort())
- err1 := s.xrayApi.DelInbound(tag)
- if err1 == nil {
- logger.Debug("Inbound deleted by api:", tag)
- } else {
- logger.Debug("Unable to delete inbound by api:", err1)
- needRestart = true
- }
- s.xrayApi.Close()
- } else {
- logger.Debug("No enabled inbound founded to removing by api", tag)
- }
- // Delete client traffics of inbounds
- err := db.Where("inbound_id = ?", id).Delete(xray.ClientTraffic{}).Error
- if err != nil {
- return false, err
- }
- inbound, err := s.GetInbound(id)
- if err != nil {
- return false, err
- }
- clients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- for _, client := range clients {
- err := s.DelClientIPs(db, client.Email)
- if err != nil {
- return false, err
- }
- }
- return needRestart, db.Delete(model.Inbound{}, id).Error
- }
- func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
- db := database.GetDB()
- inbound := &model.Inbound{}
- err := db.Model(model.Inbound{}).First(inbound, id).Error
- if err != nil {
- return nil, err
- }
- return inbound, nil
- }
- func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
- exist, err := s.checkPortExist(inbound.Listen, inbound.Port, inbound.Id)
- if err != nil {
- return inbound, false, err
- }
- if exist {
- return inbound, false, common.NewError("Port already exists:", inbound.Port)
- }
- oldInbound, err := s.GetInbound(inbound.Id)
- if err != nil {
- return inbound, false, err
- }
- tag := oldInbound.Tag
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- err = s.updateClientTraffics(tx, oldInbound, inbound)
- if err != nil {
- return inbound, false, err
- }
- oldInbound.Up = inbound.Up
- oldInbound.Down = inbound.Down
- oldInbound.Total = inbound.Total
- oldInbound.Remark = inbound.Remark
- oldInbound.Enable = inbound.Enable
- oldInbound.ExpiryTime = inbound.ExpiryTime
- oldInbound.Listen = inbound.Listen
- oldInbound.Port = inbound.Port
- oldInbound.Protocol = inbound.Protocol
- oldInbound.Settings = inbound.Settings
- oldInbound.StreamSettings = inbound.StreamSettings
- oldInbound.Sniffing = inbound.Sniffing
- if inbound.Listen == "" || inbound.Listen == "0.0.0.0" || inbound.Listen == "::" || inbound.Listen == "::0" {
- oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
- } else {
- oldInbound.Tag = fmt.Sprintf("inbound-%v:%v", inbound.Listen, inbound.Port)
- }
- needRestart := false
- s.xrayApi.Init(p.GetAPIPort())
- if s.xrayApi.DelInbound(tag) == nil {
- logger.Debug("Old inbound deleted by api:", tag)
- }
- if inbound.Enable {
- inboundJson, err2 := json.MarshalIndent(oldInbound.GenXrayInboundConfig(), "", " ")
- if err2 != nil {
- logger.Debug("Unable to marshal updated inbound config:", err2)
- needRestart = true
- } else {
- err2 = s.xrayApi.AddInbound(inboundJson)
- if err2 == nil {
- logger.Debug("Updated inbound added by api:", oldInbound.Tag)
- } else {
- logger.Debug("Unable to update inbound by api:", err2)
- needRestart = true
- }
- }
- }
- s.xrayApi.Close()
- return inbound, needRestart, tx.Save(oldInbound).Error
- }
- func (s *InboundService) updateClientTraffics(tx *gorm.DB, oldInbound *model.Inbound, newInbound *model.Inbound) error {
- oldClients, err := s.GetClients(oldInbound)
- if err != nil {
- return err
- }
- newClients, err := s.GetClients(newInbound)
- if err != nil {
- return err
- }
- var emailExists bool
- for _, oldClient := range oldClients {
- emailExists = false
- for _, newClient := range newClients {
- if oldClient.Email == newClient.Email {
- emailExists = true
- break
- }
- }
- if !emailExists {
- err = s.DelClientStat(tx, oldClient.Email)
- if err != nil {
- return err
- }
- }
- }
- for _, newClient := range newClients {
- emailExists = false
- for _, oldClient := range oldClients {
- if newClient.Email == oldClient.Email {
- emailExists = true
- break
- }
- }
- if !emailExists {
- err = s.AddClientStat(tx, oldInbound.Id, &newClient)
- if err != nil {
- return err
- }
- }
- }
- return nil
- }
- func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) {
- clients, err := s.GetClients(data)
- if err != nil {
- return false, err
- }
- email := clients[0].Email
- valid, err := validateEmail(email)
- if !valid {
- return false, err
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(data.Settings), &settings)
- if err != nil {
- return false, err
- }
- interfaceClients := settings["clients"].([]interface{})
- existEmail, err := s.checkEmailsExistForClients(clients)
- if err != nil {
- return false, err
- }
- if existEmail != "" {
- return false, common.NewError("Duplicate email:", existEmail)
- }
- oldInbound, err := s.GetInbound(data.Id)
- if err != nil {
- return false, err
- }
- // Secure client ID
- for _, client := range clients {
- if oldInbound.Protocol == "trojan" {
- if client.Password == "" {
- return false, common.NewError("empty client ID")
- }
- } else if oldInbound.Protocol == "shadowsocks" {
- if client.Email == "" {
- return false, common.NewError("empty client ID")
- }
- } else {
- if client.ID == "" {
- return false, common.NewError("empty client ID")
- }
- }
- }
- var oldSettings map[string]interface{}
- err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
- if err != nil {
- return false, err
- }
- oldClients := oldSettings["clients"].([]interface{})
- oldClients = append(oldClients, interfaceClients...)
- oldSettings["clients"] = oldClients
- newSettings, err := json.MarshalIndent(oldSettings, "", " ")
- if err != nil {
- return false, err
- }
- oldInbound.Settings = string(newSettings)
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- needRestart := false
- s.xrayApi.Init(p.GetAPIPort())
- for _, client := range clients {
- if len(client.Email) > 0 {
- s.AddClientStat(tx, data.Id, &client)
- if client.Enable {
- cipher := ""
- if oldInbound.Protocol == "shadowsocks" {
- cipher = oldSettings["method"].(string)
- }
- err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
- "email": client.Email,
- "id": client.ID,
- "security": client.Security,
- "flow": client.Flow,
- "password": client.Password,
- "cipher": cipher,
- })
- if err1 == nil {
- logger.Debug("Client added by api:", client.Email)
- } else {
- logger.Debug("Error in adding client by api:", err1)
- needRestart = true
- }
- }
- } else {
- needRestart = true
- }
- }
- s.xrayApi.Close()
- return needRestart, tx.Save(oldInbound).Error
- }
- func (s *InboundService) DelInboundClient(inboundId int, clientId string) (bool, error) {
- oldInbound, err := s.GetInbound(inboundId)
- if err != nil {
- logger.Error("Load Old Data Error")
- return false, err
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(oldInbound.Settings), &settings)
- if err != nil {
- return false, err
- }
- email := ""
- client_key := "id"
- if oldInbound.Protocol == "trojan" {
- client_key = "password"
- }
- if oldInbound.Protocol == "shadowsocks" {
- client_key = "email"
- }
- interfaceClients := settings["clients"].([]interface{})
- var newClients []interface{}
- needApiDel := false
- for _, client := range interfaceClients {
- c := client.(map[string]interface{})
- c_id := c[client_key].(string)
- if c_id == clientId {
- email, _ = c["email"].(string)
- needApiDel, _ = c["enable"].(bool)
- } else {
- newClients = append(newClients, client)
- }
- }
- if len(newClients) == 0 {
- return false, common.NewError("no client remained in Inbound")
- }
- settings["clients"] = newClients
- newSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, err
- }
- oldInbound.Settings = string(newSettings)
- db := database.GetDB()
- err = s.DelClientIPs(db, email)
- if err != nil {
- logger.Error("Error in delete client IPs")
- return false, err
- }
- needRestart := false
- if len(email) > 0 {
- notDepleted := true
- err = db.Model(xray.ClientTraffic{}).Select("enable").Where("email = ?", email).First(¬Depleted).Error
- if err != nil {
- logger.Error("Get stats error")
- return false, err
- }
- err = s.DelClientStat(db, email)
- if err != nil {
- logger.Error("Delete stats Data Error")
- return false, err
- }
- if needApiDel && notDepleted {
- s.xrayApi.Init(p.GetAPIPort())
- err1 := s.xrayApi.RemoveUser(oldInbound.Tag, email)
- if err1 == nil {
- logger.Debug("Client deleted by api:", email)
- needRestart = false
- } else {
- logger.Debug("Unable to del client by api:", err1)
- needRestart = true
- }
- s.xrayApi.Close()
- }
- }
- return needRestart, db.Save(oldInbound).Error
- }
- func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId string) (bool, error) {
- clients, err := s.GetClients(data)
- if err != nil {
- return false, err
- }
- email := clients[0].Email
- valid, err := validateEmail(email)
- if !valid {
- return false, err
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(data.Settings), &settings)
- if err != nil {
- return false, err
- }
- interfaceClients := settings["clients"].([]interface{})
- oldInbound, err := s.GetInbound(data.Id)
- if err != nil {
- return false, err
- }
- oldClients, err := s.GetClients(oldInbound)
- if err != nil {
- return false, err
- }
- oldEmail := ""
- newClientId := ""
- clientIndex := -1
- for index, oldClient := range oldClients {
- oldClientId := ""
- if oldInbound.Protocol == "trojan" {
- oldClientId = oldClient.Password
- newClientId = clients[0].Password
- } else if oldInbound.Protocol == "shadowsocks" {
- oldClientId = oldClient.Email
- newClientId = clients[0].Email
- } else {
- oldClientId = oldClient.ID
- newClientId = clients[0].ID
- }
- if clientId == oldClientId {
- oldEmail = oldClient.Email
- clientIndex = index
- break
- }
- }
- // Validate new client ID
- if newClientId == "" || clientIndex == -1 {
- return false, common.NewError("empty client ID")
- }
- if len(clients[0].Email) > 0 && clients[0].Email != oldEmail {
- existEmail, err := s.checkEmailsExistForClients(clients)
- if err != nil {
- return false, err
- }
- if existEmail != "" {
- return false, common.NewError("Duplicate email:", existEmail)
- }
- }
- var oldSettings map[string]interface{}
- err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
- if err != nil {
- return false, err
- }
- settingsClients := oldSettings["clients"].([]interface{})
- settingsClients[clientIndex] = interfaceClients[0]
- oldSettings["clients"] = settingsClients
- newSettings, err := json.MarshalIndent(oldSettings, "", " ")
- if err != nil {
- return false, err
- }
- oldInbound.Settings = string(newSettings)
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- if len(clients[0].Email) > 0 {
- if len(oldEmail) > 0 {
- err = s.UpdateClientStat(tx, oldEmail, &clients[0])
- if err != nil {
- return false, err
- }
- err = s.UpdateClientIPs(tx, oldEmail, clients[0].Email)
- if err != nil {
- return false, err
- }
- } else {
- s.AddClientStat(tx, data.Id, &clients[0])
- }
- } else {
- err = s.DelClientStat(tx, oldEmail)
- if err != nil {
- return false, err
- }
- err = s.DelClientIPs(tx, oldEmail)
- if err != nil {
- return false, err
- }
- }
- needRestart := false
- if len(oldEmail) > 0 {
- s.xrayApi.Init(p.GetAPIPort())
- if oldClients[clientIndex].Enable {
- err1 := s.xrayApi.RemoveUser(oldInbound.Tag, oldEmail)
- if err1 == nil {
- logger.Debug("Old client deleted by api:", clients[0].Email)
- } else {
- logger.Debug("Error in deleting client by api:", err1)
- needRestart = true
- }
- }
- if clients[0].Enable {
- cipher := ""
- if oldInbound.Protocol == "shadowsocks" {
- cipher = oldSettings["method"].(string)
- }
- err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
- "email": clients[0].Email,
- "id": clients[0].ID,
- "security": clients[0].Security,
- "flow": clients[0].Flow,
- "password": clients[0].Password,
- "cipher": cipher,
- })
- if err1 == nil {
- logger.Debug("Client edited by api:", clients[0].Email)
- } else {
- logger.Debug("Error in adding client by api:", err1)
- needRestart = true
- }
- }
- s.xrayApi.Close()
- } else {
- logger.Debug("Client old email not found")
- needRestart = true
- }
- return needRestart, tx.Save(oldInbound).Error
- }
- func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) {
- var err error
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- err = s.addInboundTraffic(tx, inboundTraffics)
- if err != nil {
- return err, false
- }
- err = s.addClientTraffic(tx, clientTraffics)
- if err != nil {
- return err, false
- }
- needRestart0, count, err := s.autoRenewClients(tx)
- if err != nil {
- logger.Warning("Error in renew clients:", err)
- } else if count > 0 {
- logger.Debugf("%v clients renewed", count)
- }
- needRestart1, count, err := s.disableInvalidClients(tx)
- if err != nil {
- logger.Warning("Error in disabling invalid clients:", err)
- } else if count > 0 {
- logger.Debugf("%v clients disabled", count)
- }
- needRestart2, count, err := s.disableInvalidInbounds(tx)
- if err != nil {
- logger.Warning("Error in disabling invalid inbounds:", err)
- } else if count > 0 {
- logger.Debugf("%v inbounds disabled", count)
- }
- return nil, (needRestart0 || needRestart1 || needRestart2)
- }
- func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
- if len(traffics) == 0 {
- return nil
- }
- var err error
- for _, traffic := range traffics {
- if traffic.IsInbound {
- err = tx.Model(&model.Inbound{}).Where("tag = ?", traffic.Tag).
- Updates(map[string]interface{}{
- "up": gorm.Expr("up + ?", traffic.Up),
- "down": gorm.Expr("down + ?", traffic.Down),
- }).Error
- if err != nil {
- return err
- }
- }
- }
- return nil
- }
- func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
- if len(traffics) == 0 {
- // Empty onlineUsers
- if p != nil {
- p.SetOnlineClients(nil)
- }
- return nil
- }
- var onlineClients []string
- emails := make([]string, 0, len(traffics))
- for _, traffic := range traffics {
- emails = append(emails, traffic.Email)
- }
- dbClientTraffics := make([]*xray.ClientTraffic, 0, len(traffics))
- err = tx.Model(xray.ClientTraffic{}).Where("email IN (?)", emails).Find(&dbClientTraffics).Error
- if err != nil {
- return err
- }
- // Avoid empty slice error
- if len(dbClientTraffics) == 0 {
- return nil
- }
- dbClientTraffics, err = s.adjustTraffics(tx, dbClientTraffics)
- if err != nil {
- return err
- }
- for dbTraffic_index := range dbClientTraffics {
- for traffic_index := range traffics {
- if dbClientTraffics[dbTraffic_index].Email == traffics[traffic_index].Email {
- dbClientTraffics[dbTraffic_index].Up += traffics[traffic_index].Up
- dbClientTraffics[dbTraffic_index].Down += traffics[traffic_index].Down
- // Add user in onlineUsers array on traffic
- if traffics[traffic_index].Up+traffics[traffic_index].Down > 0 {
- onlineClients = append(onlineClients, traffics[traffic_index].Email)
- }
- break
- }
- }
- }
- // Set onlineUsers
- p.SetOnlineClients(onlineClients)
- err = tx.Save(dbClientTraffics).Error
- if err != nil {
- logger.Warning("AddClientTraffic update data ", err)
- }
- return nil
- }
- func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
- inboundIds := make([]int, 0, len(dbClientTraffics))
- for _, dbClientTraffic := range dbClientTraffics {
- if dbClientTraffic.ExpiryTime < 0 {
- inboundIds = append(inboundIds, dbClientTraffic.InboundId)
- }
- }
- if len(inboundIds) > 0 {
- var inbounds []*model.Inbound
- err := tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
- if err != nil {
- return nil, err
- }
- for inbound_index := range inbounds {
- settings := map[string]interface{}{}
- json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
- clients, ok := settings["clients"].([]interface{})
- if ok {
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- for traffic_index := range dbClientTraffics {
- if dbClientTraffics[traffic_index].ExpiryTime < 0 && c["email"] == dbClientTraffics[traffic_index].Email {
- oldExpiryTime := c["expiryTime"].(float64)
- newExpiryTime := (time.Now().Unix() * 1000) - int64(oldExpiryTime)
- c["expiryTime"] = newExpiryTime
- dbClientTraffics[traffic_index].ExpiryTime = newExpiryTime
- break
- }
- }
- newClients = append(newClients, interface{}(c))
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return nil, err
- }
- inbounds[inbound_index].Settings = string(modifiedSettings)
- }
- }
- err = tx.Save(inbounds).Error
- if err != nil {
- logger.Warning("AddClientTraffic update inbounds ", err)
- logger.Error(inbounds)
- }
- }
- return dbClientTraffics, nil
- }
- func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
- // check for time expired
- var traffics []*xray.ClientTraffic
- now := time.Now().Unix() * 1000
- var err, err1 error
- err = tx.Model(xray.ClientTraffic{}).Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).Find(&traffics).Error
- if err != nil {
- return false, 0, err
- }
- // return if there is no client to renew
- if len(traffics) == 0 {
- return false, 0, nil
- }
- var inbound_ids []int
- var inbounds []*model.Inbound
- needRestart := false
- var clientsToAdd []struct {
- protocol string
- tag string
- client map[string]interface{}
- }
- for _, traffic := range traffics {
- inbound_ids = append(inbound_ids, traffic.InboundId)
- }
- err = tx.Model(model.Inbound{}).Where("id IN ?", inbound_ids).Find(&inbounds).Error
- if err != nil {
- return false, 0, err
- }
- for inbound_index := range inbounds {
- settings := map[string]interface{}{}
- json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
- clients := settings["clients"].([]interface{})
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- for traffic_index, traffic := range traffics {
- if traffic.Email == c["email"].(string) {
- newExpiryTime := traffic.ExpiryTime
- for newExpiryTime < now {
- newExpiryTime += (int64(traffic.Reset) * 86400000)
- }
- c["expiryTime"] = newExpiryTime
- traffics[traffic_index].ExpiryTime = newExpiryTime
- traffics[traffic_index].Down = 0
- traffics[traffic_index].Up = 0
- if !traffic.Enable {
- traffics[traffic_index].Enable = true
- clientsToAdd = append(clientsToAdd,
- struct {
- protocol string
- tag string
- client map[string]interface{}
- }{
- protocol: string(inbounds[inbound_index].Protocol),
- tag: inbounds[inbound_index].Tag,
- client: c,
- })
- }
- clients[client_index] = interface{}(c)
- break
- }
- }
- }
- settings["clients"] = clients
- newSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, 0, err
- }
- inbounds[inbound_index].Settings = string(newSettings)
- }
- err = tx.Save(inbounds).Error
- if err != nil {
- return false, 0, err
- }
- err = tx.Save(traffics).Error
- if err != nil {
- return false, 0, err
- }
- if p != nil {
- err1 = s.xrayApi.Init(p.GetAPIPort())
- if err1 != nil {
- return true, int64(len(traffics)), nil
- }
- for _, clientToAdd := range clientsToAdd {
- err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
- if err1 != nil {
- needRestart = true
- }
- }
- s.xrayApi.Close()
- }
- return needRestart, int64(len(traffics)), nil
- }
- func (s *InboundService) disableInvalidInbounds(tx *gorm.DB) (bool, int64, error) {
- now := time.Now().Unix() * 1000
- needRestart := false
- if p != nil {
- var tags []string
- err := tx.Table("inbounds").
- Select("inbounds.tag").
- Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
- Scan(&tags).Error
- if err != nil {
- return false, 0, err
- }
- s.xrayApi.Init(p.GetAPIPort())
- for _, tag := range tags {
- err1 := s.xrayApi.DelInbound(tag)
- if err1 == nil {
- logger.Debug("Inbound disabled by api:", tag)
- } else {
- logger.Debug("Error in disabling inbound by api:", err1)
- needRestart = true
- }
- }
- s.xrayApi.Close()
- }
- result := tx.Model(model.Inbound{}).
- Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
- Update("enable", false)
- err := result.Error
- count := result.RowsAffected
- return needRestart, count, err
- }
- func (s *InboundService) disableInvalidClients(tx *gorm.DB) (bool, int64, error) {
- now := time.Now().Unix() * 1000
- needRestart := false
- if p != nil {
- var results []struct {
- Tag string
- Email string
- }
- err := tx.Table("inbounds").
- Select("inbounds.tag, client_traffics.email").
- Joins("JOIN client_traffics ON inbounds.id = client_traffics.inbound_id").
- Where("((client_traffics.total > 0 AND client_traffics.up + client_traffics.down >= client_traffics.total) OR (client_traffics.expiry_time > 0 AND client_traffics.expiry_time <= ?)) AND client_traffics.enable = ?", now, true).
- Scan(&results).Error
- if err != nil {
- return false, 0, err
- }
- s.xrayApi.Init(p.GetAPIPort())
- for _, result := range results {
- err1 := s.xrayApi.RemoveUser(result.Tag, result.Email)
- if err1 == nil {
- logger.Debug("Client disabled by api:", result.Email)
- } else {
- logger.Debug("Error in disabling client by api:", err1)
- needRestart = true
- }
- }
- s.xrayApi.Close()
- }
- result := tx.Model(xray.ClientTraffic{}).
- Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
- Update("enable", false)
- err := result.Error
- count := result.RowsAffected
- return needRestart, count, err
- }
- func (s *InboundService) GetInboundTags() (string, error) {
- db := database.GetDB()
- var inboundTags []string
- err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- return "", err
- }
- tags, _ := json.Marshal(inboundTags)
- return string(tags), nil
- }
- func (s *InboundService) MigrationRemoveOrphanedTraffics() {
- db := database.GetDB()
- db.Exec(`
- DELETE FROM client_traffics
- WHERE email NOT IN (
- SELECT JSON_EXTRACT(client.value, '$.email')
- FROM inbounds,
- JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
- )
- `)
- }
- func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
- clientTraffic := xray.ClientTraffic{}
- clientTraffic.InboundId = inboundId
- clientTraffic.Email = client.Email
- clientTraffic.Total = client.TotalGB
- clientTraffic.ExpiryTime = client.ExpiryTime
- clientTraffic.Enable = true
- clientTraffic.Up = 0
- clientTraffic.Down = 0
- clientTraffic.Reset = client.Reset
- result := tx.Create(&clientTraffic)
- err := result.Error
- return err
- }
- func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
- result := tx.Model(xray.ClientTraffic{}).
- Where("email = ?", email).
- Updates(map[string]interface{}{
- "enable": true,
- "email": client.Email,
- "total": client.TotalGB,
- "expiry_time": client.ExpiryTime,
- "reset": client.Reset,
- })
- err := result.Error
- return err
- }
- func (s *InboundService) UpdateClientIPs(tx *gorm.DB, oldEmail string, newEmail string) error {
- return tx.Model(model.InboundClientIps{}).Where("client_email = ?", oldEmail).Update("client_email", newEmail).Error
- }
- func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
- return tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error
- }
- func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error {
- return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error
- }
- func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
- db := database.GetDB()
- var traffics []*xray.ClientTraffic
- err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
- if err != nil {
- logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
- return nil, nil, err
- }
- if len(traffics) > 0 {
- inbound, err = s.GetInbound(traffics[0].InboundId)
- return traffics[0], inbound, err
- }
- return nil, nil, nil
- }
- func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
- db := database.GetDB()
- var traffics []*xray.ClientTraffic
- err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
- if err != nil {
- logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
- return nil, nil, err
- }
- if len(traffics) > 0 {
- inbound, err = s.GetInbound(traffics[0].InboundId)
- return traffics[0], inbound, err
- }
- return nil, nil, nil
- }
- func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
- traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return nil, nil, err
- }
- if inbound == nil {
- return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- clients, err := s.GetClients(inbound)
- if err != nil {
- return nil, nil, err
- }
- for _, client := range clients {
- if client.Email == clientEmail {
- return traffic, &client, nil
- }
- }
- return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
- }
- func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId int64) (bool, error) {
- traffic, inbound, err := s.GetClientInboundByTrafficID(trafficId)
- if err != nil {
- return false, err
- }
- if inbound == nil {
- return false, common.NewError("Inbound Not Found For Traffic ID:", trafficId)
- }
- clientEmail := traffic.Email
- oldClients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- clientId := ""
- for _, oldClient := range oldClients {
- if oldClient.Email == clientEmail {
- if inbound.Protocol == "trojan" {
- clientId = oldClient.Password
- } else if inbound.Protocol == "shadowsocks" {
- clientId = oldClient.Email
- } else {
- clientId = oldClient.ID
- }
- break
- }
- }
- if len(clientId) == 0 {
- return false, common.NewError("Client Not Found For Email:", clientEmail)
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &settings)
- if err != nil {
- return false, err
- }
- clients := settings["clients"].([]interface{})
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- if c["email"] == clientEmail {
- c["tgId"] = tgId
- newClients = append(newClients, interface{}(c))
- }
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, err
- }
- inbound.Settings = string(modifiedSettings)
- needRestart, err := s.UpdateInboundClient(inbound, clientId)
- return needRestart, err
- }
- func (s *InboundService) checkIsEnabledByEmail(clientEmail string) (bool, error) {
- _, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return false, err
- }
- if inbound == nil {
- return false, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- clients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- isEnable := false
- for _, client := range clients {
- if client.Email == clientEmail {
- isEnable = client.Enable
- break
- }
- }
- return isEnable, err
- }
- func (s *InboundService) ToggleClientEnableByEmail(clientEmail string) (bool, bool, error) {
- _, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return false, false, err
- }
- if inbound == nil {
- return false, false, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- oldClients, err := s.GetClients(inbound)
- if err != nil {
- return false, false, err
- }
- clientId := ""
- clientOldEnabled := false
- for _, oldClient := range oldClients {
- if oldClient.Email == clientEmail {
- if inbound.Protocol == "trojan" {
- clientId = oldClient.Password
- } else if inbound.Protocol == "shadowsocks" {
- clientId = oldClient.Email
- } else {
- clientId = oldClient.ID
- }
- clientOldEnabled = oldClient.Enable
- break
- }
- }
- if len(clientId) == 0 {
- return false, false, common.NewError("Client Not Found For Email:", clientEmail)
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &settings)
- if err != nil {
- return false, false, err
- }
- clients := settings["clients"].([]interface{})
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- if c["email"] == clientEmail {
- c["enable"] = !clientOldEnabled
- newClients = append(newClients, interface{}(c))
- }
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, false, err
- }
- inbound.Settings = string(modifiedSettings)
- needRestart, err := s.UpdateInboundClient(inbound, clientId)
- if err != nil {
- return false, needRestart, err
- }
- return !clientOldEnabled, needRestart, nil
- }
- func (s *InboundService) ResetClientIpLimitByEmail(clientEmail string, count int) (bool, error) {
- _, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return false, err
- }
- if inbound == nil {
- return false, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- oldClients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- clientId := ""
- for _, oldClient := range oldClients {
- if oldClient.Email == clientEmail {
- if inbound.Protocol == "trojan" {
- clientId = oldClient.Password
- } else if inbound.Protocol == "shadowsocks" {
- clientId = oldClient.Email
- } else {
- clientId = oldClient.ID
- }
- break
- }
- }
- if len(clientId) == 0 {
- return false, common.NewError("Client Not Found For Email:", clientEmail)
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &settings)
- if err != nil {
- return false, err
- }
- clients := settings["clients"].([]interface{})
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- if c["email"] == clientEmail {
- c["limitIp"] = count
- newClients = append(newClients, interface{}(c))
- }
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, err
- }
- inbound.Settings = string(modifiedSettings)
- needRestart, err := s.UpdateInboundClient(inbound, clientId)
- return needRestart, err
- }
- func (s *InboundService) ResetClientExpiryTimeByEmail(clientEmail string, expiry_time int64) (bool, error) {
- _, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return false, err
- }
- if inbound == nil {
- return false, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- oldClients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- clientId := ""
- for _, oldClient := range oldClients {
- if oldClient.Email == clientEmail {
- if inbound.Protocol == "trojan" {
- clientId = oldClient.Password
- } else if inbound.Protocol == "shadowsocks" {
- clientId = oldClient.Email
- } else {
- clientId = oldClient.ID
- }
- break
- }
- }
- if len(clientId) == 0 {
- return false, common.NewError("Client Not Found For Email:", clientEmail)
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &settings)
- if err != nil {
- return false, err
- }
- clients := settings["clients"].([]interface{})
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- if c["email"] == clientEmail {
- c["expiryTime"] = expiry_time
- newClients = append(newClients, interface{}(c))
- }
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, err
- }
- inbound.Settings = string(modifiedSettings)
- needRestart, err := s.UpdateInboundClient(inbound, clientId)
- return needRestart, err
- }
- func (s *InboundService) ResetClientTrafficLimitByEmail(clientEmail string, totalGB int) (bool, error) {
- if totalGB < 0 {
- return false, common.NewError("totalGB must be >= 0")
- }
- _, inbound, err := s.GetClientInboundByEmail(clientEmail)
- if err != nil {
- return false, err
- }
- if inbound == nil {
- return false, common.NewError("Inbound Not Found For Email:", clientEmail)
- }
- oldClients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- clientId := ""
- for _, oldClient := range oldClients {
- if oldClient.Email == clientEmail {
- if inbound.Protocol == "trojan" {
- clientId = oldClient.Password
- } else if inbound.Protocol == "shadowsocks" {
- clientId = oldClient.Email
- } else {
- clientId = oldClient.ID
- }
- break
- }
- }
- if len(clientId) == 0 {
- return false, common.NewError("Client Not Found For Email:", clientEmail)
- }
- var settings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &settings)
- if err != nil {
- return false, err
- }
- clients := settings["clients"].([]interface{})
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- if c["email"] == clientEmail {
- c["totalGB"] = totalGB * 1024 * 1024 * 1024
- newClients = append(newClients, interface{}(c))
- }
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return false, err
- }
- inbound.Settings = string(modifiedSettings)
- needRestart, err := s.UpdateInboundClient(inbound, clientId)
- return needRestart, err
- }
- func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
- db := database.GetDB()
- result := db.Model(xray.ClientTraffic{}).
- Where("email = ?", clientEmail).
- Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
- err := result.Error
- if err != nil {
- return err
- }
- return nil
- }
- func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (bool, error) {
- needRestart := false
- traffic, err := s.GetClientTrafficByEmail(clientEmail)
- if err != nil {
- return false, err
- }
- if !traffic.Enable {
- inbound, err := s.GetInbound(id)
- if err != nil {
- return false, err
- }
- clients, err := s.GetClients(inbound)
- if err != nil {
- return false, err
- }
- for _, client := range clients {
- if client.Email == clientEmail {
- s.xrayApi.Init(p.GetAPIPort())
- cipher := ""
- if string(inbound.Protocol) == "shadowsocks" {
- var oldSettings map[string]interface{}
- err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
- if err != nil {
- return false, err
- }
- cipher = oldSettings["method"].(string)
- }
- err1 := s.xrayApi.AddUser(string(inbound.Protocol), inbound.Tag, map[string]interface{}{
- "email": client.Email,
- "id": client.ID,
- "security": client.Security,
- "flow": client.Flow,
- "password": client.Password,
- "cipher": cipher,
- })
- if err1 == nil {
- logger.Debug("Client enabled due to reset traffic:", clientEmail)
- } else {
- logger.Debug("Error in enabling client by api:", err1)
- needRestart = true
- }
- s.xrayApi.Close()
- break
- }
- }
- }
- traffic.Up = 0
- traffic.Down = 0
- traffic.Enable = true
- db := database.GetDB()
- err = db.Save(traffic).Error
- if err != nil {
- return false, err
- }
- return needRestart, nil
- }
- func (s *InboundService) ResetAllClientTraffics(id int) error {
- db := database.GetDB()
- whereText := "inbound_id "
- if id == -1 {
- whereText += " > ?"
- } else {
- whereText += " = ?"
- }
- result := db.Model(xray.ClientTraffic{}).
- Where(whereText, id).
- Updates(map[string]interface{}{"enable": true, "up": 0, "down": 0})
- err := result.Error
- return err
- }
- func (s *InboundService) ResetAllTraffics() error {
- db := database.GetDB()
- result := db.Model(model.Inbound{}).
- Where("user_id > ?", 0).
- Updates(map[string]interface{}{"up": 0, "down": 0})
- err := result.Error
- return err
- }
- func (s *InboundService) DelDepletedClients(id int) (err error) {
- db := database.GetDB()
- tx := db.Begin()
- defer func() {
- if err == nil {
- tx.Commit()
- } else {
- tx.Rollback()
- }
- }()
- whereText := "reset = 0 and inbound_id "
- if id < 0 {
- whereText += "> ?"
- } else {
- whereText += "= ?"
- }
- depletedClients := []xray.ClientTraffic{}
- err = db.Model(xray.ClientTraffic{}).Where(whereText+" and enable = ?", id, false).Select("inbound_id, GROUP_CONCAT(email) as email").Group("inbound_id").Find(&depletedClients).Error
- if err != nil {
- return err
- }
- for _, depletedClient := range depletedClients {
- emails := strings.Split(depletedClient.Email, ",")
- oldInbound, err := s.GetInbound(depletedClient.InboundId)
- if err != nil {
- return err
- }
- var oldSettings map[string]interface{}
- err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
- if err != nil {
- return err
- }
- oldClients := oldSettings["clients"].([]interface{})
- var newClients []interface{}
- for _, client := range oldClients {
- deplete := false
- c := client.(map[string]interface{})
- for _, email := range emails {
- if email == c["email"].(string) {
- deplete = true
- break
- }
- }
- if !deplete {
- newClients = append(newClients, client)
- }
- }
- if len(newClients) > 0 {
- oldSettings["clients"] = newClients
- newSettings, err := json.MarshalIndent(oldSettings, "", " ")
- if err != nil {
- return err
- }
- oldInbound.Settings = string(newSettings)
- err = tx.Save(oldInbound).Error
- if err != nil {
- return err
- }
- } else {
- // Delete inbound if no client remains
- s.DelInbound(depletedClient.InboundId)
- }
- }
- err = tx.Where(whereText+" and enable = ?", id, false).Delete(xray.ClientTraffic{}).Error
- if err != nil {
- return err
- }
- return nil
- }
- func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
- db := database.GetDB()
- var inbounds []*model.Inbound
- // Retrieve inbounds where settings contain the given tgId
- err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
- return nil, err
- }
- var emails []string
- for _, inbound := range inbounds {
- clients, err := s.GetClients(inbound)
- if err != nil {
- logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
- continue
- }
- for _, client := range clients {
- if client.TgID == tgId {
- emails = append(emails, client.Email)
- }
- }
- }
- var traffics []*xray.ClientTraffic
- err = db.Model(xray.ClientTraffic{}).Where("email IN ?", emails).Find(&traffics).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- logger.Warning("No ClientTraffic records found for emails:", emails)
- return nil, nil
- }
- logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", emails, err)
- return nil, err
- }
- return traffics, nil
- }
- func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
- db := database.GetDB()
- var traffics []*xray.ClientTraffic
- err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
- if err != nil {
- logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
- return nil, err
- }
- if len(traffics) > 0 {
- return traffics[0], nil
- }
- return nil, nil
- }
- func (s *InboundService) GetClientTrafficByID(id string) ([]xray.ClientTraffic, error) {
- db := database.GetDB()
- var traffics []xray.ClientTraffic
- err := db.Model(xray.ClientTraffic{}).Where(`email IN(
- SELECT JSON_EXTRACT(client.value, '$.email') as email
- FROM inbounds,
- JSON_EACH(JSON_EXTRACT(inbounds.settings, '$.clients')) AS client
- WHERE
- JSON_EXTRACT(client.value, '$.id') in (?)
- )`, id).Find(&traffics).Error
- if err != nil {
- logger.Debug(err)
- return nil, err
- }
- return traffics, err
- }
- func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
- db := database.GetDB()
- inbound := &model.Inbound{}
- traffic = &xray.ClientTraffic{}
- // Search for inbound settings that contain the query
- err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
- return nil, err
- }
- logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
- return nil, err
- }
- traffic.InboundId = inbound.Id
- // Unmarshal settings to get clients
- settings := map[string][]model.Client{}
- if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
- logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
- return nil, err
- }
- clients := settings["clients"]
- for _, client := range clients {
- if (client.ID == query || client.Password == query) && client.Email != "" {
- traffic.Email = client.Email
- break
- }
- }
- if traffic.Email == "" {
- logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
- return nil, gorm.ErrRecordNotFound
- }
- // Retrieve ClientTraffic based on the found email
- err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
- return nil, err
- }
- logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
- return nil, err
- }
- return traffic, nil
- }
- func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
- db := database.GetDB()
- InboundClientIps := &model.InboundClientIps{}
- err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
- if err != nil {
- return "", err
- }
- return InboundClientIps.Ips, nil
- }
- func (s *InboundService) ClearClientIps(clientEmail string) error {
- db := database.GetDB()
- result := db.Model(model.InboundClientIps{}).
- Where("client_email = ?", clientEmail).
- Update("ips", "")
- err := result.Error
- if err != nil {
- return err
- }
- return nil
- }
- func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
- db := database.GetDB()
- var inbounds []*model.Inbound
- err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, err
- }
- return inbounds, nil
- }
- func (s *InboundService) MigrationRequirements() {
- db := database.GetDB()
- tx := db.Begin()
- var err error
- defer func() {
- if err == nil {
- tx.Commit()
- } else {
- tx.Rollback()
- }
- }()
- // Fix inbounds based problems
- var inbounds []*model.Inbound
- err = tx.Model(model.Inbound{}).Where("protocol IN (?)", []string{"vmess", "vless", "trojan"}).Find(&inbounds).Error
- if err != nil && err != gorm.ErrRecordNotFound {
- return
- }
- for inbound_index := range inbounds {
- settings := map[string]interface{}{}
- json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
- clients, ok := settings["clients"].([]interface{})
- if ok {
- // Fix Client configuration problems
- var newClients []interface{}
- for client_index := range clients {
- c := clients[client_index].(map[string]interface{})
- // Add email='' if it is not exists
- if _, ok := c["email"]; !ok {
- c["email"] = ""
- }
- // Convert string tgId to int64
- if _, ok := c["tgId"]; ok {
- var tgId interface{} = c["tgId"]
- if tgIdStr, ok2 := tgId.(string); ok2 {
- tgIdInt64, err := strconv.ParseInt(strings.ReplaceAll(tgIdStr, " ", ""), 10, 64)
- if err == nil {
- c["tgId"] = tgIdInt64
- }
- }
- }
- // Remove "flow": "xtls-rprx-direct"
- if _, ok := c["flow"]; ok {
- if c["flow"] == "xtls-rprx-direct" {
- c["flow"] = ""
- }
- }
- newClients = append(newClients, interface{}(c))
- }
- settings["clients"] = newClients
- modifiedSettings, err := json.MarshalIndent(settings, "", " ")
- if err != nil {
- return
- }
- inbounds[inbound_index].Settings = string(modifiedSettings)
- }
- // Add client traffic row for all clients which has email
- modelClients, err := s.GetClients(inbounds[inbound_index])
- if err != nil {
- return
- }
- for _, modelClient := range modelClients {
- if len(modelClient.Email) > 0 {
- var count int64
- tx.Model(xray.ClientTraffic{}).Where("email = ?", modelClient.Email).Count(&count)
- if count == 0 {
- s.AddClientStat(tx, inbounds[inbound_index].Id, &modelClient)
- }
- }
- }
- }
- tx.Save(inbounds)
- // Remove orphaned traffics
- tx.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
- // Migrate old MultiDomain to External Proxy
- var externalProxy []struct {
- Id int
- Port int
- StreamSettings []byte
- }
- err = tx.Raw(`select id, port, stream_settings
- from inbounds
- WHERE protocol in ('vmess','vless','trojan')
- AND json_extract(stream_settings, '$.security') = 'tls'
- AND json_extract(stream_settings, '$.tlsSettings.settings.domains') IS NOT NULL`).Scan(&externalProxy).Error
- if err != nil || len(externalProxy) == 0 {
- return
- }
- for _, ep := range externalProxy {
- var reverses interface{}
- var stream map[string]interface{}
- json.Unmarshal(ep.StreamSettings, &stream)
- if tlsSettings, ok := stream["tlsSettings"].(map[string]interface{}); ok {
- if settings, ok := tlsSettings["settings"].(map[string]interface{}); ok {
- if domains, ok := settings["domains"].([]interface{}); ok {
- for _, domain := range domains {
- if domainMap, ok := domain.(map[string]interface{}); ok {
- domainMap["forceTls"] = "same"
- domainMap["port"] = ep.Port
- domainMap["dest"] = domainMap["domain"].(string)
- delete(domainMap, "domain")
- }
- }
- }
- reverses = settings["domains"]
- delete(settings, "domains")
- }
- }
- stream["externalProxy"] = reverses
- newStream, _ := json.MarshalIndent(stream, " ", " ")
- tx.Model(model.Inbound{}).Where("id = ?", ep.Id).Update("stream_settings", newStream)
- }
- err = tx.Raw(`UPDATE inbounds
- SET tag = REPLACE(tag, '0.0.0.0:', '')
- WHERE INSTR(tag, '0.0.0.0:') > 0;`).Error
- if err != nil {
- return
- }
- }
- func (s *InboundService) MigrateDB() {
- s.MigrationRequirements()
- s.MigrationRemoveOrphanedTraffics()
- }
- func (s *InboundService) GetOnlineClients() []string {
- return p.GetOnlineClients()
- }
- func validateEmail(email string) (bool, error) {
- if strings.Contains(email, " ") {
- return false, errors.New("email contains spaces, please remove them")
- }
- if email != strings.ToLower(email) {
- return false, errors.New("email contains uppercase letters, please convert to lowercase")
- }
- emailPattern := `^[a-z0-9._-]+$`
- if !regexp.MustCompile(emailPattern).MatchString(email) {
- return false, errors.New("email contains invalid characters, please use only lowercase letters, digits, dots, dashes, and underscores")
- }
- return true, nil
- }
|