passthrough_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. TunnelProtocolPorts: map[string]int{tunnelProtocol: 4000},
  82. Passthrough: true,
  83. LegacyPassthrough: legacy,
  84. }
  85. serverConfigJSON, _, _, _, encodedServerEntry, err := GenerateConfig(generateConfigParams)
  86. if err != nil {
  87. t.Fatalf("error generating server config: %s", err)
  88. }
  89. var serverConfig map[string]interface{}
  90. json.Unmarshal(serverConfigJSON, &serverConfig)
  91. serverConfig["LogFilename"] = filepath.Join(testDataDirName, "psiphond.log")
  92. serverConfig["LogLevel"] = "debug"
  93. serverConfig["TunnelProtocolPassthroughAddresses"] = map[string]string{tunnelProtocol: webServerAddress}
  94. serverConfigJSON, _ = json.Marshal(serverConfig)
  95. serverWaitGroup := new(sync.WaitGroup)
  96. serverWaitGroup.Add(1)
  97. go func() {
  98. defer serverWaitGroup.Done()
  99. err := RunServices(serverConfigJSON)
  100. if err != nil {
  101. t.Errorf("error running server: %s", err)
  102. }
  103. }()
  104. defer func() {
  105. p, _ := os.FindProcess(os.Getpid())
  106. p.Signal(os.Interrupt)
  107. serverWaitGroup.Wait()
  108. }()
  109. // TODO: monitor logs for more robust wait-until-loaded.
  110. time.Sleep(1 * time.Second)
  111. // Test: normal client connects successfully
  112. clientConfigJSON := fmt.Sprintf(`
  113. {
  114. "DataRootDirectory" : "%s",
  115. "ClientPlatform" : "Windows",
  116. "ClientVersion" : "0",
  117. "SponsorId" : "0000000000000000",
  118. "PropagationChannelId" : "0000000000000000",
  119. "TargetServerEntry" : "%s"
  120. }`, testDataDirName, string(encodedServerEntry))
  121. clientConfig, err := psiphon.LoadConfig([]byte(clientConfigJSON))
  122. if err != nil {
  123. t.Fatalf("error processing configuration file: %s", err)
  124. }
  125. err = clientConfig.Commit(false)
  126. if err != nil {
  127. t.Fatalf("error committing configuration file: %s", err)
  128. }
  129. err = psiphon.OpenDataStore(clientConfig)
  130. if err != nil {
  131. t.Fatalf("error initializing client datastore: %s", err)
  132. }
  133. defer psiphon.CloseDataStore()
  134. controller, err := psiphon.NewController(clientConfig)
  135. if err != nil {
  136. t.Fatalf("error creating client controller: %s", err)
  137. }
  138. tunnelEstablished := make(chan struct{}, 1)
  139. psiphon.SetNoticeWriter(psiphon.NewNoticeReceiver(
  140. func(notice []byte) {
  141. noticeType, payload, err := psiphon.GetNotice(notice)
  142. if err != nil {
  143. return
  144. }
  145. if noticeType == "Tunnels" {
  146. count := int(payload["count"].(float64))
  147. if count >= 1 {
  148. tunnelEstablished <- struct{}{}
  149. }
  150. }
  151. }))
  152. ctx, cancelFunc := context.WithCancel(context.Background())
  153. controllerWaitGroup := new(sync.WaitGroup)
  154. controllerWaitGroup.Add(1)
  155. go func() {
  156. defer controllerWaitGroup.Done()
  157. controller.Run(ctx)
  158. }()
  159. <-tunnelEstablished
  160. cancelFunc()
  161. controllerWaitGroup.Wait()
  162. // Test: passthrough
  163. // Non-psiphon HTTPS request routed to passthrough web server
  164. verifiedCertificate := int32(0)
  165. httpClient := &http.Client{
  166. Transport: &http.Transport{
  167. TLSClientConfig: &tls.Config{
  168. InsecureSkipVerify: true,
  169. VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
  170. if len(rawCerts) < 1 {
  171. return errors.New("no certificate to verify")
  172. }
  173. if !bytes.Equal(rawCerts[0], []byte(webCertificate.Certificate[0])) {
  174. return errors.New("unexpected certificate")
  175. }
  176. atomic.StoreInt32(&verifiedCertificate, 1)
  177. return nil
  178. },
  179. },
  180. },
  181. }
  182. response, err := httpClient.Get("https://127.0.0.1:4000")
  183. if err != nil {
  184. t.Fatalf("http.Get failed: %s", err)
  185. }
  186. defer response.Body.Close()
  187. if atomic.LoadInt32(&verifiedCertificate) != 1 {
  188. t.Fatalf("certificate not verified")
  189. }
  190. if response.StatusCode != http.StatusOK {
  191. t.Fatalf("unexpected response.StatusCode: %d", response.StatusCode)
  192. }
  193. responseBody, err := ioutil.ReadAll(response.Body)
  194. if err != nil {
  195. t.Fatalf("ioutil.ReadAll failed: %s", err)
  196. }
  197. if !bytes.Equal(responseBody, webResponseBody) {
  198. t.Fatalf("unexpected responseBody: %s", string(responseBody))
  199. }
  200. }