cdn.go 26 KB

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