v2ray_test.go 9.9 KB

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