v2ray_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/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/app/router/routercommon"
  14. "github.com/v2fly/v2ray-core/v4/common"
  15. clog "github.com/v2fly/v2ray-core/v4/common/log"
  16. "github.com/v2fly/v2ray-core/v4/common/net"
  17. "github.com/v2fly/v2ray-core/v4/common/protocol"
  18. "github.com/v2fly/v2ray-core/v4/common/serial"
  19. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/muxcfg"
  20. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist"
  21. // Geo loaders
  22. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/memconservative"
  23. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
  24. v4 "github.com/v2fly/v2ray-core/v4/infra/conf/v4"
  25. "github.com/v2fly/v2ray-core/v4/proxy/blackhole"
  26. dns_proxy "github.com/v2fly/v2ray-core/v4/proxy/dns"
  27. "github.com/v2fly/v2ray-core/v4/proxy/freedom"
  28. "github.com/v2fly/v2ray-core/v4/proxy/vmess"
  29. "github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
  30. "github.com/v2fly/v2ray-core/v4/transport/internet"
  31. "github.com/v2fly/v2ray-core/v4/transport/internet/http"
  32. "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
  33. "github.com/v2fly/v2ray-core/v4/transport/internet/websocket"
  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. Error: &log.LogSpecification{
  147. Type: log.LogType_File,
  148. Level: clog.Severity_Error,
  149. Path: "/var/log/v2ray/error.log",
  150. },
  151. Access: &log.LogSpecification{
  152. Type: log.LogType_File,
  153. Path: "/var/log/v2ray/access.log",
  154. },
  155. }),
  156. serial.ToTypedMessage(&dispatcher.Config{}),
  157. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  158. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  159. serial.ToTypedMessage(&router.Config{
  160. DomainStrategy: router.DomainStrategy_AsIs,
  161. Rule: []*router.RoutingRule{
  162. {
  163. Geoip: []*routercommon.GeoIP{
  164. {
  165. Cidr: []*routercommon.CIDR{
  166. {
  167. Ip: []byte{10, 0, 0, 0},
  168. Prefix: 8,
  169. },
  170. },
  171. },
  172. },
  173. TargetTag: &router.RoutingRule_Tag{
  174. Tag: "blocked",
  175. },
  176. },
  177. },
  178. }),
  179. },
  180. Outbound: []*core.OutboundHandlerConfig{
  181. {
  182. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  183. StreamSettings: &internet.StreamConfig{
  184. ProtocolName: "tcp",
  185. TransportSettings: []*internet.TransportConfig{
  186. {
  187. ProtocolName: "http",
  188. Settings: serial.ToTypedMessage(&http.Config{
  189. Path: "/test",
  190. }),
  191. },
  192. },
  193. },
  194. }),
  195. ProxySettings: serial.ToTypedMessage(&freedom.Config{
  196. DomainStrategy: freedom.Config_AS_IS,
  197. UserLevel: 0,
  198. }),
  199. },
  200. {
  201. Tag: "blocked",
  202. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  203. StreamSettings: &internet.StreamConfig{
  204. ProtocolName: "tcp",
  205. TransportSettings: []*internet.TransportConfig{
  206. {
  207. ProtocolName: "http",
  208. Settings: serial.ToTypedMessage(&http.Config{
  209. Path: "/test",
  210. }),
  211. },
  212. },
  213. },
  214. }),
  215. ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
  216. },
  217. {
  218. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
  219. StreamSettings: &internet.StreamConfig{
  220. ProtocolName: "tcp",
  221. TransportSettings: []*internet.TransportConfig{
  222. {
  223. ProtocolName: "http",
  224. Settings: serial.ToTypedMessage(&http.Config{
  225. Path: "/test",
  226. }),
  227. },
  228. },
  229. },
  230. }),
  231. ProxySettings: serial.ToTypedMessage(&dns_proxy.Config{
  232. Server: &net.Endpoint{},
  233. }),
  234. },
  235. },
  236. Inbound: []*core.InboundHandlerConfig{
  237. {
  238. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  239. PortRange: &net.PortRange{
  240. From: 443,
  241. To: 443,
  242. },
  243. StreamSettings: &internet.StreamConfig{
  244. ProtocolName: "websocket",
  245. TransportSettings: []*internet.TransportConfig{
  246. {
  247. ProtocolName: "websocket",
  248. Settings: serial.ToTypedMessage(&websocket.Config{
  249. Header: []*websocket.Header{
  250. {
  251. Key: "host",
  252. Value: "example.domain",
  253. },
  254. },
  255. }),
  256. },
  257. {
  258. ProtocolName: "http",
  259. Settings: serial.ToTypedMessage(&http.Config{
  260. Path: "/test",
  261. }),
  262. },
  263. },
  264. SecurityType: "v2ray.core.transport.internet.tls.Config",
  265. SecuritySettings: []*anypb.Any{
  266. serial.ToTypedMessage(&tls.Config{
  267. NextProtocol: []string{"h2"},
  268. }),
  269. },
  270. },
  271. }),
  272. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  273. User: []*protocol.User{
  274. {
  275. Level: 0,
  276. Account: serial.ToTypedMessage(&vmess.Account{
  277. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  278. AlterId: 100,
  279. SecuritySettings: &protocol.SecurityConfig{
  280. Type: protocol.SecurityType_AES128_GCM,
  281. },
  282. }),
  283. },
  284. },
  285. }),
  286. },
  287. {
  288. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  289. PortRange: &net.PortRange{
  290. From: 443,
  291. To: 500,
  292. },
  293. AllocationStrategy: &proxyman.AllocationStrategy{
  294. Type: proxyman.AllocationStrategy_Random,
  295. Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  296. Value: 3,
  297. },
  298. },
  299. StreamSettings: &internet.StreamConfig{
  300. ProtocolName: "websocket",
  301. TransportSettings: []*internet.TransportConfig{
  302. {
  303. ProtocolName: "websocket",
  304. Settings: serial.ToTypedMessage(&websocket.Config{
  305. Header: []*websocket.Header{
  306. {
  307. Key: "host",
  308. Value: "example.domain",
  309. },
  310. },
  311. }),
  312. },
  313. {
  314. ProtocolName: "http",
  315. Settings: serial.ToTypedMessage(&http.Config{
  316. Path: "/test",
  317. }),
  318. },
  319. },
  320. SecurityType: "v2ray.core.transport.internet.tls.Config",
  321. SecuritySettings: []*anypb.Any{
  322. serial.ToTypedMessage(&tls.Config{
  323. NextProtocol: []string{"h2"},
  324. }),
  325. },
  326. },
  327. }),
  328. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  329. User: []*protocol.User{
  330. {
  331. Level: 0,
  332. Account: serial.ToTypedMessage(&vmess.Account{
  333. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  334. AlterId: 100,
  335. SecuritySettings: &protocol.SecurityConfig{
  336. Type: protocol.SecurityType_AES128_GCM,
  337. },
  338. }),
  339. },
  340. },
  341. }),
  342. },
  343. },
  344. },
  345. },
  346. })
  347. }
  348. func TestMuxConfig_Build(t *testing.T) {
  349. tests := []struct {
  350. name string
  351. fields string
  352. want *proxyman.MultiplexingConfig
  353. }{
  354. {"default", `{"enabled": true, "concurrency": 16}`, &proxyman.MultiplexingConfig{
  355. Enabled: true,
  356. Concurrency: 16,
  357. }},
  358. {"empty def", `{}`, &proxyman.MultiplexingConfig{
  359. Enabled: false,
  360. Concurrency: 8,
  361. }},
  362. {"not enable", `{"enabled": false, "concurrency": 4}`, &proxyman.MultiplexingConfig{
  363. Enabled: false,
  364. Concurrency: 4,
  365. }},
  366. {"forbidden", `{"enabled": false, "concurrency": -1}`, nil},
  367. }
  368. for _, tt := range tests {
  369. t.Run(tt.name, func(t *testing.T) {
  370. m := &muxcfg.MuxConfig{}
  371. common.Must(json.Unmarshal([]byte(tt.fields), m))
  372. if got := m.Build(); !reflect.DeepEqual(got, tt.want) {
  373. t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
  374. }
  375. })
  376. }
  377. }