xray_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/xtls/xray-core/app/dispatcher"
  8. "github.com/xtls/xray-core/app/log"
  9. "github.com/xtls/xray-core/app/proxyman"
  10. "github.com/xtls/xray-core/app/router"
  11. "github.com/xtls/xray-core/common"
  12. clog "github.com/xtls/xray-core/common/log"
  13. "github.com/xtls/xray-core/common/net"
  14. "github.com/xtls/xray-core/common/protocol"
  15. "github.com/xtls/xray-core/common/serial"
  16. core "github.com/xtls/xray-core/core"
  17. . "github.com/xtls/xray-core/infra/conf"
  18. "github.com/xtls/xray-core/proxy/blackhole"
  19. dns_proxy "github.com/xtls/xray-core/proxy/dns"
  20. "github.com/xtls/xray-core/proxy/freedom"
  21. "github.com/xtls/xray-core/proxy/vmess"
  22. "github.com/xtls/xray-core/proxy/vmess/inbound"
  23. "github.com/xtls/xray-core/transport/internet"
  24. "github.com/xtls/xray-core/transport/internet/http"
  25. "github.com/xtls/xray-core/transport/internet/tls"
  26. "github.com/xtls/xray-core/transport/internet/websocket"
  27. "google.golang.org/protobuf/proto"
  28. )
  29. func TestXrayConfig(t *testing.T) {
  30. createParser := func() func(string) (proto.Message, error) {
  31. return func(s string) (proto.Message, error) {
  32. config := new(Config)
  33. if err := json.Unmarshal([]byte(s), config); err != nil {
  34. return nil, err
  35. }
  36. return config.Build()
  37. }
  38. }
  39. runMultiTestCase(t, []TestCase{
  40. {
  41. Input: `{
  42. "outbound": {
  43. "protocol": "freedom",
  44. "settings": {}
  45. },
  46. "log": {
  47. "access": "/var/log/xray/access.log",
  48. "loglevel": "error",
  49. "error": "/var/log/xray/error.log"
  50. },
  51. "inbound": {
  52. "streamSettings": {
  53. "network": "ws",
  54. "wsSettings": {
  55. "headers": {
  56. "host": "example.domain"
  57. },
  58. "path": ""
  59. },
  60. "tlsSettings": {
  61. "alpn": "h2"
  62. },
  63. "security": "tls"
  64. },
  65. "protocol": "vmess",
  66. "port": 443,
  67. "settings": {
  68. "clients": [
  69. {
  70. "security": "aes-128-gcm",
  71. "id": "0cdf8a45-303d-4fed-9780-29aa7f54175e"
  72. }
  73. ]
  74. }
  75. },
  76. "inbounds": [{
  77. "streamSettings": {
  78. "network": "ws",
  79. "wsSettings": {
  80. "headers": {
  81. "host": "example.domain"
  82. },
  83. "path": ""
  84. },
  85. "tlsSettings": {
  86. "alpn": "h2"
  87. },
  88. "security": "tls"
  89. },
  90. "protocol": "vmess",
  91. "port": "443-500",
  92. "allocate": {
  93. "strategy": "random",
  94. "concurrency": 3
  95. },
  96. "settings": {
  97. "clients": [
  98. {
  99. "security": "aes-128-gcm",
  100. "id": "0cdf8a45-303d-4fed-9780-29aa7f54175e"
  101. }
  102. ]
  103. }
  104. }],
  105. "outboundDetour": [
  106. {
  107. "tag": "blocked",
  108. "protocol": "blackhole"
  109. },
  110. {
  111. "protocol": "dns"
  112. }
  113. ],
  114. "routing": {
  115. "strategy": "rules",
  116. "settings": {
  117. "rules": [
  118. {
  119. "ip": [
  120. "10.0.0.0/8"
  121. ],
  122. "type": "field",
  123. "outboundTag": "blocked"
  124. }
  125. ]
  126. }
  127. },
  128. "transport": {
  129. "httpSettings": {
  130. "path": "/test"
  131. }
  132. }
  133. }`,
  134. Parser: createParser(),
  135. Output: &core.Config{
  136. App: []*serial.TypedMessage{
  137. serial.ToTypedMessage(&log.Config{
  138. ErrorLogType: log.LogType_File,
  139. ErrorLogPath: "/var/log/xray/error.log",
  140. ErrorLogLevel: clog.Severity_Error,
  141. AccessLogType: log.LogType_File,
  142. AccessLogPath: "/var/log/xray/access.log",
  143. }),
  144. serial.ToTypedMessage(&dispatcher.Config{}),
  145. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  146. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  147. serial.ToTypedMessage(&router.Config{
  148. DomainStrategy: router.Config_AsIs,
  149. Rule: []*router.RoutingRule{
  150. {
  151. Geoip: []*router.GeoIP{
  152. {
  153. Cidr: []*router.CIDR{
  154. {
  155. Ip: []byte{10, 0, 0, 0},
  156. Prefix: 8,
  157. },
  158. },
  159. },
  160. },
  161. TargetTag: &router.RoutingRule_Tag{
  162. Tag: "blocked",
  163. },
  164. },
  165. },
  166. }),
  167. },
  168. Outbound: []*core.OutboundHandlerConfig{
  169. {
  170. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  171. StreamSettings: &internet.StreamConfig{
  172. ProtocolName: "tcp",
  173. TransportSettings: []*internet.TransportConfig{
  174. {
  175. ProtocolName: "http",
  176. Settings: serial.ToTypedMessage(&http.Config{
  177. Path: "/test",
  178. }),
  179. },
  180. },
  181. },
  182. }),
  183. ProxySettings: serial.ToTypedMessage(&freedom.Config{
  184. DomainStrategy: freedom.Config_AS_IS,
  185. UserLevel: 0,
  186. }),
  187. },
  188. {
  189. Tag: "blocked",
  190. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  191. StreamSettings: &internet.StreamConfig{
  192. ProtocolName: "tcp",
  193. TransportSettings: []*internet.TransportConfig{
  194. {
  195. ProtocolName: "http",
  196. Settings: serial.ToTypedMessage(&http.Config{
  197. Path: "/test",
  198. }),
  199. },
  200. },
  201. },
  202. }),
  203. ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
  204. },
  205. {
  206. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  207. StreamSettings: &internet.StreamConfig{
  208. ProtocolName: "tcp",
  209. TransportSettings: []*internet.TransportConfig{
  210. {
  211. ProtocolName: "http",
  212. Settings: serial.ToTypedMessage(&http.Config{
  213. Path: "/test",
  214. }),
  215. },
  216. },
  217. },
  218. }),
  219. ProxySettings: serial.ToTypedMessage(&dns_proxy.Config{
  220. Server: &net.Endpoint{},
  221. Non_IPQuery: "drop",
  222. }),
  223. },
  224. },
  225. Inbound: []*core.InboundHandlerConfig{
  226. {
  227. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  228. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(443)}},
  229. StreamSettings: &internet.StreamConfig{
  230. ProtocolName: "websocket",
  231. TransportSettings: []*internet.TransportConfig{
  232. {
  233. ProtocolName: "websocket",
  234. Settings: serial.ToTypedMessage(&websocket.Config{
  235. Host: "example.domain",
  236. Header: map[string]string{
  237. "host": "example.domain",
  238. },
  239. }),
  240. },
  241. {
  242. ProtocolName: "http",
  243. Settings: serial.ToTypedMessage(&http.Config{
  244. Path: "/test",
  245. }),
  246. },
  247. },
  248. SecurityType: "xray.transport.internet.tls.Config",
  249. SecuritySettings: []*serial.TypedMessage{
  250. serial.ToTypedMessage(&tls.Config{
  251. NextProtocol: []string{"h2"},
  252. }),
  253. },
  254. },
  255. }),
  256. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  257. User: []*protocol.User{
  258. {
  259. Level: 0,
  260. Account: serial.ToTypedMessage(&vmess.Account{
  261. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  262. SecuritySettings: &protocol.SecurityConfig{
  263. Type: protocol.SecurityType_AES128_GCM,
  264. },
  265. }),
  266. },
  267. },
  268. }),
  269. },
  270. {
  271. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  272. PortList: &net.PortList{Range: []*net.PortRange{{
  273. From: 443,
  274. To: 500,
  275. }}},
  276. AllocationStrategy: &proxyman.AllocationStrategy{
  277. Type: proxyman.AllocationStrategy_Random,
  278. Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  279. Value: 3,
  280. },
  281. },
  282. StreamSettings: &internet.StreamConfig{
  283. ProtocolName: "websocket",
  284. TransportSettings: []*internet.TransportConfig{
  285. {
  286. ProtocolName: "websocket",
  287. Settings: serial.ToTypedMessage(&websocket.Config{
  288. Host: "example.domain",
  289. Header: map[string]string{
  290. "host": "example.domain",
  291. },
  292. }),
  293. },
  294. {
  295. ProtocolName: "http",
  296. Settings: serial.ToTypedMessage(&http.Config{
  297. Path: "/test",
  298. }),
  299. },
  300. },
  301. SecurityType: "xray.transport.internet.tls.Config",
  302. SecuritySettings: []*serial.TypedMessage{
  303. serial.ToTypedMessage(&tls.Config{
  304. NextProtocol: []string{"h2"},
  305. }),
  306. },
  307. },
  308. }),
  309. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  310. User: []*protocol.User{
  311. {
  312. Level: 0,
  313. Account: serial.ToTypedMessage(&vmess.Account{
  314. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  315. SecuritySettings: &protocol.SecurityConfig{
  316. Type: protocol.SecurityType_AES128_GCM,
  317. },
  318. }),
  319. },
  320. },
  321. }),
  322. },
  323. },
  324. },
  325. },
  326. })
  327. }
  328. func TestMuxConfig_Build(t *testing.T) {
  329. tests := []struct {
  330. name string
  331. fields string
  332. want *proxyman.MultiplexingConfig
  333. }{
  334. {"default", `{"enabled": true, "concurrency": 16}`, &proxyman.MultiplexingConfig{
  335. Enabled: true,
  336. Concurrency: 16,
  337. XudpConcurrency: 0,
  338. XudpProxyUDP443: "reject",
  339. }},
  340. {"empty def", `{}`, &proxyman.MultiplexingConfig{
  341. Enabled: false,
  342. Concurrency: 0,
  343. XudpConcurrency: 0,
  344. XudpProxyUDP443: "reject",
  345. }},
  346. {"not enable", `{"enabled": false, "concurrency": 4}`, &proxyman.MultiplexingConfig{
  347. Enabled: false,
  348. Concurrency: 4,
  349. XudpConcurrency: 0,
  350. XudpProxyUDP443: "reject",
  351. }},
  352. {"forbidden", `{"enabled": false, "concurrency": -1}`, &proxyman.MultiplexingConfig{
  353. Enabled: false,
  354. Concurrency: -1,
  355. XudpConcurrency: 0,
  356. XudpProxyUDP443: "reject",
  357. }},
  358. }
  359. for _, tt := range tests {
  360. t.Run(tt.name, func(t *testing.T) {
  361. m := &MuxConfig{}
  362. common.Must(json.Unmarshal([]byte(tt.fields), m))
  363. if got, _ := m.Build(); !reflect.DeepEqual(got, tt.want) {
  364. t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
  365. }
  366. })
  367. }
  368. }
  369. func TestConfig_Override(t *testing.T) {
  370. tests := []struct {
  371. name string
  372. orig *Config
  373. over *Config
  374. fn string
  375. want *Config
  376. }{
  377. {
  378. "combine/empty",
  379. &Config{},
  380. &Config{
  381. LogConfig: &LogConfig{},
  382. RouterConfig: &RouterConfig{},
  383. DNSConfig: &DNSConfig{},
  384. Transport: &TransportConfig{},
  385. Policy: &PolicyConfig{},
  386. API: &APIConfig{},
  387. Stats: &StatsConfig{},
  388. Reverse: &ReverseConfig{},
  389. },
  390. "",
  391. &Config{
  392. LogConfig: &LogConfig{},
  393. RouterConfig: &RouterConfig{},
  394. DNSConfig: &DNSConfig{},
  395. Transport: &TransportConfig{},
  396. Policy: &PolicyConfig{},
  397. API: &APIConfig{},
  398. Stats: &StatsConfig{},
  399. Reverse: &ReverseConfig{},
  400. },
  401. },
  402. {
  403. "combine/newattr",
  404. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  405. &Config{LogConfig: &LogConfig{}}, "",
  406. &Config{LogConfig: &LogConfig{}, InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  407. },
  408. {
  409. "replace/inbounds",
  410. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  411. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  412. "",
  413. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  414. },
  415. {
  416. "replace/inbounds-replaceall",
  417. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  418. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  419. "",
  420. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  421. },
  422. {
  423. "replace/notag-append",
  424. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}}},
  425. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  426. "",
  427. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}, {Tag: "pos1", Protocol: "kcp"}}},
  428. },
  429. {
  430. "replace/outbounds",
  431. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  432. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  433. "",
  434. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  435. },
  436. {
  437. "replace/outbounds-prepend",
  438. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos3"}}},
  439. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}}},
  440. "config.json",
  441. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}, {Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos3"}}},
  442. },
  443. {
  444. "replace/outbounds-append",
  445. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  446. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}}},
  447. "config_tail.json",
  448. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos2", Protocol: "kcp"}}},
  449. },
  450. }
  451. for _, tt := range tests {
  452. t.Run(tt.name, func(t *testing.T) {
  453. tt.orig.Override(tt.over, tt.fn)
  454. if r := cmp.Diff(tt.orig, tt.want); r != "" {
  455. t.Error(r)
  456. }
  457. })
  458. }
  459. }