v2ray_transport_test.go 7.9 KB

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