v2ray_test.go 9.1 KB

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