cdn.go 23 KB

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