cdn.go 33 KB

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