v2ray_test.go 13 KB

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