mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 07:43:41 +08:00 
			
		
		
		
	feat: add subnet validation (#1275)
This commit is contained in:
		@@ -2,14 +2,20 @@ package network
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/logger"
 | 
			
		||||
	"net"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func IsValidSubnet(subnet string) error {
 | 
			
		||||
	_, _, err := net.ParseCIDR(subnet)
 | 
			
		||||
	return fmt.Errorf("failed to parse subnet: %w", err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IsIpInSubnet(ctx context.Context, ip string, subnet string) bool {
 | 
			
		||||
	_, ipNet, err := net.ParseCIDR(subnet)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Errorf(ctx, "failed to parse subnet: %s, subnet: %s", err.Error(), subnet)
 | 
			
		||||
		logger.Errorf(ctx, "failed to parse subnet: %s", err.Error())
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	return ipNet.Contains(net.ParseIP(ip))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user