|
@@ -43,6 +43,8 @@ type WafFormatterService interface {
|
|
|
ParseCert(ctx context.Context, httpsCert string, httpKey string) (serverName string, commonName []string, DNSNames []string, before int64, after int64, isSelfSigned bool, err error)
|
|
|
AddSSLPolicy(ctx context.Context, req v1.SSL) (sslPolicyId int64, sslCertId int64, err error)
|
|
|
EditSSL(ctx context.Context, req v1.SSL) error
|
|
|
+ // 验证端口重复
|
|
|
+ VerifyPort(ctx context.Context,protocol string, port string,hostId int64,domain string) error
|
|
|
}
|
|
|
|
|
|
func NewWafFormatterService(
|
|
@@ -638,3 +640,66 @@ func (s *wafFormatterService) EditSSL(ctx context.Context, req v1.SSL) error {
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// 验证端口重复
|
|
|
+func (s *wafFormatterService) VerifyPort(ctx context.Context,protocol string, port string,hostId int64,domain string) error {
|
|
|
+ errPortInUse := fmt.Errorf("端口 %s 已经被使用,无法添加", port)
|
|
|
+ switch protocol {
|
|
|
+ case "http", "https":
|
|
|
+ domains, err := s.webForwardingRep.GetDomainByHostIdPort(ctx, hostId, port)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ tcpCount, err := s.tcpforwardingRep.GetPortCount(ctx, hostId, port)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if tcpCount > 0 {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range domains {
|
|
|
+ if v == "" {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+ if net.ParseIP(v) != nil {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if net.ParseIP(domain) != nil || domain == "" {
|
|
|
+ if len(domains) > 0 {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+
|
|
|
+
|
|
|
+ case "tcp":
|
|
|
+ count, err := s.tcpforwardingRep.GetPortCount(ctx, hostId, port)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ webCount, err := s.webForwardingRep.GetDomainByHostIdPort(ctx, hostId, port)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if count + int64(len(webCount)) > 0 {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ case "udp":
|
|
|
+ count, err := s.udpForWardingRep.GetPortCount(ctx, hostId, port)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ return errPortInUse
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ default:
|
|
|
+ return fmt.Errorf("不支持的协议类型:%s", protocol)
|
|
|
+ }
|
|
|
+}
|