v2ray_transport_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package main
  2. import (
  3. "net/netip"
  4. "testing"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/option"
  7. "github.com/gofrs/uuid"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestV2RayGRPCSelf(t *testing.T) {
  11. testV2RayTransportSelf(t, &option.V2RayTransportOptions{
  12. Type: C.V2RayTransportTypeGRPC,
  13. GRPCOptions: option.V2RayGRPCOptions{
  14. ServiceName: "TunService",
  15. },
  16. })
  17. }
  18. func TestV2RayWebscoketSelf(t *testing.T) {
  19. t.Run("basic", func(t *testing.T) {
  20. testV2RayTransportSelf(t, &option.V2RayTransportOptions{
  21. Type: C.V2RayTransportTypeWebsocket,
  22. })
  23. })
  24. t.Run("v2ray early data", func(t *testing.T) {
  25. testV2RayTransportSelf(t, &option.V2RayTransportOptions{
  26. Type: C.V2RayTransportTypeWebsocket,
  27. WebsocketOptions: option.V2RayWebsocketOptions{
  28. MaxEarlyData: 2048,
  29. },
  30. })
  31. })
  32. t.Run("xray early data", func(t *testing.T) {
  33. testV2RayTransportSelf(t, &option.V2RayTransportOptions{
  34. Type: C.V2RayTransportTypeWebsocket,
  35. WebsocketOptions: option.V2RayWebsocketOptions{
  36. MaxEarlyData: 2048,
  37. EarlyDataHeaderName: "Sec-WebSocket-Protocol",
  38. },
  39. })
  40. })
  41. }
  42. func TestV2RayHTTPSelf(t *testing.T) {
  43. testV2RayTransportSelf(t, &option.V2RayTransportOptions{
  44. Type: C.V2RayTransportTypeHTTP,
  45. })
  46. }
  47. func TestV2RayHTTPPlainSelf(t *testing.T) {
  48. testV2RayTransportNOTLSSelf(t, &option.V2RayTransportOptions{
  49. Type: C.V2RayTransportTypeHTTP,
  50. })
  51. }
  52. func testV2RayTransportSelf(t *testing.T, transport *option.V2RayTransportOptions) {
  53. t.Run("vmess", func(t *testing.T) {
  54. testVMessTransportSelf(t, transport)
  55. })
  56. t.Run("trojan", func(t *testing.T) {
  57. testTrojanTransportSelf(t, transport)
  58. })
  59. }
  60. func testVMessTransportSelf(t *testing.T, transport *option.V2RayTransportOptions) {
  61. user, err := uuid.DefaultGenerator.NewV4()
  62. require.NoError(t, err)
  63. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  64. startInstance(t, option.Options{
  65. Log: &option.LogOptions{
  66. Level: "error",
  67. },
  68. Inbounds: []option.Inbound{
  69. {
  70. Type: C.TypeMixed,
  71. Tag: "mixed-in",
  72. MixedOptions: option.HTTPMixedInboundOptions{
  73. ListenOptions: option.ListenOptions{
  74. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  75. ListenPort: clientPort,
  76. },
  77. },
  78. },
  79. {
  80. Type: C.TypeVMess,
  81. VMessOptions: option.VMessInboundOptions{
  82. ListenOptions: option.ListenOptions{
  83. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  84. ListenPort: serverPort,
  85. },
  86. Users: []option.VMessUser{
  87. {
  88. Name: "sekai",
  89. UUID: user.String(),
  90. },
  91. },
  92. TLS: &option.InboundTLSOptions{
  93. Enabled: true,
  94. ServerName: "example.org",
  95. CertificatePath: certPem,
  96. KeyPath: keyPem,
  97. },
  98. Transport: transport,
  99. },
  100. },
  101. },
  102. Outbounds: []option.Outbound{
  103. {
  104. Type: C.TypeDirect,
  105. },
  106. {
  107. Type: C.TypeVMess,
  108. Tag: "vmess-out",
  109. VMessOptions: option.VMessOutboundOptions{
  110. ServerOptions: option.ServerOptions{
  111. Server: "127.0.0.1",
  112. ServerPort: serverPort,
  113. },
  114. UUID: user.String(),
  115. Security: "zero",
  116. TLS: &option.OutboundTLSOptions{
  117. Enabled: true,
  118. ServerName: "example.org",
  119. CertificatePath: certPem,
  120. },
  121. Transport: transport,
  122. },
  123. },
  124. },
  125. Route: &option.RouteOptions{
  126. Rules: []option.Rule{
  127. {
  128. DefaultOptions: option.DefaultRule{
  129. Inbound: []string{"mixed-in"},
  130. Outbound: "vmess-out",
  131. },
  132. },
  133. },
  134. },
  135. })
  136. testSuit(t, clientPort, testPort)
  137. }
  138. func testTrojanTransportSelf(t *testing.T, transport *option.V2RayTransportOptions) {
  139. user, err := uuid.DefaultGenerator.NewV4()
  140. require.NoError(t, err)
  141. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  142. startInstance(t, option.Options{
  143. Log: &option.LogOptions{
  144. Level: "error",
  145. },
  146. Inbounds: []option.Inbound{
  147. {
  148. Type: C.TypeMixed,
  149. Tag: "mixed-in",
  150. MixedOptions: option.HTTPMixedInboundOptions{
  151. ListenOptions: option.ListenOptions{
  152. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  153. ListenPort: clientPort,
  154. },
  155. },
  156. },
  157. {
  158. Type: C.TypeTrojan,
  159. TrojanOptions: option.TrojanInboundOptions{
  160. ListenOptions: option.ListenOptions{
  161. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  162. ListenPort: serverPort,
  163. },
  164. Users: []option.TrojanUser{
  165. {
  166. Name: "sekai",
  167. Password: user.String(),
  168. },
  169. },
  170. TLS: &option.InboundTLSOptions{
  171. Enabled: true,
  172. ServerName: "example.org",
  173. CertificatePath: certPem,
  174. KeyPath: keyPem,
  175. },
  176. Transport: transport,
  177. },
  178. },
  179. },
  180. Outbounds: []option.Outbound{
  181. {
  182. Type: C.TypeDirect,
  183. },
  184. {
  185. Type: C.TypeTrojan,
  186. Tag: "vmess-out",
  187. TrojanOptions: option.TrojanOutboundOptions{
  188. ServerOptions: option.ServerOptions{
  189. Server: "127.0.0.1",
  190. ServerPort: serverPort,
  191. },
  192. Password: user.String(),
  193. TLS: &option.OutboundTLSOptions{
  194. Enabled: true,
  195. ServerName: "example.org",
  196. CertificatePath: certPem,
  197. },
  198. Transport: transport,
  199. },
  200. },
  201. },
  202. Route: &option.RouteOptions{
  203. Rules: []option.Rule{
  204. {
  205. DefaultOptions: option.DefaultRule{
  206. Inbound: []string{"mixed-in"},
  207. Outbound: "vmess-out",
  208. },
  209. },
  210. },
  211. },
  212. })
  213. testSuit(t, clientPort, testPort)
  214. }
  215. func TestVMessQUICSelf(t *testing.T) {
  216. transport := &option.V2RayTransportOptions{
  217. Type: C.V2RayTransportTypeQUIC,
  218. }
  219. user, err := uuid.DefaultGenerator.NewV4()
  220. require.NoError(t, err)
  221. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  222. startInstance(t, option.Options{
  223. Log: &option.LogOptions{
  224. Level: "error",
  225. },
  226. Inbounds: []option.Inbound{
  227. {
  228. Type: C.TypeMixed,
  229. Tag: "mixed-in",
  230. MixedOptions: option.HTTPMixedInboundOptions{
  231. ListenOptions: option.ListenOptions{
  232. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  233. ListenPort: clientPort,
  234. },
  235. },
  236. },
  237. {
  238. Type: C.TypeVMess,
  239. VMessOptions: option.VMessInboundOptions{
  240. ListenOptions: option.ListenOptions{
  241. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  242. ListenPort: serverPort,
  243. },
  244. Users: []option.VMessUser{
  245. {
  246. Name: "sekai",
  247. UUID: user.String(),
  248. },
  249. },
  250. TLS: &option.InboundTLSOptions{
  251. Enabled: true,
  252. ServerName: "example.org",
  253. CertificatePath: certPem,
  254. KeyPath: keyPem,
  255. },
  256. Transport: transport,
  257. },
  258. },
  259. },
  260. Outbounds: []option.Outbound{
  261. {
  262. Type: C.TypeDirect,
  263. },
  264. {
  265. Type: C.TypeVMess,
  266. Tag: "vmess-out",
  267. VMessOptions: option.VMessOutboundOptions{
  268. ServerOptions: option.ServerOptions{
  269. Server: "127.0.0.1",
  270. ServerPort: serverPort,
  271. },
  272. UUID: user.String(),
  273. Security: "zero",
  274. TLS: &option.OutboundTLSOptions{
  275. Enabled: true,
  276. ServerName: "example.org",
  277. CertificatePath: certPem,
  278. },
  279. Transport: transport,
  280. },
  281. },
  282. },
  283. Route: &option.RouteOptions{
  284. Rules: []option.Rule{
  285. {
  286. DefaultOptions: option.DefaultRule{
  287. Inbound: []string{"mixed-in"},
  288. Outbound: "vmess-out",
  289. },
  290. },
  291. },
  292. },
  293. })
  294. testSuitQUIC(t, clientPort, testPort)
  295. }
  296. func testV2RayTransportNOTLSSelf(t *testing.T, transport *option.V2RayTransportOptions) {
  297. user, err := uuid.DefaultGenerator.NewV4()
  298. require.NoError(t, err)
  299. startInstance(t, option.Options{
  300. Log: &option.LogOptions{
  301. Level: "error",
  302. },
  303. Inbounds: []option.Inbound{
  304. {
  305. Type: C.TypeMixed,
  306. Tag: "mixed-in",
  307. MixedOptions: option.HTTPMixedInboundOptions{
  308. ListenOptions: option.ListenOptions{
  309. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  310. ListenPort: clientPort,
  311. },
  312. },
  313. },
  314. {
  315. Type: C.TypeVMess,
  316. VMessOptions: option.VMessInboundOptions{
  317. ListenOptions: option.ListenOptions{
  318. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  319. ListenPort: serverPort,
  320. },
  321. Users: []option.VMessUser{
  322. {
  323. Name: "sekai",
  324. UUID: user.String(),
  325. },
  326. },
  327. Transport: transport,
  328. },
  329. },
  330. },
  331. Outbounds: []option.Outbound{
  332. {
  333. Type: C.TypeDirect,
  334. },
  335. {
  336. Type: C.TypeVMess,
  337. Tag: "vmess-out",
  338. VMessOptions: option.VMessOutboundOptions{
  339. ServerOptions: option.ServerOptions{
  340. Server: "127.0.0.1",
  341. ServerPort: serverPort,
  342. },
  343. UUID: user.String(),
  344. Security: "zero",
  345. Transport: transport,
  346. },
  347. },
  348. },
  349. Route: &option.RouteOptions{
  350. Rules: []option.Rule{
  351. {
  352. DefaultOptions: option.DefaultRule{
  353. Inbound: []string{"mixed-in"},
  354. Outbound: "vmess-out",
  355. },
  356. },
  357. },
  358. },
  359. })
  360. testSuitQUIC(t, clientPort, testPort)
  361. }