cdn.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package service
  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"
  8. "github.com/spf13/viper"
  9. "strings"
  10. )
  11. type CdnService interface {
  12. // GetToken 获取token
  13. GetToken(ctx context.Context) (string, error)
  14. // AddUser 注册用户
  15. AddUser(ctx context.Context, req v1.User) (int64, error)
  16. CreateGroup(ctx context.Context, req v1.Group) (int64, error)
  17. BindPlan(ctx context.Context, req v1.Plan) (int64, error)
  18. RenewPlan(ctx context.Context, req v1.RenewalPlan) error
  19. CreateWebsite(ctx context.Context, req v1.Website) (int64, error)
  20. EditServerType(ctx context.Context, req v1.EditWebsite, apiType string) error
  21. EditProtocol(ctx context.Context, req v1.ProxyJson, action string) error
  22. CreateOrigin(ctx context.Context, req v1.Origin) (int64, error)
  23. EditOrigin(ctx context.Context, req v1.Origin) error
  24. AddServerOrigin(ctx context.Context, serverId int64, originId int64) error
  25. EditOriginIsOn(ctx context.Context, originId int64, isOn bool) error
  26. // 修改网站基本信息
  27. EditServerBasic(ctx context.Context, serverId int64, name string) error
  28. // 从网站中删除某个源站
  29. DelServerOrigin(ctx context.Context, serverId int64, originId int64) error
  30. // 删除网站
  31. DelServer(ctx context.Context, serverId int64) error
  32. // 添加ssl证书
  33. AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, error)
  34. // 修改网站域名
  35. EditServerName(ctx context.Context, req v1.EditServerNames) error
  36. // 添加ssl策略
  37. AddSSLPolicy(ctx context.Context, req v1.AddSSLPolicy) (int64, error)
  38. DelSSLCert(ctx context.Context, sslCertId int64) error
  39. GetSSLPolicy(ctx context.Context, sslPolicyId int64) (v1.SSLPolicy, error)
  40. }
  41. func NewCdnService(
  42. service *Service,
  43. conf *viper.Viper,
  44. request RequestService,
  45. cdnRepository repository.CdnRepository,
  46. ) CdnService {
  47. return &cdnService{
  48. Service: service,
  49. Url: conf.GetString("flexCdn.Url"),
  50. AccessKeyID: conf.GetString("flexCdn.AccessKeyID"),
  51. AccessKeySecret: conf.GetString("flexCdn.AccessKeySecret"),
  52. request: request,
  53. cdnRepository: cdnRepository,
  54. maxRetryCount: 3, // 可以配置最大重试次数
  55. retryDelaySeconds: 2, // 可以配置重试间隔
  56. }
  57. }
  58. type cdnService struct {
  59. *Service
  60. Url string
  61. AccessKeyID string
  62. AccessKeySecret string
  63. request RequestService
  64. cdnRepository repository.CdnRepository
  65. maxRetryCount int
  66. retryDelaySeconds int
  67. }
  68. // SendData 是一个通用的请求发送方法,它封装了 token 过期重试的逻辑
  69. func (s *cdnService) sendDataWithTokenRetry(ctx context.Context, formData map[string]interface{}, apiUrl string) ([]byte, error) {
  70. var resBody []byte
  71. for i := 0; i < s.maxRetryCount; i++ {
  72. token, err := s.Token(ctx) // 确保使用最新的 token
  73. if err != nil {
  74. return nil, fmt.Errorf("获取或刷新 token 失败: %w", err)
  75. }
  76. resBody, err = s.request.Request(ctx, formData, apiUrl, "X-Cloud-Access-Token", token)
  77. if err != nil {
  78. // 检查错误是否是由于 token 无效引起的
  79. if s.isTokenInvalidError(resBody, err) { // 判断是否是 token 无效错误
  80. _, getTokenErr := s.GetToken(ctx)
  81. if getTokenErr != nil {
  82. return nil, fmt.Errorf("刷新 token 失败: %w", getTokenErr)
  83. }
  84. continue // 继续下一次循环,使用新的 token
  85. }
  86. return nil, fmt.Errorf("请求失败: %w", err)
  87. }
  88. // 成功获取到响应,处理响应体
  89. var generalResponse v1.GeneralResponse[any]
  90. if err := json.Unmarshal(resBody, &generalResponse); err != nil {
  91. return nil, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  92. }
  93. // 检查 API 返回的 code 和 message
  94. if generalResponse.Code == 400 && generalResponse.Message == "invalid access token" {
  95. fmt.Printf("尝试 %d/%d:API 返回无效 token 错误,准备刷新并重试...\n", i+1, s.maxRetryCount)
  96. _, getTokenErr := s.GetToken(ctx)
  97. if getTokenErr != nil {
  98. return nil, fmt.Errorf("刷新 token 失败: %w", getTokenErr)
  99. }
  100. continue // 继续下一次循环,使用新的 token
  101. }
  102. // 成功处理,返回结果
  103. return resBody, nil
  104. }
  105. // 如果循环结束仍未成功,则返回最终错误
  106. return nil, fmt.Errorf("达到最大重试次数后请求仍然失败")
  107. }
  108. // isTokenInvalidError 是一个辅助函数,用于判断错误是否是由于 token 无效引起的。
  109. // 你需要根据你的 request.Request 实现来具体实现这个函数。
  110. // 例如,你可以检查 resBody 是否包含特定的错误信息。
  111. func (s *cdnService) isTokenInvalidError(resBody []byte, err error) bool {
  112. // 示例:如果请求本身就返回了非 200 的错误,并且响应体中有特定信息
  113. if err != nil {
  114. // 尝试从 resBody 中解析出错误信息,判断是否是 token 无效
  115. var generalResponse v1.GeneralResponse[any]
  116. if parseErr := json.Unmarshal(resBody, &generalResponse); parseErr == nil {
  117. if generalResponse.Code == 400 && generalResponse.Message == "invalid access token" {
  118. return true
  119. }
  120. }
  121. // 或者检查 err 本身是否有相关的错误信息
  122. // if strings.Contains(err.Error(), "invalid access token") {
  123. // return true
  124. // }
  125. }
  126. return false
  127. }
  128. func (s *cdnService) GetToken(ctx context.Context) (string, error) {
  129. formData := map[string]interface{}{
  130. "type": "admin",
  131. "accessKeyId": s.AccessKeyID,
  132. "accessKey": s.AccessKeySecret,
  133. }
  134. apiUrl := s.Url + "APIAccessTokenService/getAPIAccessToken"
  135. resBody, err := s.request.Request(ctx, formData, apiUrl, "X-Cloud-Access-Token", "")
  136. if err != nil {
  137. return "", err
  138. }
  139. var res v1.GeneralResponse[v1.FlexCdnTokenResponse]
  140. if err := json.Unmarshal(resBody, &res); err != nil {
  141. return "", fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  142. }
  143. if res.Code != 200 {
  144. return "", fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  145. }
  146. err = s.cdnRepository.PutToken(ctx, res.Data.Token)
  147. if err != nil {
  148. return "", err
  149. }
  150. return res.Data.Token, nil
  151. }
  152. func (s *cdnService) Token(ctx context.Context) (string, error) {
  153. token, err := s.cdnRepository.GetToken(ctx)
  154. if err != nil {
  155. return "", err
  156. }
  157. if token == "" {
  158. token, err = s.GetToken(ctx)
  159. if err != nil {
  160. return "", err
  161. }
  162. }
  163. return token, nil
  164. }
  165. // 注册用户
  166. func (s *cdnService) AddUser(ctx context.Context, req v1.User) (int64, error) {
  167. formData := map[string]interface{}{
  168. "id": req.ID,
  169. "username": req.Username,
  170. "password": "a7fKiKujgAzzsJ6", // 这个密码应该被妥善管理,而不是硬编码
  171. "fullname": req.Fullname,
  172. "mobile": req.Mobile,
  173. "tel": req.Tel,
  174. "email": req.Email,
  175. "remark": req.Remark,
  176. "source": req.Source,
  177. "nodeClusterId": 1,
  178. }
  179. apiUrl := s.Url + "UserService/createUser"
  180. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  181. if err != nil {
  182. return 0, err
  183. }
  184. type DataStr struct {
  185. UserId int64 `json:"userId" form:"userId"`
  186. }
  187. var res v1.GeneralResponse[DataStr]
  188. if err := json.Unmarshal(resBody, &res); err != nil {
  189. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  190. }
  191. if res.Code != 200 {
  192. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  193. }
  194. if res.Data.UserId == 0 {
  195. return 0, fmt.Errorf("添加用户失败")
  196. }
  197. return res.Data.UserId, nil
  198. }
  199. // 创建规则分组
  200. func (s *cdnService) CreateGroup(ctx context.Context, req v1.Group) (int64, error) {
  201. formData := map[string]interface{}{
  202. "name": req.Name,
  203. }
  204. apiUrl := s.Url + "ServerGroupService/createServerGroup"
  205. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  206. if err != nil {
  207. return 0, err
  208. }
  209. type DataStr struct {
  210. ServerGroupId int64 `json:"serverGroupId" form:"serverGroupId"`
  211. }
  212. var res v1.GeneralResponse[DataStr]
  213. if err := json.Unmarshal(resBody, &res); err != nil {
  214. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  215. }
  216. if res.Code != 200 {
  217. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  218. }
  219. if res.Data.ServerGroupId == 0 {
  220. return 0, fmt.Errorf("创建规则分组失败")
  221. }
  222. return res.Data.ServerGroupId, nil
  223. }
  224. // 分配套餐
  225. func (s *cdnService) BindPlan(ctx context.Context, req v1.Plan) (int64, error) {
  226. formData := map[string]interface{}{
  227. "userId": req.UserId,
  228. "planId": req.PlanId,
  229. "dayTo": req.DayTo,
  230. "period": req.Period,
  231. "countPeriod": req.CountPeriod,
  232. "name": req.Name,
  233. "isFree": req.IsFree,
  234. "periodDayTo": req.PeriodDayTo,
  235. }
  236. apiUrl := s.Url + "UserPlanService/buyUserPlan"
  237. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  238. if err != nil {
  239. return 0, err
  240. }
  241. type DataStr struct {
  242. UserPlanId int64 `json:"userPlanId" form:"userPlanId"`
  243. }
  244. var res v1.GeneralResponse[DataStr]
  245. if err := json.Unmarshal(resBody, &res); err != nil {
  246. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  247. }
  248. if res.Code != 200 {
  249. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  250. }
  251. if res.Data.UserPlanId == 0 {
  252. return 0, fmt.Errorf("分配套餐失败")
  253. }
  254. return res.Data.UserPlanId, nil
  255. }
  256. // 续费套餐
  257. func (s *cdnService) RenewPlan(ctx context.Context, req v1.RenewalPlan) error {
  258. formData := map[string]interface{}{
  259. "userPlanId": req.UserPlanId,
  260. "dayTo": req.DayTo,
  261. "period": req.Period,
  262. "countPeriod": req.CountPeriod,
  263. "isFree": req.IsFree,
  264. "periodDayTo": req.PeriodDayTo,
  265. }
  266. apiUrl := s.Url + "UserPlanService/renewUserPlan"
  267. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  268. if err != nil {
  269. return err
  270. }
  271. var res v1.GeneralResponse[any]
  272. if err := json.Unmarshal(resBody, &res); err != nil {
  273. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  274. }
  275. if res.Code != 200 {
  276. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  277. }
  278. return nil
  279. }
  280. // 创建网站
  281. func (s *cdnService) CreateWebsite(ctx context.Context, req v1.Website) (int64, error) {
  282. formData := map[string]interface{}{
  283. "userId": req.UserId,
  284. "type": req.Type,
  285. "name": req.Name,
  286. "description": req.Description,
  287. "serverNamesJSON": req.ServerNamesJSON,
  288. "httpJSON": req.HttpJSON,
  289. "httpsJSON": req.HttpsJSON,
  290. "tcpJSON": req.TcpJSON,
  291. "tlsJSON": req.TlsJSON,
  292. "udpJSON": req.UdpJSON,
  293. "webId": req.WebId,
  294. "reverseProxyJSON": req.ReverseProxyJSON,
  295. "serverGroupIds": req.ServerGroupIds,
  296. "userPlanId": req.UserPlanId,
  297. "nodeClusterId": req.NodeClusterId,
  298. }
  299. apiUrl := s.Url + "ServerService/createServer"
  300. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  301. if err != nil {
  302. return 0, err
  303. }
  304. type DataStr struct {
  305. ServerId int64 `json:"serverId" form:"serverId"`
  306. }
  307. var res v1.GeneralResponse[DataStr]
  308. if err := json.Unmarshal(resBody, &res); err != nil {
  309. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  310. }
  311. if res.Code != 200 {
  312. return 0, fmt.Errorf("创建网站API 错误: code %d, msg '%s'", res.Code, res.Message)
  313. }
  314. if res.Data.ServerId == 0 {
  315. return 0, fmt.Errorf("创建网站失败")
  316. }
  317. return res.Data.ServerId, nil
  318. }
  319. func (s *cdnService) EditProtocol(ctx context.Context, req v1.ProxyJson, action string) error {
  320. formData := map[string]interface{}{
  321. "serverId": req.ServerId,
  322. }
  323. var apiUrl string
  324. switch action {
  325. case "tcp":
  326. formData["tcpJSON"] = req.JSON
  327. apiUrl = s.Url + "ServerService/updateServerTCP"
  328. case "tls":
  329. formData["tlsJSON"] = req.JSON
  330. apiUrl = s.Url + "ServerService/updateServerTLS"
  331. case "udp":
  332. formData["udpJSON"] = req.JSON
  333. apiUrl = s.Url + "ServerService/updateServerUDP"
  334. case "http":
  335. formData["httpJSON"] = req.JSON
  336. apiUrl = s.Url + "ServerService/updateServerHTTP"
  337. case "https":
  338. formData["httpsJSON"] = req.JSON
  339. apiUrl = s.Url + "ServerService/updateServerHTTPS"
  340. default:
  341. return fmt.Errorf("不支持的协议类型")
  342. }
  343. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  344. if err != nil {
  345. return err
  346. }
  347. var res v1.GeneralResponse[any]
  348. if err := json.Unmarshal(resBody, &res); err != nil {
  349. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  350. }
  351. if res.Code != 200 {
  352. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  353. }
  354. return nil
  355. }
  356. func (s *cdnService) CreateOrigin(ctx context.Context, req v1.Origin) (int64, error) {
  357. formData := map[string]interface{}{
  358. "name": req.Name,
  359. "addr": req.Addr,
  360. "ossJSON": req.OssJSON,
  361. "description": req.Description,
  362. "weight": req.Weight,
  363. "isOn": req.IsOn,
  364. "domains": req.Domains,
  365. "certRefJSON": req.CertRefJSON,
  366. "host": req.Host,
  367. "followPort": req.FollowPort,
  368. "http2Enabled": req.Http2Enabled,
  369. "tlsSecurityVerifyMode": req.TlsSecurityVerifyMode,
  370. }
  371. apiUrl := s.Url + "OriginService/createOrigin"
  372. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl) // 使用封装后的方法
  373. if err != nil {
  374. return 0, err
  375. }
  376. type DataStr struct {
  377. OriginId int64 `json:"originId" form:"originId"`
  378. }
  379. var res v1.GeneralResponse[DataStr]
  380. if err := json.Unmarshal(resBody, &res); err != nil {
  381. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  382. }
  383. if res.Code != 200 {
  384. return 0, fmt.Errorf("添加源站API 错误: code %d, msg '%s'", res.Code, res.Message)
  385. }
  386. if res.Data.OriginId == 0 {
  387. return 0, fmt.Errorf("创建源站失败")
  388. }
  389. return res.Data.OriginId, nil
  390. }
  391. func (s *cdnService) EditServerType(ctx context.Context, req v1.EditWebsite, apiType string) error {
  392. typeName := apiType + "JSON"
  393. formData := map[string]interface{}{
  394. "serverId": req.Id,
  395. typeName: req.TypeJSON,
  396. }
  397. apiUrl := s.Url + "ServerService/updateServer" + strings.ToUpper(apiType)
  398. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  399. if err != nil {
  400. return err
  401. }
  402. var res v1.GeneralResponse[any]
  403. if err := json.Unmarshal(resBody, &res); err != nil {
  404. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  405. }
  406. if res.Code != 200 {
  407. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  408. }
  409. return nil
  410. }
  411. // EditOrigin 编辑源站
  412. func (s *cdnService) EditOrigin(ctx context.Context, req v1.Origin) error {
  413. formData := map[string]interface{}{
  414. "originId": req.OriginId,
  415. "name": req.Name,
  416. "addr": req.Addr,
  417. "ossJSON": req.OssJSON,
  418. "description": req.Description,
  419. "weight": req.Weight,
  420. "isOn": req.IsOn,
  421. "domains": req.Domains,
  422. "certRefJSON": req.CertRefJSON,
  423. "host": req.Host,
  424. "followPort": req.FollowPort,
  425. "http2Enabled": req.Http2Enabled,
  426. "tlsSecurityVerifyMode": req.TlsSecurityVerifyMode,
  427. }
  428. apiUrl := s.Url + "OriginService/updateOrigin"
  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. // AddServerOrigin 网站绑定源站
  443. func (s *cdnService) AddServerOrigin(ctx context.Context, serverId int64, originId int64) error {
  444. formData := map[string]interface{}{
  445. "serverId": serverId,
  446. "originId": originId,
  447. "isPrimary": true,
  448. }
  449. apiUrl := s.Url + "ServerService/addServerOrigin"
  450. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  451. if err != nil {
  452. return err
  453. }
  454. var res v1.GeneralResponse[any]
  455. if err := json.Unmarshal(resBody, &res); err != nil {
  456. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  457. }
  458. if res.Code != 200 {
  459. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  460. }
  461. return nil
  462. }
  463. // EditOriginIsOn 编辑源站是否开启
  464. func (s *cdnService) EditOriginIsOn(ctx context.Context, originId int64, isOn bool) error {
  465. formData := map[string]interface{}{
  466. "originId": originId,
  467. "isOn": isOn,
  468. }
  469. apiUrl := s.Url + "OriginService/updateOriginIsOn"
  470. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  471. if err != nil {
  472. return err
  473. }
  474. var res v1.GeneralResponse[any]
  475. if err := json.Unmarshal(resBody, &res); err != nil {
  476. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  477. }
  478. if res.Code != 200 {
  479. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  480. }
  481. return nil
  482. }
  483. // EditServerBasic 修改网站基本信息
  484. func (s *cdnService) EditServerBasic(ctx context.Context, serverId int64, name string) error {
  485. formData := map[string]interface{}{
  486. "serverId": serverId,
  487. "name": name,
  488. "nodeClusterId": 1,
  489. "isOn": true,
  490. }
  491. apiUrl := s.Url + "ServerService/updateServerBasic"
  492. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  493. if err != nil {
  494. return err
  495. }
  496. var res v1.GeneralResponse[any]
  497. if err := json.Unmarshal(resBody, &res); err != nil {
  498. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  499. }
  500. if res.Code != 200 {
  501. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  502. }
  503. return nil
  504. }
  505. // DelServerOrigin 从网站中删除某个源站
  506. func (s *cdnService) DelServerOrigin(ctx context.Context, serverId int64, originId int64) error {
  507. formData := map[string]interface{}{
  508. "serverId": serverId,
  509. "originId": originId,
  510. }
  511. apiUrl := s.Url + "ServerService/deleteServerOrigin"
  512. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  513. if err != nil {
  514. return err
  515. }
  516. var res v1.GeneralResponse[any]
  517. if err := json.Unmarshal(resBody, &res); err != nil {
  518. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  519. }
  520. if res.Code != 200 {
  521. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  522. }
  523. return nil
  524. }
  525. func (s *cdnService) DelServer(ctx context.Context, serverId int64) error {
  526. formData := map[string]interface{}{
  527. "serverId": serverId,
  528. }
  529. apiUrl := s.Url + "ServerService/deleteServer"
  530. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  531. if err != nil {
  532. return err
  533. }
  534. var res v1.GeneralResponse[any]
  535. if err := json.Unmarshal(resBody, &res); err != nil {
  536. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  537. }
  538. if res.Code != 200 {
  539. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  540. }
  541. return nil
  542. }
  543. // AddSSLCert 添加证书
  544. func (s *cdnService) AddSSLCert(ctx context.Context, req v1.SSlCert) (int64, error) {
  545. formData := map[string]interface{}{
  546. "isOn": req.IsOn,
  547. "userId": req.UserId,
  548. "name": req.Name,
  549. "serverName": req.ServerName,
  550. "description": req.Description,
  551. "isCA": req.IsCA,
  552. "certData": req.CertData,
  553. "keyData": req.KeyData,
  554. "timeBeginAt": req.TimeBeginAt,
  555. "timeEndAt": req.TimeEndAt,
  556. "dnsNames": req.DnsNames,
  557. "commonNames": req.CommonNames,
  558. "isSelfSigned": req.IsSelfSigned,
  559. }
  560. apiUrl := s.Url + "SSLCertService/createSSLCert"
  561. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  562. if err != nil {
  563. return 0, err
  564. }
  565. type DataStr struct {
  566. SslCertId int64 `json:"sslCertId" form:"sslCertId"`
  567. }
  568. var res v1.GeneralResponse[DataStr]
  569. if err := json.Unmarshal(resBody, &res); err != nil {
  570. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  571. }
  572. if res.Code != 200 {
  573. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  574. }
  575. return res.Data.SslCertId, nil
  576. }
  577. // 修改网站域名
  578. func (s *cdnService) EditServerName(ctx context.Context, req v1.EditServerNames) error {
  579. formData := map[string]interface{}{
  580. "serverId": req.ServerId,
  581. "serverNamesJSON": req.ServerNamesJSON,
  582. }
  583. apiUrl := s.Url + "ServerService/updateServerNames"
  584. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  585. if err != nil {
  586. return err
  587. }
  588. var res v1.GeneralResponse[any]
  589. if err := json.Unmarshal(resBody, &res); err != nil {
  590. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  591. }
  592. if res.Code != 200 {
  593. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  594. }
  595. return nil
  596. }
  597. // 添加ssl策略
  598. func (s *cdnService) AddSSLPolicy(ctx context.Context, req v1.AddSSLPolicy) (int64, error) {
  599. formData := map[string]interface{}{
  600. "http2Enabled": req.Http2Enabled,
  601. "http3Enabled": req.Http3Enabled,
  602. "minVersion": req.MinVersion,
  603. "sslCertsJSON": req.SslCertsJSON,
  604. "hstsJSON": req.HstsJSON,
  605. "clientAuthType": req.ClientAuthType,
  606. "cipherSuites": req.CipherSuites,
  607. "cipherSuitesIsOn": req.CipherSuitesIsOn,
  608. "ocspIsOn": req.OcspIsOn,
  609. }
  610. apiUrl := s.Url + "SSLPolicyService/createSSLPolicy"
  611. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  612. if err != nil {
  613. return 0, err
  614. }
  615. type DataStr struct {
  616. SslPolicyId int64 `json:"sslPolicyId" form:"sslPolicyId"`
  617. }
  618. var res v1.GeneralResponse[DataStr]
  619. if err := json.Unmarshal(resBody, &res); err != nil {
  620. return 0, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  621. }
  622. if res.Code != 200 {
  623. return 0, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  624. }
  625. return res.Data.SslPolicyId, nil
  626. }
  627. func (s *cdnService) DelSSLCert(ctx context.Context, sslCertId int64) error {
  628. formData := map[string]interface{}{
  629. "sslCertId": sslCertId,
  630. }
  631. apiUrl := s.Url + "SSLCertService/deleteSSLCert"
  632. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  633. if err != nil {
  634. return err
  635. }
  636. var res v1.GeneralResponse[any]
  637. if err := json.Unmarshal(resBody, &res); err != nil {
  638. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  639. }
  640. if res.Code != 200 {
  641. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  642. }
  643. return nil
  644. }
  645. func (s *cdnService) GetSSLPolicy(ctx context.Context, sslPolicyId int64) (v1.SSLPolicy, error) {
  646. formData := map[string]interface{}{
  647. "sslPolicyId": sslPolicyId,
  648. "ignoreData": true,
  649. }
  650. apiUrl := s.Url + "SSLPolicyService/findEnabledSSLPolicyConfig"
  651. resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
  652. if err != nil {
  653. return v1.SSLPolicy{}, err
  654. }
  655. var res v1.GeneralResponse[v1.SSLPolicy]
  656. if err := json.Unmarshal(resBody, &res); err != nil {
  657. return v1.SSLPolicy{}, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  658. }
  659. if res.Code != 200 {
  660. return v1.SSLPolicy{}, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  661. }
  662. return res.Data, nil
  663. }