2015-12-06 12:21:15 -05:00
|
|
|
package blackhole
|
|
|
|
|
2016-06-10 16:26:39 -04:00
|
|
|
import (
|
2017-10-23 11:39:15 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 07:17:34 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-09-22 06:01:36 -04:00
|
|
|
)
|
2016-06-10 16:26:39 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
http403response = `HTTP/1.1 403 Forbidden
|
|
|
|
Connection: close
|
|
|
|
Cache-Control: max-age=3600, public
|
|
|
|
Content-Length: 0
|
2016-06-10 17:01:17 -04:00
|
|
|
|
|
|
|
|
2016-06-10 16:26:39 -04:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
// ResponseConfig is the configuration for blackhole responses.
|
2016-09-22 06:01:36 -04:00
|
|
|
type ResponseConfig interface {
|
2016-12-22 07:33:28 -05:00
|
|
|
// WriteTo writes predefined response to the give buffer.
|
2018-04-17 17:35:53 -04:00
|
|
|
WriteTo(buf.Writer) int32
|
2016-09-22 06:01:36 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
// WriteTo implements ResponseConfig.WriteTo().
|
2018-04-17 17:35:53 -04:00
|
|
|
func (*NoneResponse) WriteTo(buf.Writer) int32 { return 0 }
|
2016-09-22 06:01:36 -04:00
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
// WriteTo implements ResponseConfig.WriteTo().
|
2018-04-17 17:35:53 -04:00
|
|
|
func (*HTTPResponse) WriteTo(writer buf.Writer) int32 {
|
2018-03-09 05:26:00 -05:00
|
|
|
b := buf.New()
|
2018-11-14 16:55:20 -05:00
|
|
|
common.Must2(b.WriteString(http403response))
|
2018-04-17 17:35:53 -04:00
|
|
|
n := b.Len()
|
2017-11-09 16:33:15 -05:00
|
|
|
writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
|
2018-04-17 17:35:53 -04:00
|
|
|
return n
|
2015-12-06 12:21:15 -05:00
|
|
|
}
|
2016-09-22 06:01:36 -04:00
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
// GetInternalResponse converts response settings from proto to internal data structure.
|
2017-07-25 16:14:52 -04:00
|
|
|
func (c *Config) GetInternalResponse() (ResponseConfig, error) {
|
|
|
|
if c.GetResponse() == nil {
|
2016-09-22 06:01:36 -04:00
|
|
|
return new(NoneResponse), nil
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:14:52 -04:00
|
|
|
config, err := c.GetResponse().GetInstance()
|
2016-09-22 06:01:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-16 08:22:21 -04:00
|
|
|
return config.(ResponseConfig), nil
|
2016-09-22 06:01:36 -04:00
|
|
|
}
|