mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 07:26:24 -05:00
move network to net
This commit is contained in:
parent
393a64820f
commit
a403859105
@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringList []string
|
type StringList []string
|
||||||
@ -83,7 +82,7 @@ type FieldRule struct {
|
|||||||
Domain []DomainMatcher
|
Domain []DomainMatcher
|
||||||
IP []*net.IPNet
|
IP []*net.IPNet
|
||||||
Port *v2net.PortRange
|
Port *v2net.PortRange
|
||||||
Network v2net.NetworkList
|
Network *v2net.NetworkList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
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 {
|
func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
||||||
type RawFieldRule struct {
|
type RawFieldRule struct {
|
||||||
Rule
|
Rule
|
||||||
Domain *StringList `json:"domain"`
|
Domain *StringList `json:"domain"`
|
||||||
IP *StringList `json:"ip"`
|
IP *StringList `json:"ip"`
|
||||||
Port *v2net.PortRange `json:"port"`
|
Port *v2net.PortRange `json:"port"`
|
||||||
Network *v2netjson.NetworkList `json:"network"`
|
Network *v2net.NetworkList `json:"network"`
|
||||||
}
|
}
|
||||||
rawFieldRule := RawFieldRule{}
|
rawFieldRule := RawFieldRule{}
|
||||||
err := json.Unmarshal(data, &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
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TCPNetwork = Network("tcp")
|
TCPNetwork = Network("tcp")
|
||||||
UDPNetwork = Network("udp")
|
UDPNetwork = Network("udp")
|
||||||
@ -7,6 +11,21 @@ const (
|
|||||||
|
|
||||||
type Network string
|
type Network string
|
||||||
|
|
||||||
type NetworkList interface {
|
type NetworkList []Network
|
||||||
HasNetwork(Network) bool
|
|
||||||
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
@ -14,8 +17,8 @@ func TestArrayNetworkList(t *testing.T) {
|
|||||||
var list NetworkList
|
var list NetworkList
|
||||||
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
|
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
|
||||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringNetworkList(t *testing.T) {
|
func TestStringNetworkList(t *testing.T) {
|
||||||
@ -24,6 +27,6 @@ func TestStringNetworkList(t *testing.T) {
|
|||||||
var list NetworkList
|
var list NetworkList
|
||||||
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
|
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
|
||||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
|
||||||
}
|
}
|
@ -7,6 +7,6 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Address v2net.Address
|
Address v2net.Address
|
||||||
Port v2net.Port
|
Port v2net.Port
|
||||||
Network v2net.NetworkList
|
Network *v2net.NetworkList
|
||||||
Timeout int
|
Timeout int
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
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"
|
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,10 +13,10 @@ func init() {
|
|||||||
config.RegisterInboundConnectionConfig("dokodemo-door",
|
config.RegisterInboundConnectionConfig("dokodemo-door",
|
||||||
func(data []byte) (interface{}, error) {
|
func(data []byte) (interface{}, error) {
|
||||||
type DokodemoConfig struct {
|
type DokodemoConfig struct {
|
||||||
Host *v2net.AddressJson `json:"address"`
|
Host *v2net.AddressJson `json:"address"`
|
||||||
PortValue v2net.Port `json:"port"`
|
PortValue v2net.Port `json:"port"`
|
||||||
NetworkList *v2netjson.NetworkList `json:"network"`
|
NetworkList *v2net.NetworkList `json:"network"`
|
||||||
TimeoutValue int `json:"timeout"`
|
TimeoutValue int `json:"timeout"`
|
||||||
}
|
}
|
||||||
rawConfig := new(DokodemoConfig)
|
rawConfig := new(DokodemoConfig)
|
||||||
if err := json.Unmarshal(data, rawConfig); err != nil {
|
if err := json.Unmarshal(data, rawConfig); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user