From 6b894c719dabaf2ac9e2448b3940e3e0cb8c1933 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 26 Jan 2016 10:28:09 +0000 Subject: [PATCH] own hosts in http config --- proxy/http/config.go | 14 ++++++++++++++ proxy/http/config_json.go | 28 +++++++++++++++++++++++++++- proxy/http/config_json_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 proxy/http/config_json_test.go diff --git a/proxy/http/config.go b/proxy/http/config.go index 7132bb21f..965ce395b 100644 --- a/proxy/http/config.go +++ b/proxy/http/config.go @@ -1,4 +1,18 @@ package http +import ( + v2net "github.com/v2ray/v2ray-core/common/net" +) + type Config struct { + OwnHosts []v2net.Address +} + +func (this *Config) IsOwnHost(host v2net.Address) bool { + for _, ownHost := range this.OwnHosts { + if ownHost.Equals(host) { + return true + } + } + return false } diff --git a/proxy/http/config_json.go b/proxy/http/config_json.go index b3d6ac64a..565fd720a 100644 --- a/proxy/http/config_json.go +++ b/proxy/http/config_json.go @@ -3,12 +3,38 @@ package http import ( + "encoding/json" + + v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy/internal/config" ) +func (this *Config) UnmarshalJSON(data []byte) error { + type JsonConfig struct { + Hosts []v2net.AddressJson `json:"ownHosts"` + } + jsonConfig := new(JsonConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { + return err + } + this.OwnHosts = make([]v2net.Address, len(jsonConfig.Hosts), len(jsonConfig.Hosts)+1) + for idx, host := range jsonConfig.Hosts { + this.OwnHosts[idx] = host.Address + } + + v2rayHost := v2net.DomainAddress("local.v2ray.com") + if !this.IsOwnHost(v2rayHost) { + this.OwnHosts = append(this.OwnHosts, v2rayHost) + } + + return nil +} + func init() { config.RegisterInboundConfig("http", func(data []byte) (interface{}, error) { - return new(Config), nil + rawConfig := new(Config) + err := json.Unmarshal(data, rawConfig) + return rawConfig, err }) } diff --git a/proxy/http/config_json_test.go b/proxy/http/config_json_test.go new file mode 100644 index 000000000..a34baf547 --- /dev/null +++ b/proxy/http/config_json_test.go @@ -0,0 +1,32 @@ +// +build json + +package http_test + +import ( + "encoding/json" + "testing" + + v2net "github.com/v2ray/v2ray-core/common/net" + . "github.com/v2ray/v2ray-core/proxy/http" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestOwnHosts(t *testing.T) { + v2testing.Current(t) + + rawJson := `{ + "ownHosts": [ + "127.0.0.1", + "google.com" + ] + }` + + config := new(Config) + err := json.Unmarshal([]byte(rawJson), config) + assert.Error(err).IsNil() + assert.Bool(config.IsOwnHost(v2net.IPAddress([]byte{127, 0, 0, 1}))).IsTrue() + assert.Bool(config.IsOwnHost(v2net.DomainAddress("google.com"))).IsTrue() + assert.Bool(config.IsOwnHost(v2net.DomainAddress("local.v2ray.com"))).IsTrue() + assert.Bool(config.IsOwnHost(v2net.DomainAddress("v2ray.com"))).IsFalse() +}