v2ray_transport_test.go 9.3 KB

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