configure.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package web
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. )
  7. type Configure interface {
  8. ConfigureWebsocket(ctx context.Context, webId int64) error
  9. ConfigureProxyProtocol(ctx context.Context, proxy bool, cdnWebId int64) error
  10. ConfigureCCProtection(ctx context.Context, ccConfig v1.CcConfigRequest, webId int64) error
  11. ConfigureWafFirewall(ctx context.Context, webId int64, groupId int) error
  12. }
  13. // ConfigureWebsocket 配置WebSocket
  14. func (s *aidedWebService) ConfigureWebsocket(ctx context.Context, webId int64) error {
  15. websocketId, err := s.websocket.AddWebsocket(ctx)
  16. if err != nil {
  17. return fmt.Errorf("添加WebSocket失败: %w", err)
  18. }
  19. if err := s.websocket.EnableOrDisable(ctx, webId, websocketId, true, false); err != nil {
  20. return fmt.Errorf("启用WebSocket失败: %w", err)
  21. }
  22. return nil
  23. }
  24. // ConfigureProxyProtocol 配置代理协议
  25. func (s *aidedWebService) ConfigureProxyProtocol(ctx context.Context, proxy bool, cdnWebId int64) error {
  26. if err := s.proxy.EditProxy(ctx, cdnWebId, v1.ProxyProtocolJSON{
  27. IsOn: proxy,
  28. Version: proxyProtocolVersion,
  29. }); err != nil {
  30. return fmt.Errorf("启用代理协议失败: %w", err)
  31. }
  32. return nil
  33. }
  34. // ConfigureCCProtection 配置CC防护
  35. func (s *aidedWebService) ConfigureCCProtection(ctx context.Context, ccConfig v1.CcConfigRequest, webId int64) error {
  36. if err := s.cc.EditCcConfig(ctx, webId, ccConfig); err != nil {
  37. return fmt.Errorf("配置CC防护失败: %w", err)
  38. }
  39. return nil
  40. }
  41. // ConfigureWafFirewall 配置WAF防火墙
  42. func (s *aidedWebService) ConfigureWafFirewall(ctx context.Context, webId int64, groupId int) error {
  43. if err := s.ccIpList.AddCcIpListPolicy(ctx, webId, int64(groupId)); err != nil {
  44. return fmt.Errorf("配置WAF防火墙失败: %w", err)
  45. }
  46. return nil
  47. }