config.go 1012 B

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