mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
move network to net
This commit is contained in:
parent
393a64820f
commit
a403859105
@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||
)
|
||||
|
||||
type StringList []string
|
||||
@ -83,7 +82,7 @@ type FieldRule struct {
|
||||
Domain []DomainMatcher
|
||||
IP []*net.IPNet
|
||||
Port *v2net.PortRange
|
||||
Network v2net.NetworkList
|
||||
Network *v2net.NetworkList
|
||||
}
|
||||
|
||||
func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
||||
@ -139,10 +138,10 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
||||
func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
||||
type RawFieldRule struct {
|
||||
Rule
|
||||
Domain *StringList `json:"domain"`
|
||||
IP *StringList `json:"ip"`
|
||||
Port *v2net.PortRange `json:"port"`
|
||||
Network *v2netjson.NetworkList `json:"network"`
|
||||
Domain *StringList `json:"domain"`
|
||||
IP *StringList `json:"ip"`
|
||||
Port *v2net.PortRange `json:"port"`
|
||||
Network *v2net.NetworkList `json:"network"`
|
||||
}
|
||||
rawFieldRule := RawFieldRule{}
|
||||
err := json.Unmarshal(data, &rawFieldRule)
|
||||
|
@ -1,36 +0,0 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
serialjson "github.com/v2ray/v2ray-core/common/serial/json"
|
||||
)
|
||||
|
||||
type NetworkList []string
|
||||
|
||||
func NewNetworkList(networks []string) NetworkList {
|
||||
list := NetworkList(make([]string, len(networks)))
|
||||
for idx, network := range networks {
|
||||
list[idx] = strings.ToLower(strings.TrimSpace(network))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
strlist, err := serialjson.UnmarshalStringList(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*this = NewNetworkList(strlist)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *NetworkList) HasNetwork(network v2net.Network) bool {
|
||||
for _, value := range *this {
|
||||
if value == string(network) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
TCPNetwork = Network("tcp")
|
||||
UDPNetwork = Network("udp")
|
||||
@ -7,6 +11,21 @@ const (
|
||||
|
||||
type Network string
|
||||
|
||||
type NetworkList interface {
|
||||
HasNetwork(Network) bool
|
||||
type NetworkList []Network
|
||||
|
||||
func NewNetworkList(networks []string) NetworkList {
|
||||
list := NetworkList(make([]Network, len(networks)))
|
||||
for idx, network := range networks {
|
||||
list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *NetworkList) HasNetwork(network Network) bool {
|
||||
for _, value := range *this {
|
||||
if string(value) == string(network) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
16
common/net/network_json.go
Normal file
16
common/net/network_json.go
Normal file
@ -0,0 +1,16 @@
|
||||
// +build json
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
serialjson "github.com/v2ray/v2ray-core/common/serial/json"
|
||||
)
|
||||
|
||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
strlist, err := serialjson.UnmarshalStringList(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*this = NewNetworkList(strlist)
|
||||
return nil
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package json
|
||||
// +build json
|
||||
|
||||
package net_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
. "github.com/v2ray/v2ray-core/common/net"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@ -14,8 +17,8 @@ func TestArrayNetworkList(t *testing.T) {
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
||||
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
|
||||
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
|
||||
}
|
||||
|
||||
func TestStringNetworkList(t *testing.T) {
|
||||
@ -24,6 +27,6 @@ func TestStringNetworkList(t *testing.T) {
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
||||
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
|
||||
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
|
||||
}
|
@ -7,6 +7,6 @@ import (
|
||||
type Config struct {
|
||||
Address v2net.Address
|
||||
Port v2net.Port
|
||||
Network v2net.NetworkList
|
||||
Network *v2net.NetworkList
|
||||
Timeout int
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
||||
)
|
||||
|
||||
@ -14,10 +13,10 @@ func init() {
|
||||
config.RegisterInboundConnectionConfig("dokodemo-door",
|
||||
func(data []byte) (interface{}, error) {
|
||||
type DokodemoConfig struct {
|
||||
Host *v2net.AddressJson `json:"address"`
|
||||
PortValue v2net.Port `json:"port"`
|
||||
NetworkList *v2netjson.NetworkList `json:"network"`
|
||||
TimeoutValue int `json:"timeout"`
|
||||
Host *v2net.AddressJson `json:"address"`
|
||||
PortValue v2net.Port `json:"port"`
|
||||
NetworkList *v2net.NetworkList `json:"network"`
|
||||
TimeoutValue int `json:"timeout"`
|
||||
}
|
||||
rawConfig := new(DokodemoConfig)
|
||||
if err := json.Unmarshal(data, rawConfig); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user