| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235 |
- /*
- * 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 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],
- "MeekRateLimiterHistorySize" : 10,
- "MeekRateLimiterThresholdSeconds" : 1,
- "MeekRateLimiterGarbageCollectionTriggerCount" : 1,
- "MeekRateLimiterReapHistoryFrequencySeconds" : 1,
- "MeekRateLimiterRegions" : []
- },
- "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)
- }
- }
|