feat(tenant): 隔离邀请返利与推广追踪数据
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -29,12 +31,28 @@ var reservedCodes = map[string]bool{
|
||||
}
|
||||
|
||||
func validateTargetURL(rawURL string, allowLocal bool) (string, error) {
|
||||
return validateTargetURLWithHosts(rawURL, allowLocal, "hao858.com", "hao858.com")
|
||||
}
|
||||
|
||||
func validateTargetURLForTenant(ctx context.Context, rawURL string, allowLocal bool) (string, error) {
|
||||
defaultHost := "hao858.com"
|
||||
allowedHosts := []string{"hao858.com"}
|
||||
if scope, ok := tenancy.FromContext(ctx); ok {
|
||||
if host := strings.TrimSpace(strings.ToLower(scope.Host)); host != "" {
|
||||
defaultHost = host
|
||||
allowedHosts = append(allowedHosts, host)
|
||||
}
|
||||
}
|
||||
return validateTargetURLWithHosts(rawURL, allowLocal, defaultHost, allowedHosts...)
|
||||
}
|
||||
|
||||
func validateTargetURLWithHosts(rawURL string, allowLocal bool, defaultHost string, allowedHosts ...string) (string, error) {
|
||||
targetURL := strings.TrimSpace(rawURL)
|
||||
if targetURL == "" {
|
||||
return "https://hao858.com/", nil
|
||||
return "https://" + defaultHost + "/", nil
|
||||
}
|
||||
if strings.HasPrefix(targetURL, "/") {
|
||||
return "https://hao858.com" + targetURL, nil
|
||||
return "https://" + defaultHost + targetURL, nil
|
||||
}
|
||||
if !strings.HasPrefix(targetURL, "http://") && !strings.HasPrefix(targetURL, "https://") {
|
||||
targetURL = "https://" + targetURL
|
||||
@@ -54,7 +72,15 @@ func validateTargetURL(rawURL string, allowLocal bool) (string, error) {
|
||||
}
|
||||
return targetURL, nil
|
||||
}
|
||||
if host != "hao858.com" && !strings.HasSuffix(host, ".hao858.com") {
|
||||
allowed := false
|
||||
for _, candidate := range allowedHosts {
|
||||
candidate = strings.TrimSpace(strings.ToLower(candidate))
|
||||
if candidate != "" && (host == candidate || strings.HasSuffix(host, "."+candidate)) {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return "", ErrDisallowedTargetDomain
|
||||
}
|
||||
return targetURL, nil
|
||||
@@ -89,7 +115,7 @@ func (s *Service) CreateLink(ctx context.Context, adminID uint64, req CreateTrac
|
||||
return nil, ErrCodeAlreadyExists
|
||||
}
|
||||
|
||||
targetURL, err := validateTargetURL(req.TargetURL, s.allowLocalTargets)
|
||||
targetURL, err := validateTargetURLForTenant(ctx, req.TargetURL, s.allowLocalTargets)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -100,6 +126,7 @@ func (s *Service) CreateLink(ctx context.Context, adminID uint64, req CreateTrac
|
||||
}
|
||||
|
||||
link := &AdTrackingLink{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Code: code,
|
||||
ChannelType: channelType,
|
||||
@@ -125,7 +152,7 @@ func (s *Service) UpdateLink(ctx context.Context, id uint64, req UpdateTrackingL
|
||||
return ErrLinkNotFound
|
||||
}
|
||||
|
||||
targetURL, err := validateTargetURL(req.TargetURL, s.allowLocalTargets)
|
||||
targetURL, err := validateTargetURLForTenant(ctx, req.TargetURL, s.allowLocalTargets)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -202,6 +229,7 @@ func (s *Service) ResolveRedirect(ctx context.Context, code, ip, ua, referer, vi
|
||||
}
|
||||
|
||||
click := &AdTrackingClick{
|
||||
TenantID: link.TenantID,
|
||||
LinkID: link.ID,
|
||||
Code: link.Code,
|
||||
IP: ip,
|
||||
|
||||
Reference in New Issue
Block a user