config.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package blackhole
  2. import (
  3. "v2ray.com/core/common/buf"
  4. v2io "v2ray.com/core/common/io"
  5. "github.com/golang/protobuf/ptypes"
  6. "github.com/golang/protobuf/ptypes/any"
  7. "v2ray.com/core/common/serial"
  8. )
  9. const (
  10. http403response = `HTTP/1.1 403 Forbidden
  11. Connection: close
  12. Cache-Control: max-age=3600, public
  13. Content-Length: 0
  14. `
  15. )
  16. type ResponseConfig interface {
  17. AsAny() *any.Any
  18. WriteTo(v2io.Writer)
  19. }
  20. func (v *NoneResponse) WriteTo(v2io.Writer) {}
  21. func (v *NoneResponse) AsAny() *any.Any {
  22. r, _ := ptypes.MarshalAny(v)
  23. return r
  24. }
  25. func (v *HTTPResponse) WriteTo(writer v2io.Writer) {
  26. b := buf.NewLocalBuffer(512)
  27. b.AppendFunc(serial.WriteString(http403response))
  28. writer.Write(b)
  29. }
  30. func (v *HTTPResponse) AsAny() *any.Any {
  31. r, _ := ptypes.MarshalAny(v)
  32. return r
  33. }
  34. func (v *Config) GetInternalResponse() (ResponseConfig, error) {
  35. if v.GetResponse() == nil {
  36. return new(NoneResponse), nil
  37. }
  38. config, err := v.GetResponse().GetInstance()
  39. if err != nil {
  40. return nil, err
  41. }
  42. return config.(ResponseConfig), nil
  43. }