mirror of
https://github.com/alibaba/higress.git
synced 2026-06-02 00:57:28 +08:00
optimization parseIP in xff (#1915)
This commit is contained in:
@@ -3,13 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/zmap/go-iptree/iptree"
|
"github.com/zmap/go-iptree/iptree"
|
||||||
"net"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -101,9 +101,6 @@ func getDownStreamIp(config RestrictionConfig) (net.IP, error) {
|
|||||||
|
|
||||||
if config.IPSourceType == HeaderSourceType {
|
if config.IPSourceType == HeaderSourceType {
|
||||||
s, err = proxywasm.GetHttpRequestHeader(config.IPHeaderName)
|
s, err = proxywasm.GetHttpRequestHeader(config.IPHeaderName)
|
||||||
if err == nil {
|
|
||||||
s = strings.Split(strings.Trim(s, " "), ",")[0]
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
var bs []byte
|
var bs []byte
|
||||||
bs, err = proxywasm.GetProperty([]string{"source", "address"})
|
bs, err = proxywasm.GetProperty([]string{"source", "address"})
|
||||||
@@ -112,7 +109,7 @@ func getDownStreamIp(config RestrictionConfig) (net.IP, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ip := parseIP(s)
|
ip := parseIP(s, config.IPSourceType == HeaderSourceType)
|
||||||
realIP := net.ParseIP(ip)
|
realIP := net.ParseIP(ip)
|
||||||
if realIP == nil {
|
if realIP == nil {
|
||||||
return nil, fmt.Errorf("invalid ip[%s]", ip)
|
return nil, fmt.Errorf("invalid ip[%s]", ip)
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/zmap/go-iptree/iptree"
|
"github.com/zmap/go-iptree/iptree"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// parseIPNets 解析Ip段配置
|
// parseIPNets 解析Ip段配置
|
||||||
@@ -24,7 +25,12 @@ func parseIPNets(array []gjson.Result) (*iptree.IPTree, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseIP 解析IP
|
// parseIP 解析IP
|
||||||
func parseIP(source string) string {
|
func parseIP(source string, fromHeader bool) string {
|
||||||
|
|
||||||
|
if fromHeader {
|
||||||
|
source = strings.Split(source, ",")[0]
|
||||||
|
}
|
||||||
|
source = strings.Trim(source, " ")
|
||||||
if strings.Contains(source, ".") {
|
if strings.Contains(source, ".") {
|
||||||
// parse ipv4
|
// parse ipv4
|
||||||
return strings.Split(source, ":")[0]
|
return strings.Split(source, ":")[0]
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_parseIPNets(t *testing.T) {
|
func Test_parseIPNets(t *testing.T) {
|
||||||
@@ -53,6 +54,7 @@ func Test_parseIPNets(t *testing.T) {
|
|||||||
func Test_parseIP(t *testing.T) {
|
func Test_parseIP(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
source string
|
source string
|
||||||
|
fromHeader bool
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -64,6 +66,7 @@ func Test_parseIP(t *testing.T) {
|
|||||||
name: "case 1",
|
name: "case 1",
|
||||||
args: args{
|
args: args{
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
want: "127.0.0.1",
|
want: "127.0.0.1",
|
||||||
},
|
},
|
||||||
@@ -71,6 +74,7 @@ func Test_parseIP(t *testing.T) {
|
|||||||
name: "case 2",
|
name: "case 2",
|
||||||
args: args{
|
args: args{
|
||||||
"127.0.0.1:12",
|
"127.0.0.1:12",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
want: "127.0.0.1",
|
want: "127.0.0.1",
|
||||||
},
|
},
|
||||||
@@ -78,6 +82,7 @@ func Test_parseIP(t *testing.T) {
|
|||||||
name: "case 3",
|
name: "case 3",
|
||||||
args: args{
|
args: args{
|
||||||
"fe80::14d5:8aff:fed9:2114",
|
"fe80::14d5:8aff:fed9:2114",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
want: "fe80::14d5:8aff:fed9:2114",
|
want: "fe80::14d5:8aff:fed9:2114",
|
||||||
},
|
},
|
||||||
@@ -85,6 +90,7 @@ func Test_parseIP(t *testing.T) {
|
|||||||
name: "case 4",
|
name: "case 4",
|
||||||
args: args{
|
args: args{
|
||||||
"[fe80::14d5:8aff:fed9:2114]:123",
|
"[fe80::14d5:8aff:fed9:2114]:123",
|
||||||
|
false,
|
||||||
},
|
},
|
||||||
want: "fe80::14d5:8aff:fed9:2114",
|
want: "fe80::14d5:8aff:fed9:2114",
|
||||||
},
|
},
|
||||||
@@ -92,13 +98,38 @@ func Test_parseIP(t *testing.T) {
|
|||||||
name: "case 5",
|
name: "case 5",
|
||||||
args: args{
|
args: args{
|
||||||
"127.0.0.1:12,[fe80::14d5:8aff:fed9:2114]:123",
|
"127.0.0.1:12,[fe80::14d5:8aff:fed9:2114]:123",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
want: "127.0.0.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "case 6",
|
||||||
|
args: args{
|
||||||
|
"127.0.0.1,[fe80::14d5:8aff:fed9:2114]:123",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
want: "127.0.0.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "case 7",
|
||||||
|
args: args{
|
||||||
|
"[fe80::14d5:8aff:fed9:2114]:123,127.0.0.1",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
want: "fe80::14d5:8aff:fed9:2114",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "case 8",
|
||||||
|
args: args{
|
||||||
|
"127.0.0.1 , [fe80::14d5:8aff:fed9:2114]:123",
|
||||||
|
true,
|
||||||
},
|
},
|
||||||
want: "127.0.0.1",
|
want: "127.0.0.1",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := parseIP(tt.args.source); got != tt.want {
|
if got := parseIP(tt.args.source, tt.args.fromHeader); got != tt.want {
|
||||||
t.Errorf("parseIP() = %v, want %v", got, tt.want)
|
t.Errorf("parseIP() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user