passthrough_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2020, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "bytes"
  22. "context"
  23. "crypto/tls"
  24. "crypto/x509"
  25. "encoding/json"
  26. "errors"
  27. "fmt"
  28. "io/ioutil"
  29. "net"
  30. "net/http"
  31. "os"
  32. "path/filepath"
  33. "sync"
  34. "sync/atomic"
  35. "testing"
  36. "time"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  39. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  40. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  41. )
  42. func TestPassthrough(t *testing.T) {
  43. testPassthrough(t, false)
  44. }
  45. func TestLegacyPassthrough(t *testing.T) {
  46. testPassthrough(t, true)
  47. }
  48. func testPassthrough(t *testing.T, legacy bool) {
  49. psiphon.SetEmitDiagnosticNotices(true, true)
  50. // Run passthrough web server
  51. webServerCertificate, webServerPrivateKey, err := common.GenerateWebServerCertificate("example.org")
  52. if err != nil {
  53. t.Fatalf("common.GenerateWebServerCertificate failed: %s", err)
  54. }
  55. webListener, err := net.Listen("tcp", "127.0.0.1:0")
  56. if err != nil {
  57. t.Fatalf("net.Listen failed: %s", err)
  58. }
  59. defer webListener.Close()
  60. webCertificate, err := tls.X509KeyPair(
  61. []byte(webServerCertificate),
  62. []byte(webServerPrivateKey))
  63. if err != nil {
  64. t.Fatalf("tls.X509KeyPair failed: %s", err)
  65. }
  66. webListener = tls.NewListener(webListener, &tls.Config{
  67. Certificates: []tls.Certificate{webCertificate},
  68. })
  69. webServerAddress := webListener.Addr().String()
  70. webResponseBody := []byte(prng.HexString(32))
  71. webServer := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  72. w.Write(webResponseBody)
  73. })
  74. go func() {
  75. http.Serve(webListener, webServer)
  76. }()
  77. // Run Psiphon server
  78. tunnelProtocol := protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  79. generateConfigParams := &GenerateConfigParams{
  80. ServerIPAddress: "127.0.0.1",
  81. EnableSSHAPIRequests: true,
  82. WebServerPort: 8000,
  83. TunnelProtocolPorts: map[string]int{tunnelProtocol: 4000},
  84. Passthrough: true,
  85. LegacyPassthrough: legacy,
  86. }
  87. serverConfigJSON, _, _, _, encodedServerEntry, err := GenerateConfig(generateConfigParams)
  88. if err != nil {
  89. t.Fatalf("error generating server config: %s", err)
  90. }
  91. var serverConfig map[string]interface{}
  92. json.Unmarshal(serverConfigJSON, &serverConfig)
  93. serverConfig["LogFilename"] = filepath.Join(testDataDirName, "psiphond.log")
  94. serverConfig["LogLevel"] = "debug"
  95. serverConfig["TunnelProtocolPassthroughAddresses"] = map[string]string{tunnelProtocol: webServerAddress}
  96. serverConfigJSON, _ = json.Marshal(serverConfig)
  97. serverWaitGroup := new(sync.WaitGroup)
  98. serverWaitGroup.Add(1)
  99. go func() {
  100. defer serverWaitGroup.Done()
  101. err := RunServices(serverConfigJSON)
  102. if err != nil {
  103. t.Errorf("error running server: %s", err)
  104. }
  105. }()
  106. defer func() {
  107. p, _ := os.FindProcess(os.Getpid())
  108. p.Signal(os.Interrupt)
  109. serverWaitGroup.Wait()
  110. }()
  111. // TODO: monitor logs for more robust wait-until-loaded.
  112. time.Sleep(1 * time.Second)
  113. // Test: normal client connects successfully
  114. clientConfigJSON := fmt.Sprintf(`
  115. {
  116. "DataRootDirectory" : "%s",
  117. "ClientPlatform" : "Windows",
  118. "ClientVersion" : "0",
  119. "SponsorId" : "0",
  120. "PropagationChannelId" : "0",
  121. "TargetServerEntry" : "%s"
  122. }`, testDataDirName, string(encodedServerEntry))
  123. clientConfig, err := psiphon.LoadConfig([]byte(clientConfigJSON))
  124. if err != nil {
  125. t.Fatalf("error processing configuration file: %s", err)
  126. }
  127. err = clientConfig.Commit(false)
  128. if err != nil {
  129. t.Fatalf("error committing configuration file: %s", err)
  130. }
  131. err = psiphon.OpenDataStore(clientConfig)
  132. if err != nil {
  133. t.Fatalf("error initializing client datastore: %s", err)
  134. }
  135. defer psiphon.CloseDataStore()
  136. controller, err := psiphon.NewController(clientConfig)
  137. if err != nil {
  138. t.Fatalf("error creating client controller: %s", err)
  139. }
  140. tunnelEstablished := make(chan struct{}, 1)
  141. psiphon.SetNoticeWriter(psiphon.NewNoticeReceiver(
  142. func(notice []byte) {
  143. noticeType, payload, err := psiphon.GetNotice(notice)
  144. if err != nil {
  145. return
  146. }
  147. if noticeType == "Tunnels" {
  148. count := int(payload["count"].(float64))
  149. if count >= 1 {
  150. tunnelEstablished <- struct{}{}
  151. }
  152. }
  153. }))
  154. ctx, cancelFunc := context.WithCancel(context.Background())
  155. controllerWaitGroup := new(sync.WaitGroup)
  156. controllerWaitGroup.Add(1)
  157. go func() {
  158. defer controllerWaitGroup.Done()
  159. controller.Run(ctx)
  160. }()
  161. <-tunnelEstablished
  162. cancelFunc()
  163. controllerWaitGroup.Wait()
  164. // Test: passthrough
  165. // Non-psiphon HTTPS request routed to passthrough web server
  166. verifiedCertificate := int32(0)
  167. httpClient := &http.Client{
  168. Transport: &http.Transport{
  169. TLSClientConfig: &tls.Config{
  170. InsecureSkipVerify: true,
  171. VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
  172. if len(rawCerts) < 1 {
  173. return errors.New("no certificate to verify")
  174. }
  175. if !bytes.Equal(rawCerts[0], []byte(webCertificate.Certificate[0])) {
  176. return errors.New("unexpected certificate")
  177. }
  178. atomic.StoreInt32(&verifiedCertificate, 1)
  179. return nil
  180. },
  181. },
  182. },
  183. }
  184. response, err := httpClient.Get("https://127.0.0.1:4000")
  185. if err != nil {
  186. t.Fatalf("http.Get failed: %s", err)
  187. }
  188. defer response.Body.Close()
  189. if atomic.LoadInt32(&verifiedCertificate) != 1 {
  190. t.Fatalf("certificate not verified")
  191. }
  192. if response.StatusCode != http.StatusOK {
  193. t.Fatalf("unexpected response.StatusCode: %d", response.StatusCode)
  194. }
  195. responseBody, err := ioutil.ReadAll(response.Body)
  196. if err != nil {
  197. t.Fatalf("ioutil.ReadAll failed: %s", err)
  198. }
  199. if !bytes.Equal(responseBody, webResponseBody) {
  200. t.Fatalf("unexpected responseBody: %s", string(responseBody))
  201. }
  202. }