cdn.go 32 KB

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