| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245 |
- /*
- * Copyright (c) 2016, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package server
- import (
- "context"
- "encoding/json"
- "errors"
- "flag"
- "fmt"
- "io/ioutil"
- "net"
- "net/http"
- "net/url"
- "os"
- "path/filepath"
- "strconv"
- "sync"
- "syscall"
- "testing"
- "time"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/accesscontrol"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
- "golang.org/x/net/proxy"
- )
- var serverIPAddress, testDataDirName string
- var mockWebServerURL, mockWebServerExpectedResponse string
- var mockWebServerPort = 8080
- func TestMain(m *testing.M) {
- flag.Parse()
- var err error
- for _, interfaceName := range []string{"eth0", "en0"} {
- var serverIPv4Address, serverIPv6Address net.IP
- serverIPv4Address, serverIPv6Address, err = common.GetInterfaceIPAddresses(interfaceName)
- if err == nil {
- if serverIPv4Address != nil {
- serverIPAddress = serverIPv4Address.String()
- } else {
- serverIPAddress = serverIPv6Address.String()
- }
- break
- }
- }
- if err != nil {
- fmt.Printf("error getting server IP address: %s", err)
- os.Exit(1)
- }
- testDataDirName, err = ioutil.TempDir("", "psiphon-server-test")
- if err != nil {
- fmt.Printf("TempDir failed: %s\n", err)
- os.Exit(1)
- }
- defer os.RemoveAll(testDataDirName)
- os.Remove(filepath.Join(testDataDirName, psiphon.DATA_STORE_FILENAME))
- psiphon.SetEmitDiagnosticNotices(true)
- mockWebServerURL, mockWebServerExpectedResponse = runMockWebServer()
- os.Exit(m.Run())
- }
- func runMockWebServer() (string, string) {
- responseBody, _ := common.MakeSecureRandomStringHex(100000)
- serveMux := http.NewServeMux()
- serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte(responseBody))
- })
- webServerAddress := fmt.Sprintf("%s:%d", serverIPAddress, mockWebServerPort)
- server := &http.Server{
- Addr: webServerAddress,
- Handler: serveMux,
- }
- go func() {
- err := server.ListenAndServe()
- if err != nil {
- fmt.Printf("error running mock web server: %s\n", err)
- os.Exit(1)
- }
- }()
- // TODO: properly synchronize with web server readiness
- time.Sleep(1 * time.Second)
- return fmt.Sprintf("http://%s/", webServerAddress), responseBody
- }
- // Note: not testing fronting meek protocols, which client is
- // hard-wired to except running on privileged ports 80 and 443.
- func TestSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "SSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestUnfrontedMeek(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestUnfrontedMeekHTTPS(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-HTTPS-OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestUnfrontedMeekSessionTicket(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-SESSION-TICKET-OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestQUICOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "QUIC-OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestWebTransportAPIRequests(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: false,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: false,
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestHotReload(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestDefaultSessionID(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: true,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestDenyTrafficRules(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: false,
- denyTrafficRules: true,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestOmitAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestNoAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: false,
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestUnusedAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: false,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- })
- }
- func TestTCPOnlySLOK(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: false,
- })
- }
- func TestUDPOnlySLOK(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: false,
- doDefaultSponsorID: false,
- denyTrafficRules: false,
- requireAuthorization: true,
- omitAuthorization: false,
- doTunneledWebRequest: false,
- doTunneledNTPRequest: true,
- })
- }
- type runServerConfig struct {
- tunnelProtocol string
- enableSSHAPIRequests bool
- doHotReload bool
- doDefaultSponsorID bool
- denyTrafficRules bool
- requireAuthorization bool
- omitAuthorization bool
- doTunneledWebRequest bool
- doTunneledNTPRequest bool
- }
- func runServer(t *testing.T, runConfig *runServerConfig) {
- // configure authorized access
- accessType := "test-access-type"
- accessControlSigningKey, accessControlVerificationKey, err := accesscontrol.NewKeyPair(accessType)
- if err != nil {
- t.Fatalf("error creating access control key pair: %s", err)
- }
- accessControlVerificationKeyRing := accesscontrol.VerificationKeyRing{
- Keys: []*accesscontrol.VerificationKey{accessControlVerificationKey},
- }
- var authorizationID [32]byte
- clientAuthorization, err := accesscontrol.IssueAuthorization(
- accessControlSigningKey,
- authorizationID[:],
- time.Now().Add(1*time.Hour))
- if err != nil {
- t.Fatalf("error issuing authorization: %s", err)
- }
- // Enable tactics when the test protocol is meek. Both the client and the
- // server will be configured to support tactics. The client config will be
- // set with a nonfunctional config so that the tactics request must
- // succeed, overriding the nonfunctional values, for the tunnel to
- // establish.
- doTactics := protocol.TunnelProtocolUsesMeek(runConfig.tunnelProtocol)
- // All servers require a tactics config with valid keys.
- tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey, err :=
- tactics.GenerateKeys()
- if err != nil {
- t.Fatalf("error generating tactics keys: %s", err)
- }
- // create a server
- generateConfigParams := &GenerateConfigParams{
- ServerIPAddress: serverIPAddress,
- EnableSSHAPIRequests: runConfig.enableSSHAPIRequests,
- WebServerPort: 8000,
- TunnelProtocolPorts: map[string]int{runConfig.tunnelProtocol: 4000},
- }
- if doTactics {
- generateConfigParams.TacticsRequestPublicKey = tacticsRequestPublicKey
- generateConfigParams.TacticsRequestObfuscatedKey = tacticsRequestObfuscatedKey
- }
- serverConfigJSON, _, _, _, encodedServerEntry, err := GenerateConfig(generateConfigParams)
- if err != nil {
- t.Fatalf("error generating server config: %s", err)
- }
- // customize server config
- // Pave psinet with random values to test handshake homepages.
- psinetFilename := filepath.Join(testDataDirName, "psinet.json")
- sponsorID, expectedHomepageURL := pavePsinetDatabaseFile(
- t, runConfig.doDefaultSponsorID, psinetFilename)
- // Pave OSL config for SLOK testing
- oslConfigFilename := filepath.Join(testDataDirName, "osl_config.json")
- propagationChannelID := paveOSLConfigFile(t, oslConfigFilename)
- // Pave traffic rules file which exercises handshake parameter filtering. Client
- // must handshake with specified sponsor ID in order to allow ports for tunneled
- // requests.
- trafficRulesFilename := filepath.Join(testDataDirName, "traffic_rules.json")
- paveTrafficRulesFile(
- t, trafficRulesFilename, propagationChannelID, accessType,
- runConfig.requireAuthorization, runConfig.denyTrafficRules)
- var tacticsConfigFilename string
- // Only pave the tactics config when tactics are required. This exercises the
- // case where the tactics config is omitted.
- if doTactics {
- tacticsConfigFilename = filepath.Join(testDataDirName, "tactics_config.json")
- paveTacticsConfigFile(
- t, tacticsConfigFilename,
- tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey,
- runConfig.tunnelProtocol,
- propagationChannelID)
- }
- var serverConfig map[string]interface{}
- json.Unmarshal(serverConfigJSON, &serverConfig)
- serverConfig["GeoIPDatabaseFilename"] = ""
- serverConfig["PsinetDatabaseFilename"] = psinetFilename
- serverConfig["TrafficRulesFilename"] = trafficRulesFilename
- serverConfig["OSLConfigFilename"] = oslConfigFilename
- if doTactics {
- serverConfig["TacticsConfigFilename"] = tacticsConfigFilename
- }
- serverConfig["LogFilename"] = filepath.Join(testDataDirName, "psiphond.log")
- serverConfig["LogLevel"] = "debug"
- serverConfig["AccessControlVerificationKeyRing"] = accessControlVerificationKeyRing
- // Set this parameter so at least the semaphore functions are called.
- // TODO: test that the concurrency limit is correctly enforced.
- serverConfig["MaxConcurrentSSHHandshakes"] = 1
- // Exercise this option.
- serverConfig["PeriodicGarbageCollectionSeconds"] = 1
- serverConfigJSON, _ = json.Marshal(serverConfig)
- // run server
- serverWaitGroup := new(sync.WaitGroup)
- serverWaitGroup.Add(1)
- go func() {
- defer serverWaitGroup.Done()
- err := RunServices(serverConfigJSON)
- if err != nil {
- // TODO: wrong goroutine for t.FatalNow()
- t.Fatalf("error running server: %s", err)
- }
- }()
- defer func() {
- // Test: orderly server shutdown
- p, _ := os.FindProcess(os.Getpid())
- p.Signal(os.Interrupt)
- shutdownTimeout := time.NewTimer(5 * time.Second)
- shutdownOk := make(chan struct{}, 1)
- go func() {
- serverWaitGroup.Wait()
- shutdownOk <- *new(struct{})
- }()
- select {
- case <-shutdownOk:
- case <-shutdownTimeout.C:
- t.Fatalf("server shutdown timeout exceeded")
- }
- }()
- // TODO: monitor logs for more robust wait-until-loaded
- time.Sleep(1 * time.Second)
- // Test: hot reload (of psinet and traffic rules)
- if runConfig.doHotReload {
- // Pave new config files with different random values.
- sponsorID, expectedHomepageURL = pavePsinetDatabaseFile(
- t, runConfig.doDefaultSponsorID, psinetFilename)
- propagationChannelID = paveOSLConfigFile(t, oslConfigFilename)
- paveTrafficRulesFile(
- t, trafficRulesFilename, propagationChannelID, accessType,
- runConfig.requireAuthorization, runConfig.denyTrafficRules)
- p, _ := os.FindProcess(os.Getpid())
- p.Signal(syscall.SIGUSR1)
- // TODO: monitor logs for more robust wait-until-reloaded
- time.Sleep(1 * time.Second)
- // After reloading psinet, the new sponsorID/expectedHomepageURL
- // should be active, as tested in the client "Homepage" notice
- // handler below.
- }
- // Exercise server_load logging
- p, _ := os.FindProcess(os.Getpid())
- p.Signal(syscall.SIGUSR2)
- // connect to server with client
- // TODO: currently, TargetServerEntry only works with one tunnel
- numTunnels := 1
- localSOCKSProxyPort := 1081
- localHTTPProxyPort := 8081
- jsonNetworkID := ""
- if doTactics {
- // Use a distinct prefix for network ID for each test run to
- // ensure tactics from different runs don't apply; this is
- // a workaround for the singleton datastore.
- prefix := time.Now().String()
- jsonNetworkID = fmt.Sprintf(`,"NetworkID" : "%s-%s"`, prefix, "NETWORK1")
- }
- clientConfigJSON := fmt.Sprintf(`
- {
- "ClientPlatform" : "Windows",
- "ClientVersion" : "0",
- "SponsorId" : "0",
- "PropagationChannelId" : "0",
- "DisableRemoteServerListFetcher" : true,
- "UseIndistinguishableTLS" : true,
- "EstablishTunnelPausePeriodSeconds" : 1,
- "ConnectionWorkerPoolSize" : %d,
- "TunnelProtocols" : ["%s"]
- %s
- }`, numTunnels, runConfig.tunnelProtocol, jsonNetworkID)
- clientConfig, err := psiphon.LoadConfig([]byte(clientConfigJSON))
- if err != nil {
- t.Fatalf("error processing configuration file: %s", err)
- }
- clientConfig.DataStoreDirectory = testDataDirName
- if !runConfig.doDefaultSponsorID {
- clientConfig.SponsorId = sponsorID
- }
- clientConfig.PropagationChannelId = propagationChannelID
- clientConfig.TunnelPoolSize = numTunnels
- clientConfig.TargetServerEntry = string(encodedServerEntry)
- clientConfig.LocalSocksProxyPort = localSOCKSProxyPort
- clientConfig.LocalHttpProxyPort = localHTTPProxyPort
- clientConfig.EmitSLOKs = true
- if !runConfig.omitAuthorization {
- clientConfig.Authorizations = []string{clientAuthorization}
- }
- err = clientConfig.Commit()
- if err != nil {
- t.Fatalf("error committing configuration file: %s", err)
- }
- if doTactics {
- // Configure nonfunctional values that must be overridden by tactics.
- applyParameters := make(map[string]interface{})
- applyParameters[parameters.TunnelConnectTimeout] = "1s"
- applyParameters[parameters.TunnelRateLimits] = common.RateLimits{WriteBytesPerSecond: 1}
- err = clientConfig.SetClientParameters("", true, applyParameters)
- if err != nil {
- t.Fatalf("SetClientParameters failed: %s", err)
- }
- }
- err = psiphon.InitDataStore(clientConfig)
- if err != nil {
- t.Fatalf("error initializing client datastore: %s", err)
- }
- psiphon.DeleteSLOKs()
- controller, err := psiphon.NewController(clientConfig)
- if err != nil {
- t.Fatalf("error creating client controller: %s", err)
- }
- tunnelsEstablished := make(chan struct{}, 1)
- homepageReceived := make(chan struct{}, 1)
- slokSeeded := make(chan struct{}, 1)
- psiphon.SetNoticeWriter(psiphon.NewNoticeReceiver(
- func(notice []byte) {
- //fmt.Printf("%s\n", string(notice))
- noticeType, payload, err := psiphon.GetNotice(notice)
- if err != nil {
- return
- }
- switch noticeType {
- case "Tunnels":
- count := int(payload["count"].(float64))
- if count >= numTunnels {
- sendNotificationReceived(tunnelsEstablished)
- }
- case "Homepage":
- homepageURL := payload["url"].(string)
- if homepageURL != expectedHomepageURL {
- // TODO: wrong goroutine for t.FatalNow()
- t.Fatalf("unexpected homepage: %s", homepageURL)
- }
- sendNotificationReceived(homepageReceived)
- case "SLOKSeeded":
- sendNotificationReceived(slokSeeded)
- }
- }))
- ctx, cancelFunc := context.WithCancel(context.Background())
- controllerWaitGroup := new(sync.WaitGroup)
- controllerWaitGroup.Add(1)
- go func() {
- defer controllerWaitGroup.Done()
- controller.Run(ctx)
- }()
- defer func() {
- cancelFunc()
- shutdownTimeout := time.NewTimer(20 * time.Second)
- shutdownOk := make(chan struct{}, 1)
- go func() {
- controllerWaitGroup.Wait()
- shutdownOk <- *new(struct{})
- }()
- select {
- case <-shutdownOk:
- case <-shutdownTimeout.C:
- t.Fatalf("controller shutdown timeout exceeded")
- }
- }()
- // Test: tunnels must be established, and correct homepage
- // must be received, within 30 seconds
- timeoutSignal := make(chan struct{})
- go func() {
- timer := time.NewTimer(30 * time.Second)
- <-timer.C
- close(timeoutSignal)
- }()
- waitOnNotification(t, tunnelsEstablished, timeoutSignal, "tunnel establish timeout exceeded")
- waitOnNotification(t, homepageReceived, timeoutSignal, "homepage received timeout exceeded")
- expectTrafficFailure := runConfig.denyTrafficRules || (runConfig.omitAuthorization && runConfig.requireAuthorization)
- if runConfig.doTunneledWebRequest {
- // Test: tunneled web site fetch
- err = makeTunneledWebRequest(
- t, localHTTPProxyPort, mockWebServerURL, mockWebServerExpectedResponse)
- if err == nil {
- if expectTrafficFailure {
- t.Fatalf("unexpected tunneled web request success")
- }
- } else {
- if !expectTrafficFailure {
- t.Fatalf("tunneled web request failed: %s", err)
- }
- }
- }
- if runConfig.doTunneledNTPRequest {
- // Test: tunneled UDP packets
- udpgwServerAddress := serverConfig["UDPInterceptUdpgwServerAddress"].(string)
- err = makeTunneledNTPRequest(t, localSOCKSProxyPort, udpgwServerAddress)
- if err == nil {
- if expectTrafficFailure {
- t.Fatalf("unexpected tunneled NTP request success")
- }
- } else {
- if !expectTrafficFailure {
- t.Fatalf("tunneled NTP request failed: %s", err)
- }
- }
- }
- // Test: await SLOK payload
- if !expectTrafficFailure {
- time.Sleep(1 * time.Second)
- waitOnNotification(t, slokSeeded, timeoutSignal, "SLOK seeded timeout exceeded")
- numSLOKs := psiphon.CountSLOKs()
- if numSLOKs != expectedNumSLOKs {
- t.Fatalf("unexpected number of SLOKs: %d", numSLOKs)
- }
- }
- }
- func makeTunneledWebRequest(
- t *testing.T,
- localHTTPProxyPort int,
- requestURL, expectedResponseBody string) error {
- roundTripTimeout := 30 * time.Second
- proxyUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", localHTTPProxyPort))
- if err != nil {
- return fmt.Errorf("error initializing proxied HTTP request: %s", err)
- }
- httpClient := &http.Client{
- Transport: &http.Transport{
- Proxy: http.ProxyURL(proxyUrl),
- },
- Timeout: roundTripTimeout,
- }
- response, err := httpClient.Get(requestURL)
- if err != nil {
- return fmt.Errorf("error sending proxied HTTP request: %s", err)
- }
- body, err := ioutil.ReadAll(response.Body)
- if err != nil {
- return fmt.Errorf("error reading proxied HTTP response: %s", err)
- }
- response.Body.Close()
- if string(body) != expectedResponseBody {
- return fmt.Errorf("unexpected proxied HTTP response")
- }
- return nil
- }
- func makeTunneledNTPRequest(t *testing.T, localSOCKSProxyPort int, udpgwServerAddress string) error {
- timeout := 20 * time.Second
- var err error
- for _, testHostname := range []string{"time.google.com", "time.nist.gov", "pool.ntp.org"} {
- err = makeTunneledNTPRequestAttempt(t, testHostname, timeout, localSOCKSProxyPort, udpgwServerAddress)
- if err == nil {
- break
- }
- t.Logf("makeTunneledNTPRequestAttempt failed: %s", err)
- }
- return err
- }
- var nextUDPProxyPort = 7300
- func makeTunneledNTPRequestAttempt(
- t *testing.T, testHostname string, timeout time.Duration, localSOCKSProxyPort int, udpgwServerAddress string) error {
- nextUDPProxyPort++
- localUDPProxyAddress, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", nextUDPProxyPort))
- if err != nil {
- return fmt.Errorf("ResolveUDPAddr failed: %s", err)
- }
- // Note: this proxy is intended for this test only -- it only accepts a single connection,
- // handles it, and then terminates.
- localUDPProxy := func(destinationIP net.IP, destinationPort uint16, waitGroup *sync.WaitGroup) {
- if waitGroup != nil {
- defer waitGroup.Done()
- }
- destination := net.JoinHostPort(destinationIP.String(), strconv.Itoa(int(destinationPort)))
- serverUDPConn, err := net.ListenUDP("udp", localUDPProxyAddress)
- if err != nil {
- t.Logf("ListenUDP for %s failed: %s", destination, err)
- return
- }
- defer serverUDPConn.Close()
- udpgwPreambleSize := 11 // see writeUdpgwPreamble
- buffer := make([]byte, udpgwProtocolMaxMessageSize)
- packetSize, clientAddr, err := serverUDPConn.ReadFromUDP(
- buffer[udpgwPreambleSize:])
- if err != nil {
- t.Logf("serverUDPConn.Read for %s failed: %s", destination, err)
- return
- }
- socksProxyAddress := fmt.Sprintf("127.0.0.1:%d", localSOCKSProxyPort)
- dialer, err := proxy.SOCKS5("tcp", socksProxyAddress, nil, proxy.Direct)
- if err != nil {
- t.Logf("proxy.SOCKS5 for %s failed: %s", destination, err)
- return
- }
- socksTCPConn, err := dialer.Dial("tcp", udpgwServerAddress)
- if err != nil {
- t.Logf("dialer.Dial for %s failed: %s", destination, err)
- return
- }
- defer socksTCPConn.Close()
- flags := uint8(0)
- if destinationPort == 53 {
- flags = udpgwProtocolFlagDNS
- }
- err = writeUdpgwPreamble(
- udpgwPreambleSize,
- flags,
- 0,
- destinationIP,
- destinationPort,
- uint16(packetSize),
- buffer)
- if err != nil {
- t.Logf("writeUdpgwPreamble for %s failed: %s", destination, err)
- return
- }
- _, err = socksTCPConn.Write(buffer[0 : udpgwPreambleSize+packetSize])
- if err != nil {
- t.Logf("socksTCPConn.Write for %s failed: %s", destination, err)
- return
- }
- udpgwProtocolMessage, err := readUdpgwMessage(socksTCPConn, buffer)
- if err != nil {
- t.Logf("readUdpgwMessage for %s failed: %s", destination, err)
- return
- }
- _, err = serverUDPConn.WriteToUDP(udpgwProtocolMessage.packet, clientAddr)
- if err != nil {
- t.Logf("serverUDPConn.Write for %s failed: %s", destination, err)
- return
- }
- }
- // Tunneled DNS request
- waitGroup := new(sync.WaitGroup)
- waitGroup.Add(1)
- go localUDPProxy(
- net.IP(make([]byte, 4)), // ignored due to transparent DNS forwarding
- 53,
- waitGroup)
- // TODO: properly synchronize with local UDP proxy startup
- time.Sleep(1 * time.Second)
- clientUDPConn, err := net.DialUDP("udp", nil, localUDPProxyAddress)
- if err != nil {
- return fmt.Errorf("DialUDP failed: %s", err)
- }
- clientUDPConn.SetReadDeadline(time.Now().Add(timeout))
- clientUDPConn.SetWriteDeadline(time.Now().Add(timeout))
- addrs, _, err := psiphon.ResolveIP(testHostname, clientUDPConn)
- clientUDPConn.Close()
- if err == nil && (len(addrs) == 0 || len(addrs[0]) < 4) {
- err = errors.New("no address")
- }
- if err != nil {
- return fmt.Errorf("ResolveIP failed: %s", err)
- }
- waitGroup.Wait()
- // Tunneled NTP request
- waitGroup = new(sync.WaitGroup)
- waitGroup.Add(1)
- go localUDPProxy(
- addrs[0][len(addrs[0])-4:],
- 123,
- waitGroup)
- // TODO: properly synchronize with local UDP proxy startup
- time.Sleep(1 * time.Second)
- clientUDPConn, err = net.DialUDP("udp", nil, localUDPProxyAddress)
- if err != nil {
- return fmt.Errorf("DialUDP failed: %s", err)
- }
- clientUDPConn.SetReadDeadline(time.Now().Add(timeout))
- clientUDPConn.SetWriteDeadline(time.Now().Add(timeout))
- // NTP protocol code from: https://groups.google.com/d/msg/golang-nuts/FlcdMU5fkLQ/CAeoD9eqm-IJ
- ntpData := make([]byte, 48)
- ntpData[0] = 3<<3 | 3
- _, err = clientUDPConn.Write(ntpData)
- if err != nil {
- clientUDPConn.Close()
- return fmt.Errorf("NTP Write failed: %s", err)
- }
- _, err = clientUDPConn.Read(ntpData)
- if err != nil {
- clientUDPConn.Close()
- return fmt.Errorf("NTP Read failed: %s", err)
- }
- clientUDPConn.Close()
- var sec, frac uint64
- sec = uint64(ntpData[43]) | uint64(ntpData[42])<<8 | uint64(ntpData[41])<<16 | uint64(ntpData[40])<<24
- frac = uint64(ntpData[47]) | uint64(ntpData[46])<<8 | uint64(ntpData[45])<<16 | uint64(ntpData[44])<<24
- nsec := sec * 1e9
- nsec += (frac * 1e9) >> 32
- ntpNow := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nsec)).Local()
- now := time.Now()
- diff := ntpNow.Sub(now)
- if diff < 0 {
- diff = -diff
- }
- if diff > 1*time.Minute {
- return fmt.Errorf("Unexpected NTP time: %s; local time: %s", ntpNow, now)
- }
- waitGroup.Wait()
- return nil
- }
- func pavePsinetDatabaseFile(
- t *testing.T, useDefaultSponsorID bool, psinetFilename string) (string, string) {
- sponsorID, _ := common.MakeSecureRandomStringHex(8)
- fakeDomain, _ := common.MakeSecureRandomStringHex(4)
- fakePath, _ := common.MakeSecureRandomStringHex(4)
- expectedHomepageURL := fmt.Sprintf("https://%s.com/%s", fakeDomain, fakePath)
- psinetJSONFormat := `
- {
- "default_sponsor_id" : "%s",
- "sponsors": {
- "%s": {
- "home_pages": {
- "None": [
- {
- "region": null,
- "url": "%s"
- }
- ]
- }
- }
- }
- }
- `
- defaultSponsorID := ""
- if useDefaultSponsorID {
- defaultSponsorID = sponsorID
- }
- psinetJSON := fmt.Sprintf(
- psinetJSONFormat, defaultSponsorID, sponsorID, expectedHomepageURL)
- err := ioutil.WriteFile(psinetFilename, []byte(psinetJSON), 0600)
- if err != nil {
- t.Fatalf("error paving psinet database file: %s", err)
- }
- return sponsorID, expectedHomepageURL
- }
- func paveTrafficRulesFile(
- t *testing.T, trafficRulesFilename, propagationChannelID, accessType string,
- requireAuthorization, deny bool) {
- allowTCPPorts := fmt.Sprintf("%d", mockWebServerPort)
- allowUDPPorts := "53, 123"
- if deny {
- allowTCPPorts = "0"
- allowUDPPorts = "0"
- }
- authorizationFilterFormat := `,
- "AuthorizedAccessTypes" : ["%s"]
- `
- authorizationFilter := ""
- if requireAuthorization {
- authorizationFilter = fmt.Sprintf(authorizationFilterFormat, accessType)
- }
- trafficRulesJSONFormat := `
- {
- "DefaultRules" : {
- "RateLimits" : {
- "ReadBytesPerSecond": 16384,
- "WriteBytesPerSecond": 16384
- },
- "AllowTCPPorts" : [0],
- "AllowUDPPorts" : [0]
- },
- "FilteredRules" : [
- {
- "Filter" : {
- "HandshakeParameters" : {
- "propagation_channel_id" : ["%s"]
- }%s
- },
- "Rules" : {
- "RateLimits" : {
- "ReadUnthrottledBytes": 132352,
- "WriteUnthrottledBytes": 132352
- },
- "AllowTCPPorts" : [%s],
- "AllowUDPPorts" : [%s]
- }
- }
- ]
- }
- `
- trafficRulesJSON := fmt.Sprintf(
- trafficRulesJSONFormat, propagationChannelID, authorizationFilter, allowTCPPorts, allowUDPPorts)
- err := ioutil.WriteFile(trafficRulesFilename, []byte(trafficRulesJSON), 0600)
- if err != nil {
- t.Fatalf("error paving traffic rules file: %s", err)
- }
- }
- var expectedNumSLOKs = 3
- func paveOSLConfigFile(t *testing.T, oslConfigFilename string) string {
- oslConfigJSONFormat := `
- {
- "Schemes" : [
- {
- "Epoch" : "%s",
- "Regions" : [],
- "PropagationChannelIDs" : ["%s"],
- "MasterKey" : "wFuSbqU/pJ/35vRmoM8T9ys1PgDa8uzJps1Y+FNKa5U=",
- "SeedSpecs" : [
- {
- "ID" : "IXHWfVgWFkEKvgqsjmnJuN3FpaGuCzQMETya+DSQvsk=",
- "UpstreamSubnets" : ["0.0.0.0/0"],
- "Targets" :
- {
- "BytesRead" : 1,
- "BytesWritten" : 1,
- "PortForwardDurationNanoseconds" : 1
- }
- },
- {
- "ID" : "qvpIcORLE2Pi5TZmqRtVkEp+OKov0MhfsYPLNV7FYtI=",
- "UpstreamSubnets" : ["0.0.0.0/0"],
- "Targets" :
- {
- "BytesRead" : 1,
- "BytesWritten" : 1,
- "PortForwardDurationNanoseconds" : 1
- }
- }
- ],
- "SeedSpecThreshold" : 2,
- "SeedPeriodNanoseconds" : 2592000000000000,
- "SeedPeriodKeySplits": [
- {
- "Total": 2,
- "Threshold": 2
- }
- ]
- },
- {
- "Epoch" : "%s",
- "Regions" : [],
- "PropagationChannelIDs" : ["%s"],
- "MasterKey" : "HDc/mvd7e+lKDJD0fMpJW66YJ/VW4iqDRjeclEsMnro=",
- "SeedSpecs" : [
- {
- "ID" : "/M0vsT0IjzmI0MvTI9IYe8OVyeQGeaPZN2xGxfLw/UQ=",
- "UpstreamSubnets" : ["0.0.0.0/0"],
- "Targets" :
- {
- "BytesRead" : 1,
- "BytesWritten" : 1,
- "PortForwardDurationNanoseconds" : 1
- }
- }
- ],
- "SeedSpecThreshold" : 1,
- "SeedPeriodNanoseconds" : 2592000000000000,
- "SeedPeriodKeySplits": [
- {
- "Total": 1,
- "Threshold": 1
- }
- ]
- }
- ]
- }
- `
- propagationChannelID, _ := common.MakeSecureRandomStringHex(8)
- now := time.Now().UTC()
- epoch := now.Truncate(720 * time.Hour)
- epochStr := epoch.Format(time.RFC3339Nano)
- oslConfigJSON := fmt.Sprintf(
- oslConfigJSONFormat,
- epochStr, propagationChannelID,
- epochStr, propagationChannelID)
- err := ioutil.WriteFile(oslConfigFilename, []byte(oslConfigJSON), 0600)
- if err != nil {
- t.Fatalf("error paving osl config file: %s", err)
- }
- return propagationChannelID
- }
- func paveTacticsConfigFile(
- t *testing.T, tacticsConfigFilename string,
- tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey string,
- tunnelProtocol string,
- propagationChannelID string) {
- // Setting LimitTunnelProtocols passively exercises the
- // server-side LimitTunnelProtocols enforcement.
- tacticsConfigJSONFormat := `
- {
- "RequestPublicKey" : "%s",
- "RequestPrivateKey" : "%s",
- "RequestObfuscatedKey" : "%s",
- "EnforceServerSide" : true,
- "DefaultTactics" : {
- "TTL" : "60s",
- "Probability" : 1.0,
- "Parameters" : {
- "LimitTunnelProtocols" : ["%s"]
- }
- },
- "FilteredTactics" : [
- {
- "Filter" : {
- "APIParameters" : {"propagation_channel_id" : ["%s"]},
- "SpeedTestRTTMilliseconds" : {
- "Aggregation" : "Median",
- "AtLeast" : 1
- }
- },
- "Tactics" : {
- "Parameters" : {
- "TunnelConnectTimeout" : "20s",
- "TunnelRateLimits" : {"WriteBytesPerSecond": 1000000}
- }
- }
- }
- ]
- }
- `
- tacticsConfigJSON := fmt.Sprintf(
- tacticsConfigJSONFormat,
- tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey,
- tunnelProtocol,
- propagationChannelID)
- err := ioutil.WriteFile(tacticsConfigFilename, []byte(tacticsConfigJSON), 0600)
- if err != nil {
- t.Fatalf("error paving tactics config file: %s", err)
- }
- }
- func sendNotificationReceived(c chan<- struct{}) {
- select {
- case c <- *new(struct{}):
- default:
- }
- }
- func waitOnNotification(t *testing.T, c, timeoutSignal <-chan struct{}, timeoutMessage string) {
- select {
- case <-c:
- case <-timeoutSignal:
- t.Fatalf(timeoutMessage)
- }
- }
|