v2ray_test.go 9.8 KB

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