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:
parent
e732ba62a6
commit
8b3875050d
@ -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)
|
||||
|
@ -25,7 +25,8 @@ const (
|
||||
Connection: close
|
||||
Cache-Control: max-age=3600, public
|
||||
Content-Length: 0
|
||||
|
||||
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
|
@ -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
proxy/blackhole/config_test.go
Normal file
26
proxy/blackhole/config_test.go
Normal 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)
|
||||
}
|
@ -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"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user