From 8b3875050d9628533374d8537a889389eb574365 Mon Sep 17 00:00:00 2001 From: v2ray Date: Fri, 10 Jun 2016 23:01:17 +0200 Subject: [PATCH] http response in blackhole --- common/loader/json_conf.go | 6 ++--- proxy/blackhole/config.go | 3 ++- proxy/blackhole/config_json.go | 23 +++++++++++++++++ proxy/blackhole/config_test.go | 26 ++++++++++++++++++++ testing/scenarios/data/test_5_server.json | 19 ++++++++++++++ testing/scenarios/http_test.go | 30 +++++++++++++++++++++++ 6 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 proxy/blackhole/config_test.go diff --git a/common/loader/json_conf.go b/common/loader/json_conf.go index 1865ab610..a9f778366 100644 --- a/common/loader/json_conf.go +++ b/common/loader/json_conf.go @@ -35,8 +35,8 @@ func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, er } func (this *JSONConfigLoader) Load(raw []byte) (interface{}, error) { - obj := make(map[string]json.RawMessage) - if err := json.Unmarshal(raw, obj); err != nil { + var obj map[string]json.RawMessage + if err := json.Unmarshal(raw, &obj); err != nil { return nil, err } rawID, found := obj[this.idKey] @@ -45,7 +45,7 @@ func (this *JSONConfigLoader) Load(raw []byte) (interface{}, error) { return nil, ErrConfigIDKeyNotFound } var id string - if err := json.Unmarshal(rawID, id); err != nil { + if err := json.Unmarshal(rawID, &id); err != nil { return nil, err } rawConfig := json.RawMessage(raw) diff --git a/proxy/blackhole/config.go b/proxy/blackhole/config.go index e24529124..378bf64b5 100644 --- a/proxy/blackhole/config.go +++ b/proxy/blackhole/config.go @@ -25,7 +25,8 @@ const ( Connection: close Cache-Control: max-age=3600, public Content-Length: 0 - + + ` ) diff --git a/proxy/blackhole/config_json.go b/proxy/blackhole/config_json.go index adbe642f1..f12e2cb73 100644 --- a/proxy/blackhole/config_json.go +++ b/proxy/blackhole/config_json.go @@ -3,10 +3,33 @@ package blackhole import ( + "encoding/json" + + "github.com/v2ray/v2ray-core/common/loader" "github.com/v2ray/v2ray-core/proxy/internal" ) func (this *Config) UnmarshalJSON(data []byte) error { + type JSONConfig struct { + Response json.RawMessage `json:"response"` + } + jsonConfig := new(JSONConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { + return err + } + if jsonConfig.Response == nil { + this.Response = new(NoneResponse) + } else { + loader := loader.NewJSONConfigLoader("type", "") + loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) }) + loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) }) + response, err := loader.Load(jsonConfig.Response) + if err != nil { + return err + } + this.Response = response.(Response) + } + return nil } diff --git a/proxy/blackhole/config_test.go b/proxy/blackhole/config_test.go new file mode 100644 index 000000000..f0152679f --- /dev/null +++ b/proxy/blackhole/config_test.go @@ -0,0 +1,26 @@ +package blackhole_test + +import ( + "bufio" + "net/http" + "testing" + + "github.com/v2ray/v2ray-core/common/alloc" + v2io "github.com/v2ray/v2ray-core/common/io" + . "github.com/v2ray/v2ray-core/proxy/blackhole" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestHTTPResponse(t *testing.T) { + assert := assert.On(t) + + buffer := alloc.NewBuffer().Clear() + + httpResponse := new(HTTPResponse) + httpResponse.WriteTo(v2io.NewAdaptiveWriter(buffer)) + + reader := bufio.NewReader(buffer) + response, err := http.ReadResponse(reader, nil) + assert.Error(err).IsNil() + assert.Int(response.StatusCode).Equals(403) +} diff --git a/testing/scenarios/data/test_5_server.json b/testing/scenarios/data/test_5_server.json index 487303b88..7838fec3a 100644 --- a/testing/scenarios/data/test_5_server.json +++ b/testing/scenarios/data/test_5_server.json @@ -16,5 +16,24 @@ "outbound": { "protocol": "freedom", "settings": {} + }, + "outboundDetour": [{ + "protocol": "blackhole", + "settings": { + "response": { + "type": "http" + } + }, + "tag": "blocked" + }], + "routing": { + "strategy": "rules", + "settings": { + "rules": [{ + "type": "field", + "port": "50049", + "outboundTag": "blocked" + }] + } } } diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 0934e6f29..590c6db8b 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -44,3 +44,33 @@ func TestHttpProxy(t *testing.T) { CloseAllServers() } + +func TestBlockHTTP(t *testing.T) { + assert := assert.On(t) + + httpServer := &v2http.Server{ + Port: v2net.Port(50042), + PathHandler: make(map[string]http.HandlerFunc), + } + _, err := httpServer.Start() + assert.Error(err).IsNil() + defer httpServer.Close() + + assert.Error(InitializeServerSetOnce("test_5")).IsNil() + + transport := &http.Transport{ + Proxy: func(req *http.Request) (*url.URL, error) { + return url.Parse("http://127.0.0.1:50040/") + }, + } + + client := &http.Client{ + Transport: transport, + } + + resp, err := client.Get("http://127.0.0.1:50049/") + assert.Error(err).IsNil() + assert.Int(resp.StatusCode).Equals(403) + + CloseAllServers() +}