client.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Package service implements the panel's business-logic layer.
  2. //
  3. // ClientService owns the lifecycle of VPN clients: creation, update, deletion,
  4. // attach/detach to inbounds, bulk operations, group membership, traffic resets,
  5. // and the paginated clients listing. Its surface is split across client_*.go
  6. // files by responsibility (see each file's contents); they all belong to the
  7. // same package, so the split is purely organizational. ClientService and
  8. // InboundService are mutually dependent — most ClientService methods take an
  9. // *InboundService and InboundService embeds a ClientService — which is why the
  10. // client code lives in package service rather than a sub-package.
  11. package service
  12. import (
  13. "encoding/json"
  14. "errors"
  15. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  16. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  17. )
  18. type ClientWithAttachments struct {
  19. model.ClientRecord
  20. InboundIds []int `json:"inboundIds"`
  21. Traffic *xray.ClientTraffic `json:"traffic,omitempty"`
  22. }
  23. // MarshalJSON is required because model.ClientRecord defines its own
  24. // MarshalJSON. Go promotes the embedded method to the outer struct, so without
  25. // this the encoder would call ClientRecord.MarshalJSON for the whole value and
  26. // silently drop InboundIds and Traffic from the API response.
  27. func (c ClientWithAttachments) MarshalJSON() ([]byte, error) {
  28. rec, err := json.Marshal(c.ClientRecord)
  29. if err != nil {
  30. return nil, err
  31. }
  32. extras := struct {
  33. InboundIds []int `json:"inboundIds"`
  34. Traffic *xray.ClientTraffic `json:"traffic,omitempty"`
  35. }{InboundIds: c.InboundIds, Traffic: c.Traffic}
  36. extra, err := json.Marshal(extras)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if len(rec) < 2 || rec[len(rec)-1] != '}' || len(extra) <= 2 {
  41. return rec, nil
  42. }
  43. const maxMarshalSize = 256 << 20
  44. if len(rec) > maxMarshalSize || len(extra) > maxMarshalSize {
  45. return rec, nil
  46. }
  47. out := make([]byte, 0, len(rec)+len(extra))
  48. out = append(out, rec[:len(rec)-1]...)
  49. if len(rec) > 2 {
  50. out = append(out, ',')
  51. }
  52. out = append(out, extra[1:]...)
  53. return out, nil
  54. }
  55. type ClientService struct{}
  56. // ErrClientNotInInbound is returned (wrapped) when a client cannot be located
  57. // in an inbound's settings during deletion. Deletion treats it as non-fatal so
  58. // the operation stays idempotent and tolerant of pre-existing data drift
  59. // between the clients table and the inbound's settings JSON.
  60. var ErrClientNotInInbound = errors.New("client not found in inbound")
  61. type ClientCreatePayload struct {
  62. Client model.Client `json:"client"`
  63. InboundIds []int `json:"inboundIds"`
  64. }
  65. const sqlInChunk = 400