tls_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package scenarios
  2. import (
  3. "net"
  4. "path/filepath"
  5. "testing"
  6. "io/ioutil"
  7. "os"
  8. "time"
  9. "v2ray.com/core"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/protocol"
  12. "v2ray.com/core/common/serial"
  13. "v2ray.com/core/common/uuid"
  14. "v2ray.com/core/proxy/dokodemo"
  15. "v2ray.com/core/proxy/freedom"
  16. "v2ray.com/core/proxy/vmess"
  17. "v2ray.com/core/proxy/vmess/inbound"
  18. "v2ray.com/core/proxy/vmess/outbound"
  19. "v2ray.com/core/testing/assert"
  20. "v2ray.com/core/testing/servers/tcp"
  21. "v2ray.com/core/transport/internet"
  22. "v2ray.com/core/transport/internet/tls"
  23. )
  24. func mustReadFile(name string) []byte {
  25. content, err := ioutil.ReadFile(name)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return content
  30. }
  31. func TestSimpleTLSConnection(t *testing.T) {
  32. assert := assert.On(t)
  33. tcpServer := tcp.Server{
  34. MsgProcessor: xor,
  35. }
  36. dest, err := tcpServer.Start()
  37. assert.Error(err).IsNil()
  38. defer tcpServer.Close()
  39. userID := protocol.NewID(uuid.New())
  40. serverPort := pickPort()
  41. serverConfig := &core.Config{
  42. Inbound: []*core.InboundConnectionConfig{
  43. {
  44. PortRange: v2net.SinglePortRange(serverPort),
  45. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  46. Settings: serial.ToTypedMessage(&inbound.Config{
  47. User: []*protocol.User{
  48. {
  49. Account: serial.ToTypedMessage(&vmess.Account{
  50. Id: userID.String(),
  51. }),
  52. },
  53. },
  54. }),
  55. StreamSettings: &internet.StreamConfig{
  56. SecurityType: serial.GetMessageType(&tls.Config{}),
  57. SecuritySettings: []*serial.TypedMessage{
  58. serial.ToTypedMessage(&tls.Config{
  59. Certificate: []*tls.Certificate{
  60. {
  61. Certificate: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "cert.pem")),
  62. Key: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "key.pem")),
  63. },
  64. },
  65. }),
  66. },
  67. },
  68. },
  69. },
  70. Outbound: []*core.OutboundConnectionConfig{
  71. {
  72. Settings: serial.ToTypedMessage(&freedom.Config{}),
  73. },
  74. },
  75. }
  76. clientPort := pickPort()
  77. clientConfig := &core.Config{
  78. Inbound: []*core.InboundConnectionConfig{
  79. {
  80. PortRange: v2net.SinglePortRange(clientPort),
  81. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  82. Settings: serial.ToTypedMessage(&dokodemo.Config{
  83. Address: v2net.NewIPOrDomain(dest.Address),
  84. Port: uint32(dest.Port),
  85. NetworkList: &v2net.NetworkList{
  86. Network: []v2net.Network{v2net.Network_TCP},
  87. },
  88. }),
  89. },
  90. },
  91. Outbound: []*core.OutboundConnectionConfig{
  92. {
  93. Settings: serial.ToTypedMessage(&outbound.Config{
  94. Receiver: []*protocol.ServerEndpoint{
  95. {
  96. Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
  97. Port: uint32(serverPort),
  98. User: []*protocol.User{
  99. {
  100. Account: serial.ToTypedMessage(&vmess.Account{
  101. Id: userID.String(),
  102. }),
  103. },
  104. },
  105. },
  106. },
  107. }),
  108. StreamSettings: &internet.StreamConfig{
  109. SecurityType: serial.GetMessageType(&tls.Config{}),
  110. SecuritySettings: []*serial.TypedMessage{
  111. serial.ToTypedMessage(&tls.Config{
  112. AllowInsecure: true,
  113. }),
  114. },
  115. },
  116. },
  117. },
  118. }
  119. assert.Error(InitializeServerConfig(serverConfig)).IsNil()
  120. assert.Error(InitializeServerConfig(clientConfig)).IsNil()
  121. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  122. IP: []byte{127, 0, 0, 1},
  123. Port: int(clientPort),
  124. })
  125. assert.Error(err).IsNil()
  126. payload := "dokodemo request."
  127. nBytes, err := conn.Write([]byte(payload))
  128. assert.Error(err).IsNil()
  129. assert.Int(nBytes).Equals(len(payload))
  130. response := readFrom(conn, time.Second*2, len(payload))
  131. assert.Bytes(response).Equals(xor([]byte(payload)))
  132. assert.Error(conn.Close()).IsNil()
  133. CloseAllServers()
  134. }
  135. func TestTLSConnectionReuse(t *testing.T) {
  136. assert := assert.On(t)
  137. tcpServer := tcp.Server{
  138. MsgProcessor: xor,
  139. }
  140. dest, err := tcpServer.Start()
  141. assert.Error(err).IsNil()
  142. defer tcpServer.Close()
  143. userID := protocol.NewID(uuid.New())
  144. serverPort := pickPort()
  145. serverConfig := &core.Config{
  146. Inbound: []*core.InboundConnectionConfig{
  147. {
  148. PortRange: v2net.SinglePortRange(serverPort),
  149. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  150. Settings: serial.ToTypedMessage(&inbound.Config{
  151. User: []*protocol.User{
  152. {
  153. Account: serial.ToTypedMessage(&vmess.Account{
  154. Id: userID.String(),
  155. }),
  156. },
  157. },
  158. }),
  159. StreamSettings: &internet.StreamConfig{
  160. SecurityType: serial.GetMessageType(&tls.Config{}),
  161. SecuritySettings: []*serial.TypedMessage{
  162. serial.ToTypedMessage(&tls.Config{
  163. Certificate: []*tls.Certificate{
  164. {
  165. Certificate: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "cert.pem")),
  166. Key: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "key.pem")),
  167. },
  168. },
  169. }),
  170. },
  171. },
  172. },
  173. },
  174. Outbound: []*core.OutboundConnectionConfig{
  175. {
  176. Settings: serial.ToTypedMessage(&freedom.Config{}),
  177. },
  178. },
  179. }
  180. clientPort := pickPort()
  181. clientConfig := &core.Config{
  182. Inbound: []*core.InboundConnectionConfig{
  183. {
  184. PortRange: v2net.SinglePortRange(clientPort),
  185. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  186. Settings: serial.ToTypedMessage(&dokodemo.Config{
  187. Address: v2net.NewIPOrDomain(dest.Address),
  188. Port: uint32(dest.Port),
  189. NetworkList: &v2net.NetworkList{
  190. Network: []v2net.Network{v2net.Network_TCP},
  191. },
  192. }),
  193. },
  194. },
  195. Outbound: []*core.OutboundConnectionConfig{
  196. {
  197. Settings: serial.ToTypedMessage(&outbound.Config{
  198. Receiver: []*protocol.ServerEndpoint{
  199. {
  200. Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
  201. Port: uint32(serverPort),
  202. User: []*protocol.User{
  203. {
  204. Account: serial.ToTypedMessage(&vmess.Account{
  205. Id: userID.String(),
  206. }),
  207. },
  208. },
  209. },
  210. },
  211. }),
  212. StreamSettings: &internet.StreamConfig{
  213. SecurityType: serial.GetMessageType(&tls.Config{}),
  214. SecuritySettings: []*serial.TypedMessage{
  215. serial.ToTypedMessage(&tls.Config{
  216. AllowInsecure: true,
  217. }),
  218. },
  219. },
  220. },
  221. },
  222. }
  223. assert.Error(InitializeServerConfig(serverConfig)).IsNil()
  224. assert.Error(InitializeServerConfig(clientConfig)).IsNil()
  225. for i := 0; i < 5; i++ {
  226. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  227. IP: []byte{127, 0, 0, 1},
  228. Port: int(clientPort),
  229. })
  230. assert.Error(err).IsNil()
  231. payload := "dokodemo request."
  232. nBytes, err := conn.Write([]byte(payload))
  233. assert.Error(err).IsNil()
  234. assert.Int(nBytes).Equals(len(payload))
  235. response := readFrom(conn, time.Second*2, len(payload))
  236. assert.Bytes(response).Equals(xor([]byte(payload)))
  237. assert.Error(conn.Close()).IsNil()
  238. }
  239. time.Sleep(time.Second * 10)
  240. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  241. IP: []byte{127, 0, 0, 1},
  242. Port: int(clientPort),
  243. })
  244. assert.Error(err).IsNil()
  245. payload := "dokodemo request."
  246. nBytes, err := conn.Write([]byte(payload))
  247. assert.Error(err).IsNil()
  248. assert.Int(nBytes).Equals(len(payload))
  249. response := readFrom(conn, time.Second*2, len(payload))
  250. assert.Bytes(response).Equals(xor([]byte(payload)))
  251. assert.Error(conn.Close()).IsNil()
  252. CloseAllServers()
  253. }