فهرست منبع

http response in blackhole

v2ray 9 سال پیش
والد
کامیت
8b3875050d

+ 3 - 3
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)

+ 2 - 1
proxy/blackhole/config.go

@@ -25,7 +25,8 @@ const (
 Connection: close
 Cache-Control: max-age=3600, public
 Content-Length: 0
- 
+
+
 `
 )
 

+ 23 - 0
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
 }
 

+ 26 - 0
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)
+}

+ 19 - 0
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"
+      }]
+    }
   }
 }

+ 30 - 0
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()
+}