passthrough_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. psiphon.SetEmitDiagnosticNotices(true, true)
  44. // Run passthrough web server
  45. webServerCertificate, webServerPrivateKey, err := common.GenerateWebServerCertificate("example.org")
  46. if err != nil {
  47. t.Fatalf("common.GenerateWebServerCertificate failed: %s", err)
  48. }
  49. webListener, err := net.Listen("tcp", "127.0.0.1:0")
  50. if err != nil {
  51. t.Fatalf("net.Listen failed: %s", err)
  52. }
  53. defer webListener.Close()
  54. webCertificate, err := tls.X509KeyPair(
  55. []byte(webServerCertificate),
  56. []byte(webServerPrivateKey))
  57. if err != nil {
  58. t.Fatalf("tls.X509KeyPair failed: %s", err)
  59. }
  60. webListener = tls.NewListener(webListener, &tls.Config{
  61. Certificates: []tls.Certificate{webCertificate},
  62. })
  63. webServerAddress := webListener.Addr().String()
  64. webResponseBody := []byte(prng.HexString(32))
  65. webServer := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  66. w.Write(webResponseBody)
  67. })
  68. go func() {
  69. http.Serve(webListener, webServer)
  70. }()
  71. // Run Psiphon server
  72. tunnelProtocol := protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  73. generateConfigParams := &GenerateConfigParams{
  74. ServerIPAddress: "127.0.0.1",
  75. EnableSSHAPIRequests: true,
  76. WebServerPort: 8000,
  77. TunnelProtocolPorts: map[string]int{tunnelProtocol: 4000},
  78. }
  79. serverConfigJSON, _, _, _, encodedServerEntry, err := GenerateConfig(generateConfigParams)
  80. if err != nil {
  81. t.Fatalf("error generating server config: %s", err)
  82. }
  83. var serverConfig map[string]interface{}
  84. json.Unmarshal(serverConfigJSON, &serverConfig)
  85. serverConfig["LogFilename"] = filepath.Join(testDataDirName, "psiphond.log")
  86. serverConfig["LogLevel"] = "debug"
  87. serverConfig["TunnelProtocolPassthroughAddresses"] = map[string]string{tunnelProtocol: webServerAddress}
  88. serverConfigJSON, _ = json.Marshal(serverConfig)
  89. serverWaitGroup := new(sync.WaitGroup)
  90. serverWaitGroup.Add(1)
  91. go func() {
  92. defer serverWaitGroup.Done()
  93. err := RunServices(serverConfigJSON)
  94. if err != nil {
  95. t.Errorf("error running server: %s", err)
  96. }
  97. }()
  98. defer func() {
  99. p, _ := os.FindProcess(os.Getpid())
  100. p.Signal(os.Interrupt)
  101. serverWaitGroup.Wait()
  102. }()
  103. // TODO: monitor logs for more robust wait-until-loaded.
  104. time.Sleep(1 * time.Second)
  105. // Test: normal client connects successfully
  106. clientConfigJSON := fmt.Sprintf(`
  107. {
  108. "DataRootDirectory" : "%s",
  109. "ClientPlatform" : "Windows",
  110. "ClientVersion" : "0",
  111. "SponsorId" : "0",
  112. "PropagationChannelId" : "0",
  113. "TargetServerEntry" : "%s"
  114. }`, testDataDirName, string(encodedServerEntry))
  115. clientConfig, err := psiphon.LoadConfig([]byte(clientConfigJSON))
  116. if err != nil {
  117. t.Fatalf("error processing configuration file: %s", err)
  118. }
  119. err = clientConfig.Commit(false)
  120. if err != nil {
  121. t.Fatalf("error committing configuration file: %s", err)
  122. }
  123. err = psiphon.OpenDataStore(clientConfig)
  124. if err != nil {
  125. t.Fatalf("error initializing client datastore: %s", err)
  126. }
  127. defer psiphon.CloseDataStore()
  128. controller, err := psiphon.NewController(clientConfig)
  129. if err != nil {
  130. t.Fatalf("error creating client controller: %s", err)
  131. }
  132. tunnelEstablished := make(chan struct{}, 1)
  133. psiphon.SetNoticeWriter(psiphon.NewNoticeReceiver(
  134. func(notice []byte) {
  135. noticeType, payload, err := psiphon.GetNotice(notice)
  136. if err != nil {
  137. return
  138. }
  139. if noticeType == "Tunnels" {
  140. count := int(payload["count"].(float64))
  141. if count >= 1 {
  142. tunnelEstablished <- struct{}{}
  143. }
  144. }
  145. }))
  146. ctx, cancelFunc := context.WithCancel(context.Background())
  147. controllerWaitGroup := new(sync.WaitGroup)
  148. controllerWaitGroup.Add(1)
  149. go func() {
  150. defer controllerWaitGroup.Done()
  151. controller.Run(ctx)
  152. }()
  153. <-tunnelEstablished
  154. cancelFunc()
  155. controllerWaitGroup.Wait()
  156. // Test: passthrough
  157. // Non-psiphon HTTPS request routed to passthrough web server
  158. verifiedCertificate := int32(0)
  159. httpClient := &http.Client{
  160. Transport: &http.Transport{
  161. TLSClientConfig: &tls.Config{
  162. InsecureSkipVerify: true,
  163. VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
  164. if len(rawCerts) < 1 {
  165. return errors.New("no certificate to verify")
  166. }
  167. if !bytes.Equal(rawCerts[0], []byte(webCertificate.Certificate[0])) {
  168. return errors.New("unexpected certificate")
  169. }
  170. atomic.StoreInt32(&verifiedCertificate, 1)
  171. return nil
  172. },
  173. },
  174. },
  175. }
  176. response, err := httpClient.Get("https://" + webServerAddress)
  177. if err != nil {
  178. t.Fatalf("http.Get failed: %s", err)
  179. }
  180. defer response.Body.Close()
  181. if atomic.LoadInt32(&verifiedCertificate) != 1 {
  182. t.Fatalf("certificate not verified")
  183. }
  184. if response.StatusCode != http.StatusOK {
  185. t.Fatalf("unexpected response.StatusCode: %d", response.StatusCode)
  186. }
  187. responseBody, err := ioutil.ReadAll(response.Body)
  188. if err != nil {
  189. t.Fatalf("ioutil.ReadAll failed: %s", err)
  190. }
  191. if !bytes.Equal(responseBody, webResponseBody) {
  192. t.Fatalf("unexpected responseBody: %s", string(responseBody))
  193. }
  194. }