httpupgrade_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package httpupgrade_test
  2. import (
  3. "context"
  4. "runtime"
  5. "testing"
  6. "time"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/common/protocol/tls/cert"
  10. "github.com/xtls/xray-core/testing/servers/tcp"
  11. "github.com/xtls/xray-core/transport/internet"
  12. . "github.com/xtls/xray-core/transport/internet/httpupgrade"
  13. "github.com/xtls/xray-core/transport/internet/stat"
  14. "github.com/xtls/xray-core/transport/internet/tls"
  15. )
  16. func Test_listenHTTPUpgradeAndDial(t *testing.T) {
  17. listenPort := tcp.PickPort()
  18. listen, err := ListenHTTPUpgrade(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  19. ProtocolName: "httpupgrade",
  20. ProtocolSettings: &Config{
  21. Path: "httpupgrade",
  22. },
  23. }, func(conn stat.Connection) {
  24. go func(c stat.Connection) {
  25. defer c.Close()
  26. var b [1024]byte
  27. c.SetReadDeadline(time.Now().Add(2 * time.Second))
  28. _, err := c.Read(b[:])
  29. if err != nil {
  30. return
  31. }
  32. common.Must2(c.Write([]byte("Response")))
  33. }(conn)
  34. })
  35. common.Must(err)
  36. ctx := context.Background()
  37. streamSettings := &internet.MemoryStreamConfig{
  38. ProtocolName: "httpupgrade",
  39. ProtocolSettings: &Config{Path: "httpupgrade"},
  40. }
  41. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  42. common.Must(err)
  43. _, err = conn.Write([]byte("Test connection 1"))
  44. common.Must(err)
  45. var b [1024]byte
  46. n, err := conn.Read(b[:])
  47. common.Must(err)
  48. if string(b[:n]) != "Response" {
  49. t.Error("response: ", string(b[:n]))
  50. }
  51. common.Must(conn.Close())
  52. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  53. common.Must(err)
  54. _, err = conn.Write([]byte("Test connection 2"))
  55. common.Must(err)
  56. n, err = conn.Read(b[:])
  57. common.Must(err)
  58. if string(b[:n]) != "Response" {
  59. t.Error("response: ", string(b[:n]))
  60. }
  61. common.Must(conn.Close())
  62. common.Must(listen.Close())
  63. }
  64. func Test_listenHTTPUpgradeAndDialWithHeaders(t *testing.T) {
  65. listenPort := tcp.PickPort()
  66. listen, err := ListenHTTPUpgrade(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  67. ProtocolName: "httpupgrade",
  68. ProtocolSettings: &Config{
  69. Path: "httpupgrade",
  70. Header: map[string]string{
  71. "User-Agent": "Mozilla",
  72. },
  73. },
  74. }, func(conn stat.Connection) {
  75. go func(c stat.Connection) {
  76. defer c.Close()
  77. var b [1024]byte
  78. c.SetReadDeadline(time.Now().Add(2 * time.Second))
  79. _, err := c.Read(b[:])
  80. if err != nil {
  81. return
  82. }
  83. common.Must2(c.Write([]byte("Response")))
  84. }(conn)
  85. })
  86. common.Must(err)
  87. ctx := context.Background()
  88. streamSettings := &internet.MemoryStreamConfig{
  89. ProtocolName: "httpupgrade",
  90. ProtocolSettings: &Config{Path: "httpupgrade"},
  91. }
  92. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  93. common.Must(err)
  94. _, err = conn.Write([]byte("Test connection 1"))
  95. common.Must(err)
  96. var b [1024]byte
  97. n, err := conn.Read(b[:])
  98. common.Must(err)
  99. if string(b[:n]) != "Response" {
  100. t.Error("response: ", string(b[:n]))
  101. }
  102. common.Must(conn.Close())
  103. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  104. common.Must(err)
  105. _, err = conn.Write([]byte("Test connection 2"))
  106. common.Must(err)
  107. n, err = conn.Read(b[:])
  108. common.Must(err)
  109. if string(b[:n]) != "Response" {
  110. t.Error("response: ", string(b[:n]))
  111. }
  112. common.Must(conn.Close())
  113. common.Must(listen.Close())
  114. }
  115. func TestDialWithRemoteAddr(t *testing.T) {
  116. listenPort := tcp.PickPort()
  117. listen, err := ListenHTTPUpgrade(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  118. ProtocolName: "httpupgrade",
  119. ProtocolSettings: &Config{
  120. Path: "httpupgrade",
  121. },
  122. }, func(conn stat.Connection) {
  123. go func(c stat.Connection) {
  124. defer c.Close()
  125. var b [1024]byte
  126. _, err := c.Read(b[:])
  127. // common.Must(err)
  128. if err != nil {
  129. return
  130. }
  131. _, err = c.Write([]byte(c.RemoteAddr().String()))
  132. common.Must(err)
  133. }(conn)
  134. })
  135. common.Must(err)
  136. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), &internet.MemoryStreamConfig{
  137. ProtocolName: "httpupgrade",
  138. ProtocolSettings: &Config{Path: "httpupgrade", Header: map[string]string{"X-Forwarded-For": "1.1.1.1"}},
  139. })
  140. common.Must(err)
  141. _, err = conn.Write([]byte("Test connection 1"))
  142. common.Must(err)
  143. var b [1024]byte
  144. n, err := conn.Read(b[:])
  145. common.Must(err)
  146. if string(b[:n]) != "1.1.1.1:0" {
  147. t.Error("response: ", string(b[:n]))
  148. }
  149. common.Must(listen.Close())
  150. }
  151. func Test_listenHTTPUpgradeAndDial_TLS(t *testing.T) {
  152. listenPort := tcp.PickPort()
  153. if runtime.GOARCH == "arm64" {
  154. return
  155. }
  156. start := time.Now()
  157. streamSettings := &internet.MemoryStreamConfig{
  158. ProtocolName: "httpupgrade",
  159. ProtocolSettings: &Config{
  160. Path: "httpupgrades",
  161. },
  162. SecurityType: "tls",
  163. SecuritySettings: &tls.Config{
  164. AllowInsecure: true,
  165. Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
  166. },
  167. }
  168. listen, err := ListenHTTPUpgrade(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
  169. go func() {
  170. _ = conn.Close()
  171. }()
  172. })
  173. common.Must(err)
  174. defer listen.Close()
  175. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  176. common.Must(err)
  177. _ = conn.Close()
  178. end := time.Now()
  179. if !end.Before(start.Add(time.Second * 5)) {
  180. t.Error("end: ", end, " start: ", start)
  181. }
  182. }