socks5_helper.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package scenarios
  2. import (
  3. "net"
  4. "github.com/v2ray/v2ray-core/app/point"
  5. "github.com/v2ray/v2ray-core/app/point/config/testing/mocks"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  8. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  9. _ "github.com/v2ray/v2ray-core/proxy/socks"
  10. socksjson "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  11. _ "github.com/v2ray/v2ray-core/proxy/vmess"
  12. "github.com/v2ray/v2ray-core/proxy/vmess/config"
  13. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
  14. )
  15. const (
  16. socks5Version = byte(0x05)
  17. )
  18. func socks5AuthMethodRequest(methods ...byte) []byte {
  19. request := []byte{socks5Version, byte(len(methods))}
  20. request = append(request, methods...)
  21. return request
  22. }
  23. func appendAddress(request []byte, address v2net.Address) []byte {
  24. switch {
  25. case address.IsIPv4():
  26. request = append(request, byte(0x01))
  27. request = append(request, address.IP()...)
  28. case address.IsIPv6():
  29. request = append(request, byte(0x04))
  30. request = append(request, address.IP()...)
  31. case address.IsDomain():
  32. request = append(request, byte(0x03), byte(len(address.Domain())))
  33. request = append(request, []byte(address.Domain())...)
  34. }
  35. request = append(request, address.PortBytes()...)
  36. return request
  37. }
  38. func socks5Request(command byte, address v2net.Address) []byte {
  39. request := []byte{socks5Version, command, 0}
  40. request = appendAddress(request, address)
  41. return request
  42. }
  43. func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
  44. request := make([]byte, 0, 1024)
  45. request = append(request, 0, 0, 0)
  46. request = appendAddress(request, address)
  47. request = append(request, payload...)
  48. return request
  49. }
  50. func setUpV2Ray() (uint16, error) {
  51. id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  52. if err != nil {
  53. return 0, err
  54. }
  55. id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e")
  56. if err != nil {
  57. return 0, err
  58. }
  59. users := []*vmessjson.ConfigUser{
  60. &vmessjson.ConfigUser{Id: id1},
  61. &vmessjson.ConfigUser{Id: id2},
  62. }
  63. portB := v2nettesting.PickPort()
  64. configB := mocks.Config{
  65. PortValue: portB,
  66. InboundConfigValue: &mocks.ConnectionConfig{
  67. ProtocolValue: "vmess",
  68. SettingsValue: &vmessjson.Inbound{
  69. AllowedClients: users,
  70. },
  71. },
  72. OutboundConfigValue: &mocks.ConnectionConfig{
  73. ProtocolValue: "freedom",
  74. SettingsValue: nil,
  75. },
  76. }
  77. pointB, err := point.NewPoint(&configB)
  78. if err != nil {
  79. return 0, err
  80. }
  81. err = pointB.Start()
  82. if err != nil {
  83. return 0, err
  84. }
  85. portA := v2nettesting.PickPort()
  86. configA := mocks.Config{
  87. PortValue: portA,
  88. InboundConfigValue: &mocks.ConnectionConfig{
  89. ProtocolValue: "socks",
  90. SettingsValue: &socksjson.SocksConfig{
  91. AuthMethod: "noauth",
  92. UDPEnabled: true,
  93. HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
  94. },
  95. },
  96. OutboundConfigValue: &mocks.ConnectionConfig{
  97. ProtocolValue: "vmess",
  98. SettingsValue: &vmessjson.Outbound{
  99. []*vmessjson.ConfigTarget{
  100. &vmessjson.ConfigTarget{
  101. Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
  102. Users: users,
  103. },
  104. },
  105. },
  106. },
  107. }
  108. pointA, err := point.NewPoint(&configA)
  109. if err != nil {
  110. return 0, err
  111. }
  112. err = pointA.Start()
  113. if err != nil {
  114. return 0, err
  115. }
  116. return portA, nil
  117. }