v2ray_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. Server: &net.Endpoint{},
  221. }),
  222. },
  223. },
  224. Inbound: []*core.InboundHandlerConfig{
  225. {
  226. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  227. PortRange: &net.PortRange{
  228. From: 443,
  229. To: 443,
  230. },
  231. StreamSettings: &internet.StreamConfig{
  232. ProtocolName: "websocket",
  233. TransportSettings: []*internet.TransportConfig{
  234. {
  235. ProtocolName: "websocket",
  236. Settings: serial.ToTypedMessage(&websocket.Config{
  237. Header: []*websocket.Header{
  238. {
  239. Key: "host",
  240. Value: "example.domain",
  241. },
  242. },
  243. }),
  244. },
  245. {
  246. ProtocolName: "http",
  247. Settings: serial.ToTypedMessage(&http.Config{
  248. Path: "/test",
  249. }),
  250. },
  251. },
  252. SecurityType: "v2ray.core.transport.internet.tls.Config",
  253. SecuritySettings: []*serial.TypedMessage{
  254. serial.ToTypedMessage(&tls.Config{
  255. NextProtocol: []string{"h2"},
  256. }),
  257. },
  258. },
  259. }),
  260. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  261. User: []*protocol.User{
  262. {
  263. Level: 0,
  264. Account: serial.ToTypedMessage(&vmess.Account{
  265. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  266. AlterId: 100,
  267. SecuritySettings: &protocol.SecurityConfig{
  268. Type: protocol.SecurityType_AES128_GCM,
  269. },
  270. }),
  271. },
  272. },
  273. }),
  274. },
  275. {
  276. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  277. PortRange: &net.PortRange{
  278. From: 443,
  279. To: 500,
  280. },
  281. AllocationStrategy: &proxyman.AllocationStrategy{
  282. Type: proxyman.AllocationStrategy_Random,
  283. Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
  284. Value: 3,
  285. },
  286. },
  287. StreamSettings: &internet.StreamConfig{
  288. ProtocolName: "websocket",
  289. TransportSettings: []*internet.TransportConfig{
  290. {
  291. ProtocolName: "websocket",
  292. Settings: serial.ToTypedMessage(&websocket.Config{
  293. Header: []*websocket.Header{
  294. {
  295. Key: "host",
  296. Value: "example.domain",
  297. },
  298. },
  299. }),
  300. },
  301. {
  302. ProtocolName: "http",
  303. Settings: serial.ToTypedMessage(&http.Config{
  304. Path: "/test",
  305. }),
  306. },
  307. },
  308. SecurityType: "v2ray.core.transport.internet.tls.Config",
  309. SecuritySettings: []*serial.TypedMessage{
  310. serial.ToTypedMessage(&tls.Config{
  311. NextProtocol: []string{"h2"},
  312. }),
  313. },
  314. },
  315. }),
  316. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  317. User: []*protocol.User{
  318. {
  319. Level: 0,
  320. Account: serial.ToTypedMessage(&vmess.Account{
  321. Id: "0cdf8a45-303d-4fed-9780-29aa7f54175e",
  322. AlterId: 100,
  323. SecuritySettings: &protocol.SecurityConfig{
  324. Type: protocol.SecurityType_AES128_GCM,
  325. },
  326. }),
  327. },
  328. },
  329. }),
  330. },
  331. },
  332. },
  333. },
  334. })
  335. }