2015-12-06 17:21:15 +00:00
|
|
|
package blackhole
|
|
|
|
|
2016-06-10 20:26:39 +00:00
|
|
|
import (
|
2017-10-23 15:39:15 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 12:17:34 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-06 10:03:42 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-09-22 10:01:36 +00:00
|
|
|
)
|
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-12-22 12:33:28 +00:00
|
|
|
// ResponseConfig is the configuration for blackhole responses.
|
2016-09-22 10:01:36 +00:00
|
|
|
type ResponseConfig interface {
|
2016-12-22 12:33:28 +00:00
|
|
|
// WriteTo writes predefined response to the give buffer.
|
2016-12-09 12:17:34 +00:00
|
|
|
WriteTo(buf.Writer)
|
2016-09-22 10:01:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 12:33:28 +00:00
|
|
|
// WriteTo implements ResponseConfig.WriteTo().
|
2017-07-25 20:14:52 +00:00
|
|
|
func (*NoneResponse) WriteTo(buf.Writer) {}
|
2016-09-22 10:01:36 +00:00
|
|
|
|
2016-12-22 12:33:28 +00:00
|
|
|
// WriteTo implements ResponseConfig.WriteTo().
|
2017-07-25 20:14:52 +00:00
|
|
|
func (*HTTPResponse) WriteTo(writer buf.Writer) {
|
2016-12-09 11:08:25 +00:00
|
|
|
b := buf.NewLocal(512)
|
2017-10-23 15:39:15 +00:00
|
|
|
common.Must(b.AppendSupplier(serial.WriteString(http403response)))
|
2017-11-09 21:33:15 +00:00
|
|
|
writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
|
2015-12-06 17:21:15 +00:00
|
|
|
}
|
2016-09-22 10:01:36 +00:00
|
|
|
|
2016-12-22 12:33:28 +00:00
|
|
|
// GetInternalResponse converts response settings from proto to internal data structure.
|
2017-07-25 20:14:52 +00:00
|
|
|
func (c *Config) GetInternalResponse() (ResponseConfig, error) {
|
|
|
|
if c.GetResponse() == nil {
|
2016-09-22 10:01:36 +00:00
|
|
|
return new(NoneResponse), nil
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:14:52 +00:00
|
|
|
config, err := c.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
|
|
|
}
|