1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-01 03:24:14 +08:00

feat: API interface IP whitelist supports IPv6 (#7981)

Refs #7836
This commit is contained in:
2025-02-24 18:05:13 +08:00 committed by GitHub
parent b61dae638b
commit 6c8d48c2dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 14 deletions

View File

@ -7,10 +7,10 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net" "net"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -102,27 +102,38 @@ func isValid1PanelToken(panelToken string, panelTimestamp string) bool {
func isIPInWhiteList(clientIP string) bool { func isIPInWhiteList(clientIP string) bool {
ipWhiteString := global.CONF.System.IpWhiteList ipWhiteString := global.CONF.System.IpWhiteList
ipWhiteList := strings.Split(ipWhiteString, "\n") if len(ipWhiteString) == 0 {
global.LOG.Error("IP whitelist is empty")
return false
}
ipWhiteList, ipErr := common.HandleIPList(ipWhiteString)
if ipErr != nil {
global.LOG.Errorf("Failed to handle IP list: %v", ipErr)
return false
}
clientParsedIP := net.ParseIP(clientIP)
if clientParsedIP == nil {
return false
}
iPv4 := clientParsedIP.To4()
iPv6 := clientParsedIP.To16()
for _, cidr := range ipWhiteList { for _, cidr := range ipWhiteList {
if cidr == "0.0.0.0" { if (iPv4 != nil && (cidr == "0.0.0.0" || cidr == "0.0.0.0/0" || iPv4.String() == cidr)) || (iPv6 != nil && (cidr == "::/0" || iPv6.String() == cidr)) {
return true return true
} }
_, ipNet, err := net.ParseCIDR(cidr) _, ipNet, err := net.ParseCIDR(cidr)
if err != nil { if err != nil {
if cidr == clientIP {
return true
}
continue continue
} }
if ipNet.Contains(net.ParseIP(clientIP)) { if (iPv4 != nil && ipNet.Contains(iPv4)) || (iPv6 != nil && ipNet.Contains(iPv6)) {
return true return true
} }
} }
return false return false
} }
func GenerateMD5(input string) string { func GenerateMD5(param string) string {
hash := md5.New() hash := md5.New()
hash.Write([]byte(input)) hash.Write([]byte(param))
return hex.EncodeToString(hash.Sum(nil)) return hex.EncodeToString(hash.Sum(nil))
} }

View File

@ -358,7 +358,7 @@ export function checkCidrV6(value: string): boolean {
if (checkIpV6(value.split('/')[0])) { if (checkIpV6(value.split('/')[0])) {
return true; return true;
} }
const reg = /^(?:[1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])$/; const reg = /^(?:[0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])$/;
if (!reg.test(value.split('/')[1])) { if (!reg.test(value.split('/')[1])) {
return true; return true;
} }

View File

@ -98,7 +98,7 @@ import i18n from '@/lang';
import { MsgSuccess } from '@/utils/message'; import { MsgSuccess } from '@/utils/message';
import { ElMessageBox, FormInstance } from 'element-plus'; import { ElMessageBox, FormInstance } from 'element-plus';
import DrawerHeader from '@/components/drawer-header/index.vue'; import DrawerHeader from '@/components/drawer-header/index.vue';
import { checkCidr, checkIp } from '@/utils/util'; import { checkCidr, checkCidrV6, checkIpV4V6 } from '@/utils/util';
import { GlobalStore } from '@/store'; import { GlobalStore } from '@/store';
const globalStore = GlobalStore(); const globalStore = GlobalStore();
@ -138,10 +138,16 @@ function checkIPs(rule: any, value: any, callback: any) {
continue; continue;
} }
if (item.indexOf('/') !== -1) { if (item.indexOf('/') !== -1) {
if (checkCidr(item)) { if (item.indexOf(':') !== -1) {
return callback(new Error(i18n.global.t('firewall.addressFormatError'))); if (checkCidrV6(item)) {
return callback(new Error(i18n.global.t('firewall.addressFormatError')));
}
} else {
if (checkCidr(item)) {
return callback(new Error(i18n.global.t('firewall.addressFormatError')));
}
} }
} else if (checkIp(item)) { } else if (checkIpV4V6(item)) {
return callback(new Error(i18n.global.t('firewall.addressFormatError'))); return callback(new Error(i18n.global.t('firewall.addressFormatError')));
} }
} }