xray_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/xtls/xray-core/app/dispatcher"
  9. "github.com/xtls/xray-core/app/log"
  10. "github.com/xtls/xray-core/app/proxyman"
  11. "github.com/xtls/xray-core/app/router"
  12. "github.com/xtls/xray-core/common"
  13. clog "github.com/xtls/xray-core/common/log"
  14. "github.com/xtls/xray-core/common/net"
  15. "github.com/xtls/xray-core/common/protocol"
  16. "github.com/xtls/xray-core/common/serial"
  17. core "github.com/xtls/xray-core/core"
  18. . "github.com/xtls/xray-core/infra/conf"
  19. "github.com/xtls/xray-core/proxy/blackhole"
  20. dns_proxy "github.com/xtls/xray-core/proxy/dns"
  21. "github.com/xtls/xray-core/proxy/freedom"
  22. "github.com/xtls/xray-core/proxy/vmess"
  23. "github.com/xtls/xray-core/proxy/vmess/inbound"
  24. "github.com/xtls/xray-core/transport/internet"
  25. "github.com/xtls/xray-core/transport/internet/http"
  26. "github.com/xtls/xray-core/transport/internet/tls"
  27. "github.com/xtls/xray-core/transport/internet/websocket"
  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. }),
  222. },
  223. },
  224. Inbound: []*core.InboundHandlerConfig{
  225. {
  226. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  227. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(443)}},
  228. StreamSettings: &internet.StreamConfig{
  229. ProtocolName: "websocket",
  230. TransportSettings: []*internet.TransportConfig{
  231. {
  232. ProtocolName: "websocket",
  233. Settings: serial.ToTypedMessage(&websocket.Config{
  234. Header: []*websocket.Header{
  235. {
  236. Key: "host",
  237. Value: "example.domain",
  238. },
  239. },
  240. }),
  241. },
  242. {
  243. ProtocolName: "http",
  244. Settings: serial.ToTypedMessage(&http.Config{
  245. Path: "/test",
  246. }),
  247. },
  248. },
  249. SecurityType: "xray.transport.internet.tls.Config",
  250. SecuritySettings: []*serial.TypedMessage{
  251. serial.ToTypedMessage(&tls.Config{
  252. NextProtocol: []string{"h2"},
  253. }),
  254. },
  255. },
  256. }),
  257. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  258. User: []*protocol.User{
  259. {
  260. Level: 0,
  261. Account: serial.ToTypedMessage(&vmess.Account{
  262. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  263. SecuritySettings: &protocol.SecurityConfig{
  264. Type: protocol.SecurityType_AES128_GCM,
  265. },
  266. }),
  267. },
  268. },
  269. }),
  270. },
  271. {
  272. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  273. PortList: &net.PortList{Range: []*net.PortRange{{
  274. From: 443,
  275. To: 500,
  276. }}},
  277. AllocationStrategy: &proxyman.AllocationStrategy{
  278. Type: proxyman.AllocationStrategy_Random,
  279. Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  280. Value: 3,
  281. },
  282. },
  283. StreamSettings: &internet.StreamConfig{
  284. ProtocolName: "websocket",
  285. TransportSettings: []*internet.TransportConfig{
  286. {
  287. ProtocolName: "websocket",
  288. Settings: serial.ToTypedMessage(&websocket.Config{
  289. Header: []*websocket.Header{
  290. {
  291. Key: "host",
  292. Value: "example.domain",
  293. },
  294. },
  295. }),
  296. },
  297. {
  298. ProtocolName: "http",
  299. Settings: serial.ToTypedMessage(&http.Config{
  300. Path: "/test",
  301. }),
  302. },
  303. },
  304. SecurityType: "xray.transport.internet.tls.Config",
  305. SecuritySettings: []*serial.TypedMessage{
  306. serial.ToTypedMessage(&tls.Config{
  307. NextProtocol: []string{"h2"},
  308. }),
  309. },
  310. },
  311. }),
  312. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  313. User: []*protocol.User{
  314. {
  315. Level: 0,
  316. Account: serial.ToTypedMessage(&vmess.Account{
  317. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  318. SecuritySettings: &protocol.SecurityConfig{
  319. Type: protocol.SecurityType_AES128_GCM,
  320. },
  321. }),
  322. },
  323. },
  324. }),
  325. },
  326. },
  327. },
  328. },
  329. })
  330. }
  331. func TestMuxConfig_Build(t *testing.T) {
  332. tests := []struct {
  333. name string
  334. fields string
  335. want *proxyman.MultiplexingConfig
  336. }{
  337. {"default", `{"enabled": true, "concurrency": 16}`, &proxyman.MultiplexingConfig{
  338. Enabled: true,
  339. Concurrency: 16,
  340. }},
  341. {"empty def", `{}`, &proxyman.MultiplexingConfig{
  342. Enabled: false,
  343. Concurrency: 0,
  344. }},
  345. {"not enable", `{"enabled": false, "concurrency": 4}`, &proxyman.MultiplexingConfig{
  346. Enabled: false,
  347. Concurrency: 4,
  348. }},
  349. {"forbidden", `{"enabled": false, "concurrency": -1}`, &proxyman.MultiplexingConfig{
  350. Enabled: false,
  351. Concurrency: -1,
  352. }},
  353. }
  354. for _, tt := range tests {
  355. t.Run(tt.name, func(t *testing.T) {
  356. m := &MuxConfig{}
  357. common.Must(json.Unmarshal([]byte(tt.fields), m))
  358. if got, _ := m.Build(); !reflect.DeepEqual(got, tt.want) {
  359. t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
  360. }
  361. })
  362. }
  363. }
  364. func TestConfig_Override(t *testing.T) {
  365. tests := []struct {
  366. name string
  367. orig *Config
  368. over *Config
  369. fn string
  370. want *Config
  371. }{
  372. {
  373. "combine/empty",
  374. &Config{},
  375. &Config{
  376. LogConfig: &LogConfig{},
  377. RouterConfig: &RouterConfig{},
  378. DNSConfig: &DNSConfig{},
  379. Transport: &TransportConfig{},
  380. Policy: &PolicyConfig{},
  381. API: &APIConfig{},
  382. Stats: &StatsConfig{},
  383. Reverse: &ReverseConfig{},
  384. },
  385. "",
  386. &Config{
  387. LogConfig: &LogConfig{},
  388. RouterConfig: &RouterConfig{},
  389. DNSConfig: &DNSConfig{},
  390. Transport: &TransportConfig{},
  391. Policy: &PolicyConfig{},
  392. API: &APIConfig{},
  393. Stats: &StatsConfig{},
  394. Reverse: &ReverseConfig{},
  395. },
  396. },
  397. {
  398. "combine/newattr",
  399. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  400. &Config{LogConfig: &LogConfig{}}, "",
  401. &Config{LogConfig: &LogConfig{}, InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  402. },
  403. {
  404. "replace/inbounds",
  405. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  406. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  407. "",
  408. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  409. },
  410. {
  411. "replace/inbounds-replaceall",
  412. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  413. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  414. "",
  415. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  416. },
  417. {
  418. "replace/notag-append",
  419. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}}},
  420. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  421. "",
  422. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}, {Tag: "pos1", Protocol: "kcp"}}},
  423. },
  424. {
  425. "replace/outbounds",
  426. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  427. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  428. "",
  429. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  430. },
  431. {
  432. "replace/outbounds-prepend",
  433. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  434. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  435. "config.json",
  436. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  437. },
  438. {
  439. "replace/outbounds-append",
  440. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  441. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}}},
  442. "config_tail.json",
  443. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos2", Protocol: "kcp"}}},
  444. },
  445. }
  446. for _, tt := range tests {
  447. t.Run(tt.name, func(t *testing.T) {
  448. tt.orig.Override(tt.over, tt.fn)
  449. if r := cmp.Diff(tt.orig, tt.want); r != "" {
  450. t.Error(r)
  451. }
  452. })
  453. }
  454. }