mirror of https://github.com/v2ray/v2ray-core
support domain as host in VMess outbound config.
parent
bc144248e2
commit
ed09b4ea6a
|
@ -0,0 +1,53 @@
|
||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"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
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
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")
|
||||||
|
}
|
|
@ -42,7 +42,7 @@ func TestDokodemoTCP(t *testing.T) {
|
||||||
InboundConfigValue: &mocks.ConnectionConfig{
|
InboundConfigValue: &mocks.ConnectionConfig{
|
||||||
ProtocolValue: "dokodemo-door",
|
ProtocolValue: "dokodemo-door",
|
||||||
SettingsValue: &json.DokodemoConfig{
|
SettingsValue: &json.DokodemoConfig{
|
||||||
Host: "127.0.0.1",
|
Host: v2netjson.NewIPHost(net.ParseIP("127.0.0.1")),
|
||||||
Port: port,
|
Port: port,
|
||||||
NetworkList: &networkList,
|
NetworkList: &networkList,
|
||||||
TimeoutValue: 0,
|
TimeoutValue: 0,
|
||||||
|
@ -104,7 +104,7 @@ func TestDokodemoUDP(t *testing.T) {
|
||||||
InboundConfigValue: &mocks.ConnectionConfig{
|
InboundConfigValue: &mocks.ConnectionConfig{
|
||||||
ProtocolValue: "dokodemo-door",
|
ProtocolValue: "dokodemo-door",
|
||||||
SettingsValue: &json.DokodemoConfig{
|
SettingsValue: &json.DokodemoConfig{
|
||||||
Host: "127.0.0.1",
|
Host: v2netjson.NewIPHost(net.ParseIP("127.0.0.1")),
|
||||||
Port: port,
|
Port: port,
|
||||||
NetworkList: &networkList,
|
NetworkList: &networkList,
|
||||||
TimeoutValue: 0,
|
TimeoutValue: 0,
|
||||||
|
|
|
@ -1,26 +1,23 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
|
||||||
|
|
||||||
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"
|
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DokodemoConfig struct {
|
type DokodemoConfig struct {
|
||||||
Host string `json:"address"`
|
Host *v2netjson.Host `json:"address"`
|
||||||
Port v2net.Port `json:"port"`
|
Port v2net.Port `json:"port"`
|
||||||
NetworkList *v2netjson.NetworkList `json:"network"`
|
NetworkList *v2netjson.NetworkList `json:"network"`
|
||||||
TimeoutValue int `json:"timeout"`
|
TimeoutValue int `json:"timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DokodemoConfig) Address() v2net.Address {
|
func (this *DokodemoConfig) Address() v2net.Address {
|
||||||
ip := net.ParseIP(this.Host)
|
if this.Host.IsIP() {
|
||||||
if ip != nil {
|
return v2net.IPAddress(this.Host.IP(), this.Port)
|
||||||
return v2net.IPAddress(ip, this.Port)
|
|
||||||
} else {
|
} else {
|
||||||
return v2net.DomainAddress(this.Host, this.Port)
|
return v2net.DomainAddress(this.Host.Domain(), this.Port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
|
||||||
|
|
||||||
"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"
|
||||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||||
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
|
@ -20,7 +20,7 @@ type ConfigTarget struct {
|
||||||
|
|
||||||
func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
|
func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
|
||||||
type RawConfigTarget struct {
|
type RawConfigTarget struct {
|
||||||
Address string `json:"address"`
|
Address *v2netjson.Host `json:"address"`
|
||||||
Port v2net.Port `json:"port"`
|
Port v2net.Port `json:"port"`
|
||||||
Users []*vmessjson.ConfigUser `json:"users"`
|
Users []*vmessjson.ConfigUser `json:"users"`
|
||||||
}
|
}
|
||||||
|
@ -33,12 +33,15 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
|
||||||
return proxyconfig.BadConfiguration
|
return proxyconfig.BadConfiguration
|
||||||
}
|
}
|
||||||
t.Users = rawConfig.Users
|
t.Users = rawConfig.Users
|
||||||
ip := net.ParseIP(rawConfig.Address)
|
if rawConfig.Address == nil {
|
||||||
if ip == nil {
|
log.Error("Address is not set in VMess outbound config.")
|
||||||
log.Error("Unable to parse IP: %s", rawConfig.Address)
|
|
||||||
return proxyconfig.BadConfiguration
|
return proxyconfig.BadConfiguration
|
||||||
}
|
}
|
||||||
t.Address = v2net.IPAddress(ip, rawConfig.Port)
|
if rawConfig.Address.IsIP() {
|
||||||
|
t.Address = v2net.IPAddress(rawConfig.Address.IP(), rawConfig.Port)
|
||||||
|
} else {
|
||||||
|
t.Address = v2net.DomainAddress(rawConfig.Address.Domain(), rawConfig.Port)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue