cdn.go 27 KB

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