config.go 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package blackhole
  2. import (
  3. "github.com/golang/protobuf/ptypes"
  4. "github.com/golang/protobuf/ptypes/any"
  5. "v2ray.com/core/common/buf"
  6. "v2ray.com/core/common/serial"
  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(buf.Writer)
  18. }
  19. func (v *NoneResponse) WriteTo(buf.Writer) {}
  20. func (v *NoneResponse) AsAny() *any.Any {
  21. r, _ := ptypes.MarshalAny(v)
  22. return r
  23. }
  24. func (v *HTTPResponse) WriteTo(writer buf.Writer) {
  25. b := buf.NewLocal(512)
  26. b.AppendSupplier(serial.WriteString(http403response))
  27. writer.Write(b)
  28. }
  29. func (v *HTTPResponse) AsAny() *any.Any {
  30. r, _ := ptypes.MarshalAny(v)
  31. return r
  32. }
  33. func (v *Config) GetInternalResponse() (ResponseConfig, error) {
  34. if v.GetResponse() == nil {
  35. return new(NoneResponse), nil
  36. }
  37. config, err := v.GetResponse().GetInstance()
  38. if err != nil {
  39. return nil, err
  40. }
  41. return config.(ResponseConfig), nil
  42. }