1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/proxy/blackhole/config.go

49 lines
1.1 KiB
Go
Raw Normal View History

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