v2ray_test.go 9.7 KB

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