cdn.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. package flexCdn
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/flexCdn"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  9. "github.com/spf13/viper"
  10. "strings"
  11. )
  12. type CdnService interface {
  13. // GetToken 获取token
  14. GetToken(ctx context.Context) (string, error)
  15. // AddUser 注册用户
  16. AddUser(ctx context.Context, req v1.User) (int64, error)
  17. CreateGroup(ctx context.Context, req v1.Group) (int64, error)
  18. BindPlan(ctx context.Context, req v1.Plan) (int64, error)
  19. // 续费套餐
  20. RenewPlan(ctx context.Context, req v1.RenewalPlan) error
  21. CreateWebsite(ctx context.Context, req v1.WebsiteSend) (int64, error)
  22. EditServerType(ctx context.Context, req v1.EditWebsite, apiType string) error
  23. EditProtocol(ctx context.Context, req v1.ProxyJson, action string) error
  24. CreateOrigin(ctx context.Context, req v1.Origin) (int64, error)
  25. EditOrigin(ctx context.Context, req v1.Origin) error
  26. AddServerOrigin(ctx context.Context, serverId int64, originId int64) error
  27. EditOriginIsOn(ctx context.Context, originId int64, isOn bool) error
  28. // 修改网站基本信息
  29. EditServerBasic(ctx context.Context, serverId int64, name string,nodeId int64) error
  30. // 从网站中删除某个源站
  31. DelServerOrigin(ctx context.Context, serverId int64, originId int64) error
  32. // 删除网站
  33. DelServer(ctx context.Context, serverId int64) error
  34. // 添加ssl证书
  35. AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, error)
  36. // 修改网站域名
  37. EditServerName(ctx context.Context, req v1.EditServerNames) error
  38. // 添加ssl策略
  39. AddSSLPolicy(ctx context.Context, req v1.AddSSLPolicy) (int64, error)
  40. DelSSLCert(ctx context.Context, sslCertId int64) error
  41. EditSSLCert(ctx context.Context, req v1.SSlCert) error
  42. EditSSLPolicy(ctx context.Context, req v1.SSLPolicy) error
  43. // 修改反向代理
  44. EditProxy(ctx context.Context, req v1.Proxy) error
  45. // 修改日志
  46. EditWebLog(ctx context.Context,webId int64, req v1.WebLog) error
  47. // 修改CC配置
  48. EditCcConfig(ctx context.Context,webId int64, req v1.CcConfig) error
  49. // 添加webSocket
  50. AddWebSockets(ctx context.Context, req v1.WebSocket) (int64,error)
  51. // 修改webSocket
  52. EditWebSockets(ctx context.Context, req v1.WebSocket) error
  53. // 启用webSocket
  54. EditHTTPWebWebsocket(ctx context.Context,websocketId int64,websocketJSON []byte) error
  55. // 启用/禁用网站
  56. EditWebIsOn(ctx context.Context,serverId int64,isOn bool) error
  57. // 删除已购套餐
  58. DelUserPlan(ctx context.Context,planId int64) error
  59. // 删除网站分组
  60. DelServerGroup(ctx context.Context,serverId int64) error
  61. // 删除IP
  62. DelIpItem(ctx context.Context,ipitemId int64,value string,ipFrom string,ipTo string,ipListId int64) error
  63. // 创建空防火墙策略
  64. AddWafPolicy(ctx context.Context,req v1.AddWafPolicy) (int64, error)
  65. // 更改防火墙设置
  66. EditHTTPWebFirewal(ctx context.Context,httpWebId int64,FirewallJSON v1.Firewall) error
  67. // 创建IP列表
  68. AddIPList(ctx context.Context, req v1.IPListRequest) (int64, error)
  69. // 添加IP
  70. AddIpItem(ctx context.Context, req v1.AddIpItem) error
  71. }
  72. func NewCdnService(
  73. service *service.Service,
  74. conf *viper.Viper,
  75. request service.RequestService,
  76. cdnRepository flexCdn.CdnRepository,
  77. ) CdnService {
  78. return &cdnService{
  79. Service: service,
  80. Url: conf.GetString("flexCdn.Url"),
  81. AccessKeyID: conf.GetString("flexCdn.AccessKeyID"),
  82. AccessKeySecret: conf.GetString("flexCdn.AccessKeySecret"),
  83. request: request,
  84. cdnRepository: cdnRepository,
  85. maxRetryCount: 3, // 可以配置最大重试次数
  86. retryDelaySeconds: 2, // 可以配置重试间隔
  87. }
  88. }
  89. type cdnService struct {
  90. *service.Service
  91. Url string
  92. AccessKeyID string
  93. AccessKeySecret string
  94. request service.RequestService
  95. cdnRepository flexCdn.CdnRepository
  96. maxRetryCount int
  97. retryDelaySeconds int
  98. }
  99. // SendData 是一个通用的请求发送方法,它封装了 token 过期重试的逻辑
  100. func (s *cdnService) sendDataWithTokenRetry(ctx context.Context, formData map[string]interface{}, apiUrl string) ([]byte, error) {
  101. var resBody []byte
  102. for i := 0; i < s.maxRetryCount; i++ {
  103. token, err := s.Token(ctx) // 确保使用最新的 token
  104. if err != nil {
  105. return nil, fmt.Errorf("获取或刷新 token 失败: %w", err)
  106. }
  107. resBody, err = s.request.Request(ctx, formData, apiUrl, "X-Cloud-Access-Token", token)
  108. if err != nil {
  109. // 检查错误是否是由于 token 无效引起的
  110. if s.isTokenInvalidError(resBody, err) { // 判断是否是 token 无效错误
  111. _, getTokenErr := s.GetToken(ctx)
  112. if getTokenErr != nil {
  113. return nil, fmt.Errorf("刷新 token 失败: %w", getTokenErr)
  114. }
  115. continue // 继续下一次循环,使用新的 token
  116. }
  117. return nil, fmt.Errorf("请求失败: %w", err)
  118. }
  119. // 成功获取到响应,处理响应体
  120. var generalResponse v1.GeneralResponse[any]
  121. if err := json.Unmarshal(resBody, &generalResponse); err != nil {
  122. return nil, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  123. }
  124. // 检查 API 返回的 code 和 message
  125. if generalResponse.Code == 400 && generalResponse.Message == "invalid access token" {
  126. fmt.Printf("尝试 %d/%d:API 返回无效 token 错误,准备刷新并重试...\n", i+1, s.maxRetryCount)
  127. _, getTokenErr := s.GetToken(ctx)
  128. if getTokenErr != nil {
  129. return nil, fmt.Errorf("刷新 token 失败: %w", getTokenErr)
  130. }
  131. continue // 继续下一次循环,使用新的 token
  132. }
  133. // 成功处理,返回结果
  134. return resBody, nil
  135. }
  136. // 如果循环结束仍未成功,则返回最终错误
  137. return nil, fmt.Errorf("达到最大重试次数后请求仍然失败")
  138. }
  139. // isTokenInvalidError 是一个辅助函数,用于判断错误是否是由于 token 无效引起的。
  140. // 你需要根据你的 request.Request 实现来具体实现这个函数。
  141. // 例如,你可以检查 resBody 是否包含特定的错误信息。
  142. func (s *cdnService) isTokenInvalidError(resBody []byte, err error) bool {
  143. // 示例:如果请求本身就返回了非 200 的错误,并且响应体中有特定信息
  144. if err != nil {
  145. // 尝试从 resBody 中解析出错误信息,判断是否是 token 无效
  146. var generalResponse v1.GeneralResponse[any]
  147. if parseErr := json.Unmarshal(resBody, &generalResponse); parseErr == nil {
  148. if generalResponse.Code == 400 && generalResponse.Message == "invalid access token" {
  149. return true
  150. }
  151. }
  152. // 或者检查 err 本身是否有相关的错误信息
  153. // if strings.Contains(err.Error(), "invalid access token") {
  154. // return true
  155. // }
  156. }
  157. return false
  158. }
  159. func (s *cdnService) GetToken(ctx context.Context) (string, error) {
  160. formData := map[string]interface{}{
  161. "type": "admin",
  162. "accessKeyId": s.AccessKeyID,
  163. "accessKey": s.AccessKeySecret,
  164. }
  165. apiUrl := s.Url + "APIAccessTokenService/getAPIAccessToken"
  166. resBody, err := s.request.Request(ctx, formData, apiUrl, "X-Cloud-Access-Token", "")
  167. if err != nil {
  168. return "", err
  169. }
  170. var res v1.GeneralResponse[v1.FlexCdnTokenResponse]
  171. if err := json.Unmarshal(resBody, &res); err != nil {
  172. return "", fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  173. }
  174. if res.Code != 200 {
  175. return "", fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  176. }
  177. err = s.cdnRepository.PutToken(ctx, res.Data.Token)
  178. if err != nil {
  179. return "", err
  180. }
  181. return res.Data.Token, nil
  182. }
  183. func (s *cdnService) Token(ctx context.Context) (string, error) {
  184. token, err := s.cdnRepository.GetToken(ctx)
  185. if err != nil {
  186. return "", err
  187. }
  188. if token == "" {
  189. token, err = s.GetToken(ctx)
  190. if err != nil {
  191. return "", err
  192. }
  193. }
  194. return token, nil
  195. }
  196. // 注册用户
  197. func (s *cdnService) AddUser(ctx context.Context, req v1.User) (int64, error) {
  198. formData := map[string]interface{}{
  199. "id": req.ID,
  200. "username": req.Username,
  201. "password": "a7fKiKujgAzzsJ6", // 这个密码应该被妥善管理,而不是硬编码
  202. "fullname": req.Fullname,
  203. "mobile": req.Mobile,
  204. "tel": req.Tel,
  205. "email": req.Email,
  206. "remark": req.Remark,
  207. "source": req.Source,
  208. "nodeClusterId": 1,
  209. }
  210. apiUrl := s.Url + "UserService/createUser"
  211. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  212. if err != nil {
  213. return 0, err
  214. }
  215. type DataStr struct {
  216. UserId int64 `json:"userId" form:"userId"`
  217. }
  218. var res v1.GeneralResponse[DataStr]
  219. if err := json.Unmarshal(resBody, &res); err != nil {
  220. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  221. }
  222. if res.Code != 200 {
  223. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  224. }
  225. if res.Data.UserId == 0 {
  226. return 0, fmt.Errorf("添加用户失败")
  227. }
  228. return res.Data.UserId, nil
  229. }
  230. // 创建规则分组
  231. func (s *cdnService) CreateGroup(ctx context.Context, req v1.Group) (int64, error) {
  232. formData := map[string]interface{}{
  233. "name": req.Name,
  234. }
  235. apiUrl := s.Url + "ServerGroupService/createServerGroup"
  236. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  237. if err != nil {
  238. return 0, err
  239. }
  240. type DataStr struct {
  241. ServerGroupId int64 `json:"serverGroupId" form:"serverGroupId"`
  242. }
  243. var res v1.GeneralResponse[DataStr]
  244. if err := json.Unmarshal(resBody, &res); err != nil {
  245. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  246. }
  247. if res.Code != 200 {
  248. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  249. }
  250. if res.Data.ServerGroupId == 0 {
  251. return 0, fmt.Errorf("创建规则分组失败")
  252. }
  253. return res.Data.ServerGroupId, nil
  254. }
  255. // 分配套餐
  256. func (s *cdnService) BindPlan(ctx context.Context, req v1.Plan) (int64, error) {
  257. formData := map[string]interface{}{
  258. "userId": req.UserId,
  259. "planId": req.PlanId,
  260. "dayTo": req.DayTo,
  261. "period": req.Period,
  262. "countPeriod": req.CountPeriod,
  263. "name": req.Name,
  264. "isFree": req.IsFree,
  265. "periodDayTo": req.PeriodDayTo,
  266. }
  267. apiUrl := s.Url + "UserPlanService/buyUserPlan"
  268. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  269. if err != nil {
  270. return 0, err
  271. }
  272. type DataStr struct {
  273. UserPlanId int64 `json:"userPlanId" form:"userPlanId"`
  274. }
  275. var res v1.GeneralResponse[DataStr]
  276. if err := json.Unmarshal(resBody, &res); err != nil {
  277. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  278. }
  279. if res.Code != 200 {
  280. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  281. }
  282. if res.Data.UserPlanId == 0 {
  283. return 0, fmt.Errorf("分配套餐失败")
  284. }
  285. return res.Data.UserPlanId, nil
  286. }
  287. // 续费套餐
  288. func (s *cdnService) RenewPlan(ctx context.Context, req v1.RenewalPlan) error {
  289. formData := map[string]interface{}{
  290. "userPlanId": req.UserPlanId,
  291. "dayTo": req.DayTo,
  292. "period": req.Period,
  293. "countPeriod": req.CountPeriod,
  294. "isFree": req.IsFree,
  295. "periodDayTo": req.PeriodDayTo,
  296. }
  297. apiUrl := s.Url + "UserPlanService/renewUserPlan"
  298. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  299. if err != nil {
  300. return err
  301. }
  302. var res v1.GeneralResponse[any]
  303. if err := json.Unmarshal(resBody, &res); err != nil {
  304. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  305. }
  306. if res.Code != 200 {
  307. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  308. }
  309. return nil
  310. }
  311. // 创建网站
  312. func (s *cdnService) CreateWebsite(ctx context.Context, req v1.WebsiteSend) (int64, error) {
  313. formData := map[string]interface{}{
  314. "userId": req.UserId,
  315. "type": req.Type,
  316. "name": req.Name,
  317. "description": req.Description,
  318. "serverNamesJSON": req.ServerNamesJSON,
  319. "httpJSON": req.HttpJSON,
  320. "httpsJSON": req.HttpsJSON,
  321. "tcpJSON": req.TcpJSON,
  322. "tlsJSON": req.TlsJSON,
  323. "udpJSON": req.UdpJSON,
  324. "webId": req.WebId,
  325. "reverseProxyJSON": req.ReverseProxyJSON,
  326. "serverGroupIds": req.ServerGroupIds,
  327. "userPlanId": req.UserPlanId,
  328. "nodeClusterId": req.NodeClusterId,
  329. }
  330. apiUrl := s.Url + "ServerService/createServer"
  331. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  332. if err != nil {
  333. return 0, err
  334. }
  335. type DataStr struct {
  336. ServerId int64 `json:"serverId" form:"serverId"`
  337. }
  338. var res v1.GeneralResponse[DataStr]
  339. if err := json.Unmarshal(resBody, &res); err != nil {
  340. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  341. }
  342. if res.Code != 200 {
  343. return 0, fmt.Errorf("创建网站API 错误: code %d, msg '%s'", res.Code, res.Message)
  344. }
  345. if res.Data.ServerId == 0 {
  346. return 0, fmt.Errorf("创建网站失败")
  347. }
  348. return res.Data.ServerId, nil
  349. }
  350. func (s *cdnService) EditProtocol(ctx context.Context, req v1.ProxyJson, action string) error {
  351. formData := map[string]interface{}{
  352. "serverId": req.ServerId,
  353. }
  354. var apiUrl string
  355. switch action {
  356. case "tcp":
  357. formData["tcpJSON"] = req.JSON
  358. apiUrl = s.Url + "ServerService/updateServerTCP"
  359. case "tls":
  360. formData["tlsJSON"] = req.JSON
  361. apiUrl = s.Url + "ServerService/updateServerTLS"
  362. case "udp":
  363. formData["udpJSON"] = req.JSON
  364. apiUrl = s.Url + "ServerService/updateServerUDP"
  365. case "http":
  366. formData["httpJSON"] = req.JSON
  367. apiUrl = s.Url + "ServerService/updateServerHTTP"
  368. case "https":
  369. formData["httpsJSON"] = req.JSON
  370. apiUrl = s.Url + "ServerService/updateServerHTTPS"
  371. default:
  372. return fmt.Errorf("不支持的协议类型")
  373. }
  374. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  375. if err != nil {
  376. return err
  377. }
  378. var res v1.GeneralResponse[any]
  379. if err := json.Unmarshal(resBody, &res); err != nil {
  380. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  381. }
  382. if res.Code != 200 {
  383. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  384. }
  385. return nil
  386. }
  387. func (s *cdnService) CreateOrigin(ctx context.Context, req v1.Origin) (int64, error) {
  388. formData := map[string]interface{}{
  389. "name": req.Name,
  390. "addr": req.Addr,
  391. "ossJSON": req.OssJSON,
  392. "description": req.Description,
  393. "weight": req.Weight,
  394. "isOn": req.IsOn,
  395. "domains": req.Domains,
  396. "certRefJSON": req.CertRefJSON,
  397. "host": req.Host,
  398. "followPort": req.FollowPort,
  399. "http2Enabled": req.Http2Enabled,
  400. "tlsSecurityVerifyMode": req.TlsSecurityVerifyMode,
  401. }
  402. apiUrl := s.Url + "OriginService/createOrigin"
  403. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  404. if err != nil {
  405. return 0, err
  406. }
  407. type DataStr struct {
  408. OriginId int64 `json:"originId" form:"originId"`
  409. }
  410. var res v1.GeneralResponse[DataStr]
  411. if err := json.Unmarshal(resBody, &res); err != nil {
  412. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  413. }
  414. if res.Code != 200 {
  415. return 0, fmt.Errorf("添加源站API 错误: code %d, msg '%s'", res.Code, res.Message)
  416. }
  417. if res.Data.OriginId == 0 {
  418. return 0, fmt.Errorf("创建源站失败")
  419. }
  420. return res.Data.OriginId, nil
  421. }
  422. func (s *cdnService) EditServerType(ctx context.Context, req v1.EditWebsite, apiType string) error {
  423. typeName := apiType + "JSON"
  424. formData := map[string]interface{}{
  425. "serverId": req.Id,
  426. typeName: req.TypeJSON,
  427. }
  428. apiUrl := s.Url + "ServerService/updateServer" + strings.ToUpper(apiType)
  429. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  430. if err != nil {
  431. return err
  432. }
  433. var res v1.GeneralResponse[any]
  434. if err := json.Unmarshal(resBody, &res); err != nil {
  435. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  436. }
  437. if res.Code != 200 {
  438. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  439. }
  440. return nil
  441. }
  442. // EditOrigin 编辑源站
  443. func (s *cdnService) EditOrigin(ctx context.Context, req v1.Origin) error {
  444. formData := map[string]interface{}{
  445. "originId": req.OriginId,
  446. "name": req.Name,
  447. "addr": req.Addr,
  448. "ossJSON": req.OssJSON,
  449. "description": req.Description,
  450. "weight": req.Weight,
  451. "isOn": req.IsOn,
  452. "domains": req.Domains,
  453. "certRefJSON": req.CertRefJSON,
  454. "host": req.Host,
  455. "followPort": req.FollowPort,
  456. "http2Enabled": req.Http2Enabled,
  457. "tlsSecurityVerifyMode": req.TlsSecurityVerifyMode,
  458. }
  459. apiUrl := s.Url + "OriginService/updateOrigin"
  460. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  461. if err != nil {
  462. return err
  463. }
  464. var res v1.GeneralResponse[any]
  465. if err := json.Unmarshal(resBody, &res); err != nil {
  466. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  467. }
  468. if res.Code != 200 {
  469. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  470. }
  471. return nil
  472. }
  473. // AddServerOrigin 网站绑定源站
  474. func (s *cdnService) AddServerOrigin(ctx context.Context, serverId int64, originId int64) error {
  475. formData := map[string]interface{}{
  476. "serverId": serverId,
  477. "originId": originId,
  478. "isPrimary": true,
  479. }
  480. apiUrl := s.Url + "ServerService/addServerOrigin"
  481. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  482. if err != nil {
  483. return err
  484. }
  485. var res v1.GeneralResponse[any]
  486. if err := json.Unmarshal(resBody, &res); err != nil {
  487. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  488. }
  489. if res.Code != 200 {
  490. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  491. }
  492. return nil
  493. }
  494. // EditOriginIsOn 编辑源站是否开启
  495. func (s *cdnService) EditOriginIsOn(ctx context.Context, originId int64, isOn bool) error {
  496. formData := map[string]interface{}{
  497. "originId": originId,
  498. "isOn": isOn,
  499. }
  500. apiUrl := s.Url + "OriginService/updateOriginIsOn"
  501. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  502. if err != nil {
  503. return err
  504. }
  505. var res v1.GeneralResponse[any]
  506. if err := json.Unmarshal(resBody, &res); err != nil {
  507. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  508. }
  509. if res.Code != 200 {
  510. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  511. }
  512. return nil
  513. }
  514. // EditServerBasic 修改网站基本信息
  515. func (s *cdnService) EditServerBasic(ctx context.Context, serverId int64, name string,nodeId int64) error {
  516. formData := map[string]interface{}{
  517. "serverId": serverId,
  518. "name": name,
  519. "nodeClusterId": nodeId,
  520. "isOn": true,
  521. }
  522. apiUrl := s.Url + "ServerService/updateServerBasic"
  523. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  524. if err != nil {
  525. return err
  526. }
  527. var res v1.GeneralResponse[any]
  528. if err := json.Unmarshal(resBody, &res); err != nil {
  529. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  530. }
  531. if res.Code != 200 {
  532. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  533. }
  534. return nil
  535. }
  536. // DelServerOrigin 从网站中删除某个源站
  537. func (s *cdnService) DelServerOrigin(ctx context.Context, serverId int64, originId int64) error {
  538. formData := map[string]interface{}{
  539. "serverId": serverId,
  540. "originId": originId,
  541. }
  542. apiUrl := s.Url + "ServerService/deleteServerOrigin"
  543. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  544. if err != nil {
  545. return err
  546. }
  547. var res v1.GeneralResponse[any]
  548. if err := json.Unmarshal(resBody, &res); err != nil {
  549. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  550. }
  551. if res.Code != 200 {
  552. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  553. }
  554. return nil
  555. }
  556. func (s *cdnService) DelServer(ctx context.Context, serverId int64) error {
  557. formData := map[string]interface{}{
  558. "serverId": serverId,
  559. }
  560. apiUrl := s.Url + "ServerService/deleteServer"
  561. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  562. if err != nil {
  563. return err
  564. }
  565. var res v1.GeneralResponse[any]
  566. if err := json.Unmarshal(resBody, &res); err != nil {
  567. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  568. }
  569. if res.Code != 200 {
  570. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  571. }
  572. return nil
  573. }
  574. // AddSSLCert 添加证书
  575. func (s *cdnService) AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, error) {
  576. formData := map[string]interface{}{
  577. "isOn": req.IsOn,
  578. "userId": req.UserId,
  579. "name": req.Name,
  580. "serverName": req.ServerName,
  581. "description": req.Description,
  582. "isCA": req.IsCA,
  583. "certData": req.CertData,
  584. "keyData": req.KeyData,
  585. "timeBeginAt": req.TimeBeginAt,
  586. "timeEndAt": req.TimeEndAt,
  587. "dnsNames": req.DnsNames,
  588. "commonNames": req.CommonNames,
  589. "isSelfSigned": req.IsSelfSigned,
  590. }
  591. apiUrl := s.Url + "SSLCertService/createSSLCert"
  592. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  593. if err != nil {
  594. return 0, err
  595. }
  596. type DataStr struct {
  597. SslCertId int64 `json:"sslCertId" form:"sslCertId"`
  598. }
  599. var res v1.GeneralResponse[DataStr]
  600. if err := json.Unmarshal(resBody, &res); err != nil {
  601. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  602. }
  603. if res.Code != 200 {
  604. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  605. }
  606. return res.Data.SslCertId, nil
  607. }
  608. // 修改网站域名
  609. func (s *cdnService) EditServerName(ctx context.Context, req v1.EditServerNames) error {
  610. formData := map[string]interface{}{
  611. "serverId": req.ServerId,
  612. "serverNamesJSON": req.ServerNamesJSON,
  613. }
  614. apiUrl := s.Url + "ServerService/updateServerNames"
  615. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  616. if err != nil {
  617. return err
  618. }
  619. var res v1.GeneralResponse[any]
  620. if err := json.Unmarshal(resBody, &res); err != nil {
  621. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  622. }
  623. if res.Code != 200 {
  624. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  625. }
  626. return nil
  627. }
  628. // 添加ssl策略
  629. func (s *cdnService) AddSSLPolicy(ctx context.Context, req v1.AddSSLPolicy) (int64, error) {
  630. formData := map[string]interface{}{
  631. "http2Enabled": req.Http2Enabled,
  632. "http3Enabled": req.Http3Enabled,
  633. "minVersion": req.MinVersion,
  634. "sslCertsJSON": req.SslCertsJSON,
  635. "hstsJSON": req.HstsJSON,
  636. "clientAuthType": req.ClientAuthType,
  637. "cipherSuites": req.CipherSuites,
  638. "cipherSuitesIsOn": req.CipherSuitesIsOn,
  639. "ocspIsOn": req.OcspIsOn,
  640. }
  641. apiUrl := s.Url + "SSLPolicyService/createSSLPolicy"
  642. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  643. if err != nil {
  644. return 0, err
  645. }
  646. type DataStr struct {
  647. SslPolicyId int64 `json:"sslPolicyId" form:"sslPolicyId"`
  648. }
  649. var res v1.GeneralResponse[DataStr]
  650. if err := json.Unmarshal(resBody, &res); err != nil {
  651. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  652. }
  653. if res.Code != 200 {
  654. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  655. }
  656. return res.Data.SslPolicyId, nil
  657. }
  658. func (s *cdnService) DelSSLCert(ctx context.Context, sslCertId int64) error {
  659. formData := map[string]interface{}{
  660. "sslCertId": sslCertId,
  661. }
  662. apiUrl := s.Url + "SSLCertService/deleteSSLCert"
  663. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  664. if err != nil {
  665. return err
  666. }
  667. var res v1.GeneralResponse[any]
  668. if err := json.Unmarshal(resBody, &res); err != nil {
  669. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  670. }
  671. if res.Code != 200 {
  672. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  673. }
  674. return nil
  675. }
  676. func (s *cdnService) GetSSLPolicy(ctx context.Context, sslPolicyId int64) (v1.SSLPolicy, error) {
  677. formData := map[string]interface{}{
  678. "sslPolicyId": sslPolicyId,
  679. "ignoreData": true,
  680. }
  681. apiUrl := s.Url + "SSLPolicyService/findEnabledSSLPolicyConfig"
  682. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  683. if err != nil {
  684. return v1.SSLPolicy{}, err
  685. }
  686. var res v1.GeneralResponse[v1.SSLPolicy]
  687. if err := json.Unmarshal(resBody, &res); err != nil {
  688. return v1.SSLPolicy{}, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  689. }
  690. if res.Code != 200 {
  691. return v1.SSLPolicy{}, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  692. }
  693. return res.Data, nil
  694. }
  695. func (s *cdnService) EditSSLCert(ctx context.Context, req v1.SSlCert) error {
  696. formData := map[string]interface{}{
  697. "sslCertId": req.SslCertId,
  698. "userId": req.UserId,
  699. "isOn": req.IsOn,
  700. "name": req.Name,
  701. "description": req.Description,
  702. "isCA": req.IsCA,
  703. "certData": req.CertData,
  704. "keyData": req.KeyData,
  705. "timeBeginAt": req.TimeBeginAt,
  706. "timeEndAt": req.TimeEndAt,
  707. "dnsNames": req.DnsNames,
  708. "commonNames": req.CommonNames,
  709. "isSelfSigned": req.IsSelfSigned,
  710. }
  711. apiUrl := s.Url + "SSLCertService/updateSSLCert"
  712. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  713. if err != nil {
  714. return err
  715. }
  716. var res v1.GeneralResponse[any]
  717. if err := json.Unmarshal(resBody, &res); err != nil {
  718. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  719. }
  720. if res.Code != 200 {
  721. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  722. }
  723. return nil
  724. }
  725. func (s *cdnService) EditSSLPolicy(ctx context.Context, req v1.SSLPolicy) error {
  726. formData := map[string]interface{}{
  727. "sslPolicyId": req.SslPolicyId,
  728. "http2Enabled": req.Http2Enabled,
  729. "http3Enabled": req.Http3Enabled,
  730. "minVersion": req.MinVersion,
  731. "sslCertsJSON": req.SslCertsJSON,
  732. "hstsJSON": req.HstsJSON,
  733. "clientAuthType": req.ClientAuthType,
  734. "cipherSuites": req.CipherSuites,
  735. "cipherSuitesIsOn": req.CipherSuitesIsOn,
  736. }
  737. apiUrl := s.Url + "SSLPolicyService/updateSSLPolicy"
  738. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  739. if err != nil {
  740. return err
  741. }
  742. var res v1.GeneralResponse[any]
  743. if err := json.Unmarshal(resBody, &res); err != nil {
  744. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  745. }
  746. if res.Code != 200 {
  747. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  748. }
  749. return nil
  750. }
  751. // 修改反向代理
  752. func (s *cdnService) EditProxy(ctx context.Context, req v1.Proxy) error {
  753. formData := map[string]interface{}{
  754. "reverseProxyId" : req.ReverseProxyId,
  755. "requestHostType" : req.RequestHostType,
  756. "requestHost" : req.RequestHost,
  757. "requestHostExcludingPort" : req.RequestHostExcludingPort,
  758. "requestURI" : req.RequestURI,
  759. "stripPrefix" : req.StripPrefix,
  760. "autoFlush" : req.AutoFlush,
  761. "addHeaders" : req.AddHeaders,
  762. "proxyProtocolJSON": req.ProxyProtocolJSON,
  763. "followRedirects" : req.FollowRedirects,
  764. "retry50X" : req.Retry50X,
  765. "retry40X" : req.Retry40X,
  766. }
  767. apiUrl := s.Url + "ReverseProxyService/updateReverseProxy"
  768. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  769. if err != nil {
  770. return err
  771. }
  772. var res v1.GeneralResponse[any]
  773. if err := json.Unmarshal(resBody, &res); err != nil {
  774. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  775. }
  776. if res.Code != 200 {
  777. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  778. }
  779. return nil
  780. }
  781. // 修改网站日志配置
  782. func (s *cdnService) EditWebLog(ctx context.Context,webId int64, req v1.WebLog) error {
  783. reqJson, err := json.Marshal(req)
  784. if err != nil {
  785. return err
  786. }
  787. formData := map[string]interface{}{
  788. "httpWebId": webId,
  789. "accessLogJSON": reqJson,
  790. }
  791. apiUrl := s.Url + "HTTPWebService/updateHTTPWebAccessLog"
  792. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  793. if err != nil {
  794. return err
  795. }
  796. var res v1.GeneralResponse[any]
  797. if err := json.Unmarshal(resBody, &res); err != nil {
  798. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  799. }
  800. if res.Code != 200 {
  801. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  802. }
  803. return nil
  804. }
  805. // 修改网站CC配置
  806. func (s *cdnService) EditCcConfig(ctx context.Context,webId int64, req v1.CcConfig) error {
  807. reqJson, err := json.Marshal(req)
  808. if err != nil {
  809. return err
  810. }
  811. formData := map[string]interface{}{
  812. "httpWebId": webId,
  813. "ccJSON": reqJson,
  814. }
  815. apiUrl := s.Url + "HTTPWebService/updateHTTPWebCC"
  816. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  817. if err != nil {
  818. return err
  819. }
  820. var res v1.GeneralResponse[any]
  821. if err := json.Unmarshal(resBody, &res); err != nil {
  822. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  823. }
  824. if res.Code != 200 {
  825. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  826. }
  827. return nil
  828. }
  829. // 创建websockets配置
  830. func (s *cdnService) AddWebSockets(ctx context.Context, req v1.WebSocket) (int64,error) {
  831. formData := map[string]interface{}{
  832. "handshakeTimeoutJSON": req.HandshakeTimeoutJSON,
  833. "allowAllOrigins" : req.AllowAllOrigins,
  834. "allowedOrigins" : req.AllowedOrigins,
  835. "requestSameOrigin" : req.RequestSameOrigin,
  836. "requestOrigin" : req.RequestOrigin,
  837. }
  838. apiUrl := s.Url + "HTTPWebsocketService/createHTTPWebsocket"
  839. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  840. if err != nil {
  841. return 0,err
  842. }
  843. type WebSocket struct {
  844. WebSocketId int64 `json:"websocketId"`
  845. }
  846. var res v1.GeneralResponse[WebSocket]
  847. if err := json.Unmarshal(resBody, &res); err != nil {
  848. return 0,fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  849. }
  850. if res.Code != 200 {
  851. return 0,fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  852. }
  853. return res.Data.WebSocketId,nil
  854. }
  855. func (s *cdnService) EditWebSockets(ctx context.Context,req v1.WebSocket) error {
  856. formData := map[string]interface{}{
  857. "websocketId" : req.WebsocketId,
  858. "handshakeTimeoutJSON": req.HandshakeTimeoutJSON,
  859. "allowAllOrigins" : req.AllowAllOrigins,
  860. "allowedOrigins" : req.AllowedOrigins,
  861. "requestSameOrigin" : req.RequestSameOrigin,
  862. "requestOrigin" : req.RequestOrigin,
  863. }
  864. apiUrl := s.Url + "HTTPWebsocketService/updateHTTPWebsocket"
  865. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  866. if err != nil {
  867. return err
  868. }
  869. var res v1.GeneralResponse[any]
  870. if err := json.Unmarshal(resBody, &res); err != nil {
  871. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  872. }
  873. if res.Code != 200 {
  874. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  875. }
  876. return nil
  877. }
  878. // 启用/禁用websockets
  879. func (s *cdnService) EditHTTPWebWebsocket(ctx context.Context,websocketId int64,websocketJSON []byte) error {
  880. formData := map[string]interface{}{
  881. "httpWebId" : websocketId,
  882. "websocketJSON": websocketJSON,
  883. }
  884. apiUrl := s.Url + "HTTPWebService/updateHTTPWebWebsocket"
  885. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  886. if err != nil {
  887. return err
  888. }
  889. var res v1.GeneralResponse[any]
  890. if err := json.Unmarshal(resBody, &res); err != nil {
  891. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  892. }
  893. if res.Code != 200 {
  894. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  895. }
  896. return nil
  897. }
  898. // 启用/禁用网站
  899. func (s *cdnService) EditWebIsOn(ctx context.Context,serverId int64,isOn bool) error {
  900. formData := map[string]interface{}{
  901. "serverId": serverId,
  902. "isOn": isOn,
  903. }
  904. apiUrl := s.Url + "ServerService/updateServerIsOn"
  905. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  906. if err != nil {
  907. return err
  908. }
  909. var res v1.GeneralResponse[any]
  910. if err := json.Unmarshal(resBody, &res); err != nil {
  911. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  912. }
  913. if res.Code != 200 {
  914. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  915. }
  916. return nil
  917. }
  918. // 删除已购套餐
  919. func (s *cdnService) DelUserPlan(ctx context.Context,planId int64) error {
  920. formData := map[string]interface{}{
  921. "userPlanId": planId,
  922. }
  923. apiUrl := s.Url + "UserPlanService/deleteUserPlan"
  924. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  925. if err != nil {
  926. return err
  927. }
  928. var res v1.GeneralResponse[any]
  929. if err := json.Unmarshal(resBody, &res); err != nil {
  930. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  931. }
  932. if res.Code != 200 {
  933. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  934. }
  935. return nil
  936. }
  937. // 删除网站分组
  938. func (s *cdnService) DelServerGroup(ctx context.Context,serverId int64) error {
  939. formData := map[string]interface{}{
  940. "serverId": serverId,
  941. }
  942. apiUrl := s.Url + "ServerService/deleteServer"
  943. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  944. if err != nil {
  945. return err
  946. }
  947. var res v1.GeneralResponse[any]
  948. if err := json.Unmarshal(resBody, &res); err != nil {
  949. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  950. }
  951. if res.Code != 200 {
  952. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  953. }
  954. return nil
  955. }
  956. // 删除IP
  957. func (s *cdnService) DelIpItem(ctx context.Context,ipitemId int64,value string,ipFrom string,ipTo string,ipListId int64) error {
  958. formData := map[string]interface{}{
  959. "ipitemId": ipitemId,
  960. "value": value,
  961. "ipFrom": ipFrom,
  962. "ipTo": ipTo,
  963. "ipListId": ipListId,
  964. }
  965. apiUrl := s.Url + "IPItemService/deleteIPItem"
  966. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  967. if err != nil {
  968. return err
  969. }
  970. var res v1.GeneralResponse[any]
  971. if err := json.Unmarshal(resBody, &res); err != nil {
  972. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  973. }
  974. if res.Code != 200 {
  975. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  976. }
  977. return nil
  978. }
  979. // 创建空防火墙策略
  980. func (s *cdnService) AddWafPolicy(ctx context.Context,req v1.AddWafPolicy) (int64, error) {
  981. formData := map[string]interface{}{
  982. "isOn": req.IsOn,
  983. "name" : req.Name,
  984. "description" : req.Description,
  985. "serverId" : req.ServerId,
  986. "serverGroupId" : req.ServerGroupId,
  987. }
  988. apiUrl := s.Url + "HTTPFirewallPolicyService/createEmptyHTTPFirewallPolicy"
  989. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  990. if err != nil {
  991. return 0, err
  992. }
  993. var res v1.GeneralResponse[int64]
  994. if err := json.Unmarshal(resBody, &res); err != nil {
  995. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  996. }
  997. if res.Code != 200 {
  998. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  999. }
  1000. return res.Data, nil
  1001. }
  1002. // 更改防火墙设置
  1003. func (s *cdnService) EditHTTPWebFirewal(ctx context.Context,httpWebId int64,FirewallJSON v1.Firewall) error {
  1004. frireWallJson,err := json.Marshal(FirewallJSON)
  1005. if err != nil {
  1006. return err
  1007. }
  1008. formData := map[string]interface{}{
  1009. "httpWebId" : httpWebId,
  1010. "firewallJSON" : frireWallJson,
  1011. }
  1012. apiUrl := s.Url + "HTTPWebService/updateHTTPWebFirewall"
  1013. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  1014. if err != nil {
  1015. return err
  1016. }
  1017. var res v1.GeneralResponse[any]
  1018. if err := json.Unmarshal(resBody, &res); err != nil {
  1019. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  1020. }
  1021. if res.Code != 200 {
  1022. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  1023. }
  1024. return nil
  1025. }
  1026. // 创建IP列表
  1027. func (s *cdnService) AddIPList(ctx context.Context, req v1.IPListRequest) (int64, error) {
  1028. formData := map[string]interface{}{
  1029. "type": req.Type,
  1030. "name": req.Name,
  1031. "code": req.Code,
  1032. "timeoutJSON": req.TimeoutJSON,
  1033. "isPublic": req.IsPublic,
  1034. "description": req.Description,
  1035. "isGlobal": req.IsGlobal,
  1036. "serverId": req.ServerId,
  1037. }
  1038. apiUrl := s.Url + "IPListService/createIPList"
  1039. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  1040. if err != nil {
  1041. return 0, err
  1042. }
  1043. var res v1.GeneralResponse[int64]
  1044. if err := json.Unmarshal(resBody, &res); err != nil {
  1045. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  1046. }
  1047. if res.Code != 200 {
  1048. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  1049. }
  1050. return res.Data, nil
  1051. }
  1052. // 创建IP
  1053. func (s *cdnService) AddIpItem(ctx context.Context, req v1.AddIpItem) error {
  1054. formData := map[string]interface{}{
  1055. "ipListId": req.IpListId,
  1056. "value": req.Value,
  1057. "ipFrom": req.IpFrom,
  1058. "ipTo": req.IpTo,
  1059. "expiredAt": req.ExpiredAt,
  1060. "reason": req.Reason,
  1061. "type": req.Type,
  1062. "eventLevel": req.EventLevel,
  1063. "nodeId": req.NodeId,
  1064. "serverId": req.ServerId,
  1065. "sourceNodeId": req.SourceNodeId,
  1066. "sourceServerId": req.SourceServerId,
  1067. "sourceHTTPFirewallPolicyId": req.SourceHTTPFirewallPolicyId,
  1068. "sourceHTTPFirewallRuleGroupId": req.SourceHTTPFirewallRuleGroupId,
  1069. "sourceHTTPFirewallRuleSetId": req.SourceHTTPFirewallRuleSetId,
  1070. "sourceURL": req.SourceURL,
  1071. "sourceUserAgent": req.SourceUserAgent,
  1072. "sourceCategory": req.SourceCategory,
  1073. }
  1074. apiUrl := s.Url + "IPItemService/createIPItem"
  1075. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  1076. if err != nil {
  1077. return err
  1078. }
  1079. var res v1.GeneralResponse[any]
  1080. if err := json.Unmarshal(resBody, &res); err != nil {
  1081. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  1082. }
  1083. if res.Code != 200 {
  1084. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  1085. }
  1086. return nil
  1087. }