v2ray_transport_test.go 8.4 KB

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