memory_settings.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package internet
  2. import "github.com/xtls/xray-core/common/net"
  3. // MemoryStreamConfig is a parsed form of StreamConfig. It is used to reduce the number of Protobuf parses.
  4. type MemoryStreamConfig struct {
  5. Destination *net.Destination
  6. ProtocolName string
  7. ProtocolSettings interface{}
  8. SecurityType string
  9. SecuritySettings interface{}
  10. SocketSettings *SocketConfig
  11. DownloadSettings *MemoryStreamConfig
  12. }
  13. // ToMemoryStreamConfig converts a StreamConfig to MemoryStreamConfig. It returns a default non-nil MemoryStreamConfig for nil input.
  14. func ToMemoryStreamConfig(s *StreamConfig) (*MemoryStreamConfig, error) {
  15. ets, err := s.GetEffectiveTransportSettings()
  16. if err != nil {
  17. return nil, err
  18. }
  19. mss := &MemoryStreamConfig{
  20. ProtocolName: s.GetEffectiveProtocol(),
  21. ProtocolSettings: ets,
  22. }
  23. if s != nil {
  24. if s.Address != nil {
  25. mss.Destination = &net.Destination{
  26. Address: s.Address.AsAddress(),
  27. Port: net.Port(s.Port),
  28. Network: net.Network_TCP,
  29. }
  30. }
  31. mss.SocketSettings = s.SocketSettings
  32. }
  33. if s != nil && s.HasSecuritySettings() {
  34. ess, err := s.GetEffectiveSecuritySettings()
  35. if err != nil {
  36. return nil, err
  37. }
  38. mss.SecurityType = s.SecurityType
  39. mss.SecuritySettings = ess
  40. }
  41. return mss, nil
  42. }