tls_fingerprint_profile_repo.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package repository
  2. import (
  3. "context"
  4. "github.com/Wei-Shaw/sub2api/ent"
  5. "github.com/Wei-Shaw/sub2api/ent/tlsfingerprintprofile"
  6. "github.com/Wei-Shaw/sub2api/internal/model"
  7. "github.com/Wei-Shaw/sub2api/internal/service"
  8. )
  9. type tlsFingerprintProfileRepository struct {
  10. client *ent.Client
  11. }
  12. // NewTLSFingerprintProfileRepository 创建 TLS 指纹模板仓库
  13. func NewTLSFingerprintProfileRepository(client *ent.Client) service.TLSFingerprintProfileRepository {
  14. return &tlsFingerprintProfileRepository{client: client}
  15. }
  16. // List 获取所有模板
  17. func (r *tlsFingerprintProfileRepository) List(ctx context.Context) ([]*model.TLSFingerprintProfile, error) {
  18. profiles, err := r.client.TLSFingerprintProfile.Query().
  19. Order(ent.Asc(tlsfingerprintprofile.FieldName)).
  20. All(ctx)
  21. if err != nil {
  22. return nil, err
  23. }
  24. result := make([]*model.TLSFingerprintProfile, len(profiles))
  25. for i, p := range profiles {
  26. result[i] = r.toModel(p)
  27. }
  28. return result, nil
  29. }
  30. // GetByID 根据 ID 获取模板
  31. func (r *tlsFingerprintProfileRepository) GetByID(ctx context.Context, id int64) (*model.TLSFingerprintProfile, error) {
  32. p, err := r.client.TLSFingerprintProfile.Get(ctx, id)
  33. if err != nil {
  34. if ent.IsNotFound(err) {
  35. return nil, nil
  36. }
  37. return nil, err
  38. }
  39. return r.toModel(p), nil
  40. }
  41. // Create 创建模板
  42. func (r *tlsFingerprintProfileRepository) Create(ctx context.Context, p *model.TLSFingerprintProfile) (*model.TLSFingerprintProfile, error) {
  43. builder := r.client.TLSFingerprintProfile.Create().
  44. SetName(p.Name).
  45. SetEnableGrease(p.EnableGREASE)
  46. if p.Description != nil {
  47. builder.SetDescription(*p.Description)
  48. }
  49. if len(p.CipherSuites) > 0 {
  50. builder.SetCipherSuites(p.CipherSuites)
  51. }
  52. if len(p.Curves) > 0 {
  53. builder.SetCurves(p.Curves)
  54. }
  55. if len(p.PointFormats) > 0 {
  56. builder.SetPointFormats(p.PointFormats)
  57. }
  58. if len(p.SignatureAlgorithms) > 0 {
  59. builder.SetSignatureAlgorithms(p.SignatureAlgorithms)
  60. }
  61. if len(p.ALPNProtocols) > 0 {
  62. builder.SetAlpnProtocols(p.ALPNProtocols)
  63. }
  64. if len(p.SupportedVersions) > 0 {
  65. builder.SetSupportedVersions(p.SupportedVersions)
  66. }
  67. if len(p.KeyShareGroups) > 0 {
  68. builder.SetKeyShareGroups(p.KeyShareGroups)
  69. }
  70. if len(p.PSKModes) > 0 {
  71. builder.SetPskModes(p.PSKModes)
  72. }
  73. if len(p.Extensions) > 0 {
  74. builder.SetExtensions(p.Extensions)
  75. }
  76. created, err := builder.Save(ctx)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return r.toModel(created), nil
  81. }
  82. // Update 更新模板
  83. func (r *tlsFingerprintProfileRepository) Update(ctx context.Context, p *model.TLSFingerprintProfile) (*model.TLSFingerprintProfile, error) {
  84. builder := r.client.TLSFingerprintProfile.UpdateOneID(p.ID).
  85. SetName(p.Name).
  86. SetEnableGrease(p.EnableGREASE)
  87. if p.Description != nil {
  88. builder.SetDescription(*p.Description)
  89. } else {
  90. builder.ClearDescription()
  91. }
  92. if len(p.CipherSuites) > 0 {
  93. builder.SetCipherSuites(p.CipherSuites)
  94. } else {
  95. builder.ClearCipherSuites()
  96. }
  97. if len(p.Curves) > 0 {
  98. builder.SetCurves(p.Curves)
  99. } else {
  100. builder.ClearCurves()
  101. }
  102. if len(p.PointFormats) > 0 {
  103. builder.SetPointFormats(p.PointFormats)
  104. } else {
  105. builder.ClearPointFormats()
  106. }
  107. if len(p.SignatureAlgorithms) > 0 {
  108. builder.SetSignatureAlgorithms(p.SignatureAlgorithms)
  109. } else {
  110. builder.ClearSignatureAlgorithms()
  111. }
  112. if len(p.ALPNProtocols) > 0 {
  113. builder.SetAlpnProtocols(p.ALPNProtocols)
  114. } else {
  115. builder.ClearAlpnProtocols()
  116. }
  117. if len(p.SupportedVersions) > 0 {
  118. builder.SetSupportedVersions(p.SupportedVersions)
  119. } else {
  120. builder.ClearSupportedVersions()
  121. }
  122. if len(p.KeyShareGroups) > 0 {
  123. builder.SetKeyShareGroups(p.KeyShareGroups)
  124. } else {
  125. builder.ClearKeyShareGroups()
  126. }
  127. if len(p.PSKModes) > 0 {
  128. builder.SetPskModes(p.PSKModes)
  129. } else {
  130. builder.ClearPskModes()
  131. }
  132. if len(p.Extensions) > 0 {
  133. builder.SetExtensions(p.Extensions)
  134. } else {
  135. builder.ClearExtensions()
  136. }
  137. updated, err := builder.Save(ctx)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return r.toModel(updated), nil
  142. }
  143. // Delete 删除模板
  144. func (r *tlsFingerprintProfileRepository) Delete(ctx context.Context, id int64) error {
  145. return r.client.TLSFingerprintProfile.DeleteOneID(id).Exec(ctx)
  146. }
  147. // toModel 将 Ent 实体转换为服务模型
  148. func (r *tlsFingerprintProfileRepository) toModel(e *ent.TLSFingerprintProfile) *model.TLSFingerprintProfile {
  149. p := &model.TLSFingerprintProfile{
  150. ID: e.ID,
  151. Name: e.Name,
  152. Description: e.Description,
  153. EnableGREASE: e.EnableGrease,
  154. CipherSuites: e.CipherSuites,
  155. Curves: e.Curves,
  156. PointFormats: e.PointFormats,
  157. SignatureAlgorithms: e.SignatureAlgorithms,
  158. ALPNProtocols: e.AlpnProtocols,
  159. SupportedVersions: e.SupportedVersions,
  160. KeyShareGroups: e.KeyShareGroups,
  161. PSKModes: e.PskModes,
  162. Extensions: e.Extensions,
  163. CreatedAt: e.CreatedAt,
  164. UpdatedAt: e.UpdatedAt,
  165. }
  166. // 确保切片不为 nil
  167. if p.CipherSuites == nil {
  168. p.CipherSuites = []uint16{}
  169. }
  170. if p.Curves == nil {
  171. p.Curves = []uint16{}
  172. }
  173. if p.PointFormats == nil {
  174. p.PointFormats = []uint16{}
  175. }
  176. if p.SignatureAlgorithms == nil {
  177. p.SignatureAlgorithms = []uint16{}
  178. }
  179. if p.ALPNProtocols == nil {
  180. p.ALPNProtocols = []string{}
  181. }
  182. if p.SupportedVersions == nil {
  183. p.SupportedVersions = []uint16{}
  184. }
  185. if p.KeyShareGroups == nil {
  186. p.KeyShareGroups = []uint16{}
  187. }
  188. if p.PSKModes == nil {
  189. p.PSKModes = []uint16{}
  190. }
  191. if p.Extensions == nil {
  192. p.Extensions = []uint16{}
  193. }
  194. return p
  195. }