mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
move host from net/json to net
This commit is contained in:
parent
1f1f79b90a
commit
5ceac7a6e2
26
common/net/address_json.go
Normal file
26
common/net/address_json.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AddressJson struct {
|
||||||
|
Address Address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *AddressJson) UnmarshalJSON(data []byte) error {
|
||||||
|
var rawStr string
|
||||||
|
if err := json.Unmarshal(data, &rawStr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ip := net.ParseIP(rawStr)
|
||||||
|
if ip != nil {
|
||||||
|
this.Address = IPAddress(ip)
|
||||||
|
} else {
|
||||||
|
this.Address = DomainAddress(rawStr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
37
common/net/address_json_test.go
Normal file
37
common/net/address_json_test.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// +build json
|
||||||
|
|
||||||
|
package net_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/common/net"
|
||||||
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIPParsing(t *testing.T) {
|
||||||
|
v2testing.Current(t)
|
||||||
|
|
||||||
|
rawJson := "\"8.8.8.8\""
|
||||||
|
var address AddressJson
|
||||||
|
err := json.Unmarshal([]byte(rawJson), &address)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
assert.Bool(address.Address.IsIPv4()).IsTrue()
|
||||||
|
assert.Bool(address.Address.IsDomain()).IsFalse()
|
||||||
|
assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDomainParsing(t *testing.T) {
|
||||||
|
v2testing.Current(t)
|
||||||
|
|
||||||
|
rawJson := "\"v2ray.com\""
|
||||||
|
var address AddressJson
|
||||||
|
err := json.Unmarshal([]byte(rawJson), &address)
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
assert.Bool(address.Address.IsIPv4()).IsFalse()
|
||||||
|
assert.Bool(address.Address.IsDomain()).IsTrue()
|
||||||
|
assert.StringLiteral(address.Address.Domain()).Equals("v2ray.com")
|
||||||
|
}
|
@ -1,63 +0,0 @@
|
|||||||
package json
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Host struct {
|
|
||||||
domain string
|
|
||||||
ip net.IP
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIPHost(ip net.IP) *Host {
|
|
||||||
return &Host{
|
|
||||||
ip: ip,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDomainHost(domain string) *Host {
|
|
||||||
return &Host{
|
|
||||||
domain: domain,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) UnmarshalJSON(data []byte) error {
|
|
||||||
var rawStr string
|
|
||||||
if err := json.Unmarshal(data, &rawStr); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ip := net.ParseIP(rawStr)
|
|
||||||
if ip != nil {
|
|
||||||
this.ip = ip
|
|
||||||
} else {
|
|
||||||
this.domain = rawStr
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) IsIP() bool {
|
|
||||||
return this.ip != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) IsDomain() bool {
|
|
||||||
return !this.IsIP()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) IP() net.IP {
|
|
||||||
return this.ip
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) Domain() string {
|
|
||||||
return this.domain
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Host) Address() v2net.Address {
|
|
||||||
if this.IsIP() {
|
|
||||||
return v2net.IPAddress(this.IP())
|
|
||||||
} else {
|
|
||||||
return v2net.DomainAddress(this.Domain())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package json_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
. "github.com/v2ray/v2ray-core/common/net/json"
|
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIPParsing(t *testing.T) {
|
|
||||||
v2testing.Current(t)
|
|
||||||
|
|
||||||
rawJson := "\"8.8.8.8\""
|
|
||||||
host := &Host{}
|
|
||||||
err := json.Unmarshal([]byte(rawJson), host)
|
|
||||||
assert.Error(err).IsNil()
|
|
||||||
assert.Bool(host.IsIP()).IsTrue()
|
|
||||||
assert.Bool(host.IsDomain()).IsFalse()
|
|
||||||
assert.Bool(host.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDomainParsing(t *testing.T) {
|
|
||||||
v2testing.Current(t)
|
|
||||||
|
|
||||||
rawJson := "\"v2ray.com\""
|
|
||||||
host := &Host{}
|
|
||||||
err := json.Unmarshal([]byte(rawJson), host)
|
|
||||||
assert.Error(err).IsNil()
|
|
||||||
assert.Bool(host.IsIP()).IsFalse()
|
|
||||||
assert.Bool(host.IsDomain()).IsTrue()
|
|
||||||
assert.StringLiteral(host.Domain()).Equals("v2ray.com")
|
|
||||||
}
|
|
@ -14,7 +14,7 @@ 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 *v2netjson.Host `json:"address"`
|
Host *v2net.AddressJson `json:"address"`
|
||||||
PortValue v2net.Port `json:"port"`
|
PortValue v2net.Port `json:"port"`
|
||||||
NetworkList *v2netjson.NetworkList `json:"network"`
|
NetworkList *v2netjson.NetworkList `json:"network"`
|
||||||
TimeoutValue int `json:"timeout"`
|
TimeoutValue int `json:"timeout"`
|
||||||
@ -24,7 +24,7 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Config{
|
return &Config{
|
||||||
Address: rawConfig.Host.Address(),
|
Address: rawConfig.Host.Address,
|
||||||
Port: rawConfig.PortValue,
|
Port: rawConfig.PortValue,
|
||||||
Network: rawConfig.NetworkList,
|
Network: rawConfig.NetworkList,
|
||||||
Timeout: rawConfig.TimeoutValue,
|
Timeout: rawConfig.TimeoutValue,
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
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"
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||||
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
||||||
)
|
)
|
||||||
@ -26,10 +25,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SocksConfig struct {
|
type SocksConfig struct {
|
||||||
AuthMethod string `json:"auth"`
|
AuthMethod string `json:"auth"`
|
||||||
Accounts []*SocksAccount `json:"accounts"`
|
Accounts []*SocksAccount `json:"accounts"`
|
||||||
UDP bool `json:"udp"`
|
UDP bool `json:"udp"`
|
||||||
Host *v2netjson.Host `json:"ip"`
|
Host *v2net.AddressJson `json:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
rawConfig := new(SocksConfig)
|
rawConfig := new(SocksConfig)
|
||||||
@ -55,7 +54,7 @@ func init() {
|
|||||||
|
|
||||||
socksConfig.UDPEnabled = rawConfig.UDP
|
socksConfig.UDPEnabled = rawConfig.UDP
|
||||||
if rawConfig.Host != nil {
|
if rawConfig.Host != nil {
|
||||||
socksConfig.Address = rawConfig.Host.Address()
|
socksConfig.Address = rawConfig.Host.Address
|
||||||
} else {
|
} else {
|
||||||
socksConfig.Address = v2net.IPAddress([]byte{127, 0, 0, 1})
|
socksConfig.Address = v2net.IPAddress([]byte{127, 0, 0, 1})
|
||||||
}
|
}
|
||||||
|
@ -7,16 +7,15 @@ import (
|
|||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
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"
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *Receiver) UnmarshalJSON(data []byte) error {
|
func (this *Receiver) UnmarshalJSON(data []byte) error {
|
||||||
type RawConfigTarget struct {
|
type RawConfigTarget struct {
|
||||||
Address *v2netjson.Host `json:"address"`
|
Address *v2net.AddressJson `json:"address"`
|
||||||
Port v2net.Port `json:"port"`
|
Port v2net.Port `json:"port"`
|
||||||
Users []*vmess.User `json:"users"`
|
Users []*vmess.User `json:"users"`
|
||||||
}
|
}
|
||||||
var rawConfig RawConfigTarget
|
var rawConfig RawConfigTarget
|
||||||
if err := json.Unmarshal(data, &rawConfig); err != nil {
|
if err := json.Unmarshal(data, &rawConfig); err != nil {
|
||||||
@ -31,6 +30,6 @@ func (this *Receiver) UnmarshalJSON(data []byte) error {
|
|||||||
log.Error("VMess: Address is not set in VMess outbound config.")
|
log.Error("VMess: Address is not set in VMess outbound config.")
|
||||||
return internal.ErrorBadConfiguration
|
return internal.ErrorBadConfiguration
|
||||||
}
|
}
|
||||||
this.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port)
|
this.Destination = v2net.TCPDestination(rawConfig.Address.Address, rawConfig.Port)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user