cdn.go 28 KB

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