v2ray_test.go 8.3 KB

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