cdn.go 33 KB

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