xray_test.go 8.8 KB

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