xray_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/vmess"
  19. "github.com/xtls/xray-core/proxy/vmess/inbound"
  20. "github.com/xtls/xray-core/transport/internet"
  21. "github.com/xtls/xray-core/transport/internet/tls"
  22. "github.com/xtls/xray-core/transport/internet/websocket"
  23. "google.golang.org/protobuf/proto"
  24. )
  25. func TestXrayConfig(t *testing.T) {
  26. createParser := func() func(string) (proto.Message, error) {
  27. return func(s string) (proto.Message, error) {
  28. config := new(Config)
  29. if err := json.Unmarshal([]byte(s), config); err != nil {
  30. return nil, err
  31. }
  32. return config.Build()
  33. }
  34. }
  35. runMultiTestCase(t, []TestCase{
  36. {
  37. Input: `{
  38. "log": {
  39. "access": "/var/log/xray/access.log",
  40. "loglevel": "error",
  41. "error": "/var/log/xray/error.log"
  42. },
  43. "inbounds": [{
  44. "streamSettings": {
  45. "network": "ws",
  46. "wsSettings": {
  47. "host": "example.domain",
  48. "path": ""
  49. },
  50. "tlsSettings": {
  51. "alpn": "h2"
  52. },
  53. "security": "tls"
  54. },
  55. "protocol": "vmess",
  56. "port": "443-500",
  57. "settings": {
  58. "clients": [
  59. {
  60. "security": "aes-128-gcm",
  61. "id": "0cdf8a45-303d-4fed-9780-29aa7f54175e"
  62. }
  63. ]
  64. }
  65. }],
  66. "routing": {
  67. "rules": [
  68. {
  69. "ip": [
  70. "10.0.0.0/8"
  71. ],
  72. "outboundTag": "blocked"
  73. }
  74. ]
  75. }
  76. }`,
  77. Parser: createParser(),
  78. Output: &core.Config{
  79. App: []*serial.TypedMessage{
  80. serial.ToTypedMessage(&log.Config{
  81. ErrorLogType: log.LogType_File,
  82. ErrorLogPath: "/var/log/xray/error.log",
  83. ErrorLogLevel: clog.Severity_Error,
  84. AccessLogType: log.LogType_File,
  85. AccessLogPath: "/var/log/xray/access.log",
  86. }),
  87. serial.ToTypedMessage(&dispatcher.Config{}),
  88. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  89. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  90. serial.ToTypedMessage(&router.Config{
  91. DomainStrategy: router.Config_AsIs,
  92. Rule: []*router.RoutingRule{
  93. {
  94. Geoip: []*router.GeoIP{
  95. {
  96. Cidr: []*router.CIDR{
  97. {
  98. Ip: []byte{10, 0, 0, 0},
  99. Prefix: 8,
  100. },
  101. },
  102. },
  103. },
  104. TargetTag: &router.RoutingRule_Tag{
  105. Tag: "blocked",
  106. },
  107. },
  108. },
  109. }),
  110. },
  111. Inbound: []*core.InboundHandlerConfig{
  112. {
  113. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  114. PortList: &net.PortList{Range: []*net.PortRange{{
  115. From: 443,
  116. To: 500,
  117. }}},
  118. StreamSettings: &internet.StreamConfig{
  119. ProtocolName: "websocket",
  120. TransportSettings: []*internet.TransportConfig{
  121. {
  122. ProtocolName: "websocket",
  123. Settings: serial.ToTypedMessage(&websocket.Config{
  124. Host: "example.domain",
  125. }),
  126. },
  127. },
  128. SecurityType: "xray.transport.internet.tls.Config",
  129. SecuritySettings: []*serial.TypedMessage{
  130. serial.ToTypedMessage(&tls.Config{
  131. NextProtocol: []string{"h2"},
  132. }),
  133. },
  134. },
  135. }),
  136. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  137. User: []*protocol.User{
  138. {
  139. Level: 0,
  140. Account: serial.ToTypedMessage(&vmess.Account{
  141. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  142. SecuritySettings: &protocol.SecurityConfig{
  143. Type: protocol.SecurityType_AES128_GCM,
  144. },
  145. }),
  146. },
  147. },
  148. }),
  149. },
  150. },
  151. },
  152. },
  153. })
  154. }
  155. func TestMuxConfig_Build(t *testing.T) {
  156. tests := []struct {
  157. name string
  158. fields string
  159. want *proxyman.MultiplexingConfig
  160. }{
  161. {"default", `{"enabled": true, "concurrency": 16}`, &proxyman.MultiplexingConfig{
  162. Enabled: true,
  163. Concurrency: 16,
  164. XudpConcurrency: 0,
  165. XudpProxyUDP443: "reject",
  166. }},
  167. {"empty def", `{}`, &proxyman.MultiplexingConfig{
  168. Enabled: false,
  169. Concurrency: 0,
  170. XudpConcurrency: 0,
  171. XudpProxyUDP443: "reject",
  172. }},
  173. {"not enable", `{"enabled": false, "concurrency": 4}`, &proxyman.MultiplexingConfig{
  174. Enabled: false,
  175. Concurrency: 4,
  176. XudpConcurrency: 0,
  177. XudpProxyUDP443: "reject",
  178. }},
  179. {"forbidden", `{"enabled": false, "concurrency": -1}`, &proxyman.MultiplexingConfig{
  180. Enabled: false,
  181. Concurrency: -1,
  182. XudpConcurrency: 0,
  183. XudpProxyUDP443: "reject",
  184. }},
  185. }
  186. for _, tt := range tests {
  187. t.Run(tt.name, func(t *testing.T) {
  188. m := &MuxConfig{}
  189. common.Must(json.Unmarshal([]byte(tt.fields), m))
  190. if got, _ := m.Build(); !reflect.DeepEqual(got, tt.want) {
  191. t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
  192. }
  193. })
  194. }
  195. }
  196. func TestConfig_Override(t *testing.T) {
  197. tests := []struct {
  198. name string
  199. orig *Config
  200. over *Config
  201. fn string
  202. want *Config
  203. }{
  204. {
  205. "combine/empty",
  206. &Config{},
  207. &Config{
  208. LogConfig: &LogConfig{},
  209. RouterConfig: &RouterConfig{},
  210. DNSConfig: &DNSConfig{},
  211. Policy: &PolicyConfig{},
  212. API: &APIConfig{},
  213. Stats: &StatsConfig{},
  214. Reverse: &ReverseConfig{},
  215. },
  216. "",
  217. &Config{
  218. LogConfig: &LogConfig{},
  219. RouterConfig: &RouterConfig{},
  220. DNSConfig: &DNSConfig{},
  221. Policy: &PolicyConfig{},
  222. API: &APIConfig{},
  223. Stats: &StatsConfig{},
  224. Reverse: &ReverseConfig{},
  225. },
  226. },
  227. {
  228. "combine/newattr",
  229. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  230. &Config{LogConfig: &LogConfig{}}, "",
  231. &Config{LogConfig: &LogConfig{}, InboundConfigs: []InboundDetourConfig{{Tag: "old"}}},
  232. },
  233. {
  234. "replace/inbounds",
  235. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  236. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  237. "",
  238. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  239. },
  240. {
  241. "replace/inbounds-replaceall",
  242. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  243. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  244. "",
  245. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
  246. },
  247. {
  248. "replace/notag-append",
  249. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}}},
  250. &Config{InboundConfigs: []InboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  251. "",
  252. &Config{InboundConfigs: []InboundDetourConfig{{}, {Protocol: "vmess"}, {Tag: "pos1", Protocol: "kcp"}}},
  253. },
  254. {
  255. "replace/outbounds",
  256. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  257. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}}},
  258. "",
  259. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}}},
  260. },
  261. {
  262. "replace/outbounds-prepend",
  263. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos3"}}},
  264. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}}},
  265. "config.json",
  266. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}, {Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos3"}}},
  267. },
  268. {
  269. "replace/outbounds-append",
  270. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
  271. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}}},
  272. "config_tail.json",
  273. &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos2", Protocol: "kcp"}}},
  274. },
  275. }
  276. for _, tt := range tests {
  277. t.Run(tt.name, func(t *testing.T) {
  278. tt.orig.Override(tt.over, tt.fn)
  279. if r := cmp.Diff(tt.orig, tt.want); r != "" {
  280. t.Error(r)
  281. }
  282. })
  283. }
  284. }