v2ray_transport_test.go 8.8 KB

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