ldap_sync_job.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package job
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/google/uuid"
  6. "github.com/mhsanaei/3x-ui/v3/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/logger"
  8. ldaputil "github.com/mhsanaei/3x-ui/v3/util/ldap"
  9. "github.com/mhsanaei/3x-ui/v3/web/service"
  10. )
  11. var DefaultTruthyValues = []string{"true", "1", "yes", "on"}
  12. type LdapSyncJob struct {
  13. settingService service.SettingService
  14. inboundService service.InboundService
  15. clientService service.ClientService
  16. xrayService service.XrayService
  17. }
  18. // --- Helper functions for mustGet ---
  19. func mustGetString(fn func() (string, error)) string {
  20. v, err := fn()
  21. if err != nil {
  22. panic(err)
  23. }
  24. return v
  25. }
  26. func mustGetInt(fn func() (int, error)) int {
  27. v, err := fn()
  28. if err != nil {
  29. panic(err)
  30. }
  31. return v
  32. }
  33. func mustGetBool(fn func() (bool, error)) bool {
  34. v, err := fn()
  35. if err != nil {
  36. panic(err)
  37. }
  38. return v
  39. }
  40. func mustGetStringOr(fn func() (string, error), fallback string) string {
  41. v, err := fn()
  42. if err != nil || v == "" {
  43. return fallback
  44. }
  45. return v
  46. }
  47. func NewLdapSyncJob() *LdapSyncJob {
  48. return new(LdapSyncJob)
  49. }
  50. func (j *LdapSyncJob) Run() {
  51. logger.Info("LDAP sync job started")
  52. enabled, err := j.settingService.GetLdapEnable()
  53. if err != nil || !enabled {
  54. logger.Warning("LDAP disabled or failed to fetch flag")
  55. return
  56. }
  57. // --- LDAP fetch ---
  58. cfg := ldaputil.Config{
  59. Host: mustGetString(j.settingService.GetLdapHost),
  60. Port: mustGetInt(j.settingService.GetLdapPort),
  61. UseTLS: mustGetBool(j.settingService.GetLdapUseTLS),
  62. BindDN: mustGetString(j.settingService.GetLdapBindDN),
  63. Password: mustGetString(j.settingService.GetLdapPassword),
  64. BaseDN: mustGetString(j.settingService.GetLdapBaseDN),
  65. UserFilter: mustGetString(j.settingService.GetLdapUserFilter),
  66. UserAttr: mustGetString(j.settingService.GetLdapUserAttr),
  67. FlagField: mustGetStringOr(j.settingService.GetLdapFlagField, mustGetString(j.settingService.GetLdapVlessField)),
  68. TruthyVals: splitCsv(mustGetString(j.settingService.GetLdapTruthyValues)),
  69. Invert: mustGetBool(j.settingService.GetLdapInvertFlag),
  70. }
  71. flags, err := ldaputil.FetchVlessFlags(cfg)
  72. if err != nil {
  73. logger.Warning("LDAP fetch failed:", err)
  74. return
  75. }
  76. logger.Infof("Fetched %d LDAP flags", len(flags))
  77. // --- Load all inbounds and all clients once ---
  78. inboundTags := splitCsv(mustGetString(j.settingService.GetLdapInboundTags))
  79. inbounds, err := j.inboundService.GetAllInbounds()
  80. if err != nil {
  81. logger.Warning("Failed to get inbounds:", err)
  82. return
  83. }
  84. allClients := map[string]*model.Client{} // email -> client
  85. inboundMap := map[string]*model.Inbound{} // tag -> inbound
  86. for _, ib := range inbounds {
  87. inboundMap[ib.Tag] = ib
  88. clients, _ := j.inboundService.GetClients(ib)
  89. for i := range clients {
  90. allClients[clients[i].Email] = &clients[i]
  91. }
  92. }
  93. // --- Prepare batch operations ---
  94. autoCreate := mustGetBool(j.settingService.GetLdapAutoCreate)
  95. defGB := mustGetInt(j.settingService.GetLdapDefaultTotalGB)
  96. defExpiryDays := mustGetInt(j.settingService.GetLdapDefaultExpiryDays)
  97. defLimitIP := mustGetInt(j.settingService.GetLdapDefaultLimitIP)
  98. clientsToCreate := map[string][]model.Client{} // tag -> []new clients
  99. clientsToEnable := map[string][]string{} // tag -> []email
  100. clientsToDisable := map[string][]string{} // tag -> []email
  101. for email, allowed := range flags {
  102. exists := allClients[email] != nil
  103. for _, tag := range inboundTags {
  104. if !exists && allowed && autoCreate {
  105. newClient := j.buildClient(inboundMap[tag], email, defGB, defExpiryDays, defLimitIP)
  106. clientsToCreate[tag] = append(clientsToCreate[tag], newClient)
  107. } else if exists {
  108. if allowed && !allClients[email].Enable {
  109. clientsToEnable[tag] = append(clientsToEnable[tag], email)
  110. } else if !allowed && allClients[email].Enable {
  111. clientsToDisable[tag] = append(clientsToDisable[tag], email)
  112. }
  113. }
  114. }
  115. }
  116. for tag, newClients := range clientsToCreate {
  117. if len(newClients) == 0 {
  118. continue
  119. }
  120. ib := inboundMap[tag]
  121. created := 0
  122. restartNeeded := false
  123. for _, c := range newClients {
  124. nr, err := j.clientService.CreateOne(&j.inboundService, ib.Id, c)
  125. if err != nil {
  126. logger.Warningf("Failed to add client %s for tag %s: %v", c.Email, tag, err)
  127. continue
  128. }
  129. created++
  130. if nr {
  131. restartNeeded = true
  132. }
  133. }
  134. if created > 0 {
  135. logger.Infof("LDAP auto-create: %d clients for %s", created, tag)
  136. if restartNeeded {
  137. j.xrayService.SetToNeedRestart()
  138. }
  139. }
  140. }
  141. // --- Execute enable/disable batch ---
  142. for tag, emails := range clientsToEnable {
  143. j.batchSetEnable(inboundMap[tag], emails, true)
  144. }
  145. for tag, emails := range clientsToDisable {
  146. j.batchSetEnable(inboundMap[tag], emails, false)
  147. }
  148. // --- Auto delete clients not in LDAP ---
  149. autoDelete := mustGetBool(j.settingService.GetLdapAutoDelete)
  150. if autoDelete {
  151. ldapEmailSet := map[string]struct{}{}
  152. for e := range flags {
  153. ldapEmailSet[e] = struct{}{}
  154. }
  155. for _, tag := range inboundTags {
  156. j.deleteClientsNotInLDAP(tag, ldapEmailSet)
  157. }
  158. }
  159. }
  160. func splitCsv(s string) []string {
  161. if s == "" {
  162. return DefaultTruthyValues
  163. }
  164. parts := strings.Split(s, ",")
  165. out := make([]string, 0, len(parts))
  166. for _, p := range parts {
  167. v := strings.TrimSpace(p)
  168. if v != "" {
  169. out = append(out, v)
  170. }
  171. }
  172. return out
  173. }
  174. // buildClient creates a new client for auto-create
  175. func (j *LdapSyncJob) buildClient(ib *model.Inbound, email string, defGB, defExpiryDays, defLimitIP int) model.Client {
  176. c := model.Client{
  177. Email: email,
  178. Enable: true,
  179. LimitIP: defLimitIP,
  180. TotalGB: int64(defGB),
  181. }
  182. if defExpiryDays > 0 {
  183. c.ExpiryTime = time.Now().Add(time.Duration(defExpiryDays) * 24 * time.Hour).UnixMilli()
  184. }
  185. switch ib.Protocol {
  186. case model.Trojan, model.Shadowsocks:
  187. c.Password = uuid.NewString()
  188. default:
  189. c.ID = uuid.NewString()
  190. }
  191. return c
  192. }
  193. func (j *LdapSyncJob) batchSetEnable(ib *model.Inbound, emails []string, enable bool) {
  194. if len(emails) == 0 {
  195. return
  196. }
  197. restartNeeded := false
  198. changed := 0
  199. for _, email := range emails {
  200. ok, needRestart, err := j.clientService.SetClientEnableByEmail(&j.inboundService, email, enable)
  201. if err != nil {
  202. logger.Warningf("Batch set enable failed for %s in inbound %s: %v", email, ib.Tag, err)
  203. continue
  204. }
  205. if ok {
  206. changed++
  207. }
  208. if needRestart {
  209. restartNeeded = true
  210. }
  211. }
  212. if changed > 0 {
  213. logger.Infof("Batch set enable=%v for %d clients in inbound %s", enable, changed, ib.Tag)
  214. }
  215. if restartNeeded {
  216. j.xrayService.SetToNeedRestart()
  217. }
  218. }
  219. // deleteClientsNotInLDAP deletes clients not in LDAP using batches and a single restart
  220. func (j *LdapSyncJob) deleteClientsNotInLDAP(inboundTag string, ldapEmails map[string]struct{}) {
  221. inbounds, err := j.inboundService.GetAllInbounds()
  222. if err != nil {
  223. logger.Warning("Failed to get inbounds for deletion:", err)
  224. return
  225. }
  226. batchSize := 50 // clients in 1 batch
  227. restartNeeded := false
  228. for _, ib := range inbounds {
  229. if ib.Tag != inboundTag {
  230. continue
  231. }
  232. clients, err := j.inboundService.GetClients(ib)
  233. if err != nil {
  234. logger.Warningf("Failed to get clients for inbound %s: %v", ib.Tag, err)
  235. continue
  236. }
  237. // Collect clients for deletion
  238. toDelete := []model.Client{}
  239. for _, c := range clients {
  240. if _, ok := ldapEmails[c.Email]; !ok {
  241. toDelete = append(toDelete, c)
  242. }
  243. }
  244. if len(toDelete) == 0 {
  245. continue
  246. }
  247. for i := 0; i < len(toDelete); i += batchSize {
  248. end := min(i+batchSize, len(toDelete))
  249. batch := toDelete[i:end]
  250. for _, c := range batch {
  251. nr, err := j.clientService.DetachByEmail(&j.inboundService, ib.Id, c.Email)
  252. if err != nil {
  253. logger.Warningf("Failed to delete client %s from inbound id=%d(tag=%s): %v",
  254. c.Email, ib.Id, ib.Tag, err)
  255. continue
  256. }
  257. logger.Infof("Deleted client %s from inbound id=%d(tag=%s)",
  258. c.Email, ib.Id, ib.Tag)
  259. if nr {
  260. restartNeeded = true
  261. }
  262. }
  263. }
  264. }
  265. if restartNeeded {
  266. j.xrayService.SetToNeedRestart()
  267. logger.Info("Xray restart scheduled after batch deletion")
  268. }
  269. }