1
0

ldap_sync_job.go 8.1 KB

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