v2ray.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. package conf
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "strings"
  7. "google.golang.org/protobuf/reflect/protoreflect"
  8. "google.golang.org/protobuf/reflect/protoregistry"
  9. core "github.com/v2fly/v2ray-core/v4"
  10. "github.com/v2fly/v2ray-core/v4/app/dispatcher"
  11. "github.com/v2fly/v2ray-core/v4/app/proxyman"
  12. "github.com/v2fly/v2ray-core/v4/app/stats"
  13. "github.com/v2fly/v2ray-core/v4/common/serial"
  14. )
  15. var (
  16. inboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
  17. "dokodemo-door": func() interface{} { return new(DokodemoConfig) },
  18. "http": func() interface{} { return new(HTTPServerConfig) },
  19. "shadowsocks": func() interface{} { return new(ShadowsocksServerConfig) },
  20. "socks": func() interface{} { return new(SocksServerConfig) },
  21. "vless": func() interface{} { return new(VLessInboundConfig) },
  22. "vmess": func() interface{} { return new(VMessInboundConfig) },
  23. "trojan": func() interface{} { return new(TrojanServerConfig) },
  24. "mtproto": func() interface{} { return new(MTProtoServerConfig) },
  25. }, "protocol", "settings")
  26. outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
  27. "blackhole": func() interface{} { return new(BlackholeConfig) },
  28. "freedom": func() interface{} { return new(FreedomConfig) },
  29. "http": func() interface{} { return new(HTTPClientConfig) },
  30. "shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
  31. "socks": func() interface{} { return new(SocksClientConfig) },
  32. "vless": func() interface{} { return new(VLessOutboundConfig) },
  33. "vmess": func() interface{} { return new(VMessOutboundConfig) },
  34. "trojan": func() interface{} { return new(TrojanClientConfig) },
  35. "mtproto": func() interface{} { return new(MTProtoClientConfig) },
  36. "dns": func() interface{} { return new(DNSOutboundConfig) },
  37. "loopback": func() interface{} { return new(LoopbackConfig) },
  38. }, "protocol", "settings")
  39. ctllog = log.New(os.Stderr, "v2ctl> ", 0)
  40. )
  41. func toProtocolList(s []string) ([]proxyman.KnownProtocols, error) {
  42. kp := make([]proxyman.KnownProtocols, 0, 8)
  43. for _, p := range s {
  44. switch strings.ToLower(p) {
  45. case "http":
  46. kp = append(kp, proxyman.KnownProtocols_HTTP)
  47. case "https", "tls", "ssl":
  48. kp = append(kp, proxyman.KnownProtocols_TLS)
  49. default:
  50. return nil, newError("Unknown protocol: ", p)
  51. }
  52. }
  53. return kp, nil
  54. }
  55. type SniffingConfig struct {
  56. Enabled bool `json:"enabled"`
  57. DestOverride *StringList `json:"destOverride"`
  58. MetadataOnly bool `json:"metadataOnly"`
  59. }
  60. // Build implements Buildable.
  61. func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) {
  62. var p []string
  63. if c.DestOverride != nil {
  64. for _, domainOverride := range *c.DestOverride {
  65. switch strings.ToLower(domainOverride) {
  66. case "http":
  67. p = append(p, "http")
  68. case "tls", "https", "ssl":
  69. p = append(p, "tls")
  70. case "fakedns":
  71. p = append(p, "fakedns")
  72. default:
  73. return nil, newError("unknown protocol: ", domainOverride)
  74. }
  75. }
  76. }
  77. return &proxyman.SniffingConfig{
  78. Enabled: c.Enabled,
  79. DestinationOverride: p,
  80. MetadataOnly: c.MetadataOnly,
  81. }, nil
  82. }
  83. type MuxConfig struct {
  84. Enabled bool `json:"enabled"`
  85. Concurrency int16 `json:"concurrency"`
  86. }
  87. // Build creates MultiplexingConfig, Concurrency < 0 completely disables mux.
  88. func (m *MuxConfig) Build() *proxyman.MultiplexingConfig {
  89. if m.Concurrency < 0 {
  90. return nil
  91. }
  92. var con uint32 = 8
  93. if m.Concurrency > 0 {
  94. con = uint32(m.Concurrency)
  95. }
  96. return &proxyman.MultiplexingConfig{
  97. Enabled: m.Enabled,
  98. Concurrency: con,
  99. }
  100. }
  101. type InboundDetourAllocationConfig struct {
  102. Strategy string `json:"strategy"`
  103. Concurrency *uint32 `json:"concurrency"`
  104. RefreshMin *uint32 `json:"refresh"`
  105. }
  106. // Build implements Buildable.
  107. func (c *InboundDetourAllocationConfig) Build() (*proxyman.AllocationStrategy, error) {
  108. config := new(proxyman.AllocationStrategy)
  109. switch strings.ToLower(c.Strategy) {
  110. case "always":
  111. config.Type = proxyman.AllocationStrategy_Always
  112. case "random":
  113. config.Type = proxyman.AllocationStrategy_Random
  114. case "external":
  115. config.Type = proxyman.AllocationStrategy_External
  116. default:
  117. return nil, newError("unknown allocation strategy: ", c.Strategy)
  118. }
  119. if c.Concurrency != nil {
  120. config.Concurrency = &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  121. Value: *c.Concurrency,
  122. }
  123. }
  124. if c.RefreshMin != nil {
  125. config.Refresh = &proxyman.AllocationStrategy_AllocationStrategyRefresh{
  126. Value: *c.RefreshMin,
  127. }
  128. }
  129. return config, nil
  130. }
  131. type InboundDetourConfig struct {
  132. Protocol string `json:"protocol"`
  133. PortRange *PortRange `json:"port"`
  134. ListenOn *Address `json:"listen"`
  135. Settings *json.RawMessage `json:"settings"`
  136. Tag string `json:"tag"`
  137. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  138. StreamSetting *StreamConfig `json:"streamSettings"`
  139. DomainOverride *StringList `json:"domainOverride"`
  140. SniffingConfig *SniffingConfig `json:"sniffing"`
  141. }
  142. // Build implements Buildable.
  143. func (c *InboundDetourConfig) Build() (*core.InboundHandlerConfig, error) {
  144. receiverSettings := &proxyman.ReceiverConfig{}
  145. if c.ListenOn == nil {
  146. // Listen on anyip, must set PortRange
  147. if c.PortRange == nil {
  148. return nil, newError("Listen on AnyIP but no Port(s) set in InboundDetour.")
  149. }
  150. receiverSettings.PortRange = c.PortRange.Build()
  151. } else {
  152. // Listen on specific IP or Unix Domain Socket
  153. receiverSettings.Listen = c.ListenOn.Build()
  154. listenDS := c.ListenOn.Family().IsDomain() && (c.ListenOn.Domain()[0] == '/' || c.ListenOn.Domain()[0] == '@')
  155. listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost")
  156. switch {
  157. case listenIP:
  158. // Listen on specific IP, must set PortRange
  159. if c.PortRange == nil {
  160. return nil, newError("Listen on specific ip without port in InboundDetour.")
  161. }
  162. // Listen on IP:Port
  163. receiverSettings.PortRange = c.PortRange.Build()
  164. case listenDS:
  165. if c.PortRange != nil {
  166. // Listen on Unix Domain Socket, PortRange should be nil
  167. receiverSettings.PortRange = nil
  168. }
  169. default:
  170. return nil, newError("unable to listen on domain address: ", c.ListenOn.Domain())
  171. }
  172. }
  173. if c.Allocation != nil {
  174. concurrency := -1
  175. if c.Allocation.Concurrency != nil && c.Allocation.Strategy == "random" {
  176. concurrency = int(*c.Allocation.Concurrency)
  177. }
  178. portRange := int(c.PortRange.To - c.PortRange.From + 1)
  179. if concurrency >= 0 && concurrency >= portRange {
  180. return nil, newError("not enough ports. concurrency = ", concurrency, " ports: ", c.PortRange.From, " - ", c.PortRange.To)
  181. }
  182. as, err := c.Allocation.Build()
  183. if err != nil {
  184. return nil, err
  185. }
  186. receiverSettings.AllocationStrategy = as
  187. }
  188. if c.StreamSetting != nil {
  189. ss, err := c.StreamSetting.Build()
  190. if err != nil {
  191. return nil, err
  192. }
  193. receiverSettings.StreamSettings = ss
  194. }
  195. if c.SniffingConfig != nil {
  196. s, err := c.SniffingConfig.Build()
  197. if err != nil {
  198. return nil, newError("failed to build sniffing config").Base(err)
  199. }
  200. receiverSettings.SniffingSettings = s
  201. }
  202. if c.DomainOverride != nil {
  203. kp, err := toProtocolList(*c.DomainOverride)
  204. if err != nil {
  205. return nil, newError("failed to parse inbound detour config").Base(err)
  206. }
  207. receiverSettings.DomainOverride = kp
  208. }
  209. settings := []byte("{}")
  210. if c.Settings != nil {
  211. settings = ([]byte)(*c.Settings)
  212. }
  213. rawConfig, err := inboundConfigLoader.LoadWithID(settings, c.Protocol)
  214. if err != nil {
  215. return nil, newError("failed to load inbound detour config.").Base(err)
  216. }
  217. if dokodemoConfig, ok := rawConfig.(*DokodemoConfig); ok {
  218. receiverSettings.ReceiveOriginalDestination = dokodemoConfig.Redirect
  219. }
  220. ts, err := rawConfig.(Buildable).Build()
  221. if err != nil {
  222. return nil, err
  223. }
  224. return &core.InboundHandlerConfig{
  225. Tag: c.Tag,
  226. ReceiverSettings: serial.ToTypedMessage(receiverSettings),
  227. ProxySettings: serial.ToTypedMessage(ts),
  228. }, nil
  229. }
  230. type OutboundDetourConfig struct {
  231. Protocol string `json:"protocol"`
  232. SendThrough *Address `json:"sendThrough"`
  233. Tag string `json:"tag"`
  234. Settings *json.RawMessage `json:"settings"`
  235. StreamSetting *StreamConfig `json:"streamSettings"`
  236. ProxySettings *ProxyConfig `json:"proxySettings"`
  237. MuxSettings *MuxConfig `json:"mux"`
  238. }
  239. // Build implements Buildable.
  240. func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) {
  241. senderSettings := &proxyman.SenderConfig{}
  242. if c.SendThrough != nil {
  243. address := c.SendThrough
  244. if address.Family().IsDomain() {
  245. return nil, newError("unable to send through: " + address.String())
  246. }
  247. senderSettings.Via = address.Build()
  248. }
  249. if c.StreamSetting != nil {
  250. ss, err := c.StreamSetting.Build()
  251. if err != nil {
  252. return nil, err
  253. }
  254. senderSettings.StreamSettings = ss
  255. }
  256. if c.ProxySettings != nil {
  257. ps, err := c.ProxySettings.Build()
  258. if err != nil {
  259. return nil, newError("invalid outbound detour proxy settings.").Base(err)
  260. }
  261. senderSettings.ProxySettings = ps
  262. }
  263. if c.MuxSettings != nil {
  264. senderSettings.MultiplexSettings = c.MuxSettings.Build()
  265. }
  266. settings := []byte("{}")
  267. if c.Settings != nil {
  268. settings = ([]byte)(*c.Settings)
  269. }
  270. rawConfig, err := outboundConfigLoader.LoadWithID(settings, c.Protocol)
  271. if err != nil {
  272. return nil, newError("failed to parse to outbound detour config.").Base(err)
  273. }
  274. ts, err := rawConfig.(Buildable).Build()
  275. if err != nil {
  276. return nil, err
  277. }
  278. return &core.OutboundHandlerConfig{
  279. SenderSettings: serial.ToTypedMessage(senderSettings),
  280. Tag: c.Tag,
  281. ProxySettings: serial.ToTypedMessage(ts),
  282. }, nil
  283. }
  284. type StatsConfig struct{}
  285. // Build implements Buildable.
  286. func (c *StatsConfig) Build() (*stats.Config, error) {
  287. return &stats.Config{}, nil
  288. }
  289. type Config struct {
  290. // Port of this Point server.
  291. // Deprecated: Port exists for historical compatibility
  292. // and should not be used.
  293. Port uint16 `json:"port"`
  294. // Deprecated: InboundConfig exists for historical compatibility
  295. // and should not be used.
  296. InboundConfig *InboundDetourConfig `json:"inbound"`
  297. // Deprecated: OutboundConfig exists for historical compatibility
  298. // and should not be used.
  299. OutboundConfig *OutboundDetourConfig `json:"outbound"`
  300. // Deprecated: InboundDetours exists for historical compatibility
  301. // and should not be used.
  302. InboundDetours []InboundDetourConfig `json:"inboundDetour"`
  303. // Deprecated: OutboundDetours exists for historical compatibility
  304. // and should not be used.
  305. OutboundDetours []OutboundDetourConfig `json:"outboundDetour"`
  306. LogConfig *LogConfig `json:"log"`
  307. RouterConfig *RouterConfig `json:"routing"`
  308. DNSConfig *DNSConfig `json:"dns"`
  309. InboundConfigs []InboundDetourConfig `json:"inbounds"`
  310. OutboundConfigs []OutboundDetourConfig `json:"outbounds"`
  311. Transport *TransportConfig `json:"transport"`
  312. Policy *PolicyConfig `json:"policy"`
  313. API *APIConfig `json:"api"`
  314. Stats *StatsConfig `json:"stats"`
  315. Reverse *ReverseConfig `json:"reverse"`
  316. FakeDNS *FakeDNSConfig `json:"fakeDns"`
  317. BrowserForwarder *BrowserForwarderConfig `json:"browserForwarder"`
  318. Services map[string]*json.RawMessage `json:"services"`
  319. }
  320. func (c *Config) findInboundTag(tag string) int {
  321. found := -1
  322. for idx, ib := range c.InboundConfigs {
  323. if ib.Tag == tag {
  324. found = idx
  325. break
  326. }
  327. }
  328. return found
  329. }
  330. func (c *Config) findOutboundTag(tag string) int {
  331. found := -1
  332. for idx, ob := range c.OutboundConfigs {
  333. if ob.Tag == tag {
  334. found = idx
  335. break
  336. }
  337. }
  338. return found
  339. }
  340. // Override method accepts another Config overrides the current attribute
  341. func (c *Config) Override(o *Config, fn string) {
  342. // only process the non-deprecated members
  343. if o.LogConfig != nil {
  344. c.LogConfig = o.LogConfig
  345. }
  346. if o.RouterConfig != nil {
  347. c.RouterConfig = o.RouterConfig
  348. }
  349. if o.DNSConfig != nil {
  350. c.DNSConfig = o.DNSConfig
  351. }
  352. if o.Transport != nil {
  353. c.Transport = o.Transport
  354. }
  355. if o.Policy != nil {
  356. c.Policy = o.Policy
  357. }
  358. if o.API != nil {
  359. c.API = o.API
  360. }
  361. if o.Stats != nil {
  362. c.Stats = o.Stats
  363. }
  364. if o.Reverse != nil {
  365. c.Reverse = o.Reverse
  366. }
  367. if o.FakeDNS != nil {
  368. c.FakeDNS = o.FakeDNS
  369. }
  370. // deprecated attrs... keep them for now
  371. if o.InboundConfig != nil {
  372. c.InboundConfig = o.InboundConfig
  373. }
  374. if o.OutboundConfig != nil {
  375. c.OutboundConfig = o.OutboundConfig
  376. }
  377. if o.InboundDetours != nil {
  378. c.InboundDetours = o.InboundDetours
  379. }
  380. if o.OutboundDetours != nil {
  381. c.OutboundDetours = o.OutboundDetours
  382. }
  383. // deprecated attrs
  384. // update the Inbound in slice if the only one in overide config has same tag
  385. if len(o.InboundConfigs) > 0 {
  386. if len(c.InboundConfigs) > 0 && len(o.InboundConfigs) == 1 {
  387. if idx := c.findInboundTag(o.InboundConfigs[0].Tag); idx > -1 {
  388. c.InboundConfigs[idx] = o.InboundConfigs[0]
  389. ctllog.Println("[", fn, "] updated inbound with tag: ", o.InboundConfigs[0].Tag)
  390. } else {
  391. c.InboundConfigs = append(c.InboundConfigs, o.InboundConfigs[0])
  392. ctllog.Println("[", fn, "] appended inbound with tag: ", o.InboundConfigs[0].Tag)
  393. }
  394. } else {
  395. c.InboundConfigs = o.InboundConfigs
  396. }
  397. }
  398. // update the Outbound in slice if the only one in overide config has same tag
  399. if len(o.OutboundConfigs) > 0 {
  400. if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 {
  401. if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 {
  402. c.OutboundConfigs[idx] = o.OutboundConfigs[0]
  403. ctllog.Println("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[0].Tag)
  404. } else {
  405. if strings.Contains(strings.ToLower(fn), "tail") {
  406. c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0])
  407. ctllog.Println("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[0].Tag)
  408. } else {
  409. c.OutboundConfigs = append(o.OutboundConfigs, c.OutboundConfigs...)
  410. ctllog.Println("[", fn, "] prepended outbound with tag: ", o.OutboundConfigs[0].Tag)
  411. }
  412. }
  413. } else {
  414. c.OutboundConfigs = o.OutboundConfigs
  415. }
  416. }
  417. }
  418. func applyTransportConfig(s *StreamConfig, t *TransportConfig) {
  419. if s.TCPSettings == nil {
  420. s.TCPSettings = t.TCPConfig
  421. }
  422. if s.KCPSettings == nil {
  423. s.KCPSettings = t.KCPConfig
  424. }
  425. if s.WSSettings == nil {
  426. s.WSSettings = t.WSConfig
  427. }
  428. if s.HTTPSettings == nil {
  429. s.HTTPSettings = t.HTTPConfig
  430. }
  431. if s.DSSettings == nil {
  432. s.DSSettings = t.DSConfig
  433. }
  434. }
  435. // Build implements Buildable.
  436. func (c *Config) Build() (*core.Config, error) {
  437. if err := PostProcessConfigureFile(c); err != nil {
  438. return nil, err
  439. }
  440. config := &core.Config{
  441. App: []*serial.TypedMessage{
  442. serial.ToTypedMessage(&dispatcher.Config{}),
  443. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  444. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  445. },
  446. }
  447. if c.API != nil {
  448. apiConf, err := c.API.Build()
  449. if err != nil {
  450. return nil, err
  451. }
  452. config.App = append(config.App, serial.ToTypedMessage(apiConf))
  453. }
  454. if c.Stats != nil {
  455. statsConf, err := c.Stats.Build()
  456. if err != nil {
  457. return nil, err
  458. }
  459. config.App = append(config.App, serial.ToTypedMessage(statsConf))
  460. }
  461. var logConfMsg *serial.TypedMessage
  462. if c.LogConfig != nil {
  463. logConfMsg = serial.ToTypedMessage(c.LogConfig.Build())
  464. } else {
  465. logConfMsg = serial.ToTypedMessage(DefaultLogConfig())
  466. }
  467. // let logger module be the first App to start,
  468. // so that other modules could print log during initiating
  469. config.App = append([]*serial.TypedMessage{logConfMsg}, config.App...)
  470. if c.RouterConfig != nil {
  471. routerConfig, err := c.RouterConfig.Build()
  472. if err != nil {
  473. return nil, err
  474. }
  475. config.App = append(config.App, serial.ToTypedMessage(routerConfig))
  476. }
  477. if c.DNSConfig != nil {
  478. dnsApp, err := c.DNSConfig.Build()
  479. if err != nil {
  480. return nil, newError("failed to parse DNS config").Base(err)
  481. }
  482. config.App = append(config.App, serial.ToTypedMessage(dnsApp))
  483. }
  484. if c.Policy != nil {
  485. pc, err := c.Policy.Build()
  486. if err != nil {
  487. return nil, err
  488. }
  489. config.App = append(config.App, serial.ToTypedMessage(pc))
  490. }
  491. if c.Reverse != nil {
  492. r, err := c.Reverse.Build()
  493. if err != nil {
  494. return nil, err
  495. }
  496. config.App = append(config.App, serial.ToTypedMessage(r))
  497. }
  498. if c.FakeDNS != nil {
  499. r, err := c.FakeDNS.Build()
  500. if err != nil {
  501. return nil, err
  502. }
  503. config.App = append(config.App, serial.ToTypedMessage(r))
  504. }
  505. if c.BrowserForwarder != nil {
  506. r, err := c.BrowserForwarder.Build()
  507. if err != nil {
  508. return nil, err
  509. }
  510. config.App = append(config.App, serial.ToTypedMessage(r))
  511. }
  512. //Load Additional Services that do not have a json translator
  513. if msg, err := c.BuildServices(c.Services); err != nil {
  514. developererr := newError("Loading a V2Ray Features as a service is intended for developers only. " +
  515. "This is used for developers to prototype new features or for an advanced client to use special features in V2Ray," +
  516. " instead of allowing end user to enable it without special tool and knowledge.")
  517. sb := strings.Builder{}
  518. protoregistry.GlobalTypes.RangeMessages(func(messageType protoreflect.MessageType) bool {
  519. switch d := messageType.Descriptor().(type) {
  520. case protoreflect.MessageDescriptor:
  521. sb.WriteString(string(d.FullName()))
  522. sb.WriteString("\n")
  523. }
  524. return true
  525. })
  526. return nil, newError("Cannot load service").Base(developererr).Base(err).Base(newError(sb.String()))
  527. } else {
  528. config.App = append(config.App, msg...)
  529. }
  530. var inbounds []InboundDetourConfig
  531. if c.InboundConfig != nil {
  532. inbounds = append(inbounds, *c.InboundConfig)
  533. }
  534. if len(c.InboundDetours) > 0 {
  535. inbounds = append(inbounds, c.InboundDetours...)
  536. }
  537. if len(c.InboundConfigs) > 0 {
  538. inbounds = append(inbounds, c.InboundConfigs...)
  539. }
  540. // Backward compatibility.
  541. if len(inbounds) > 0 && inbounds[0].PortRange == nil && c.Port > 0 {
  542. inbounds[0].PortRange = &PortRange{
  543. From: uint32(c.Port),
  544. To: uint32(c.Port),
  545. }
  546. }
  547. for _, rawInboundConfig := range inbounds {
  548. if c.Transport != nil {
  549. if rawInboundConfig.StreamSetting == nil {
  550. rawInboundConfig.StreamSetting = &StreamConfig{}
  551. }
  552. applyTransportConfig(rawInboundConfig.StreamSetting, c.Transport)
  553. }
  554. ic, err := rawInboundConfig.Build()
  555. if err != nil {
  556. return nil, err
  557. }
  558. config.Inbound = append(config.Inbound, ic)
  559. }
  560. var outbounds []OutboundDetourConfig
  561. if c.OutboundConfig != nil {
  562. outbounds = append(outbounds, *c.OutboundConfig)
  563. }
  564. if len(c.OutboundDetours) > 0 {
  565. outbounds = append(outbounds, c.OutboundDetours...)
  566. }
  567. if len(c.OutboundConfigs) > 0 {
  568. outbounds = append(outbounds, c.OutboundConfigs...)
  569. }
  570. for _, rawOutboundConfig := range outbounds {
  571. if c.Transport != nil {
  572. if rawOutboundConfig.StreamSetting == nil {
  573. rawOutboundConfig.StreamSetting = &StreamConfig{}
  574. }
  575. applyTransportConfig(rawOutboundConfig.StreamSetting, c.Transport)
  576. }
  577. oc, err := rawOutboundConfig.Build()
  578. if err != nil {
  579. return nil, err
  580. }
  581. config.Outbound = append(config.Outbound, oc)
  582. }
  583. return config, nil
  584. }