v2ray-core/proxy/blackhole/config.go

53 lines
985 B
Go
Raw Normal View History

2015-12-06 17:21:15 +00:00
package blackhole
2016-06-10 20:26:39 +00:00
import (
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
2016-06-10 20:26:39 +00:00
2016-09-22 10:01:36 +00:00
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
)
2016-06-10 20:26:39 +00:00
const (
http403response = `HTTP/1.1 403 Forbidden
Connection: close
Cache-Control: max-age=3600, public
Content-Length: 0
2016-06-10 21:01:17 +00:00
2016-06-10 20:26:39 +00:00
`
)
2016-09-22 10:01:36 +00:00
type ResponseConfig interface {
AsAny() *any.Any
WriteTo(v2io.Writer)
}
2016-11-27 20:39:09 +00:00
func (v *NoneResponse) WriteTo(v2io.Writer) {}
2016-09-22 10:01:36 +00:00
2016-11-27 20:39:09 +00:00
func (v *NoneResponse) AsAny() *any.Any {
r, _ := ptypes.MarshalAny(v)
2016-09-22 10:01:36 +00:00
return r
}
2016-11-27 20:39:09 +00:00
func (v *HTTPResponse) WriteTo(writer v2io.Writer) {
writer.Write(alloc.NewLocalBuffer(512).Clear().AppendString(http403response))
2015-12-06 17:21:15 +00:00
}
2016-09-22 10:01:36 +00:00
2016-11-27 20:39:09 +00:00
func (v *HTTPResponse) AsAny() *any.Any {
r, _ := ptypes.MarshalAny(v)
2016-09-22 10:01:36 +00:00
return r
}
2016-11-27 20:39:09 +00:00
func (v *Config) GetInternalResponse() (ResponseConfig, error) {
if v.GetResponse() == nil {
2016-09-22 10:01:36 +00:00
return new(NoneResponse), nil
}
2016-11-27 20:39:09 +00:00
config, err := v.GetResponse().GetInstance()
2016-09-22 10:01:36 +00:00
if err != nil {
return nil, err
}
2016-10-16 12:22:21 +00:00
return config.(ResponseConfig), nil
2016-09-22 10:01:36 +00:00
}