1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

http response in blackhole

This commit is contained in:
v2ray 2016-06-10 23:01:17 +02:00
parent e732ba62a6
commit 8b3875050d
6 changed files with 103 additions and 4 deletions

View File

@ -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)

View File

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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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"
}]
}
}
}

View File

@ -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()
}