JustSong 68605800af
Some checks failed
Publish Docker image (amd64, English) / Push Docker image to multiple registries (push) Has been cancelled
Publish Docker image (amd64) / Push Docker image to multiple registries (push) Has been cancelled
feat: add subnet validation (#1275)
2024-04-05 10:18:42 +08:00

23 lines
491 B
Go

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", err.Error())
return false
}
return ipNet.Contains(net.ParseIP(ip))
}