xray_test.go 8.9 KB

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