v2ray_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package v4_test
  2. import (
  3. "encoding/json"
  4. "github.com/v2fly/v2ray-core/v4/app/router/routercommon"
  5. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/muxcfg"
  6. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist"
  7. "github.com/v2fly/v2ray-core/v4/infra/conf/v4"
  8. "google.golang.org/protobuf/types/known/anypb"
  9. "reflect"
  10. "testing"
  11. "github.com/golang/protobuf/proto"
  12. core "github.com/v2fly/v2ray-core/v4"
  13. "github.com/v2fly/v2ray-core/v4/app/dispatcher"
  14. "github.com/v2fly/v2ray-core/v4/app/log"
  15. "github.com/v2fly/v2ray-core/v4/app/proxyman"
  16. "github.com/v2fly/v2ray-core/v4/app/router"
  17. "github.com/v2fly/v2ray-core/v4/common"
  18. clog "github.com/v2fly/v2ray-core/v4/common/log"
  19. "github.com/v2fly/v2ray-core/v4/common/net"
  20. "github.com/v2fly/v2ray-core/v4/common/protocol"
  21. "github.com/v2fly/v2ray-core/v4/common/serial"
  22. "github.com/v2fly/v2ray-core/v4/proxy/blackhole"
  23. dns_proxy "github.com/v2fly/v2ray-core/v4/proxy/dns"
  24. "github.com/v2fly/v2ray-core/v4/proxy/freedom"
  25. "github.com/v2fly/v2ray-core/v4/proxy/vmess"
  26. "github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
  27. "github.com/v2fly/v2ray-core/v4/transport/internet"
  28. "github.com/v2fly/v2ray-core/v4/transport/internet/http"
  29. "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
  30. "github.com/v2fly/v2ray-core/v4/transport/internet/websocket"
  31. // Geo loaders
  32. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/memconservative"
  33. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
  34. )
  35. func TestV2RayConfig(t *testing.T) {
  36. createParser := func() func(string) (proto.Message, error) {
  37. return func(s string) (proto.Message, error) {
  38. config := new(v4.Config)
  39. if err := json.Unmarshal([]byte(s), config); err != nil {
  40. return nil, err
  41. }
  42. return config.Build()
  43. }
  44. }
  45. testassist.RunMultiTestCase(t, []testassist.TestCase{
  46. {
  47. Input: `{
  48. "outbound": {
  49. "protocol": "freedom",
  50. "settings": {}
  51. },
  52. "log": {
  53. "access": "/var/log/v2ray/access.log",
  54. "loglevel": "error",
  55. "error": "/var/log/v2ray/error.log"
  56. },
  57. "inbound": {
  58. "streamSettings": {
  59. "network": "ws",
  60. "wsSettings": {
  61. "headers": {
  62. "host": "example.domain"
  63. },
  64. "path": ""
  65. },
  66. "tlsSettings": {
  67. "alpn": "h2"
  68. },
  69. "security": "tls"
  70. },
  71. "protocol": "vmess",
  72. "port": 443,
  73. "settings": {
  74. "clients": [
  75. {
  76. "alterId": 100,
  77. "security": "aes-128-gcm",
  78. "id": "0cdf8a45-303d-4fed-9780-29aa7f54175e"
  79. }
  80. ]
  81. }
  82. },
  83. "inbounds": [{
  84. "streamSettings": {
  85. "network": "ws",
  86. "wsSettings": {
  87. "headers": {
  88. "host": "example.domain"
  89. },
  90. "path": ""
  91. },
  92. "tlsSettings": {
  93. "alpn": "h2"
  94. },
  95. "security": "tls"
  96. },
  97. "protocol": "vmess",
  98. "port": "443-500",
  99. "allocate": {
  100. "strategy": "random",
  101. "concurrency": 3
  102. },
  103. "settings": {
  104. "clients": [
  105. {
  106. "alterId": 100,
  107. "security": "aes-128-gcm",
  108. "id": "0cdf8a45-303d-4fed-9780-29aa7f54175e"
  109. }
  110. ]
  111. }
  112. }],
  113. "outboundDetour": [
  114. {
  115. "tag": "blocked",
  116. "protocol": "blackhole"
  117. },
  118. {
  119. "protocol": "dns"
  120. }
  121. ],
  122. "routing": {
  123. "strategy": "rules",
  124. "settings": {
  125. "rules": [
  126. {
  127. "ip": [
  128. "10.0.0.0/8"
  129. ],
  130. "type": "field",
  131. "outboundTag": "blocked"
  132. }
  133. ]
  134. }
  135. },
  136. "transport": {
  137. "httpSettings": {
  138. "path": "/test"
  139. }
  140. }
  141. }`,
  142. Parser: createParser(),
  143. Output: &core.Config{
  144. App: []*anypb.Any{
  145. serial.ToTypedMessage(&log.Config{
  146. ErrorLogType: log.LogType_File,
  147. ErrorLogPath: "/var/log/v2ray/error.log",
  148. ErrorLogLevel: clog.Severity_Error,
  149. AccessLogType: log.LogType_File,
  150. AccessLogPath: "/var/log/v2ray/access.log",
  151. }),
  152. serial.ToTypedMessage(&dispatcher.Config{}),
  153. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  154. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  155. serial.ToTypedMessage(&router.Config{
  156. DomainStrategy: router.DomainStrategy_AsIs,
  157. Rule: []*router.RoutingRule{
  158. {
  159. Geoip: []*routercommon.GeoIP{
  160. {
  161. Cidr: []*routercommon.CIDR{
  162. {
  163. Ip: []byte{10, 0, 0, 0},
  164. Prefix: 8,
  165. },
  166. },
  167. },
  168. },
  169. TargetTag: &router.RoutingRule_Tag{
  170. Tag: "blocked",
  171. },
  172. },
  173. },
  174. }),
  175. },
  176. Outbound: []*core.OutboundHandlerConfig{
  177. {
  178. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  179. StreamSettings: &internet.StreamConfig{
  180. ProtocolName: "tcp",
  181. TransportSettings: []*internet.TransportConfig{
  182. {
  183. ProtocolName: "http",
  184. Settings: serial.ToTypedMessage(&http.Config{
  185. Path: "/test",
  186. }),
  187. },
  188. },
  189. },
  190. }),
  191. ProxySettings: serial.ToTypedMessage(&freedom.Config{
  192. DomainStrategy: freedom.Config_AS_IS,
  193. UserLevel: 0,
  194. }),
  195. },
  196. {
  197. Tag: "blocked",
  198. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  199. StreamSettings: &internet.StreamConfig{
  200. ProtocolName: "tcp",
  201. TransportSettings: []*internet.TransportConfig{
  202. {
  203. ProtocolName: "http",
  204. Settings: serial.ToTypedMessage(&http.Config{
  205. Path: "/test",
  206. }),
  207. },
  208. },
  209. },
  210. }),
  211. ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
  212. },
  213. {
  214. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  215. StreamSettings: &internet.StreamConfig{
  216. ProtocolName: "tcp",
  217. TransportSettings: []*internet.TransportConfig{
  218. {
  219. ProtocolName: "http",
  220. Settings: serial.ToTypedMessage(&http.Config{
  221. Path: "/test",
  222. }),
  223. },
  224. },
  225. },
  226. }),
  227. ProxySettings: serial.ToTypedMessage(&dns_proxy.Config{
  228. Server: &net.Endpoint{},
  229. }),
  230. },
  231. },
  232. Inbound: []*core.InboundHandlerConfig{
  233. {
  234. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  235. PortRange: &net.PortRange{
  236. From: 443,
  237. To: 443,
  238. },
  239. StreamSettings: &internet.StreamConfig{
  240. ProtocolName: "websocket",
  241. TransportSettings: []*internet.TransportConfig{
  242. {
  243. ProtocolName: "websocket",
  244. Settings: serial.ToTypedMessage(&websocket.Config{
  245. Header: []*websocket.Header{
  246. {
  247. Key: "host",
  248. Value: "example.domain",
  249. },
  250. },
  251. }),
  252. },
  253. {
  254. ProtocolName: "http",
  255. Settings: serial.ToTypedMessage(&http.Config{
  256. Path: "/test",
  257. }),
  258. },
  259. },
  260. SecurityType: "v2ray.core.transport.internet.tls.Config",
  261. SecuritySettings: []*anypb.Any{
  262. serial.ToTypedMessage(&tls.Config{
  263. NextProtocol: []string{"h2"},
  264. }),
  265. },
  266. },
  267. }),
  268. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  269. User: []*protocol.User{
  270. {
  271. Level: 0,
  272. Account: serial.ToTypedMessage(&vmess.Account{
  273. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  274. AlterId: 100,
  275. SecuritySettings: &protocol.SecurityConfig{
  276. Type: protocol.SecurityType_AES128_GCM,
  277. },
  278. }),
  279. },
  280. },
  281. }),
  282. },
  283. {
  284. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  285. PortRange: &net.PortRange{
  286. From: 443,
  287. To: 500,
  288. },
  289. AllocationStrategy: &proxyman.AllocationStrategy{
  290. Type: proxyman.AllocationStrategy_Random,
  291. Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  292. Value: 3,
  293. },
  294. },
  295. StreamSettings: &internet.StreamConfig{
  296. ProtocolName: "websocket",
  297. TransportSettings: []*internet.TransportConfig{
  298. {
  299. ProtocolName: "websocket",
  300. Settings: serial.ToTypedMessage(&websocket.Config{
  301. Header: []*websocket.Header{
  302. {
  303. Key: "host",
  304. Value: "example.domain",
  305. },
  306. },
  307. }),
  308. },
  309. {
  310. ProtocolName: "http",
  311. Settings: serial.ToTypedMessage(&http.Config{
  312. Path: "/test",
  313. }),
  314. },
  315. },
  316. SecurityType: "v2ray.core.transport.internet.tls.Config",
  317. SecuritySettings: []*anypb.Any{
  318. serial.ToTypedMessage(&tls.Config{
  319. NextProtocol: []string{"h2"},
  320. }),
  321. },
  322. },
  323. }),
  324. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  325. User: []*protocol.User{
  326. {
  327. Level: 0,
  328. Account: serial.ToTypedMessage(&vmess.Account{
  329. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  330. AlterId: 100,
  331. SecuritySettings: &protocol.SecurityConfig{
  332. Type: protocol.SecurityType_AES128_GCM,
  333. },
  334. }),
  335. },
  336. },
  337. }),
  338. },
  339. },
  340. },
  341. },
  342. })
  343. }
  344. func TestMuxConfig_Build(t *testing.T) {
  345. tests := []struct {
  346. name string
  347. fields string
  348. want *proxyman.MultiplexingConfig
  349. }{
  350. {"default", `{"enabled": true, "concurrency": 16}`, &proxyman.MultiplexingConfig{
  351. Enabled: true,
  352. Concurrency: 16,
  353. }},
  354. {"empty def", `{}`, &proxyman.MultiplexingConfig{
  355. Enabled: false,
  356. Concurrency: 8,
  357. }},
  358. {"not enable", `{"enabled": false, "concurrency": 4}`, &proxyman.MultiplexingConfig{
  359. Enabled: false,
  360. Concurrency: 4,
  361. }},
  362. {"forbidden", `{"enabled": false, "concurrency": -1}`, nil},
  363. }
  364. for _, tt := range tests {
  365. t.Run(tt.name, func(t *testing.T) {
  366. m := &muxcfg.MuxConfig{}
  367. common.Must(json.Unmarshal([]byte(tt.fields), m))
  368. if got := m.Build(); !reflect.DeepEqual(got, tt.want) {
  369. t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
  370. }
  371. })
  372. }
  373. }