transport.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package conf
  2. import (
  3. "v2ray.com/core/transport"
  4. "v2ray.com/core/transport/internet"
  5. )
  6. type TransportConfig struct {
  7. TCPConfig *TCPConfig `json:"tcpSettings"`
  8. KCPConfig *KCPConfig `json:"kcpSettings"`
  9. WSConfig *WebSocketConfig `json:"wsSettings"`
  10. }
  11. func (v *TransportConfig) Build() (*transport.Config, error) {
  12. config := new(transport.Config)
  13. if v.TCPConfig != nil {
  14. ts, err := v.TCPConfig.Build()
  15. if err != nil {
  16. return nil, newError("failed to build TCP config").Base(err).AtError()
  17. }
  18. config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
  19. Protocol: internet.TransportProtocol_TCP,
  20. Settings: ts,
  21. })
  22. }
  23. if v.KCPConfig != nil {
  24. ts, err := v.KCPConfig.Build()
  25. if err != nil {
  26. return nil, newError("failed to build mKCP config").Base(err).AtError()
  27. }
  28. config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
  29. Protocol: internet.TransportProtocol_MKCP,
  30. Settings: ts,
  31. })
  32. }
  33. if v.WSConfig != nil {
  34. ts, err := v.WSConfig.Build()
  35. if err != nil {
  36. return nil, newError("failed to build WebSocket config").Base(err)
  37. }
  38. config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
  39. Protocol: internet.TransportProtocol_WebSocket,
  40. Settings: ts,
  41. })
  42. }
  43. return config, nil
  44. }