| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655 |
- /*
- * 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 (
- "bytes"
- "context"
- "encoding/base64"
- "encoding/hex"
- "encoding/json"
- std_errors "errors"
- "flag"
- "fmt"
- "io"
- "io/ioutil"
- "math"
- "net"
- "net/http"
- "net/url"
- "os"
- "path/filepath"
- "reflect"
- "regexp"
- "strconv"
- "strings"
- "sync"
- "syscall"
- "testing"
- "time"
- "unsafe"
- socks "github.com/Psiphon-Labs/goptlib"
- "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/errors"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server/psinet"
- lrucache "github.com/cognusion/go-cache-lru"
- "github.com/miekg/dns"
- "golang.org/x/net/proxy"
- )
- var serverIPAddress, testDataDirName string
- var mockWebServerURL, mockWebServerExpectedResponse string
- var mockWebServerPort = "8080"
- func TestMain(m *testing.M) {
- flag.Parse()
- serverIPv4Address, serverIPv6Address, err := common.GetRoutableInterfaceIPAddresses()
- if err != nil {
- fmt.Printf("error getting server IP address: %s\n", err)
- os.Exit(1)
- }
- if serverIPv4Address != nil {
- serverIPAddress = serverIPv4Address.String()
- } else {
- serverIPAddress = serverIPv6Address.String()
- }
- testDataDirName, err = ioutil.TempDir("", "psiphon-server-test")
- if err != nil {
- fmt.Printf("TempDir failed: %s\n", err)
- os.Exit(1)
- }
- defer os.RemoveAll(testDataDirName)
- psiphon.SetEmitDiagnosticNotices(true, true)
- mockWebServerURL, mockWebServerExpectedResponse = runMockWebServer()
- os.Exit(m.Run())
- }
- func runMockWebServer() (string, string) {
- responseBody := prng.HexString(100000)
- serveMux := http.NewServeMux()
- serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- w.Write([]byte(responseBody))
- })
- webServerAddress := net.JoinHostPort(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 fronted meek protocols, which client is
- // hard-wired to expect running on privileged ports 80 and 443.
- func TestSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "SSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestFragmentedOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceFragmenting: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestPrefixedOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- applyPrefix: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- inspectFlows: true,
- })
- }
- func TestFragmentedPrefixedOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- applyPrefix: true,
- forceFragmenting: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- inspectFlows: true,
- })
- }
- // NOTE: breaks the naming convention of dropping the OSSH suffix
- // because TestTLS is ambiguous as there are other protocols which
- // use TLS, e.g. UNFRONTED-MEEK-HTTPS-OSSH.
- func TestTLSOSSH(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "TLS-OSSH",
- passthrough: true,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- })
- }
- func TestUnfrontedMeek(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestFragmentedUnfrontedMeek(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceFragmenting: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestUnfrontedMeekHTTPS(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-HTTPS-OSSH",
- tlsProfile: protocol.TLS_PROFILE_RANDOMIZED,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestFragmentedUnfrontedMeekHTTPS(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-HTTPS-OSSH",
- tlsProfile: protocol.TLS_PROFILE_RANDOMIZED,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceFragmenting: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestUnfrontedMeekHTTPSTLS13(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-HTTPS-OSSH",
- tlsProfile: protocol.TLS_PROFILE_CHROME_70,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestUnfrontedMeekSessionTicket(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-SESSION-TICKET-OSSH",
- tlsProfile: protocol.TLS_PROFILE_CHROME_58,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestUnfrontedMeekSessionTicketTLS13(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-SESSION-TICKET-OSSH",
- tlsProfile: protocol.TLS_PROFILE_CHROME_70,
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- })
- }
- func TestTLSOSSHOverUnfrontedMeekHTTPSDemux(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-HTTPS-OSSH",
- clientTunnelProtocol: "TLS-OSSH",
- passthrough: true,
- tlsProfile: protocol.TLS_PROFILE_CHROME_96, // TLS-OSSH requires TLS 1.3 support
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- })
- }
- func TestTLSOSSHOverUnfrontedMeekSessionTicketDemux(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-SESSION-TICKET-OSSH",
- clientTunnelProtocol: "TLS-OSSH",
- passthrough: true,
- tlsProfile: protocol.TLS_PROFILE_CHROME_96, // TLS-OSSH requires TLS 1.3 support
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- })
- }
- func TestQUICOSSH(t *testing.T) {
- if !quic.Enabled() {
- t.Skip("QUIC is not enabled")
- }
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "QUIC-OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestLimitedQUICOSSH(t *testing.T) {
- if !quic.Enabled() {
- t.Skip("QUIC is not enabled")
- }
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "QUIC-OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- limitQUICVersions: true,
- doLogHostProvider: true,
- })
- }
- func TestWebTransportAPIRequests(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestHotReload(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestHotReloadWithTactics(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "UNFRONTED-MEEK-OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestDefaultSponsorID(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doDefaultSponsorID: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestDenyTrafficRules(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- denyTrafficRules: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestOmitAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- requireAuthorization: true,
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestNoAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- omitAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestUnusedAuthorization(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- doHotReload: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestTCPOnlySLOK(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestUDPOnlySLOK(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledNTPRequest: true,
- doLogHostProvider: true,
- })
- }
- func TestLivenessTest(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceLivenessTest: true,
- doLogHostProvider: true,
- })
- }
- func TestPruneServerEntries(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceLivenessTest: true,
- doPruneServerEntries: true,
- doLogHostProvider: true,
- })
- }
- func TestBurstMonitorAndDestinationBytes(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doBurstMonitor: true,
- doDestinationBytes: true,
- doLogHostProvider: true,
- })
- }
- func TestChangeBytesConfig(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doDestinationBytes: true,
- doChangeBytesConfig: true,
- doLogHostProvider: true,
- })
- }
- func TestSplitTunnel(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doSplitTunnel: true,
- doLogHostProvider: true,
- })
- }
- func TestOmitProvider(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- doDanglingTCPConn: true,
- doSplitTunnel: true,
- })
- }
- func TestSteeringIP(t *testing.T) {
- runServer(t,
- &runServerConfig{
- tunnelProtocol: "FRONTED-MEEK-OSSH",
- enableSSHAPIRequests: true,
- requireAuthorization: true,
- doTunneledWebRequest: true,
- doTunneledNTPRequest: true,
- forceFragmenting: true,
- doDanglingTCPConn: true,
- doLogHostProvider: true,
- doSteeringIP: true,
- })
- }
- type runServerConfig struct {
- tunnelProtocol string
- clientTunnelProtocol string
- passthrough bool
- tlsProfile string
- enableSSHAPIRequests bool
- doHotReload bool
- doDefaultSponsorID bool
- denyTrafficRules bool
- requireAuthorization bool
- omitAuthorization bool
- doTunneledWebRequest bool
- doTunneledNTPRequest bool
- applyPrefix bool
- forceFragmenting bool
- forceLivenessTest bool
- doPruneServerEntries bool
- doDanglingTCPConn bool
- doPacketManipulation bool
- doBurstMonitor bool
- doSplitTunnel bool
- limitQUICVersions bool
- doDestinationBytes bool
- doChangeBytesConfig bool
- doLogHostProvider bool
- inspectFlows bool
- doSteeringIP bool
- }
- var (
- testSSHClientVersions = []string{"SSH-2.0-A", "SSH-2.0-B", "SSH-2.0-C"}
- testUserAgents = []string{"ua1", "ua2", "ua3"}
- testNetworkType = "WIFI"
- testCustomHostNameRegex = `[a-z0-9]{5,10}\.example\.org`
- testClientFeatures = []string{"feature 1", "feature 2"}
- testDisallowedTrafficAlertActionURLs = []string{"https://example.org/disallowed"}
- // A steering IP must not be a bogon; this address is not dialed.
- testSteeringIP = "1.1.1.1"
- )
- var serverRuns = 0
- func runServer(t *testing.T, runConfig *runServerConfig) {
- serverRuns += 1
- // 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 seedAuthorizationID [32]byte
- clientAuthorization, authorizationID, err := accesscontrol.IssueAuthorization(
- accessControlSigningKey,
- seedAuthorizationID[:],
- time.Now().Add(1*time.Hour))
- if err != nil {
- t.Fatalf("error issuing authorization: %s", err)
- }
- authorizationIDStr := base64.StdEncoding.EncodeToString(authorizationID)
- // 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.
- doClientTactics := protocol.TunnelProtocolUsesMeek(runConfig.tunnelProtocol)
- doServerTactics := doClientTactics ||
- runConfig.applyPrefix ||
- runConfig.forceFragmenting ||
- runConfig.doBurstMonitor ||
- runConfig.doDestinationBytes
- // 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)
- }
- livenessTestSize := 0
- if doClientTactics || runConfig.forceLivenessTest {
- livenessTestSize = 1048576
- }
- // create a server
- psiphonServerIPAddress := serverIPAddress
- if protocol.TunnelProtocolUsesQUIC(runConfig.tunnelProtocol) {
- // Workaround for macOS firewall.
- psiphonServerIPAddress = "127.0.0.1"
- }
- psiphonServerPort := 4000
- var limitQUICVersions protocol.QUICVersions
- if runConfig.limitQUICVersions {
- // Limit the server entry to one specific QUICv1 version, and check
- // that this is used (see expectQUICVersion below). This test case
- // also exercises disabling gQUIC in the server config and
- // using "QUICv1" as the server entry capability.
- selectedQUICVersion := protocol.SupportedQUICv1Versions[prng.Intn(
- len(protocol.SupportedQUICv1Versions))]
- limitQUICVersions = protocol.QUICVersions{selectedQUICVersion}
- }
- var tunnelProtocolPassthroughAddresses map[string]string
- var passthroughAddress *string
- if runConfig.passthrough {
- passthroughAddress = new(string)
- *passthroughAddress = "x.x.x.x:x"
- tunnelProtocolPassthroughAddresses = map[string]string{
- // Tests do not trigger passthrough so set invalid IP and port.
- runConfig.tunnelProtocol: *passthroughAddress,
- }
- }
- generateConfigParams := &GenerateConfigParams{
- ServerIPAddress: psiphonServerIPAddress,
- EnableSSHAPIRequests: runConfig.enableSSHAPIRequests,
- WebServerPort: 8000,
- TunnelProtocolPorts: map[string]int{runConfig.tunnelProtocol: psiphonServerPort},
- TunnelProtocolPassthroughAddresses: tunnelProtocolPassthroughAddresses,
- Passthrough: runConfig.passthrough,
- LimitQUICVersions: limitQUICVersions,
- EnableGQUIC: !runConfig.limitQUICVersions,
- }
- if doServerTactics {
- generateConfigParams.TacticsRequestPublicKey = tacticsRequestPublicKey
- generateConfigParams.TacticsRequestObfuscatedKey = tacticsRequestObfuscatedKey
- }
- if protocol.TunnelProtocolUsesFrontedMeek(runConfig.tunnelProtocol) {
- generateConfigParams.FrontingProviderID = prng.HexString(8)
- }
- serverConfigJSON, _, _, _, encodedServerEntry, err := GenerateConfig(generateConfigParams)
- if err != nil {
- t.Fatalf("error generating server config: %s", err)
- }
- // customize server config
- discoveryServers, err := newDiscoveryServers([]string{"1.1.1.1", "2.2.2.2"})
- if err != nil {
- t.Fatalf("newDiscoveryServers failed: %s\n", err)
- }
- // Initialize prune server entry test cases and associated data to pave into psinet.
- pruneServerEntryTestCases, psinetValidServerEntryTags, expectedNumPruneNotices :=
- initializePruneServerEntriesTest(t, runConfig)
- // Pave psinet with random values to test handshake homepages.
- psinetFilename := filepath.Join(testDataDirName, "psinet.json")
- sponsorID, expectedHomepageURL := pavePsinetDatabaseFile(
- t, psinetFilename, "", runConfig.doDefaultSponsorID, true, psinetValidServerEntryTags, discoveryServers)
- // 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,
- authorizationIDStr,
- runConfig.requireAuthorization,
- runConfig.denyTrafficRules,
- livenessTestSize)
- var tacticsConfigFilename string
- var tacticsTunnelProtocol string
- // Only pave the tactics config when tactics are required. This exercises the
- // case where the tactics config is omitted.
- if doServerTactics {
- tacticsConfigFilename = filepath.Join(testDataDirName, "tactics_config.json")
- if runConfig.clientTunnelProtocol != "" {
- tacticsTunnelProtocol = runConfig.clientTunnelProtocol
- } else {
- tacticsTunnelProtocol = runConfig.tunnelProtocol
- }
- paveTacticsConfigFile(
- t,
- tacticsConfigFilename,
- tacticsRequestPublicKey,
- tacticsRequestPrivateKey,
- tacticsRequestObfuscatedKey,
- tacticsTunnelProtocol,
- propagationChannelID,
- livenessTestSize,
- runConfig.doBurstMonitor,
- runConfig.doDestinationBytes,
- runConfig.applyPrefix,
- runConfig.forceFragmenting,
- "classic",
- )
- }
- blocklistFilename := filepath.Join(testDataDirName, "blocklist.csv")
- paveBlocklistFile(t, blocklistFilename)
- var serverConfig map[string]interface{}
- json.Unmarshal(serverConfigJSON, &serverConfig)
- // The test GeoIP databases map all IPs to a single, non-"None" country
- // and ASN.
- //
- // When split tunnel mode is enabled, this should cause port forwards to
- // be untunneled. When split tunnel mode is not enabled, port forwards
- // should be tunneled despite the country match.
- //
- // When destination bytes metrics are enabled, all traffic will map to the
- // single ASN.
- geoIPCityDatabaseFilename := filepath.Join(testDataDirName, "geoip_city_database.mmbd")
- geoIPISPDatabaseFilename := filepath.Join(testDataDirName, "geoip_isp_database.mmbd")
- paveGeoIPDatabaseFiles(t, geoIPCityDatabaseFilename, geoIPISPDatabaseFilename)
- serverConfig["GeoIPDatabaseFilenames"] = []string{geoIPCityDatabaseFilename, geoIPISPDatabaseFilename}
- serverConfig["PsinetDatabaseFilename"] = psinetFilename
- serverConfig["TrafficRulesFilename"] = trafficRulesFilename
- serverConfig["OSLConfigFilename"] = oslConfigFilename
- if doServerTactics {
- serverConfig["TacticsConfigFilename"] = tacticsConfigFilename
- }
- serverConfig["BlocklistFilename"] = blocklistFilename
- 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
- // Ensure peak failure rate log fields for a single port forward attempt
- serverConfig["PeakUpstreamFailureRateMinimumSampleSize"] = 1
- // Exercise this option.
- serverConfig["PeriodicGarbageCollectionSeconds"] = 1
- // Allow port forwards to local test web server.
- serverConfig["AllowBogons"] = true
- serverConfig["RunPacketManipulator"] = runConfig.doPacketManipulation
- if protocol.TunnelProtocolUsesQUIC(runConfig.tunnelProtocol) && quic.GQUICEnabled() {
- // Enable legacy QUIC version support.
- serverConfig["EnableGQUIC"] = true
- }
- if runConfig.doLogHostProvider {
- serverConfig["HostProvider"] = "example-host-provider"
- }
- if runConfig.doSteeringIP {
- serverConfig["EnableSteeringIPs"] = true
- }
- serverConfigJSON, _ = json.Marshal(serverConfig)
- uniqueUserLog := make(chan map[string]interface{}, 1)
- domainBytesLog := make(chan map[string]interface{}, 1)
- serverTunnelLog := make(chan map[string]interface{}, 1)
- // Max 3 discovery logs:
- // 1. server startup
- // 2. hot reload of psinet db (runConfig.doHotReload)
- // 3. hot reload of server tactics (runConfig.doHotReload && doServerTactics)
- discoveryLog := make(chan map[string]interface{}, 3)
- setLogCallback(func(log []byte) {
- logFields := make(map[string]interface{})
- err := json.Unmarshal(log, &logFields)
- if err != nil {
- return
- }
- if logFields["event_name"] == nil {
- if logFields["discovery_strategy"] != nil {
- select {
- case discoveryLog <- logFields:
- default:
- }
- }
- return
- }
- switch logFields["event_name"].(string) {
- case "unique_user":
- select {
- case uniqueUserLog <- logFields:
- default:
- }
- case "domain_bytes":
- select {
- case domainBytesLog <- logFields:
- default:
- }
- case "server_tunnel":
- select {
- case serverTunnelLog <- logFields:
- default:
- }
- }
- })
- // run flow inspector if requested
- var flowInspectorProxy *flowInspectorProxy
- if runConfig.inspectFlows {
- flowInspectorProxy, err = newFlowInspectorProxy()
- if err != nil {
- t.Fatalf("error starting flow inspector: %s", err)
- }
- flowInspectorProxy.start()
- defer flowInspectorProxy.close()
- }
- // run server
- serverWaitGroup := new(sync.WaitGroup)
- serverWaitGroup.Add(1)
- go func() {
- defer serverWaitGroup.Done()
- // Workaround for one-time logging initialization that persists across
- // test runs. Reset logging to uninitialized. This assumes the
- // previous run has completed and not left any dangling goroutines
- // that may access these variables.
- if log != nil {
- log = nil
- initLogging = sync.Once{}
- }
- err := RunServices(serverConfigJSON)
- if err != nil {
- // TODO: wrong goroutine for t.FatalNow()
- t.Errorf("error running server: %s", err)
- }
- }()
- stopServer := 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 <- struct{}{}
- }()
- select {
- case <-shutdownOk:
- case <-shutdownTimeout.C:
- t.Errorf("server shutdown timeout exceeded")
- }
- }
- // Stop server on early exits due to failure.
- defer func() {
- if stopServer != nil {
- stopServer()
- }
- }()
- // TODO: monitor logs for more robust wait-until-loaded. For example,
- // especially with the race detector on, QUIC-OSSH tests can fail as the
- // client sends its initial packet before the server is ready.
- time.Sleep(1 * time.Second)
- // Test: hot reload (of psinet and traffic rules)
- if runConfig.doHotReload {
- // Change discovery servers. Tests that discovery switches over to
- // these new servers.
- discoveryServers, err = newDiscoveryServers([]string{"3.3.3.3"})
- if err != nil {
- t.Fatalf("newDiscoveryServers failed: %s\n", err)
- }
- // Pave new config files with different random values.
- sponsorID, expectedHomepageURL = pavePsinetDatabaseFile(
- t, psinetFilename, "", runConfig.doDefaultSponsorID, true, psinetValidServerEntryTags, discoveryServers)
- propagationChannelID = paveOSLConfigFile(t, oslConfigFilename)
- paveTrafficRulesFile(
- t,
- trafficRulesFilename,
- propagationChannelID,
- accessType,
- authorizationIDStr,
- runConfig.requireAuthorization,
- runConfig.denyTrafficRules,
- livenessTestSize)
- if doServerTactics {
- // Pave new tactics file with different discovery strategy. Tests
- // that discovery switches over to the new strategy.
- paveTacticsConfigFile(
- t,
- tacticsConfigFilename,
- tacticsRequestPublicKey,
- tacticsRequestPrivateKey,
- tacticsRequestObfuscatedKey,
- tacticsTunnelProtocol,
- propagationChannelID,
- livenessTestSize,
- runConfig.doBurstMonitor,
- runConfig.doDestinationBytes,
- runConfig.applyPrefix,
- runConfig.forceFragmenting,
- "consistent",
- )
- }
- 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)
- // configure client
- values.SetSSHClientVersionsSpec(values.NewPickOneSpec(testSSHClientVersions))
- values.SetUserAgentsSpec(values.NewPickOneSpec(testUserAgents))
- // TODO: currently, TargetServerEntry only works with one tunnel
- numTunnels := 1
- localSOCKSProxyPort := 1081
- localHTTPProxyPort := 8081
- // Use a distinct suffix for network ID for each test run to ensure tactics
- // from different runs don't apply; this is a workaround for the singleton
- // datastore.
- networkID := fmt.Sprintf("WIFI-%s", time.Now().String())
- jsonNetworkID := fmt.Sprintf(`,"NetworkID" : "%s"`, networkID)
- jsonLimitTLSProfiles := ""
- if runConfig.tlsProfile != "" {
- jsonLimitTLSProfiles = fmt.Sprintf(`,"LimitTLSProfiles" : ["%s"]`, runConfig.tlsProfile)
- }
- testClientFeaturesJSON, _ := json.Marshal(testClientFeatures)
- clientTunnelProtocol := runConfig.tunnelProtocol
- if runConfig.clientTunnelProtocol != "" {
- clientTunnelProtocol = runConfig.clientTunnelProtocol
- }
- clientConfigJSON := fmt.Sprintf(`
- {
- "ClientPlatform" : "Android_10_com.test.app",
- "ClientVersion" : "0",
- "ClientFeatures" : %s,
- "SponsorId" : "0",
- "PropagationChannelId" : "0",
- "DeviceLocation" : "gzzzz",
- "DeviceRegion" : "US",
- "DisableRemoteServerListFetcher" : true,
- "EstablishTunnelPausePeriodSeconds" : 1,
- "ConnectionWorkerPoolSize" : %d,
- "LimitTunnelProtocols" : ["%s"]
- %s
- %s
- }`,
- string(testClientFeaturesJSON),
- numTunnels,
- clientTunnelProtocol,
- jsonLimitTLSProfiles,
- jsonNetworkID)
- clientConfig, err := psiphon.LoadConfig([]byte(clientConfigJSON))
- if err != nil {
- t.Fatalf("error processing configuration file: %s", err)
- }
- clientConfig.DataRootDirectory = 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
- clientConfig.EmitServerAlerts = true
- if runConfig.inspectFlows {
- trueVal := true
- clientConfig.UpstreamProxyURL = fmt.Sprintf("socks5://%s", flowInspectorProxy.listener.Addr())
- clientConfig.UpstreamProxyAllowAllServerEntrySources = &trueVal
- }
- if runConfig.doSplitTunnel {
- clientConfig.SplitTunnelOwnRegion = true
- }
- if !runConfig.omitAuthorization {
- clientConfig.Authorizations = []string{clientAuthorization}
- }
- // When using TLS-OSSH the test expects the server to log the fields
- // tls_ossh_sni_server_name and tls_ossh_transformed_host_name, which are
- // only shipped by the client when the host name is transformed.
- if protocol.TunnelProtocolUsesTLSOSSH(clientTunnelProtocol) {
- transformHostNameProbability := 1.0
- clientConfig.TransformHostNameProbability = &transformHostNameProbability
- clientConfig.CustomHostNameRegexes = []string{testCustomHostNameRegex}
- customHostNameProbability := 1.0
- clientConfig.CustomHostNameProbability = &customHostNameProbability
- clientConfig.CustomHostNameLimitProtocols = []string{clientTunnelProtocol}
- }
- if runConfig.doSteeringIP {
- if runConfig.tunnelProtocol != protocol.TUNNEL_PROTOCOL_FRONTED_MEEK {
- t.Fatalf("steering IP test requires FRONTED-MEEK-OSSH")
- }
- protocol.SetFrontedMeekHTTPDialPortNumber(psiphonServerPort)
- // Note that in an actual fronting deployment, the steering IP header
- // is added to the HTTP request by the CDN and any ingress steering
- // IP header would be stripped to avoid spoofing. To facilitate this
- // test case, we just have the client add the steering IP header as
- // if it were the CDN.
- headers := make(http.Header)
- headers.Set("X-Psiphon-Steering-Ip", testSteeringIP)
- clientConfig.MeekAdditionalHeaders = headers
- }
- err = clientConfig.Commit(false)
- if err != nil {
- t.Fatalf("error committing configuration file: %s", err)
- }
- if doClientTactics {
- // 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.SetParameters("", true, applyParameters)
- if err != nil {
- t.Fatalf("SetParameters failed: %s", err)
- }
- } else {
- // Directly apply same parameters that would've come from tactics.
- applyParameters := make(map[string]interface{})
- if runConfig.applyPrefix {
- applyParameters[parameters.OSSHPrefixSpecs] = transforms.Specs{
- "TEST": {{"", "\x00{200}"}},
- }
- applyParameters[parameters.OSSHPrefixScopedSpecNames] = transforms.ScopedSpecNames{
- "": {"TEST"},
- }
- applyParameters[parameters.OSSHPrefixProbability] = 1.0
- applyParameters[parameters.OSSHPrefixSplitMinDelay] = "10ms"
- applyParameters[parameters.OSSHPrefixSplitMaxDelay] = "20ms"
- applyParameters[parameters.OSSHPrefixEnableFragmentor] = runConfig.forceFragmenting
- }
- if runConfig.forceFragmenting {
- applyParameters[parameters.FragmentorLimitProtocols] = protocol.TunnelProtocols{runConfig.tunnelProtocol}
- applyParameters[parameters.FragmentorProbability] = 1.0
- applyParameters[parameters.FragmentorMinTotalBytes] = 1000
- applyParameters[parameters.FragmentorMaxTotalBytes] = 2000
- applyParameters[parameters.FragmentorMinWriteBytes] = 1
- applyParameters[parameters.FragmentorMaxWriteBytes] = 100
- applyParameters[parameters.FragmentorMinDelay] = 1 * time.Millisecond
- applyParameters[parameters.FragmentorMaxDelay] = 10 * time.Millisecond
- }
- if runConfig.forceLivenessTest {
- applyParameters[parameters.LivenessTestMinUpstreamBytes] = livenessTestSize
- applyParameters[parameters.LivenessTestMaxUpstreamBytes] = livenessTestSize
- applyParameters[parameters.LivenessTestMinDownstreamBytes] = livenessTestSize
- applyParameters[parameters.LivenessTestMaxDownstreamBytes] = livenessTestSize
- }
- if runConfig.doPruneServerEntries {
- applyParameters[parameters.PsiphonAPIStatusRequestShortPeriodMin] = 1 * time.Millisecond
- applyParameters[parameters.PsiphonAPIStatusRequestShortPeriodMax] = 1 * time.Millisecond
- }
- err = clientConfig.SetParameters("", true, applyParameters)
- if err != nil {
- t.Fatalf("SetParameters failed: %s", err)
- }
- }
- // connect to server with client
- err = psiphon.OpenDataStore(clientConfig)
- if err != nil {
- t.Fatalf("error initializing client datastore: %s", err)
- }
- defer psiphon.CloseDataStore()
- // Test unique user counting cases.
- var expectUniqueUser bool
- switch serverRuns % 3 {
- case 0:
- // Mock no last_connected.
- psiphon.SetKeyValue("lastConnected", "")
- expectUniqueUser = true
- case 1:
- // Mock previous day last_connected.
- psiphon.SetKeyValue(
- "lastConnected",
- time.Now().UTC().AddDate(0, 0, -1).Truncate(1*time.Hour).Format(time.RFC3339))
- expectUniqueUser = true
- case 2:
- // Leave previous last_connected.
- expectUniqueUser = false
- }
- // Clear SLOKs from previous test runs.
- psiphon.DeleteSLOKs()
- // Store prune server entry test server entries and failed tunnel records.
- storePruneServerEntriesTest(
- t, runConfig, testDataDirName, pruneServerEntryTestCases)
- controller, err := psiphon.NewController(clientConfig)
- if err != nil {
- t.Fatalf("error creating client controller: %s", err)
- }
- connectedServer := make(chan struct{}, 1)
- tunnelsEstablished := make(chan struct{}, 1)
- homepageReceived := make(chan struct{}, 1)
- slokSeeded := make(chan struct{}, 1)
- numPruneNotices := 0
- pruneServerEntriesNoticesEmitted := make(chan struct{}, 1)
- serverAlertDisallowedNoticesEmitted := make(chan struct{}, 1)
- untunneledPortForward := 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 "ConnectedServer":
- // Check that client connected with the expected protocol.
- protocol := payload["protocol"].(string)
- if protocol != clientTunnelProtocol {
- // TODO: wrong goroutine for t.FatalNow()
- t.Errorf("unexpected protocol: %s", protocol)
- }
- sendNotificationReceived(connectedServer)
- 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.Errorf("unexpected homepage: %s", homepageURL)
- }
- sendNotificationReceived(homepageReceived)
- case "SLOKSeeded":
- sendNotificationReceived(slokSeeded)
- case "PruneServerEntry":
- numPruneNotices += 1
- if numPruneNotices == expectedNumPruneNotices {
- sendNotificationReceived(pruneServerEntriesNoticesEmitted)
- }
- case "ServerAlert":
- reason := payload["reason"].(string)
- actionURLsPayload := payload["actionURLs"].([]interface{})
- actionURLs := make([]string, len(actionURLsPayload))
- for i, value := range actionURLsPayload {
- actionURLs[i] = value.(string)
- }
- if reason == protocol.PSIPHON_API_ALERT_DISALLOWED_TRAFFIC &&
- reflect.DeepEqual(actionURLs, testDisallowedTrafficAlertActionURLs) {
- sendNotificationReceived(serverAlertDisallowedNoticesEmitted)
- }
- case "Untunneled":
- sendNotificationReceived(untunneledPortForward)
- }
- }))
- ctx, cancelFunc := context.WithCancel(context.Background())
- controllerWaitGroup := new(sync.WaitGroup)
- controllerWaitGroup.Add(1)
- go func() {
- defer controllerWaitGroup.Done()
- controller.Run(ctx)
- }()
- stopClient := func() {
- cancelFunc()
- shutdownTimeout := time.NewTimer(20 * time.Second)
- shutdownOk := make(chan struct{}, 1)
- go func() {
- controllerWaitGroup.Wait()
- shutdownOk <- struct{}{}
- }()
- select {
- case <-shutdownOk:
- case <-shutdownTimeout.C:
- t.Errorf("controller shutdown timeout exceeded")
- }
- }
- // Stop client on early exits due to failure.
- defer func() {
- if stopClient != nil {
- stopClient()
- }
- }()
- // 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, connectedServer, timeoutSignal, "connected server timeout exceeded")
- waitOnNotification(t, tunnelsEstablished, timeoutSignal, "tunnel established timeout exceeded")
- waitOnNotification(t, homepageReceived, timeoutSignal, "homepage received timeout exceeded")
- if runConfig.doChangeBytesConfig {
- if !runConfig.doDestinationBytes {
- t.Fatalf("invalid test configuration")
- }
- // Test: now that the client is connected, change the domain bytes and
- // destination bytes configurations. No stats should be logged, even
- // with an already connected client.
- // Pave psinet without domain bytes; retain the same sponsor ID. The
- // random homepage URLs will change, but this has no effect on the
- // already connected client.
- _, _ = pavePsinetDatabaseFile(
- t, psinetFilename, sponsorID, runConfig.doDefaultSponsorID, false, psinetValidServerEntryTags, discoveryServers)
- // Pave tactics without destination bytes.
- paveTacticsConfigFile(
- t,
- tacticsConfigFilename,
- tacticsRequestPublicKey,
- tacticsRequestPrivateKey,
- tacticsRequestObfuscatedKey,
- tacticsTunnelProtocol,
- propagationChannelID,
- livenessTestSize,
- runConfig.doBurstMonitor,
- false,
- false, false,
- "consistent")
- p, _ := os.FindProcess(os.Getpid())
- p.Signal(syscall.SIGUSR1)
- // TODO: monitor logs for more robust wait-until-reloaded
- time.Sleep(1 * time.Second)
- }
- expectTrafficFailure := runConfig.denyTrafficRules || (runConfig.omitAuthorization && runConfig.requireAuthorization)
- // The client still reports zero domain_bytes when no port forwards are allowed (expectTrafficFailure)
- expectDomainBytes := !runConfig.doChangeBytesConfig
- 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 or server alert notice
- time.Sleep(1 * time.Second)
- if !expectTrafficFailure {
- waitOnNotification(t, slokSeeded, timeoutSignal, "SLOK seeded timeout exceeded")
- numSLOKs := psiphon.CountSLOKs()
- if numSLOKs != expectedNumSLOKs {
- t.Fatalf("unexpected number of SLOKs: %d", numSLOKs)
- }
- } else {
- // Note: in expectTrafficFailure case, timeoutSignal may have already fired.
- waitOnNotification(t, serverAlertDisallowedNoticesEmitted, nil, "")
- }
- // Test: await expected prune server entry notices
- //
- // Note: will take up to PsiphonAPIStatusRequestShortPeriodMax to emit.
- if expectedNumPruneNotices > 0 {
- waitOnNotification(t, pruneServerEntriesNoticesEmitted, nil, "")
- }
- if runConfig.doDanglingTCPConn {
- // Test: client that has established TCP connection but not completed
- // any handshakes must not block/delay server shutdown
- danglingConn, err := net.Dial(
- "tcp", net.JoinHostPort(psiphonServerIPAddress, strconv.Itoa(psiphonServerPort)))
- if err != nil {
- t.Fatalf("TCP dial failed: %s", err)
- }
- defer danglingConn.Close()
- }
- // Test: check for split tunnel notice
- if runConfig.doSplitTunnel {
- if !runConfig.doTunneledWebRequest || expectTrafficFailure {
- t.Fatalf("invalid test run configuration")
- }
- waitOnNotification(t, untunneledPortForward, nil, "")
- } else {
- // There should be no "Untunneled" notice. This check assumes that any
- // unexpected Untunneled notice will have been delivered at this point,
- // after the SLOK notice.
- select {
- case <-untunneledPortForward:
- t.Fatalf("unexpected untunneled port forward")
- default:
- }
- }
- // Trigger server_load logging once more, to exercise
- // sshClient.peakMetrics. As we don't have a reference to the server's
- // Support struct, we can't invoke logServerLoad directly and there's a
- // potential race between asynchronous logServerLoad invocation and
- // client shutdown. For now, we sleep as a workaround.
- p.Signal(syscall.SIGUSR2)
- time.Sleep(1 * time.Second)
- // Shutdown to ensure logs/notices are flushed
- stopClient()
- stopClient = nil
- stopServer()
- stopServer = nil
- // Test: all expected server logs were emitted
- // TODO: stops should be fully synchronous, but, intermittently,
- // server_tunnel fails to appear ("missing server tunnel log")
- // without this delay.
- time.Sleep(100 * time.Millisecond)
- expectClientBPFField := psiphon.ClientBPFEnabled() && doClientTactics
- expectServerBPFField := ServerBPFEnabled() && protocol.TunnelProtocolIsDirect(runConfig.tunnelProtocol) && doServerTactics
- expectServerPacketManipulationField := runConfig.doPacketManipulation
- expectBurstFields := runConfig.doBurstMonitor
- expectTCPPortForwardDial := runConfig.doTunneledWebRequest
- expectTCPDataTransfer := runConfig.doTunneledWebRequest && !expectTrafficFailure && !runConfig.doSplitTunnel
- // Even with expectTrafficFailure, DNS port forwards will succeed
- expectUDPDataTransfer := runConfig.doTunneledNTPRequest
- expectQUICVersion := ""
- if runConfig.limitQUICVersions {
- expectQUICVersion = limitQUICVersions[0]
- }
- expectDestinationBytesFields := runConfig.doDestinationBytes && !runConfig.doChangeBytesConfig
- select {
- case logFields := <-serverTunnelLog:
- err := checkExpectedServerTunnelLogFields(
- runConfig,
- expectClientBPFField,
- expectServerBPFField,
- expectServerPacketManipulationField,
- expectBurstFields,
- expectTCPPortForwardDial,
- expectTCPDataTransfer,
- expectUDPDataTransfer,
- expectQUICVersion,
- expectDestinationBytesFields,
- passthroughAddress,
- logFields)
- if err != nil {
- t.Fatalf("invalid server tunnel log fields: %s", err)
- }
- default:
- t.Fatalf("missing server tunnel log")
- }
- if expectUniqueUser {
- select {
- case logFields := <-uniqueUserLog:
- err := checkExpectedUniqueUserLogFields(
- runConfig,
- logFields)
- if err != nil {
- t.Fatalf("invalid unique user log fields: %s", err)
- }
- default:
- t.Fatalf("missing unique user log")
- }
- } else {
- select {
- case <-uniqueUserLog:
- t.Fatalf("unexpected unique user log")
- default:
- }
- }
- if expectDomainBytes {
- select {
- case logFields := <-domainBytesLog:
- err := checkExpectedDomainBytesLogFields(
- runConfig,
- logFields)
- if err != nil {
- t.Fatalf("invalid domain bytes log fields: %s", err)
- }
- default:
- t.Fatalf("missing domain bytes log")
- }
- } else {
- select {
- case <-domainBytesLog:
- t.Fatalf("unexpected domain bytes log")
- default:
- }
- }
- // Check logs emitted by discovery.
- var expectedDiscoveryStrategy []string
- // Discovery emits 1 log on startup.
- if doServerTactics {
- expectedDiscoveryStrategy = append(expectedDiscoveryStrategy, "classic")
- } else {
- expectedDiscoveryStrategy = append(expectedDiscoveryStrategy, "consistent")
- }
- if runConfig.doHotReload {
- if doServerTactics {
- // Discovery emits 1 log when tactics are reloaded, which happens
- // before the psinet database is reloaded.
- expectedDiscoveryStrategy = append(expectedDiscoveryStrategy, "classic")
- }
- // Discovery emits 1 when the psinet database is reloaded.
- expectedDiscoveryStrategy = append(expectedDiscoveryStrategy, "consistent")
- }
- for _, expectedStrategy := range expectedDiscoveryStrategy {
- select {
- case logFields := <-discoveryLog:
- if strategy, ok := logFields["discovery_strategy"].(string); ok {
- if strategy != expectedStrategy {
- t.Fatalf("expected discovery strategy \"%s\"", expectedStrategy)
- }
- } else {
- t.Fatalf("missing discovery_strategy field")
- }
- default:
- t.Fatalf("missing discovery log")
- }
- }
- // Check that datastore had retained/pruned server entries as expected.
- checkPruneServerEntriesTest(t, runConfig, testDataDirName, pruneServerEntryTestCases)
- // Inspect OSSH prefix flows, if applicable.
- if runConfig.inspectFlows && runConfig.applyPrefix {
- flows := <-flowInspectorProxy.ch
- serverFlows := flows[0]
- clientFlows := flows[1]
- expectedClientPrefix := bytes.Repeat([]byte{0x00}, 200)
- expectedServerPrefix := bytes.Repeat([]byte{0x01}, 200)
- if runConfig.forceFragmenting {
- // Fragmentor was applied, so check for prefix in stream dump.
- if !bytes.Equal(clientFlows.streamDump.Bytes()[:200], expectedClientPrefix) {
- t.Fatal("client flow does not have expected prefix")
- }
- if !bytes.Equal(serverFlows.streamDump.Bytes()[:200], expectedServerPrefix) {
- t.Fatal("server flow does not have expected prefix")
- }
- fragmentorMaxWriteBytes := 100
- if len(clientFlows.flows[0].data) > fragmentorMaxWriteBytes {
- t.Fatal("client flow was not fragmented")
- }
- if len(serverFlows.flows[0].data) > fragmentorMaxWriteBytes {
- t.Fatal("server flow was not fragmented")
- }
- } else {
- // Fragmentor was not applied, so check for prefix in first flow.
- if !bytes.Equal(clientFlows.flows[0].data, expectedClientPrefix) {
- t.Fatal("client flow does not have expected prefix")
- }
- if !bytes.Equal(serverFlows.flows[0].data, expectedServerPrefix) {
- t.Fatal("server flow does not have expected prefix")
- }
- // Analyze time between prefix and next packet.
- // client 10-20ms, 30-40ms for server with standard deviation of 2ms.
- clientZtest := testSampleInUniformRange(clientFlows.flows[1].timeDelta.Microseconds(), 10000, 20000, 2000)
- serverZtest := testSampleInUniformRange(serverFlows.flows[1].timeDelta.Microseconds(), 30000, 40000, 2000)
- if !clientZtest {
- t.Fatalf("client write delay after prefix too high: %f ms",
- clientFlows.flows[1].timeDelta.Seconds()*1e3)
- }
- if !serverZtest {
- t.Fatalf("server write delay after prefix too high: %f ms",
- serverFlows.flows[1].timeDelta.Seconds()*1e3)
- }
- }
- }
- if runConfig.doSteeringIP {
- // Access the unexported controller.steeringIPCache
- controllerStruct := reflect.ValueOf(controller).Elem()
- steeringIPCacheField := controllerStruct.Field(40)
- steeringIPCacheField = reflect.NewAt(
- steeringIPCacheField.Type(), unsafe.Pointer(steeringIPCacheField.UnsafeAddr())).Elem()
- steeringIPCache := steeringIPCacheField.Interface().(*lrucache.Cache)
- if steeringIPCache.ItemCount() != 1 {
- t.Fatalf("unexpected steering IP cache size: %d", steeringIPCache.ItemCount())
- }
- key := fmt.Sprintf(
- "%s %s %s",
- networkID,
- generateConfigParams.FrontingProviderID,
- runConfig.tunnelProtocol)
- entry, ok := steeringIPCache.Get(key)
- if !ok {
- t.Fatalf("no entry for steering IP cache key: %s", key)
- }
- if entry.(string) != testSteeringIP {
- t.Fatalf("unexpected cached steering IP: %v", entry)
- }
- }
- // Check that the client discovered one of the discovery servers.
- discoveredServers := make(map[string]*protocol.ServerEntry)
- // Otherwise NewServerEntryIterator only returns TargetServerEntry.
- clientConfig.TargetServerEntry = ""
- _, iterator, err := psiphon.NewServerEntryIterator(clientConfig)
- if err != nil {
- t.Fatalf("NewServerEntryIterator failed: %s", err)
- }
- defer iterator.Close()
- for {
- serverEntry, err := iterator.Next()
- if err != nil {
- t.Fatalf("ServerIterator.Next failed: %s", err)
- }
- if serverEntry == nil {
- break
- }
- discoveredServers[serverEntry.IpAddress] = serverEntry
- }
- foundOne := false
- for _, server := range discoveryServers {
- serverEntry, err := protocol.DecodeServerEntry(server.EncodedServerEntry, "", "")
- if err != nil {
- t.Fatalf("protocol.DecodeServerEntry failed: %s", err)
- }
- if v, ok := discoveredServers[serverEntry.IpAddress]; ok {
- if v.Tag == serverEntry.Tag {
- foundOne = true
- break
- }
- }
- }
- if !foundOne {
- t.Fatalf("expected client to discover at least one server")
- }
- }
- func sendNotificationReceived(c chan<- struct{}) {
- select {
- case c <- struct{}{}:
- default:
- }
- }
- func waitOnNotification(t *testing.T, c, timeoutSignal <-chan struct{}, timeoutMessage string) {
- if timeoutSignal == nil {
- <-c
- } else {
- select {
- case <-c:
- case <-timeoutSignal:
- t.Fatalf(timeoutMessage)
- }
- }
- }
- func checkExpectedServerTunnelLogFields(
- runConfig *runServerConfig,
- expectClientBPFField bool,
- expectServerBPFField bool,
- expectServerPacketManipulationField bool,
- expectBurstFields bool,
- expectTCPPortForwardDial bool,
- expectTCPDataTransfer bool,
- expectUDPDataTransfer bool,
- expectQUICVersion string,
- expectDestinationBytesFields bool,
- expectPassthroughAddress *string,
- fields map[string]interface{}) error {
- // Limitations:
- //
- // - client_build_rev not set in test build (see common/buildinfo.go)
- // - egress_region, upstream_proxy_type, upstream_proxy_custom_header_names not exercised in test
- // - fronting_provider_id/meek_dial_ip_address/meek_resolved_ip_address only logged for FRONTED meek protocols
- for _, name := range []string{
- "host_id",
- "start_time",
- "duration",
- "session_id",
- "is_first_tunnel_in_session",
- "last_connected",
- "establishment_duration",
- "propagation_channel_id",
- "sponsor_id",
- "client_platform",
- "client_features",
- "relay_protocol",
- "device_region",
- "device_location",
- "ssh_client_version",
- "server_entry_region",
- "server_entry_source",
- "server_entry_timestamp",
- "dial_port_number",
- "is_replay",
- "dial_duration",
- "candidate_number",
- "established_tunnels_count",
- "network_latency_multiplier",
- "network_type",
- // The test run ensures that logServerLoad is invoked while the client
- // is connected, so the following must be logged.
- "peak_concurrent_proximate_accepted_clients",
- "peak_concurrent_proximate_established_clients",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- if fields["host_id"].(string) != "example-host-id" {
- return fmt.Errorf("unexpected host_id '%s'", fields["host_id"])
- }
- expectedRelayProtocol := runConfig.tunnelProtocol
- if runConfig.clientTunnelProtocol != "" {
- expectedRelayProtocol = runConfig.clientTunnelProtocol
- }
- if fields["relay_protocol"].(string) != expectedRelayProtocol {
- return fmt.Errorf("unexpected relay_protocol '%s'", fields["relay_protocol"])
- }
- if !common.Contains(testSSHClientVersions, fields["ssh_client_version"].(string)) {
- return fmt.Errorf("unexpected ssh_client_version '%s'", fields["ssh_client_version"])
- }
- clientFeatures := fields["client_features"].([]interface{})
- if len(clientFeatures) != len(testClientFeatures) {
- return fmt.Errorf("unexpected client_features '%s'", fields["client_features"])
- }
- for i, feature := range testClientFeatures {
- if clientFeatures[i].(string) != feature {
- return fmt.Errorf("unexpected client_features '%s'", fields["client_features"])
- }
- }
- if fields["network_type"].(string) != testNetworkType {
- return fmt.Errorf("unexpected network_type '%s'", fields["network_type"])
- }
- // With interruptions, timeouts, and retries in some tests, there may be
- // more than one dangling accepted_client.
- peakConcurrentProximateAcceptedClients :=
- int(fields["peak_concurrent_proximate_accepted_clients"].(float64))
- if peakConcurrentProximateAcceptedClients < 0 ||
- peakConcurrentProximateAcceptedClients > 10 {
- return fmt.Errorf(
- "unexpected peak_concurrent_proximate_accepted_clients '%v'",
- fields["peak_concurrent_proximate_accepted_clients"])
- }
- peakConcurrentProximateEstablishedClients :=
- int(fields["peak_concurrent_proximate_established_clients"].(float64))
- if peakConcurrentProximateEstablishedClients != 0 {
- return fmt.Errorf(
- "unexpected peak_concurrent_proximate_established_clients '%v'",
- fields["peak_concurrent_proximate_established_clients"])
- }
- // In some negative test cases, no port forwards are attempted, in which
- // case these fields are not logged.
- if expectTCPDataTransfer {
- if fields["peak_tcp_port_forward_failure_rate"] == nil {
- return fmt.Errorf("missing expected field 'peak_tcp_port_forward_failure_rate'")
- }
- if fields["peak_tcp_port_forward_failure_rate"].(float64) != 0.0 {
- return fmt.Errorf(
- "unexpected peak_tcp_port_forward_failure_rate '%v'",
- fields["peak_tcp_port_forward_failure_rate"])
- }
- if fields["peak_tcp_port_forward_failure_rate_sample_size"] == nil {
- return fmt.Errorf("missing expected field 'peak_tcp_port_forward_failure_rate_sample_size'")
- }
- if fields["peak_tcp_port_forward_failure_rate_sample_size"].(float64) <= 0.0 {
- return fmt.Errorf(
- "unexpected peak_tcp_port_forward_failure_rate_sample_size '%v'",
- fields["peak_tcp_port_forward_failure_rate_sample_size"])
- }
- } else {
- if fields["peak_tcp_port_forward_failure_rate"] != nil {
- return fmt.Errorf("unexpected field 'peak_tcp_port_forward_failure_rate'")
- }
- if fields["peak_tcp_port_forward_failure_rate_sample_size"] != nil {
- return fmt.Errorf("unexpected field 'peak_tcp_port_forward_failure_rate_sample_size'")
- }
- }
- if expectUDPDataTransfer {
- if fields["peak_dns_failure_rate"] == nil {
- return fmt.Errorf("missing expected field 'peak_dns_failure_rate'")
- }
- if fields["peak_dns_failure_rate"].(float64) != 0.0 {
- return fmt.Errorf(
- "unexpected peak_dns_failure_rate '%v'", fields["peak_dns_failure_rate"])
- }
- if fields["peak_dns_failure_rate_sample_size"] == nil {
- return fmt.Errorf("missing expected field 'peak_dns_failure_rate_sample_size'")
- }
- if fields["peak_dns_failure_rate_sample_size"].(float64) <= 0.0 {
- return fmt.Errorf(
- "unexpected peak_dns_failure_rate_sample_size '%v'",
- fields["peak_dns_failure_rate_sample_size"])
- }
- } else {
- if fields["peak_dns_failure_rate"] != nil {
- return fmt.Errorf("unexpected field 'peak_dns_failure_rate'")
- }
- if fields["peak_dns_failure_rate_sample_size"] != nil {
- return fmt.Errorf("unexpected field 'peak_dns_failure_rate_sample_size'")
- }
- }
- // TODO: the following cases should check that fields are not logged when
- // not expected.
- if runConfig.doSplitTunnel {
- if fields["split_tunnel"] == nil {
- return fmt.Errorf("missing expected field 'split_tunnel'")
- }
- if fields["split_tunnel"].(bool) != true {
- return fmt.Errorf("missing split_tunnel value")
- }
- }
- if protocol.TunnelProtocolUsesObfuscatedSSH(runConfig.tunnelProtocol) {
- for _, name := range []string{
- "padding",
- "pad_response",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- }
- if protocol.TunnelProtocolUsesMeek(runConfig.tunnelProtocol) && (runConfig.clientTunnelProtocol == "" || protocol.TunnelProtocolUsesMeekHTTPS(runConfig.clientTunnelProtocol)) {
- for _, name := range []string{
- "user_agent",
- "meek_transformed_host_name",
- "meek_cookie_size",
- "meek_limit_request",
- "meek_underlying_connection_count",
- tactics.APPLIED_TACTICS_TAG_PARAMETER_NAME,
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- if !common.Contains(testUserAgents, fields["user_agent"].(string)) {
- return fmt.Errorf("unexpected user_agent '%s'", fields["user_agent"])
- }
- }
- if protocol.TunnelProtocolUsesMeekHTTP(runConfig.tunnelProtocol) {
- for _, name := range []string{
- "meek_host_header",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- hostName := fields["meek_host_header"].(string)
- dialPortNumber := int(fields["dial_port_number"].(float64))
- if dialPortNumber != 80 {
- hostName, _, _ = net.SplitHostPort(hostName)
- }
- if regexp.MustCompile(testCustomHostNameRegex).FindString(hostName) != hostName {
- return fmt.Errorf("unexpected meek_host_header '%s'", fields["meek_host_header"])
- }
- if !protocol.TunnelProtocolUsesFrontedMeek(runConfig.tunnelProtocol) {
- for _, name := range []string{
- "meek_dial_ip_address",
- "meek_resolved_ip_address",
- } {
- if fields[name] != nil {
- return fmt.Errorf("unexpected field '%s'", name)
- }
- }
- }
- }
- if protocol.TunnelProtocolUsesMeekHTTPS(runConfig.tunnelProtocol) && (runConfig.clientTunnelProtocol == "" || protocol.TunnelProtocolUsesMeekHTTPS(runConfig.clientTunnelProtocol)) {
- for _, name := range []string{
- "tls_profile",
- "tls_version",
- "meek_sni_server_name",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- hostName := fields["meek_sni_server_name"].(string)
- if regexp.MustCompile(testCustomHostNameRegex).FindString(hostName) != hostName {
- return fmt.Errorf("unexpected meek_sni_server_name '%s'", fields["meek_sni_server_name"])
- }
- if !protocol.TunnelProtocolUsesFrontedMeek(runConfig.tunnelProtocol) {
- for _, name := range []string{
- "meek_dial_ip_address",
- "meek_resolved_ip_address",
- "meek_host_header",
- } {
- if fields[name] != nil {
- return fmt.Errorf("unexpected field '%s'", name)
- }
- }
- }
- if !common.Contains(protocol.SupportedTLSProfiles, fields["tls_profile"].(string)) {
- return fmt.Errorf("unexpected tls_profile '%s'", fields["tls_profile"])
- }
- tlsVersion := fields["tls_version"].(string)
- if !strings.HasPrefix(tlsVersion, protocol.TLS_VERSION_12) &&
- !strings.HasPrefix(tlsVersion, protocol.TLS_VERSION_13) {
- return fmt.Errorf("unexpected tls_version '%s'", fields["tls_version"])
- }
- }
- if protocol.TunnelProtocolUsesQUIC(runConfig.tunnelProtocol) {
- for _, name := range []string{
- "quic_version",
- "quic_dial_sni_address",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- quicVersion := fields["quic_version"].(string)
- if !common.Contains(protocol.SupportedQUICVersions, quicVersion) ||
- (runConfig.limitQUICVersions && quicVersion != expectQUICVersion) {
- return fmt.Errorf("unexpected quic_version '%s'", fields["quic_version"])
- }
- }
- if protocol.TunnelProtocolUsesTLSOSSH(expectedRelayProtocol) {
- for _, name := range []string{
- "tls_padding",
- "tls_ossh_sni_server_name",
- "tls_ossh_transformed_host_name",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- hostName := fields["tls_ossh_sni_server_name"].(string)
- if regexp.MustCompile(testCustomHostNameRegex).FindString(hostName) != hostName {
- return fmt.Errorf("unexpected tls_ossh_sni_server_name '%s'", fields["tls_ossh_sni_server_name"])
- }
- }
- if runConfig.applyPrefix {
- if fields["ossh_prefix"] == nil || fmt.Sprintf("%s", fields["ossh_prefix"]) == "" {
- return fmt.Errorf("missing expected field 'ossh_prefix'")
- }
- }
- if runConfig.forceFragmenting {
- for _, name := range []string{
- "upstream_bytes_fragmented",
- "upstream_min_bytes_written",
- "upstream_max_bytes_written",
- "upstream_min_delayed",
- "upstream_max_delayed",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- }
- if expectClientBPFField {
- name := "client_bpf"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- } else if fmt.Sprintf("%s", fields[name]) != "test-client-bpf" {
- return fmt.Errorf("unexpected field value %s: '%s'", name, fields[name])
- }
- }
- if expectServerBPFField {
- name := "server_bpf"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- } else if fmt.Sprintf("%s", fields[name]) != "test-server-bpf" {
- return fmt.Errorf("unexpected field value %s: '%s'", name, fields[name])
- }
- }
- if expectServerPacketManipulationField {
- name := "server_packet_manipulation"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- } else if fmt.Sprintf("%s", fields[name]) != "test-packetman-spec" {
- return fmt.Errorf("unexpected field value %s: '%s'", name, fields[name])
- }
- }
- if expectBurstFields {
- // common.TestBurstMonitoredConn covers inclusion of additional fields.
- for _, name := range []string{
- "burst_upstream_first_rate",
- "burst_upstream_last_rate",
- "burst_upstream_min_rate",
- "burst_upstream_max_rate",
- "burst_downstream_first_rate",
- "burst_downstream_last_rate",
- "burst_downstream_min_rate",
- "burst_downstream_max_rate",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- }
- var checkTCPMetric func(float64) bool
- if expectTCPPortForwardDial {
- checkTCPMetric = func(f float64) bool { return f > 0 }
- } else {
- checkTCPMetric = func(f float64) bool { return f == 0 }
- }
- for _, name := range []string{
- "peak_concurrent_dialing_port_forward_count_tcp",
- } {
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if !checkTCPMetric(fields[name].(float64)) {
- return fmt.Errorf("unexpected field value %s: '%v'", name, fields[name])
- }
- }
- if expectTCPDataTransfer {
- checkTCPMetric = func(f float64) bool { return f > 0 }
- } else {
- checkTCPMetric = func(f float64) bool { return f == 0 }
- }
- for _, name := range []string{
- "bytes_up_tcp",
- "bytes_down_tcp",
- "peak_concurrent_port_forward_count_tcp",
- "total_port_forward_count_tcp",
- } {
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if !checkTCPMetric(fields[name].(float64)) {
- return fmt.Errorf("unexpected field value %s: '%v'", name, fields[name])
- }
- }
- var checkUDPMetric func(float64) bool
- if expectUDPDataTransfer {
- checkUDPMetric = func(f float64) bool { return f > 0 }
- } else {
- checkUDPMetric = func(f float64) bool { return f == 0 }
- }
- for _, name := range []string{
- "bytes_up_udp",
- "bytes_down_udp",
- "peak_concurrent_port_forward_count_udp",
- "total_port_forward_count_udp",
- "total_udpgw_channel_count",
- } {
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if !checkUDPMetric(fields[name].(float64)) {
- return fmt.Errorf("unexpected field value %s: '%v'", name, fields[name])
- }
- }
- for _, name := range []string{
- "dest_bytes_asn",
- "dest_bytes_up_tcp",
- "dest_bytes_down_tcp",
- "dest_bytes_up_udp",
- "dest_bytes_down_udp",
- "dest_bytes",
- } {
- if expectDestinationBytesFields && fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- } else if !expectDestinationBytesFields && fields[name] != nil {
- return fmt.Errorf("unexpected field '%s'", name)
- }
- }
- if expectDestinationBytesFields {
- name := "dest_bytes_asn"
- if fields[name].(string) != testGeoIPASN {
- return fmt.Errorf("unexpected field value %s: '%v'", name, fields[name])
- }
- for _, pair := range [][]string{
- []string{"dest_bytes_up_tcp", "bytes_up_tcp"},
- []string{"dest_bytes_down_tcp", "bytes_down_tcp"},
- []string{"dest_bytes_up_udp", "bytes_up_udp"},
- []string{"dest_bytes_down_udp", "bytes_down_udp"},
- []string{"dest_bytes", "bytes"},
- } {
- value0 := int64(fields[pair[0]].(float64))
- value1 := int64(fields[pair[1]].(float64))
- ok := value0 == value1
- if pair[0] == "dest_bytes_up_udp" || pair[0] == "dest_bytes_down_udp" || pair[0] == "dest_bytes" {
- // DNS requests are excluded from destination bytes counting
- ok = value0 > 0 && value0 < value1
- }
- if !ok {
- return fmt.Errorf("unexpected field value %s: %v != %v", pair[0], fields[pair[0]], fields[pair[1]])
- }
- }
- }
- if expectPassthroughAddress != nil {
- name := "passthrough_address"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if fields[name] != *expectPassthroughAddress {
- return fmt.Errorf("unexpected field value %s: %v != %v", name, fields[name], *expectPassthroughAddress)
- }
- }
- if runConfig.doLogHostProvider {
- name := "provider"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- } else if fmt.Sprintf("%s", fields[name]) != "example-host-provider" {
- return fmt.Errorf("unexpected field value %s: '%s'", name, fields[name])
- }
- } else {
- name := "provider"
- if fields[name] != nil {
- return fmt.Errorf("unexpected field '%s'", name)
- }
- }
- if runConfig.doSteeringIP {
- name := "relayed_steering_ip"
- if fields[name] == nil {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if fields[name] != testSteeringIP {
- return fmt.Errorf("unexpected field value %s: %v != %v", name, fields[name], testSteeringIP)
- }
- name = "steering_ip"
- if fields[name] != nil {
- return fmt.Errorf("unexpected field '%s'", name)
- }
- }
- return nil
- }
- func checkExpectedUniqueUserLogFields(
- runConfig *runServerConfig,
- fields map[string]interface{}) error {
- for _, name := range []string{
- "session_id",
- "last_connected",
- "propagation_channel_id",
- "sponsor_id",
- "client_platform",
- "device_region",
- "device_location",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- }
- return nil
- }
- func checkExpectedDomainBytesLogFields(
- runConfig *runServerConfig,
- fields map[string]interface{}) error {
- for _, name := range []string{
- "session_id",
- "propagation_channel_id",
- "sponsor_id",
- "client_platform",
- "device_region",
- "device_location",
- "domain",
- "bytes",
- } {
- if fields[name] == nil || fmt.Sprintf("%s", fields[name]) == "" {
- return fmt.Errorf("missing expected field '%s'", name)
- }
- if name == "domain" {
- if fields[name].(string) != "ALL" && fields[name].(string) != "(OTHER)" {
- return fmt.Errorf("unexpected field value %s: '%v'", name, fields[name])
- }
- }
- }
- return nil
- }
- 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
- testHostnames := []string{"time.google.com", "time.nist.gov", "pool.ntp.org"}
- indexes := prng.Perm(len(testHostnames))
- for _, index := range indexes {
- testHostname := testHostnames[index]
- 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 := resolveIP(testHostname, clientUDPConn)
- clientUDPConn.Close()
- if err == nil && (len(addrs) == 0 || len(addrs[0]) < 4) {
- err = std_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 resolveIP(host string, conn net.Conn) (addrs []net.IP, err error) {
- // Send the DNS query (A record only)
- dnsConn := &dns.Conn{Conn: conn}
- defer dnsConn.Close()
- query := new(dns.Msg)
- query.SetQuestion(dns.Fqdn(host), dns.TypeA)
- query.RecursionDesired = true
- dnsConn.WriteMsg(query)
- // Process the response
- response, err := dnsConn.ReadMsg()
- if err == nil && response.MsgHdr.Id != query.MsgHdr.Id {
- err = dns.ErrId
- }
- if err != nil {
- return nil, errors.Trace(err)
- }
- addrs = make([]net.IP, 0)
- for _, answer := range response.Answer {
- if a, ok := answer.(*dns.A); ok {
- addrs = append(addrs, a.A)
- }
- }
- return addrs, nil
- }
- func pavePsinetDatabaseFile(
- t *testing.T,
- psinetFilename string,
- sponsorID string,
- useDefaultSponsorID bool,
- doDomainBytes bool,
- validServerEntryTags []string,
- discoveryServers []*psinet.DiscoveryServer) (string, string) {
- if sponsorID == "" {
- sponsorID = prng.HexString(8)
- }
- defaultSponsorID := ""
- if useDefaultSponsorID {
- defaultSponsorID = sponsorID
- }
- fakeDomain := prng.HexString(4)
- fakePath := prng.HexString(4)
- expectedHomepageURL := fmt.Sprintf("https://%s.com/%s", fakeDomain, fakePath)
- discoverServersJSON, err := json.Marshal(discoveryServers)
- if err != nil {
- t.Fatalf("json.Marshal failed: %s\n", err)
- }
- psinetJSONFormat := `
- {
- "default_sponsor_id" : "%s",
- "sponsors" : {
- "%s" : {
- %s
- "home_pages" : {
- "None" : [
- {
- "region" : null,
- "url" : "%s"
- }
- ]
- }
- }
- },
- "default_alert_action_urls" : {
- "%s" : %s
- },
- "valid_server_entry_tags" : {
- %s
- },
- "discovery_servers" : %s
- }
- `
- domainBytes := ""
- if doDomainBytes {
- domainBytes = `
- "https_request_regexes" : [
- {
- "regex" : ".*",
- "replace" : "ALL"
- }
- ],
- `
- }
- actionURLsJSON, _ := json.Marshal(testDisallowedTrafficAlertActionURLs)
- validServerEntryTagsJSON := ""
- for _, serverEntryTag := range validServerEntryTags {
- if len(validServerEntryTagsJSON) > 0 {
- validServerEntryTagsJSON += ", "
- }
- validServerEntryTagsJSON += fmt.Sprintf("\"%s\" : true", serverEntryTag)
- }
- psinetJSON := fmt.Sprintf(
- psinetJSONFormat,
- defaultSponsorID,
- sponsorID,
- domainBytes,
- expectedHomepageURL,
- protocol.PSIPHON_API_ALERT_DISALLOWED_TRAFFIC,
- actionURLsJSON,
- validServerEntryTagsJSON,
- discoverServersJSON)
- 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 string,
- propagationChannelID string,
- accessType string,
- authorizationID string,
- requireAuthorization bool,
- deny bool,
- livenessTestSize int) {
- // Test both default and fast lookups
- if intLookupThreshold != 10 {
- t.Fatalf("unexpected intLookupThreshold")
- }
- TCPPorts := mockWebServerPort
- UDPPorts := "53, 123, 10001, 10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009, 10010"
- allowTCPPorts := TCPPorts
- allowUDPPorts := UDPPorts
- disallowTCPPorts := "1"
- disallowUDPPorts := "1"
- if deny {
- allowTCPPorts = "1"
- allowUDPPorts = "1"
- disallowTCPPorts = TCPPorts
- disallowUDPPorts = UDPPorts
- }
- authorizationFilterFormat := `,
- "AuthorizedAccessTypes" : ["%s"],
- "ActiveAuthorizationIDs" : ["%s"]
- `
- authorizationFilter := ""
- if requireAuthorization {
- authorizationFilter = fmt.Sprintf(
- authorizationFilterFormat, accessType, authorizationID)
- }
- // Supports two traffic rule test cases:
- //
- // 1. no ports are allowed until after the filtered rule is applied
- // 2. no required ports are allowed (deny = true)
- trafficRulesJSONFormat := `
- {
- "DefaultRules" : {
- "RateLimits" : {
- "ReadBytesPerSecond": 16384,
- "WriteBytesPerSecond": 16384,
- "ReadUnthrottledBytes": %d,
- "WriteUnthrottledBytes": %d
- },
- "AllowTCPPorts" : [1],
- "AllowUDPPorts" : [1],
- "MeekRateLimiterHistorySize" : 10,
- "MeekRateLimiterThresholdSeconds" : 1,
- "MeekRateLimiterGarbageCollectionTriggerCount" : 1,
- "MeekRateLimiterReapHistoryFrequencySeconds" : 1,
- "MeekRateLimiterRegions" : []
- },
- "FilteredRules" : [
- {
- "Filter" : {
- "HandshakeParameters" : {
- "propagation_channel_id" : ["%s"]
- }%s
- },
- "Rules" : {
- "RateLimits" : {
- "ReadBytesPerSecond": 2097152,
- "WriteBytesPerSecond": 2097152
- },
- "AllowTCPPorts" : [%s],
- "AllowUDPPorts" : [%s],
- "DisallowTCPPorts" : [%s],
- "DisallowUDPPorts" : [%s]
- }
- }
- ]
- }
- `
- trafficRulesJSON := fmt.Sprintf(
- trafficRulesJSONFormat,
- livenessTestSize, livenessTestSize,
- propagationChannelID, authorizationFilter,
- allowTCPPorts, allowUDPPorts, disallowTCPPorts, disallowUDPPorts)
- 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 := prng.HexString(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,
- livenessTestSize int,
- doBurstMonitor bool,
- doDestinationBytes bool,
- applyOsshPrefix bool,
- enableOsshPrefixFragmenting bool,
- discoveryStategy string) {
- // Setting LimitTunnelProtocols passively exercises the
- // server-side LimitTunnelProtocols enforcement.
- tacticsConfigJSONFormat := `
- {
- "RequestPublicKey" : "%s",
- "RequestPrivateKey" : "%s",
- "RequestObfuscatedKey" : "%s",
- "DefaultTactics" : {
- "TTL" : "60s",
- "Probability" : 1.0,
- "Parameters" : {
- %s
- %s
- %s
- "LimitTunnelProtocols" : ["%s"],
- "FragmentorLimitProtocols" : ["%s"],
- "FragmentorProbability" : 1.0,
- "FragmentorMinTotalBytes" : 1000,
- "FragmentorMaxTotalBytes" : 2000,
- "FragmentorMinWriteBytes" : 1,
- "FragmentorMaxWriteBytes" : 100,
- "FragmentorMinDelay" : "1ms",
- "FragmentorMaxDelay" : "10ms",
- "FragmentorDownstreamLimitProtocols" : ["%s"],
- "FragmentorDownstreamProbability" : 1.0,
- "FragmentorDownstreamMinTotalBytes" : 1000,
- "FragmentorDownstreamMaxTotalBytes" : 2000,
- "FragmentorDownstreamMinWriteBytes" : 1,
- "FragmentorDownstreamMaxWriteBytes" : 100,
- "FragmentorDownstreamMinDelay" : "1ms",
- "FragmentorDownstreamMaxDelay" : "10ms",
- "LivenessTestMinUpstreamBytes" : %d,
- "LivenessTestMaxUpstreamBytes" : %d,
- "LivenessTestMinDownstreamBytes" : %d,
- "LivenessTestMaxDownstreamBytes" : %d,
- "BPFServerTCPProgram": {
- "Name" : "test-server-bpf",
- "Instructions" : [
- {"Op": "RetConstant", "Args": {"Val": 65535}}]},
- "BPFServerTCPProbability" : 1.0,
- "BPFClientTCPProgram": {
- "Name" : "test-client-bpf",
- "Instructions" : [
- {"Op": "RetConstant", "Args": {"Val": 65535}}]},
- "BPFClientTCPProbability" : 1.0,
- "ServerPacketManipulationSpecs" : [{"Name": "test-packetman-spec", "PacketSpecs": [["TCP-flags S"]]}],
- "ServerPacketManipulationProbability" : 1.0,
- "ServerProtocolPacketManipulations": {"All" : ["test-packetman-spec"]},
- "ServerDiscoveryStrategy": "%s"
- }
- },
- "FilteredTactics" : [
- {
- "Filter" : {
- "APIParameters" : {"propagation_channel_id" : ["%s"]},
- "SpeedTestRTTMilliseconds" : {
- "Aggregation" : "Median",
- "AtLeast" : 1
- }
- },
- "Tactics" : {
- "Parameters" : {
- "TunnelConnectTimeout" : "20s",
- "TunnelRateLimits" : {"WriteBytesPerSecond": 1000000},
- "TransformHostNameProbability" : 1.0,
- "PickUserAgentProbability" : 1.0,
- "ApplicationParameters" : {
- "AppFlag1" : true,
- "AppConfig1" : {"Option1" : "A", "Option2" : "B"},
- "AppSwitches1" : [1, 2, 3, 4]
- },
- "CustomHostNameRegexes": ["%s"],
- "CustomHostNameProbability": 1.0,
- "CustomHostNameLimitProtocols": ["%s"]
- }
- }
- }
- ]
- }
- `
- burstParameters := ""
- if doBurstMonitor {
- burstParameters = `
- "ServerBurstUpstreamDeadline" : "100ms",
- "ServerBurstUpstreamTargetBytes" : 1000,
- "ServerBurstDownstreamDeadline" : "100ms",
- "ServerBurstDownstreamTargetBytes" : 100000,
- "ClientBurstUpstreamDeadline" : "100ms",
- "ClientBurstUpstreamTargetBytes" : 1000,
- "ClientBurstDownstreamDeadline" : "100ms",
- "ClientBurstDownstreamTargetBytes" : 100000,
- `
- }
- destinationBytesParameters := ""
- if doDestinationBytes {
- destinationBytesParameters = fmt.Sprintf(`
- "DestinationBytesMetricsASN" : "%s",
- `, testGeoIPASN)
- }
- osshPrefix := ""
- if applyOsshPrefix {
- osshPrefix = fmt.Sprintf(`
- "ServerOSSHPrefixSpecs": {
- "TEST": [["", "\\x01{200}"]]
- },
- "OSSHPrefixSplitMinDelay": "30ms",
- "OSSHPrefixSplitMaxDelay": "40ms",
- "OSSHPrefixEnableFragmentor": %s,
- `, strconv.FormatBool(enableOsshPrefixFragmenting))
- }
- tacticsConfigJSON := fmt.Sprintf(
- tacticsConfigJSONFormat,
- tacticsRequestPublicKey, tacticsRequestPrivateKey, tacticsRequestObfuscatedKey,
- burstParameters,
- destinationBytesParameters,
- osshPrefix,
- tunnelProtocol,
- tunnelProtocol,
- tunnelProtocol,
- livenessTestSize, livenessTestSize, livenessTestSize, livenessTestSize,
- discoveryStategy,
- propagationChannelID,
- strings.ReplaceAll(testCustomHostNameRegex, `\`, `\\`),
- tunnelProtocol)
- err := ioutil.WriteFile(tacticsConfigFilename, []byte(tacticsConfigJSON), 0600)
- if err != nil {
- t.Fatalf("error paving tactics config file: %s", err)
- }
- }
- func paveBlocklistFile(t *testing.T, blocklistFilename string) {
- blocklistContent :=
- "255.255.255.255,test-source,test-subject\n2001:db8:f75c::0951:58bc:ef22,test-source,test-subject\nexample.org,test-source,test-subject\n"
- err := ioutil.WriteFile(blocklistFilename, []byte(blocklistContent), 0600)
- if err != nil {
- t.Fatalf("error paving blocklist file: %s", err)
- }
- }
- type pruneServerEntryTestCase struct {
- IPAddress string
- ExplicitTag bool
- ExpectedTag string
- LocalTimestamp string
- PsinetValid bool
- ExpectPrune bool
- IsEmbedded bool
- DialPort0 bool
- ServerEntryFields protocol.ServerEntryFields
- }
- func initializePruneServerEntriesTest(
- t *testing.T,
- runConfig *runServerConfig) ([]*pruneServerEntryTestCase, []string, int) {
- if !runConfig.doPruneServerEntries {
- return nil, nil, 0
- }
- newTimeStamp := time.Now().UTC().Format(time.RFC3339)
- oldTimeStamp := time.Now().Add(-30 * 24 * time.Hour).UTC().Format(time.RFC3339)
- // Test Cases:
- // - ExplicitTag: server entry includes a tag; vs. generate a derived tag
- // - LocalTimestamp: server entry is sufficiently old to be pruned; vs. not
- // - PsinetValid: server entry is reported valid by psinet; vs. deleted
- // - ExpectPrune: prune outcome based on flags above
- // - IsEmbedded: pruned embedded server entries leave a tombstone and cannot
- // be reimported
- // - DialPort0: set dial port to 0, a special prune case (see statusAPIRequestHandler)
- pruneServerEntryTestCases := []*pruneServerEntryTestCase{
- &pruneServerEntryTestCase{IPAddress: "192.0.2.1", ExplicitTag: true, LocalTimestamp: newTimeStamp, PsinetValid: true, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.2", ExplicitTag: false, LocalTimestamp: newTimeStamp, PsinetValid: true, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.3", ExplicitTag: true, LocalTimestamp: oldTimeStamp, PsinetValid: true, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.4", ExplicitTag: false, LocalTimestamp: oldTimeStamp, PsinetValid: true, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.5", ExplicitTag: true, LocalTimestamp: newTimeStamp, PsinetValid: false, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.6", ExplicitTag: false, LocalTimestamp: newTimeStamp, PsinetValid: false, ExpectPrune: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.7", ExplicitTag: true, LocalTimestamp: oldTimeStamp, PsinetValid: false, ExpectPrune: true, IsEmbedded: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.8", ExplicitTag: false, LocalTimestamp: oldTimeStamp, PsinetValid: false, ExpectPrune: true, IsEmbedded: false},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.9", ExplicitTag: true, LocalTimestamp: oldTimeStamp, PsinetValid: false, ExpectPrune: true, IsEmbedded: true},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.10", ExplicitTag: false, LocalTimestamp: oldTimeStamp, PsinetValid: false, ExpectPrune: true, IsEmbedded: true},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.11", ExplicitTag: true, LocalTimestamp: oldTimeStamp, PsinetValid: true, ExpectPrune: true, IsEmbedded: false, DialPort0: true},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.12", ExplicitTag: false, LocalTimestamp: oldTimeStamp, PsinetValid: true, ExpectPrune: true, IsEmbedded: true, DialPort0: true},
- &pruneServerEntryTestCase{IPAddress: "192.0.2.13", ExplicitTag: true, LocalTimestamp: oldTimeStamp, PsinetValid: true, ExpectPrune: true, IsEmbedded: true, DialPort0: true},
- }
- for _, testCase := range pruneServerEntryTestCases {
- dialPort := 4000
- if testCase.DialPort0 {
- dialPort = 0
- }
- _, _, _, _, encodedServerEntry, err := GenerateConfig(
- &GenerateConfigParams{
- ServerIPAddress: testCase.IPAddress,
- WebServerPort: 8000,
- TunnelProtocolPorts: map[string]int{runConfig.tunnelProtocol: dialPort},
- })
- if err != nil {
- t.Fatalf("GenerateConfig failed: %s", err)
- }
- serverEntrySource := protocol.SERVER_ENTRY_SOURCE_REMOTE
- if testCase.IsEmbedded {
- serverEntrySource = protocol.SERVER_ENTRY_SOURCE_EMBEDDED
- }
- serverEntryFields, err := protocol.DecodeServerEntryFields(
- string(encodedServerEntry),
- testCase.LocalTimestamp,
- serverEntrySource)
- if err != nil {
- t.Fatalf("DecodeServerEntryFields failed: %s", err)
- }
- if testCase.ExplicitTag {
- testCase.ExpectedTag = prng.Base64String(32)
- serverEntryFields.SetTag(testCase.ExpectedTag)
- } else {
- testCase.ExpectedTag = protocol.GenerateServerEntryTag(
- serverEntryFields.GetIPAddress(),
- serverEntryFields.GetWebServerSecret())
- }
- testCase.ServerEntryFields = serverEntryFields
- }
- psinetValidServerEntryTags := make([]string, 0)
- expectedNumPruneNotices := 0
- for _, testCase := range pruneServerEntryTestCases {
- if testCase.PsinetValid {
- psinetValidServerEntryTags = append(
- psinetValidServerEntryTags, testCase.ExpectedTag)
- }
- if testCase.ExpectPrune {
- expectedNumPruneNotices += 1
- }
- }
- return pruneServerEntryTestCases,
- psinetValidServerEntryTags,
- expectedNumPruneNotices
- }
- func storePruneServerEntriesTest(
- t *testing.T,
- runConfig *runServerConfig,
- testDataDirName string,
- pruneServerEntryTestCases []*pruneServerEntryTestCase) {
- if !runConfig.doPruneServerEntries {
- return
- }
- for _, testCase := range pruneServerEntryTestCases {
- err := psiphon.StoreServerEntry(testCase.ServerEntryFields, true)
- if err != nil {
- t.Fatalf("StoreServerEntry failed: %s", err)
- }
- }
- clientConfig := &psiphon.Config{
- SponsorId: "0",
- PropagationChannelId: "0",
- // DataRootDirectory must to be set to avoid a migration in the current
- // working directory.
- DataRootDirectory: testDataDirName,
- }
- err := clientConfig.Commit(false)
- if err != nil {
- t.Fatalf("Commit failed: %s", err)
- }
- resolver := psiphon.NewResolver(clientConfig, true)
- defer resolver.Stop()
- clientConfig.SetResolver(resolver)
- applyParameters := make(map[string]interface{})
- applyParameters[parameters.RecordFailedTunnelPersistentStatsProbability] = 1.0
- err = clientConfig.SetParameters("", true, applyParameters)
- if err != nil {
- t.Fatalf("SetParameters failed: %s", err)
- }
- verifyTestCasesStored := make(verifyTestCasesStoredLookup)
- for _, testCase := range pruneServerEntryTestCases {
- verifyTestCasesStored.mustBeStored(testCase.IPAddress)
- }
- scanServerEntries(t, clientConfig, pruneServerEntryTestCases, func(
- t *testing.T,
- testCase *pruneServerEntryTestCase,
- serverEntry *protocol.ServerEntry) {
- verifyTestCasesStored.isStored(testCase.IPAddress)
- // Check that random tag was retained or derived tag was calculated as
- // expected
- if serverEntry.Tag != testCase.ExpectedTag {
- t.Fatalf("unexpected tag for %s got %s expected %s",
- testCase.IPAddress, serverEntry.Tag, testCase.ExpectedTag)
- }
- // Create failed tunnel event records to exercise pruning
- dialParams, err := psiphon.MakeDialParameters(
- clientConfig,
- nil,
- nil,
- func(_ *protocol.ServerEntry, _ string) bool { return true },
- func(serverEntry *protocol.ServerEntry) (string, bool) {
- return runConfig.tunnelProtocol, true
- },
- serverEntry,
- false,
- 0,
- 0)
- if err != nil {
- t.Fatalf("MakeDialParameters failed: %s", err)
- }
- err = psiphon.RecordFailedTunnelStat(
- clientConfig, dialParams, nil, 0, 0, std_errors.New("test error"))
- if err != nil {
- t.Fatalf("RecordFailedTunnelStat failed: %s", err)
- }
- })
- verifyTestCasesStored.checkStored(
- t, "missing prune test case server entries")
- }
- func checkPruneServerEntriesTest(
- t *testing.T,
- runConfig *runServerConfig,
- testDataDirName string,
- pruneServerEntryTestCases []*pruneServerEntryTestCase) {
- if !runConfig.doPruneServerEntries {
- return
- }
- clientConfig := &psiphon.Config{
- SponsorId: "0",
- PropagationChannelId: "0",
- // DataRootDirectory must to be set to avoid a migration in the current
- // working directory.
- DataRootDirectory: testDataDirName,
- }
- err := clientConfig.Commit(false)
- if err != nil {
- t.Fatalf("Commit failed: %s", err)
- }
- // Check that server entries remain or are pruned as expected
- verifyTestCasesStored := make(verifyTestCasesStoredLookup)
- for _, testCase := range pruneServerEntryTestCases {
- if !testCase.ExpectPrune {
- verifyTestCasesStored.mustBeStored(testCase.IPAddress)
- }
- }
- scanServerEntries(t, clientConfig, pruneServerEntryTestCases, func(
- t *testing.T,
- testCase *pruneServerEntryTestCase,
- serverEntry *protocol.ServerEntry) {
- if testCase.ExpectPrune {
- t.Fatalf("expected prune for %s", testCase.IPAddress)
- } else {
- verifyTestCasesStored.isStored(testCase.IPAddress)
- }
- })
- verifyTestCasesStored.checkStored(
- t, "missing prune test case server entries")
- // Check that pruned server entries reimport or not, as expected
- for _, testCase := range pruneServerEntryTestCases {
- err := psiphon.StoreServerEntry(testCase.ServerEntryFields, true)
- if err != nil {
- t.Fatalf("StoreServerEntry failed: %s", err)
- }
- }
- verifyTestCasesStored = make(verifyTestCasesStoredLookup)
- for _, testCase := range pruneServerEntryTestCases {
- if !testCase.ExpectPrune || !testCase.IsEmbedded {
- verifyTestCasesStored.mustBeStored(testCase.IPAddress)
- }
- }
- scanServerEntries(t, clientConfig, pruneServerEntryTestCases, func(
- t *testing.T,
- testCase *pruneServerEntryTestCase,
- serverEntry *protocol.ServerEntry) {
- if testCase.ExpectPrune && testCase.IsEmbedded {
- t.Fatalf("expected tombstone for %s", testCase.IPAddress)
- } else {
- verifyTestCasesStored.isStored(testCase.IPAddress)
- }
- })
- verifyTestCasesStored.checkStored(
- t, "missing reimported prune test case server entries")
- // Non-embedded server entries with tombstones _can_ be reimported
- for _, testCase := range pruneServerEntryTestCases {
- testCase.ServerEntryFields.SetLocalSource(protocol.SERVER_ENTRY_SOURCE_REMOTE)
- err := psiphon.StoreServerEntry(testCase.ServerEntryFields, true)
- if err != nil {
- t.Fatalf("StoreServerEntry failed: %s", err)
- }
- }
- verifyTestCasesStored = make(verifyTestCasesStoredLookup)
- for _, testCase := range pruneServerEntryTestCases {
- verifyTestCasesStored.mustBeStored(testCase.IPAddress)
- }
- scanServerEntries(t, clientConfig, pruneServerEntryTestCases, func(
- t *testing.T,
- testCase *pruneServerEntryTestCase,
- serverEntry *protocol.ServerEntry) {
- verifyTestCasesStored.isStored(testCase.IPAddress)
- })
- verifyTestCasesStored.checkStored(
- t, "missing non-embedded reimported prune test case server entries")
- }
- func scanServerEntries(
- t *testing.T,
- clientConfig *psiphon.Config,
- pruneServerEntryTestCases []*pruneServerEntryTestCase,
- scanner func(
- t *testing.T,
- testCase *pruneServerEntryTestCase,
- serverEntry *protocol.ServerEntry)) {
- _, iterator, err := psiphon.NewServerEntryIterator(clientConfig)
- if err != nil {
- t.Fatalf("NewServerEntryIterator failed: %s", err)
- }
- defer iterator.Close()
- for {
- serverEntry, err := iterator.Next()
- if err != nil {
- t.Fatalf("ServerIterator.Next failed: %s", err)
- }
- if serverEntry == nil {
- break
- }
- for _, testCase := range pruneServerEntryTestCases {
- if testCase.IPAddress == serverEntry.IpAddress {
- scanner(t, testCase, serverEntry)
- break
- }
- }
- }
- }
- type verifyTestCasesStoredLookup map[string]bool
- func (v verifyTestCasesStoredLookup) mustBeStored(s string) {
- v[s] = true
- }
- func (v verifyTestCasesStoredLookup) isStored(s string) {
- delete(v, s)
- }
- func (v verifyTestCasesStoredLookup) checkStored(t *testing.T, errMessage string) {
- if len(v) != 0 {
- t.Fatalf("%s: %+v", errMessage, v)
- }
- }
- type Number interface {
- int64 | float64
- }
- // testSampleInUniformRange returns true if sample is in the range [a, b],
- // or within 2 standard deviations of the range.
- func testSampleInUniformRange[V Number](sample, a, b, stddev V) bool {
- if sample >= a && sample <= b {
- return true
- }
- lower := math.Abs(float64(sample-a) / float64(stddev))
- higher := math.Abs(float64(sample-b) / float64(stddev))
- return lower <= 2.0 || higher <= 2.0
- }
- type flowInspectorProxy struct {
- listener *socks.SocksListener
- ch chan []*flows
- }
- func newFlowInspectorProxy() (*flowInspectorProxy, error) {
- listener, err := socks.ListenSocks("tcp", "127.0.0.1:0")
- if err != nil {
- fmt.Printf("socks.ListenSocks failed: %s\n", err)
- return nil, err
- }
- return &flowInspectorProxy{
- listener: listener,
- ch: make(chan []*flows, 1),
- }, nil
- }
- func (f *flowInspectorProxy) start() {
- go func() {
- for {
- localConn, err := f.listener.AcceptSocks()
- if err != nil {
- return
- }
- go func() {
- defer localConn.Close()
- remoteConn, err := net.Dial("tcp", localConn.Req.Target)
- if err != nil {
- fmt.Printf("net.Dial failed: %s\n", err)
- return
- }
- defer remoteConn.Close()
- err = localConn.Grant(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})
- if err != nil {
- fmt.Printf("localConn.Grant failed: %s\n", err)
- return
- }
- waitGroup := new(sync.WaitGroup)
- waitGroup.Add(1)
- serverFlowWriter := newFlowWriter(true)
- clientFlowWriter := newFlowWriter(false)
- go func() {
- defer waitGroup.Done()
- // Copy from remote to local, and tee to serverFlowWriter.
- io.Copy(localConn, io.TeeReader(remoteConn, serverFlowWriter))
- // fmt.Printf("Server Flows:\n%s\n\n", serverFlowWriter.String())
- localConn.Close()
- remoteConn.Close()
- }()
- // Copy from local to remote, and tee to clientFlowWriter.
- io.Copy(remoteConn, io.TeeReader(localConn, clientFlowWriter))
- // fmt.Printf("Client Flows:\n%s\n\n", clientFlowWriter.String())
- localConn.Close()
- remoteConn.Close()
- waitGroup.Wait()
- // clientFlowWriter and serverFlowWriter are synchronized by waitGroup.
- f.ch <- []*flows{serverFlowWriter, clientFlowWriter}
- }()
- }
- }()
- }
- func (f *flowInspectorProxy) close() error {
- return f.listener.Close()
- }
- type flow struct {
- // timeDelta is the time elapsed since the last flow
- timeDelta time.Duration
- data []byte
- }
- type flows struct {
- lastTime time.Time
- server bool
- streamDump *bytes.Buffer
- flows []flow
- }
- func newFlowWriter(server bool) *flows {
- return &flows{
- lastTime: time.Now(),
- streamDump: new(bytes.Buffer),
- server: server,
- }
- }
- // String returns a string representation of the first 10 flows.
- func (f *flows) String() string {
- var sb strings.Builder
- for i, flow := range f.flows[:10] {
- sb.WriteString(fmt.Sprintf("Flow %d: %.5f ms: %s\n",
- i, flow.timeDelta.Seconds()*1000, hex.EncodeToString(flow.data)))
- }
- if len(f.flows) > 10 {
- sb.WriteString("...\n")
- }
- return sb.String()
- }
- func (f *flows) Write(p []byte) (n int, err error) {
- curTime := time.Now()
- _, err = f.streamDump.Write(p)
- if err != nil {
- return 0, err
- }
- data := make([]byte, len(p))
- n = copy(data, p)
- if n < len(p) {
- return n, io.ErrShortWrite
- }
- f.flows = append(f.flows, flow{
- timeDelta: time.Since(f.lastTime),
- data: data,
- })
- f.lastTime = curTime
- return n, err
- }
- // newDiscoveryServers returns len(ipAddresses) discovery servers with the
- // given IP addresses and randomly generated tags.
- func newDiscoveryServers(ipAddresses []string) ([]*psinet.DiscoveryServer, error) {
- servers := make([]*psinet.DiscoveryServer, len(ipAddresses))
- for i, ipAddress := range ipAddresses {
- encodedServer, err := protocol.EncodeServerEntry(&protocol.ServerEntry{
- IpAddress: ipAddress,
- Tag: prng.HexString(16),
- })
- if err != nil {
- return nil, errors.Trace(err)
- }
- servers[i] = &psinet.DiscoveryServer{
- DiscoveryDateRange: []time.Time{
- time.Now().Add(-time.Hour).UTC(),
- time.Now().Add(time.Hour).UTC(),
- },
- EncodedServerEntry: encodedServer,
- }
- }
- return servers, nil
- }
|