config.go 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "crypto/md5"
  22. "encoding/base64"
  23. "encoding/binary"
  24. "encoding/json"
  25. "fmt"
  26. "io/ioutil"
  27. "net/http"
  28. "os"
  29. "path/filepath"
  30. "regexp"
  31. "strconv"
  32. "strings"
  33. "sync"
  34. "unicode"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  39. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/resolver"
  40. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
  41. )
  42. const (
  43. TUNNEL_POOL_SIZE = 1
  44. MAX_TUNNEL_POOL_SIZE = 32
  45. // Psiphon data directory name, relative to config.DataRootDirectory.
  46. // See config.GetPsiphonDataDirectory().
  47. PsiphonDataDirectoryName = "ca.psiphon.PsiphonTunnel.tunnel-core"
  48. // Filename constants, all relative to config.GetPsiphonDataDirectory().
  49. HomepageFilename = "homepage"
  50. NoticesFilename = "notices"
  51. OldNoticesFilename = "notices.1"
  52. UpgradeDownloadFilename = "upgrade"
  53. )
  54. // Config is the Psiphon configuration specified by the application. This
  55. // configuration controls the behavior of the core tunnel functionality.
  56. //
  57. // To distinguish omitted timeout params from explicit 0 value timeout params,
  58. // corresponding fields are int pointers. nil means no value was supplied and
  59. // to use the default; a non-nil pointer to 0 means no timeout.
  60. type Config struct {
  61. // DataRootDirectory is the directory in which to store persistent files,
  62. // which contain information such as server entries. By default, current
  63. // working directory.
  64. //
  65. // Psiphon will assume full control of files under this directory. They may
  66. // be deleted, moved or overwritten.
  67. DataRootDirectory string
  68. // UseNoticeFiles configures notice files for writing. If set, homepages
  69. // will be written to a file created at config.GetHomePageFilename()
  70. // and notices will be written to a file created at
  71. // config.GetNoticesFilename().
  72. //
  73. // The homepage file may be read after the Tunnels notice with count of 1.
  74. //
  75. // The value of UseNoticeFiles sets the size and frequency at which the
  76. // notices file, config.GetNoticesFilename(), will be rotated. See the
  77. // comment for UseNoticeFiles for more details. One rotated older file,
  78. // config.GetOldNoticesFilename(), is retained.
  79. //
  80. // The notice files may be may be read at any time; and should be opened
  81. // read-only for reading. Diagnostic notices are omitted from the notice
  82. // files.
  83. //
  84. // See comment for setNoticeFiles in notice.go for further details.
  85. UseNoticeFiles *UseNoticeFiles
  86. // PropagationChannelId is a string identifier which indicates how the
  87. // Psiphon client was distributed. This parameter is required. This value
  88. // is supplied by and depends on the Psiphon Network, and is typically
  89. // embedded in the client binary.
  90. PropagationChannelId string
  91. // SponsorId is a string identifier which indicates who is sponsoring this
  92. // Psiphon client. One purpose of this value is to determine the home
  93. // pages for display. This parameter is required. This value is supplied
  94. // by and depends on the Psiphon Network, and is typically embedded in the
  95. // client binary.
  96. SponsorId string
  97. // ClientVersion is the client version number that the client reports to
  98. // the server. The version number refers to the host client application,
  99. // not the core tunnel library. One purpose of this value is to enable
  100. // automatic updates. This value is supplied by and depends on the Psiphon
  101. // Network, and is typically embedded in the client binary.
  102. //
  103. // Note that sending a ClientPlatform string which includes "windows"
  104. // (case insensitive) and a ClientVersion of <= 44 will cause an error in
  105. // processing the response to DoConnectedRequest calls.
  106. ClientVersion string
  107. // ClientPlatform is the client platform ("Windows", "Android", etc.) that
  108. // the client reports to the server.
  109. ClientPlatform string
  110. // ClientFeatures is a list of feature names denoting enabled application
  111. // features. Clients report enabled features to the server for stats
  112. // purposes.
  113. ClientFeatures []string
  114. // EgressRegion is a ISO 3166-1 alpha-2 country code which indicates which
  115. // country to egress from. For the default, "", the best performing server
  116. // in any country is selected.
  117. EgressRegion string
  118. // SplitTunnelOwnRegion enables split tunnel mode for the client's own
  119. // country. When enabled, TCP port forward destinations that resolve to
  120. // the same GeoIP country as the client are connected to directly,
  121. // untunneled.
  122. SplitTunnelOwnRegion bool
  123. // SplitTunnelRegions enables selected split tunnel mode in which the
  124. // client specifies a list of ISO 3166-1 alpha-2 country codes for which
  125. // traffic should be untunneled. TCP port forwards destined to any
  126. // country specified in SplitTunnelRegions will be untunneled, regardless
  127. // of whether SplitTunnelOwnRegion is on or off.
  128. SplitTunnelRegions []string
  129. // ListenInterface specifies which interface to listen on. If no
  130. // interface is provided then listen on 127.0.0.1. If 'any' is provided
  131. // then use 0.0.0.0. If there are multiple IP addresses on an interface
  132. // use the first IPv4 address.
  133. ListenInterface string
  134. // DisableLocalSocksProxy disables running the local SOCKS proxy.
  135. DisableLocalSocksProxy bool
  136. // LocalSocksProxyPort specifies a port number for the local SOCKS proxy
  137. // running at 127.0.0.1. For the default value, 0, the system selects a
  138. // free port (a notice reporting the selected port is emitted).
  139. LocalSocksProxyPort int
  140. // LocalHttpProxyPort specifies a port number for the local HTTP proxy
  141. // running at 127.0.0.1. For the default value, 0, the system selects a
  142. // free port (a notice reporting the selected port is emitted).
  143. LocalHttpProxyPort int
  144. // DisableLocalHTTPProxy disables running the local HTTP proxy.
  145. DisableLocalHTTPProxy bool
  146. // NetworkLatencyMultiplier is a multiplier that is to be applied to
  147. // default network event timeouts. Set this to tune performance for
  148. // slow networks.
  149. // When set, must be >= 1.0.
  150. NetworkLatencyMultiplier float64
  151. // LimitTunnelProtocols indicates which protocols to use. Valid values
  152. // include: "SSH", "OSSH", "UNFRONTED-MEEK-OSSH",
  153. // "UNFRONTED-MEEK-HTTPS-OSSH", "UNFRONTED-MEEK-SESSION-TICKET-OSSH",
  154. // "FRONTED-MEEK-OSSH", "FRONTED-MEEK-HTTP-OSSH", "QUIC-OSSH",
  155. // "FRONTED-MEEK-QUIC-OSSH", "TAPDANCE-OSSH", and "CONJURE-OSSH".
  156. // For the default, an empty list, all protocols are used.
  157. LimitTunnelProtocols []string
  158. // InitialLimitTunnelProtocols is an optional initial phase of limited
  159. // protocols for the first InitialLimitTunnelProtocolsCandidateCount
  160. // candidates; after these candidates, LimitTunnelProtocols applies.
  161. //
  162. // For the default, an empty list, InitialLimitTunnelProtocols is off.
  163. InitialLimitTunnelProtocols []string
  164. // InitialLimitTunnelProtocolsCandidateCount is the number of candidates
  165. // to which InitialLimitTunnelProtocols is applied instead of
  166. // LimitTunnelProtocols.
  167. //
  168. // For the default, 0, InitialLimitTunnelProtocols is off.
  169. InitialLimitTunnelProtocolsCandidateCount int
  170. // LimitTLSProfiles indicates which TLS profiles to select from. Valid
  171. // values are listed in protocols.SupportedTLSProfiles.
  172. // For the default, an empty list, all profiles are candidates for
  173. // selection.
  174. LimitTLSProfiles []string
  175. // LimitQUICVersions indicates which QUIC versions to select from. Valid
  176. // values are listed in protocols.SupportedQUICVersions.
  177. // For the default, an empty list, all versions are candidates for
  178. // selection.
  179. LimitQUICVersions []string
  180. // EstablishTunnelTimeoutSeconds specifies a time limit after which to
  181. // halt the core tunnel controller if no tunnel has been established. The
  182. // default is parameters.EstablishTunnelTimeout.
  183. EstablishTunnelTimeoutSeconds *int
  184. // EstablishTunnelPausePeriodSeconds specifies the delay between attempts
  185. // to establish tunnels. Briefly pausing allows for network conditions to
  186. // improve and for asynchronous operations such as fetch remote server
  187. // list to complete. If omitted, a default value is used. This value is
  188. // typical overridden for testing.
  189. EstablishTunnelPausePeriodSeconds *int
  190. // EstablishTunnelPausePeriodSeconds specifies the grace period, or head
  191. // start, provided to the affinity server candidate when establishing. The
  192. // affinity server is the server used for the last established tunnel.
  193. EstablishTunnelServerAffinityGracePeriodMilliseconds *int
  194. // ConnectionWorkerPoolSize specifies how many connection attempts to
  195. // attempt in parallel. If omitted of when 0, a default is used; this is
  196. // recommended.
  197. ConnectionWorkerPoolSize int
  198. // TunnelPoolSize specifies how many tunnels to run in parallel. Port
  199. // forwards are multiplexed over multiple tunnels. If omitted or when 0,
  200. // the default is TUNNEL_POOL_SIZE, which is recommended. Any value over
  201. // MAX_TUNNEL_POOL_SIZE is treated as MAX_TUNNEL_POOL_SIZE.
  202. TunnelPoolSize int
  203. // StaggerConnectionWorkersMilliseconds adds a specified delay before
  204. // making each server candidate available to connection workers. This
  205. // option is enabled when StaggerConnectionWorkersMilliseconds > 0.
  206. StaggerConnectionWorkersMilliseconds int
  207. // LimitIntensiveConnectionWorkers limits the number of concurrent
  208. // connection workers attempting connections with resource intensive
  209. // protocols. This option is enabled when LimitIntensiveConnectionWorkers
  210. // > 0.
  211. LimitIntensiveConnectionWorkers int
  212. // LimitMeekBufferSizes selects smaller buffers for meek protocols.
  213. LimitMeekBufferSizes bool
  214. // LimitCPUThreads minimizes the number of CPU threads -- and associated
  215. // overhead -- the are used.
  216. LimitCPUThreads bool
  217. // LimitRelayBufferSizes selects smaller buffers for port forward relaying.
  218. LimitRelayBufferSizes bool
  219. // IgnoreHandshakeStatsRegexps skips compiling and using stats regexes.
  220. IgnoreHandshakeStatsRegexps bool
  221. // UpstreamProxyURL is a URL specifying an upstream proxy to use for all
  222. // outbound connections. The URL should include proxy type and
  223. // authentication information, as required. See example URLs here:
  224. // https://github.com/Psiphon-Labs/psiphon-tunnel-core/tree/master/psiphon/upstreamproxy
  225. UpstreamProxyURL string
  226. // CustomHeaders is a set of additional arbitrary HTTP headers that are
  227. // added to all plaintext HTTP requests and requests made through an HTTP
  228. // upstream proxy when specified by UpstreamProxyURL.
  229. CustomHeaders http.Header
  230. // NetworkConnectivityChecker is an interface that enables tunnel-core to
  231. // call into the host application to check for network connectivity. See:
  232. // NetworkConnectivityChecker doc.
  233. NetworkConnectivityChecker NetworkConnectivityChecker
  234. // DeviceBinder is an interface that enables tunnel-core to call into the
  235. // host application to bind sockets to specific devices. See: DeviceBinder
  236. // doc.
  237. //
  238. // When DeviceBinder is set, the "VPN" feature name is automatically added
  239. // when reporting ClientFeatures.
  240. DeviceBinder DeviceBinder
  241. // AllowDefaultDNSResolverWithBindToDevice indicates that it's safe to use
  242. // the default resolver when DeviceBinder is configured, as the host OS
  243. // will automatically exclude DNS requests from the VPN.
  244. AllowDefaultDNSResolverWithBindToDevice bool
  245. // IPv6Synthesizer is an interface that allows tunnel-core to call into
  246. // the host application to synthesize IPv6 addresses. See: IPv6Synthesizer
  247. // doc.
  248. IPv6Synthesizer IPv6Synthesizer
  249. // HasIPv6RouteGetter is an interface that allows tunnel-core to call into
  250. // the host application to determine if the host has an IPv6 route. See:
  251. // HasIPv6RouteGetter doc.
  252. HasIPv6RouteGetter HasIPv6RouteGetter
  253. // DNSServerGetter is an interface that enables tunnel-core to call into
  254. // the host application to discover the native network DNS server
  255. // settings. See: DNSServerGetter doc.
  256. DNSServerGetter DNSServerGetter
  257. // NetworkIDGetter in an interface that enables tunnel-core to call into
  258. // the host application to get an identifier for the host's current active
  259. // network. See: NetworkIDGetter doc.
  260. NetworkIDGetter NetworkIDGetter
  261. // NetworkID, when not blank, is used as the identifier for the host's
  262. // current active network.
  263. // NetworkID is ignored when NetworkIDGetter is set.
  264. NetworkID string
  265. // DisableTactics disables tactics operations including requests, payload
  266. // handling, and application of parameters.
  267. DisableTactics bool
  268. // DisableReplay causes any persisted dial parameters to be ignored when
  269. // they would otherwise be used for replay.
  270. DisableReplay bool
  271. // TargetServerEntry is an encoded server entry. When specified, this
  272. // server entry is used exclusively and all other known servers are
  273. // ignored; also, when set, ConnectionWorkerPoolSize is ignored and
  274. // the pool size is 1.
  275. TargetServerEntry string
  276. // DisableApi disables Psiphon server API calls including handshake,
  277. // connected, status, etc. This is used for special case temporary tunnels
  278. // (Windows VPN mode).
  279. DisableApi bool
  280. // TargetApiProtocol specifies whether to force use of "ssh" or "web" API
  281. // protocol. When blank, the default, the optimal API protocol is used.
  282. // Note that this capability check is not applied before the
  283. // "CandidateServers" count is emitted.
  284. //
  285. // This parameter is intended for testing and debugging only. Not all
  286. // parameters are supported in the legacy "web" API protocol, including
  287. // speed test samples.
  288. TargetApiProtocol string
  289. // RemoteServerListURLs is list of URLs which specify locations to fetch
  290. // out-of-band server entries. This facility is used when a tunnel cannot
  291. // be established to known servers. This value is supplied by and depends
  292. // on the Psiphon Network, and is typically embedded in the client binary.
  293. // All URLs must point to the same entity with the same ETag. At least one
  294. // TransferURL must have OnlyAfterAttempts = 0.
  295. RemoteServerListURLs parameters.TransferURLs
  296. // RemoteServerListSignaturePublicKey specifies a public key that's used
  297. // to authenticate the remote server list payload. This value is supplied
  298. // by and depends on the Psiphon Network, and is typically embedded in the
  299. // client binary.
  300. RemoteServerListSignaturePublicKey string
  301. // DisableRemoteServerListFetcher disables fetching remote server lists.
  302. // This is used for special case temporary tunnels.
  303. DisableRemoteServerListFetcher bool
  304. // FetchRemoteServerListRetryPeriodMilliseconds specifies the delay before
  305. // resuming a remote server list download after a failure. If omitted, a
  306. // default value is used. This value is typical overridden for testing.
  307. FetchRemoteServerListRetryPeriodMilliseconds *int
  308. // ObfuscatedServerListRootURLs is a list of URLs which specify root
  309. // locations from which to fetch obfuscated server list files. This value
  310. // is supplied by and depends on the Psiphon Network, and is typically
  311. // embedded in the client binary. All URLs must point to the same entity
  312. // with the same ETag. At least one DownloadURL must have
  313. // OnlyAfterAttempts = 0.
  314. ObfuscatedServerListRootURLs parameters.TransferURLs
  315. // UpgradeDownloadURLs is list of URLs which specify locations from which
  316. // to download a host client upgrade file, when one is available. The core
  317. // tunnel controller provides a resumable download facility which
  318. // downloads this resource and emits a notice when complete. This value is
  319. // supplied by and depends on the Psiphon Network, and is typically
  320. // embedded in the client binary. All URLs must point to the same entity
  321. // with the same ETag. At least one DownloadURL must have
  322. // OnlyAfterAttempts = 0.
  323. UpgradeDownloadURLs parameters.TransferURLs
  324. // UpgradeDownloadClientVersionHeader specifies the HTTP header name for
  325. // the entity at UpgradeDownloadURLs which specifies the client version
  326. // (an integer value). A HEAD request may be made to check the version
  327. // number available at UpgradeDownloadURLs.
  328. // UpgradeDownloadClientVersionHeader is required when UpgradeDownloadURLs
  329. // is specified.
  330. UpgradeDownloadClientVersionHeader string
  331. // FetchUpgradeRetryPeriodMilliseconds specifies the delay before resuming
  332. // a client upgrade download after a failure. If omitted, a default value
  333. // is used. This value is typical overridden for testing.
  334. FetchUpgradeRetryPeriodMilliseconds *int
  335. // FeedbackUploadURLs is a list of SecureTransferURLs which specify
  336. // locations where feedback data can be uploaded, pairing with each
  337. // location a public key with which to encrypt the feedback data. This
  338. // value is supplied by and depends on the Psiphon Network, and is
  339. // typically embedded in the client binary. At least one TransferURL must
  340. // have OnlyAfterAttempts = 0.
  341. FeedbackUploadURLs parameters.TransferURLs
  342. // FeedbackEncryptionPublicKey is a default base64-encoded, RSA public key
  343. // value used to encrypt feedback data. Used when uploading feedback with a
  344. // TransferURL which has no public key value configured, i.e.
  345. // B64EncodedPublicKey = "".
  346. FeedbackEncryptionPublicKey string
  347. // TrustedCACertificatesFilename specifies a file containing trusted CA
  348. // certs. When set, this toggles use of the trusted CA certs, specified in
  349. // TrustedCACertificatesFilename, for tunneled TLS connections that expect
  350. // server certificates signed with public certificate authorities
  351. // (currently, only upgrade downloads). This option is used with stock Go
  352. // TLS in cases where Go may fail to obtain a list of root CAs from the
  353. // operating system.
  354. TrustedCACertificatesFilename string
  355. // TransferURLsAlwaysSkipVerify, when true, forces TransferURL.SkipVerify
  356. // to true for all remote server list downloads, upgrade downloads, and
  357. // feedback uploads. Each of these transfers has additional security at
  358. // the payload level. Verifying TLS certificates is preferred, as an
  359. // additional security and circumvention layer; set
  360. // TransferURLsAlwaysSkipVerify only in cases where system root CAs
  361. // cannot be loaded; for example, if unsupported (iOS < 12) or
  362. // insufficient memory (VPN extension on iOS < 15).
  363. TransferURLsAlwaysSkipVerify bool
  364. // DisablePeriodicSshKeepAlive indicates whether to send an SSH keepalive
  365. // every 1-2 minutes, when the tunnel is idle. If the SSH keepalive times
  366. // out, the tunnel is considered to have failed.
  367. DisablePeriodicSshKeepAlive bool
  368. // DeviceRegion is the optional, reported region the host device is
  369. // running in. This input value should be a ISO 3166-1 alpha-2 country
  370. // code. The device region is reported to the server in the connected
  371. // request and recorded for Psiphon stats.
  372. //
  373. // When provided, this value may be used, pre-connection, to select
  374. // performance or circumvention optimization strategies for the given
  375. // region.
  376. DeviceRegion string
  377. // EmitDiagnosticNotices indicates whether to output notices containing
  378. // detailed information about the Psiphon session. As these notices may
  379. // contain sensitive information, they should not be insecurely distributed
  380. // or displayed to users. Default is off.
  381. EmitDiagnosticNotices bool
  382. // EmitDiagnosticNetworkParameters indicates whether to include network
  383. // parameters in diagnostic notices. As these parameters are sensitive
  384. // circumvention network information, they should not be insecurely
  385. // distributed or displayed to users. Default is off.
  386. EmitDiagnosticNetworkParameters bool
  387. // EmitBytesTransferred indicates whether to emit periodic notices showing
  388. // bytes sent and received.
  389. EmitBytesTransferred bool
  390. // EmitSLOKs indicates whether to emit notices for each seeded SLOK. As
  391. // this could reveal user browsing activity, it's intended for debugging
  392. // and testing only.
  393. EmitSLOKs bool
  394. // EmitRefractionNetworkingLogs indicates whether to emit gotapdance log
  395. // messages to stdout. Note that gotapdance log messages do not conform to
  396. // the Notice format standard. Default is off.
  397. EmitRefractionNetworkingLogs bool
  398. // EmitServerAlerts indicates whether to emit notices for server alerts.
  399. EmitServerAlerts bool
  400. // EmitClientAddress indicates whether to emit the client's public network
  401. // address, IP and port, as seen by the server.
  402. EmitClientAddress bool
  403. // RateLimits specify throttling configuration for the tunnel.
  404. RateLimits common.RateLimits
  405. // PacketTunnelTunDeviceFileDescriptor specifies a tun device file
  406. // descriptor to use for running a packet tunnel. When this value is > 0,
  407. // a packet tunnel is established through the server and packets are
  408. // relayed via the tun device file descriptor. The file descriptor is
  409. // duped in NewController. When PacketTunnelTunDeviceFileDescriptor is
  410. // set, TunnelPoolSize must be 1.
  411. PacketTunnelTunFileDescriptor int
  412. // PacketTunnelTransparentDNSIPv4Address is the IPv4 address of the DNS
  413. // server configured by a VPN using a packet tunnel. All DNS packets
  414. // destined to this DNS server are transparently redirected to the
  415. // Psiphon server DNS.
  416. PacketTunnelTransparentDNSIPv4Address string
  417. // PacketTunnelTransparentDNSIPv6Address is the IPv6 address of the DNS
  418. // server configured by a VPN using a packet tunnel. All DNS packets
  419. // destined to this DNS server are transparently redirected to the
  420. // Psiphon server DNS.
  421. PacketTunnelTransparentDNSIPv6Address string
  422. // SessionID specifies a client session ID to use in the Psiphon API. The
  423. // session ID should be a randomly generated value that is used only for a
  424. // single session, which is defined as the period between a user starting
  425. // a Psiphon client and stopping the client.
  426. //
  427. // A session ID must be 32 hex digits (lower case). When blank, a random
  428. // session ID is automatically generated. Supply a session ID when a
  429. // single client session will cross multiple Controller instances.
  430. SessionID string
  431. // Authorizations is a list of encoded, signed access control
  432. // authorizations that the client has obtained and will present to the
  433. // server.
  434. Authorizations []string
  435. // ServerEntrySignaturePublicKey is a base64-encoded, ed25519 public
  436. // key value used to verify individual server entry signatures. This value
  437. // is supplied by and depends on the Psiphon Network, and is typically
  438. // embedded in the client binary.
  439. ServerEntrySignaturePublicKey string
  440. // ExchangeObfuscationKey is a base64-encoded, NaCl secretbox key used to
  441. // obfuscate server info exchanges between clients.
  442. // Required for the exchange functionality.
  443. ExchangeObfuscationKey string
  444. // MigrateHomepageNoticesFilename migrates a homepage file from the path
  445. // previously configured with setNoticeFiles to the new path for homepage
  446. // files under the data root directory. The file specified by this config
  447. // value will be moved to config.GetHomePageFilename().
  448. //
  449. // Note: see comment for config.Commit() for a description of how file
  450. // migrations are performed.
  451. //
  452. // If not set, no migration operation will be performed.
  453. MigrateHomepageNoticesFilename string
  454. // MigrateRotatingNoticesFilename migrates notice files from the path
  455. // previously configured with setNoticeFiles to the new path for notice
  456. // files under the data root directory.
  457. //
  458. // MigrateRotatingNoticesFilename will be moved to
  459. // config.GetNoticesFilename().
  460. //
  461. // MigrateRotatingNoticesFilename.1 will be moved to
  462. // config.GetOldNoticesFilename().
  463. //
  464. // Note: see comment for config.Commit() for a description of how file
  465. // migrations are performed.
  466. //
  467. // If not set, no migration operation will be performed.
  468. MigrateRotatingNoticesFilename string
  469. // MigrateDataStoreDirectory indicates the location of the datastore
  470. // directory, as previously configured with the deprecated
  471. // DataStoreDirectory config field. Datastore files found in the specified
  472. // directory will be moved under the data root directory.
  473. //
  474. // Note: see comment for config.Commit() for a description of how file
  475. // migrations are performed.
  476. MigrateDataStoreDirectory string
  477. // MigrateRemoteServerListDownloadFilename indicates the location of
  478. // remote server list download files. The remote server list files found at
  479. // the specified path will be moved under the data root directory.
  480. //
  481. // Note: see comment for config.Commit() for a description of how file
  482. // migrations are performed.
  483. MigrateRemoteServerListDownloadFilename string
  484. // MigrateObfuscatedServerListDownloadDirectory indicates the location of
  485. // the obfuscated server list downloads directory, as previously configured
  486. // with ObfuscatedServerListDownloadDirectory. Obfuscated server list
  487. // download files found in the specified directory will be moved under the
  488. // data root directory.
  489. //
  490. // Warning: if the directory is empty after obfuscated server
  491. // list files are moved, then it will be deleted.
  492. //
  493. // Note: see comment for config.Commit() for a description of how file
  494. // migrations are performed.
  495. MigrateObfuscatedServerListDownloadDirectory string
  496. // MigrateUpgradeDownloadFilename indicates the location of downloaded
  497. // application upgrade files. Downloaded upgrade files found at the
  498. // specified path will be moved under the data root directory.
  499. //
  500. // Note: see comment for config.Commit() for a description of how file
  501. // migrations are performed.
  502. MigrateUpgradeDownloadFilename string
  503. //
  504. // The following parameters are deprecated.
  505. //
  506. // DataStoreDirectory is the directory in which to store the persistent
  507. // database, which contains information such as server entries. By
  508. // default, current working directory.
  509. //
  510. // Deprecated:
  511. // Use MigrateDataStoreDirectory. When MigrateDataStoreDirectory
  512. // is set, this parameter is ignored.
  513. //
  514. // DataStoreDirectory has been subsumed by the new data root directory,
  515. // which is configured with DataRootDirectory. If set, datastore files
  516. // found in the specified directory will be moved under the data root
  517. // directory.
  518. DataStoreDirectory string
  519. // RemoteServerListDownloadFilename specifies a target filename for
  520. // storing the remote server list download. Data is stored in co-located
  521. // files (RemoteServerListDownloadFilename.part*) to allow for resumable
  522. // downloading.
  523. //
  524. // Deprecated:
  525. // Use MigrateRemoteServerListDownloadFilename. When
  526. // MigrateRemoteServerListDownloadFilename is set, this parameter is
  527. // ignored.
  528. //
  529. // If set, remote server list download files found at the specified path
  530. // will be moved under the data root directory.
  531. RemoteServerListDownloadFilename string
  532. // ObfuscatedServerListDownloadDirectory specifies a target directory for
  533. // storing the obfuscated remote server list downloads. Data is stored in
  534. // co-located files (<OSL filename>.part*) to allow for resumable
  535. // downloading.
  536. //
  537. // Deprecated:
  538. // Use MigrateObfuscatedServerListDownloadDirectory. When
  539. // MigrateObfuscatedServerListDownloadDirectory is set, this parameter is
  540. // ignored.
  541. //
  542. // If set, obfuscated server list download files found at the specified path
  543. // will be moved under the data root directory.
  544. ObfuscatedServerListDownloadDirectory string
  545. // UpgradeDownloadFilename is the local target filename for an upgrade
  546. // download. This parameter is required when UpgradeDownloadURLs (or
  547. // UpgradeDownloadUrl) is specified. Data is stored in co-located files
  548. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  549. //
  550. // Deprecated:
  551. // Use MigrateUpgradeDownloadFilename. When MigrateUpgradeDownloadFilename
  552. // is set, this parameter is ignored.
  553. //
  554. // If set, upgrade download files found at the specified path will be moved
  555. // under the data root directory.
  556. UpgradeDownloadFilename string
  557. // TunnelProtocol indicates which protocol to use. For the default, "",
  558. // all protocols are used.
  559. //
  560. // Deprecated: Use LimitTunnelProtocols. When LimitTunnelProtocols is not
  561. // nil, this parameter is ignored.
  562. TunnelProtocol string
  563. // Deprecated: Use CustomHeaders. When CustomHeaders is not nil, this
  564. // parameter is ignored.
  565. UpstreamProxyCustomHeaders http.Header
  566. // RemoteServerListUrl is a URL which specifies a location to fetch out-
  567. // of-band server entries. This facility is used when a tunnel cannot be
  568. // established to known servers. This value is supplied by and depends on
  569. // the Psiphon Network, and is typically embedded in the client binary.
  570. //
  571. // Deprecated: Use RemoteServerListURLs. When RemoteServerListURLs is not
  572. // nil, this parameter is ignored.
  573. RemoteServerListUrl string
  574. // ObfuscatedServerListRootURL is a URL which specifies the root location
  575. // from which to fetch obfuscated server list files. This value is
  576. // supplied by and depends on the Psiphon Network, and is typically
  577. // embedded in the client binary.
  578. //
  579. // Deprecated: Use ObfuscatedServerListRootURLs. When
  580. // ObfuscatedServerListRootURLs is not nil, this parameter is ignored.
  581. ObfuscatedServerListRootURL string
  582. // UpgradeDownloadUrl specifies a URL from which to download a host client
  583. // upgrade file, when one is available. The core tunnel controller
  584. // provides a resumable download facility which downloads this resource
  585. // and emits a notice when complete. This value is supplied by and depends
  586. // on the Psiphon Network, and is typically embedded in the client binary.
  587. //
  588. // Deprecated: Use UpgradeDownloadURLs. When UpgradeDownloadURLs is not
  589. // nil, this parameter is ignored.
  590. UpgradeDownloadUrl string
  591. //
  592. // The following parameters are for testing purposes.
  593. //
  594. // TransformHostNameProbability is for testing purposes.
  595. TransformHostNameProbability *float64
  596. // FragmentorProbability and associated Fragmentor fields are for testing
  597. // purposes.
  598. FragmentorProbability *float64
  599. FragmentorLimitProtocols []string
  600. FragmentorMinTotalBytes *int
  601. FragmentorMaxTotalBytes *int
  602. FragmentorMinWriteBytes *int
  603. FragmentorMaxWriteBytes *int
  604. FragmentorMinDelayMicroseconds *int
  605. FragmentorMaxDelayMicroseconds *int
  606. // MeekTrafficShapingProbability and associated fields are for testing
  607. // purposes.
  608. MeekTrafficShapingProbability *float64
  609. MeekTrafficShapingLimitProtocols []string
  610. MeekMinTLSPadding *int
  611. MeekMaxTLSPadding *int
  612. MeekMinLimitRequestPayloadLength *int
  613. MeekMaxLimitRequestPayloadLength *int
  614. MeekRedialTLSProbability *float64
  615. MeekAlternateCookieNameProbability *float64
  616. MeekAlternateContentTypeProbability *float64
  617. // ObfuscatedSSHAlgorithms and associated ObfuscatedSSH fields are for
  618. // testing purposes. If specified, ObfuscatedSSHAlgorithms must have 4 SSH
  619. // KEX elements in order: the kex algorithm, cipher, MAC, and server host
  620. // key algorithm.
  621. ObfuscatedSSHAlgorithms []string
  622. ObfuscatedSSHMinPadding *int
  623. ObfuscatedSSHMaxPadding *int
  624. // LivenessTestMinUpstreamBytes and other LivenessTest fields are for
  625. // testing purposes.
  626. LivenessTestMinUpstreamBytes *int
  627. LivenessTestMaxUpstreamBytes *int
  628. LivenessTestMinDownstreamBytes *int
  629. LivenessTestMaxDownstreamBytes *int
  630. // ReplayCandidateCount and other Replay fields are for testing purposes.
  631. ReplayCandidateCount *int
  632. ReplayDialParametersTTLSeconds *int
  633. ReplayTargetUpstreamBytes *int
  634. ReplayTargetDownstreamBytes *int
  635. ReplayTargetTunnelDurationSeconds *int
  636. ReplayLaterRoundMoveToFrontProbability *float64
  637. ReplayRetainFailedProbability *float64
  638. // NetworkLatencyMultiplierMin and other NetworkLatencyMultiplier fields are
  639. // for testing purposes.
  640. NetworkLatencyMultiplierMin float64
  641. NetworkLatencyMultiplierMax float64
  642. NetworkLatencyMultiplierLambda float64
  643. // UseOnlyCustomTLSProfiles and other TLS configuration fields are for
  644. // testing purposes.
  645. UseOnlyCustomTLSProfiles *bool
  646. CustomTLSProfiles protocol.CustomTLSProfiles
  647. SelectRandomizedTLSProfileProbability *float64
  648. NoDefaultTLSSessionIDProbability *float64
  649. DisableFrontingProviderTLSProfiles protocol.LabeledTLSProfiles
  650. // ClientBurstUpstreamTargetBytes and other burst metric fields are for
  651. // testing purposes.
  652. ClientBurstUpstreamTargetBytes *int
  653. ClientBurstUpstreamDeadlineMilliseconds *int
  654. ClientBurstDownstreamTargetBytes *int
  655. ClientBurstDownstreamDeadlineMilliseconds *int
  656. // ApplicationParameters is for testing purposes.
  657. ApplicationParameters parameters.KeyValues
  658. // CustomHostNameRegexes and other custom host name fields are for testing
  659. // purposes.
  660. CustomHostNameRegexes []string
  661. CustomHostNameProbability *float64
  662. CustomHostNameLimitProtocols []string
  663. // ConjureCachedRegistrationTTLSeconds and other Conjure fields are for
  664. // testing purposes.
  665. ConjureCachedRegistrationTTLSeconds *int
  666. ConjureAPIRegistrarBidirectionalURL string
  667. ConjureAPIRegistrarFrontingSpecs parameters.FrontingSpecs
  668. ConjureAPIRegistrarMinDelayMilliseconds *int
  669. ConjureAPIRegistrarMaxDelayMilliseconds *int
  670. ConjureDecoyRegistrarProbability *float64
  671. ConjureDecoyRegistrarWidth *int
  672. ConjureDecoyRegistrarMinDelayMilliseconds *int
  673. ConjureDecoyRegistrarMaxDelayMilliseconds *int
  674. // HoldOffTunnelMinDurationMilliseconds and other HoldOffTunnel fields are
  675. // for testing purposes.
  676. HoldOffTunnelMinDurationMilliseconds *int
  677. HoldOffTunnelMaxDurationMilliseconds *int
  678. HoldOffTunnelProtocols []string
  679. HoldOffTunnelFrontingProviderIDs []string
  680. HoldOffTunnelProbability *float64
  681. // RestrictFrontingProviderIDs and other RestrictFrontingProviderIDs fields
  682. // are for testing purposes.
  683. RestrictFrontingProviderIDs []string
  684. RestrictFrontingProviderIDsClientProbability *float64
  685. // UpstreamProxyAllowAllServerEntrySources is for testing purposes.
  686. UpstreamProxyAllowAllServerEntrySources *bool
  687. // LimitTunnelDialPortNumbers is for testing purposes.
  688. LimitTunnelDialPortNumbers parameters.TunnelProtocolPortLists
  689. // QUICDisablePathMTUDiscoveryProbability is for testing purposes.
  690. QUICDisablePathMTUDiscoveryProbability *float64
  691. // DNSResolverAttemptsPerServer and other DNSResolver fields are for
  692. // testing purposes.
  693. DNSResolverAttemptsPerServer *int
  694. DNSResolverAttemptsPerPreferredServer *int
  695. DNSResolverRequestTimeoutMilliseconds *int
  696. DNSResolverAwaitTimeoutMilliseconds *int
  697. DNSResolverPreresolvedIPAddressCIDRs parameters.LabeledCIDRs
  698. DNSResolverPreresolvedIPAddressProbability *float64
  699. DNSResolverAlternateServers []string
  700. DNSResolverPreferredAlternateServers []string
  701. DNSResolverPreferAlternateServerProbability *float64
  702. DNSResolverProtocolTransformSpecs transforms.Specs
  703. DNSResolverProtocolTransformScopedSpecNames transforms.ScopedSpecNames
  704. DNSResolverProtocolTransformProbability *float64
  705. DNSResolverIncludeEDNS0Probability *float64
  706. DNSResolverCacheExtensionInitialTTLMilliseconds *int
  707. DNSResolverCacheExtensionVerifiedTTLMilliseconds *int
  708. DirectHTTPProtocolTransformSpecs transforms.Specs
  709. DirectHTTPProtocolTransformScopedSpecNames transforms.ScopedSpecNames
  710. DirectHTTPProtocolTransformProbability *float64
  711. FrontedHTTPProtocolTransformSpecs transforms.Specs
  712. FrontedHTTPProtocolTransformScopedSpecNames transforms.ScopedSpecNames
  713. FrontedHTTPProtocolTransformProbability *float64
  714. // params is the active parameters.Parameters with defaults, config values,
  715. // and, optionally, tactics applied.
  716. //
  717. // New tactics must be applied by calling Config.SetParameters; calling
  718. // params.Set directly will fail to add config values.
  719. params *parameters.Parameters
  720. dialParametersHash []byte
  721. dynamicConfigMutex sync.Mutex
  722. sponsorID string
  723. authorizations []string
  724. deviceBinder DeviceBinder
  725. networkIDGetter NetworkIDGetter
  726. clientFeatures []string
  727. resolverMutex sync.Mutex
  728. resolver *resolver.Resolver
  729. committed bool
  730. loadTimestamp string
  731. }
  732. // Config field which specifies if notice files should be used and at which
  733. // frequency and size they should be rotated.
  734. //
  735. // If either RotatingFileSize or RotatingSyncFrequency are <= 0, default values
  736. // are used.
  737. //
  738. // See comment for setNoticeFiles in notice.go for further details.
  739. type UseNoticeFiles struct {
  740. RotatingFileSize int
  741. RotatingSyncFrequency int
  742. }
  743. // LoadConfig parses a JSON format Psiphon config JSON string and returns a
  744. // Config struct populated with config values.
  745. //
  746. // The Config struct may then be programmatically populated with additional
  747. // values, including callbacks such as DeviceBinder.
  748. //
  749. // Before using the Config, Commit must be called, which will perform further
  750. // validation and initialize internal data structures.
  751. func LoadConfig(configJson []byte) (*Config, error) {
  752. var config Config
  753. err := json.Unmarshal(configJson, &config)
  754. if err != nil {
  755. return nil, errors.Trace(err)
  756. }
  757. config.loadTimestamp = common.TruncateTimestampToHour(
  758. common.GetCurrentTimestamp())
  759. return &config, nil
  760. }
  761. // IsCommitted checks if Commit was called.
  762. func (config *Config) IsCommitted() bool {
  763. return config.committed
  764. }
  765. // Commit validates Config fields finalizes initialization.
  766. //
  767. // Config fields should not be set after calling Config, as any changes may
  768. // not be reflected in internal data structures.
  769. //
  770. // If migrateFromLegacyFields is set to true, then an attempt to migrate from
  771. // legacy fields is made.
  772. //
  773. // Migration from legacy fields:
  774. // Config fields of the naming Migrate* (e.g. MigrateDataStoreDirectory) specify
  775. // a file migration operation which should be performed. These fields correspond
  776. // to deprecated fields, which previously could be used to specify where Psiphon
  777. // stored different sets of persistent files (e.g. MigrateDataStoreDirectory
  778. // corresponds to the deprecated field DataStoreDirectory).
  779. //
  780. // Psiphon now stores all persistent data under the configurable
  781. // DataRootDirectory (see Config.DataRootDirectory). The deprecated fields, and
  782. // corresponding Migrate* fields, are now used to specify the file or directory
  783. // path where, or under which, persistent files and directories created by
  784. // previous versions of Psiphon exist, so they can be moved under the
  785. // DataRootDirectory.
  786. //
  787. // For each migration operation:
  788. // - In the case of directories that could have defaulted to the current working
  789. // directory, persistent files and directories created by Psiphon are
  790. // precisely targeted to avoid moving files which were not created by Psiphon.
  791. // - If no file is found at the specified path, or an error is encountered while
  792. // migrating the file, then an error is logged and execution continues
  793. // normally.
  794. //
  795. // A sentinel file which signals that file migration has been completed, and
  796. // should not be attempted again, is created under DataRootDirectory after one
  797. // full pass through Commit(), regardless of whether file migration succeeds or
  798. // fails. It is better to not endlessly retry file migrations on each Commit()
  799. // because file system errors are expected to be rare and persistent files will
  800. // be re-populated over time.
  801. func (config *Config) Commit(migrateFromLegacyFields bool) error {
  802. // Do SetEmitDiagnosticNotices first, to ensure config file errors are
  803. // emitted.
  804. if config.EmitDiagnosticNotices {
  805. SetEmitDiagnosticNotices(
  806. true, config.EmitDiagnosticNetworkParameters)
  807. }
  808. // Migrate and set notice files before any operations that may emit an
  809. // error. This is to ensure config file errors are written to file when
  810. // notice files are configured with config.UseNoticeFiles.
  811. //
  812. // Note:
  813. // Errors encountered while configuring the data directory cannot be written
  814. // to notice files. This is because notices files are created within the
  815. // data directory.
  816. if config.DataRootDirectory == "" {
  817. wd, err := os.Getwd()
  818. if err != nil {
  819. return errors.Trace(common.RedactFilePathsError(err))
  820. }
  821. config.DataRootDirectory = wd
  822. }
  823. // Create root directory
  824. dataDirectoryPath := config.GetPsiphonDataDirectory()
  825. if !common.FileExists(dataDirectoryPath) {
  826. err := os.Mkdir(dataDirectoryPath, os.ModePerm)
  827. if err != nil {
  828. return errors.Tracef(
  829. "failed to create datastore directory with error: %s",
  830. common.RedactFilePathsError(err, dataDirectoryPath))
  831. }
  832. }
  833. // Check if the migration from legacy config fields has already been
  834. // completed. See the Migrate* config fields for more details.
  835. migrationCompleteFilePath := filepath.Join(config.GetPsiphonDataDirectory(), "migration_complete")
  836. needMigration := !common.FileExists(migrationCompleteFilePath)
  837. // Collect notices to emit them after notice files are set
  838. var noticeMigrationAlertMsgs []string
  839. var noticeMigrationInfoMsgs []string
  840. // Migrate notices first to ensure notice files are used for notices if
  841. // UseNoticeFiles is set.
  842. homepageFilePath := config.GetHomePageFilename()
  843. noticesFilePath := config.GetNoticesFilename()
  844. if migrateFromLegacyFields {
  845. if needMigration {
  846. // Move notice files that exist at legacy file paths under the data root
  847. // directory.
  848. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, "Config migration: need migration")
  849. noticeMigrations := migrationsFromLegacyNoticeFilePaths(config)
  850. successfulMigrations := 0
  851. for _, migration := range noticeMigrations {
  852. err := DoFileMigration(migration)
  853. if err != nil {
  854. alertMsg := fmt.Sprintf("Config migration: %s", errors.Trace(err))
  855. noticeMigrationAlertMsgs = append(noticeMigrationAlertMsgs, alertMsg)
  856. } else {
  857. successfulMigrations += 1
  858. }
  859. }
  860. infoMsg := fmt.Sprintf("Config migration: %d/%d notice files successfully migrated", successfulMigrations, len(noticeMigrations))
  861. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, infoMsg)
  862. } else {
  863. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, "Config migration: migration already completed")
  864. }
  865. }
  866. if config.UseNoticeFiles != nil {
  867. setNoticeFiles(
  868. homepageFilePath,
  869. noticesFilePath,
  870. config.UseNoticeFiles.RotatingFileSize,
  871. config.UseNoticeFiles.RotatingSyncFrequency)
  872. }
  873. // Emit notices now that notice files are set if configured
  874. for _, msg := range noticeMigrationAlertMsgs {
  875. NoticeWarning(msg)
  876. }
  877. for _, msg := range noticeMigrationInfoMsgs {
  878. NoticeInfo(msg)
  879. }
  880. // Promote legacy fields.
  881. if config.CustomHeaders == nil {
  882. config.CustomHeaders = config.UpstreamProxyCustomHeaders
  883. config.UpstreamProxyCustomHeaders = nil
  884. }
  885. if config.RemoteServerListUrl != "" && config.RemoteServerListURLs == nil {
  886. config.RemoteServerListURLs = promoteLegacyTransferURL(config.RemoteServerListUrl)
  887. }
  888. if config.ObfuscatedServerListRootURL != "" && config.ObfuscatedServerListRootURLs == nil {
  889. config.ObfuscatedServerListRootURLs = promoteLegacyTransferURL(config.ObfuscatedServerListRootURL)
  890. }
  891. if config.UpgradeDownloadUrl != "" && config.UpgradeDownloadURLs == nil {
  892. config.UpgradeDownloadURLs = promoteLegacyTransferURL(config.UpgradeDownloadUrl)
  893. }
  894. if config.TunnelProtocol != "" && len(config.LimitTunnelProtocols) == 0 {
  895. config.LimitTunnelProtocols = []string{config.TunnelProtocol}
  896. }
  897. if config.DataStoreDirectory != "" && config.MigrateDataStoreDirectory == "" {
  898. config.MigrateDataStoreDirectory = config.DataStoreDirectory
  899. }
  900. if config.RemoteServerListDownloadFilename != "" && config.MigrateRemoteServerListDownloadFilename == "" {
  901. config.MigrateRemoteServerListDownloadFilename = config.RemoteServerListDownloadFilename
  902. }
  903. if config.ObfuscatedServerListDownloadDirectory != "" && config.MigrateObfuscatedServerListDownloadDirectory == "" {
  904. config.MigrateObfuscatedServerListDownloadDirectory = config.ObfuscatedServerListDownloadDirectory
  905. }
  906. if config.UpgradeDownloadFilename != "" && config.MigrateUpgradeDownloadFilename == "" {
  907. config.MigrateUpgradeDownloadFilename = config.UpgradeDownloadFilename
  908. }
  909. // Supply default values.
  910. // Create datastore directory.
  911. dataStoreDirectoryPath := config.GetDataStoreDirectory()
  912. if !common.FileExists(dataStoreDirectoryPath) {
  913. err := os.Mkdir(dataStoreDirectoryPath, os.ModePerm)
  914. if err != nil {
  915. return errors.Tracef(
  916. "failed to create datastore directory with error: %s",
  917. common.RedactFilePathsError(err, dataStoreDirectoryPath))
  918. }
  919. }
  920. // Create OSL directory.
  921. oslDirectoryPath := config.GetObfuscatedServerListDownloadDirectory()
  922. if !common.FileExists(oslDirectoryPath) {
  923. err := os.Mkdir(oslDirectoryPath, os.ModePerm)
  924. if err != nil {
  925. return errors.Tracef(
  926. "failed to create osl directory with error: %s",
  927. common.RedactFilePathsError(err, oslDirectoryPath))
  928. }
  929. }
  930. if config.ClientVersion == "" {
  931. config.ClientVersion = "0"
  932. }
  933. if config.TunnelPoolSize == 0 {
  934. config.TunnelPoolSize = TUNNEL_POOL_SIZE
  935. }
  936. // Validate config fields.
  937. if !common.FileExists(config.DataRootDirectory) {
  938. return errors.TraceNew("DataRootDirectory does not exist")
  939. }
  940. if config.PropagationChannelId == "" {
  941. return errors.TraceNew("propagation channel ID is missing from the configuration file")
  942. }
  943. if config.SponsorId == "" {
  944. return errors.TraceNew("sponsor ID is missing from the configuration file")
  945. }
  946. _, err := strconv.Atoi(config.ClientVersion)
  947. if err != nil {
  948. return errors.Tracef("invalid client version: %s", err)
  949. }
  950. if !common.Contains(
  951. []string{"", protocol.PSIPHON_SSH_API_PROTOCOL, protocol.PSIPHON_WEB_API_PROTOCOL},
  952. config.TargetApiProtocol) {
  953. return errors.TraceNew("invalid TargetApiProtocol")
  954. }
  955. if !config.DisableRemoteServerListFetcher {
  956. if config.RemoteServerListURLs != nil {
  957. if config.RemoteServerListSignaturePublicKey == "" {
  958. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  959. }
  960. }
  961. if config.ObfuscatedServerListRootURLs != nil {
  962. if config.RemoteServerListSignaturePublicKey == "" {
  963. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  964. }
  965. }
  966. }
  967. if config.UpgradeDownloadURLs != nil {
  968. if config.UpgradeDownloadClientVersionHeader == "" {
  969. return errors.TraceNew("missing UpgradeDownloadClientVersionHeader")
  970. }
  971. }
  972. if config.FeedbackUploadURLs != nil {
  973. if config.FeedbackEncryptionPublicKey == "" {
  974. return errors.TraceNew("missing FeedbackEncryptionPublicKey")
  975. }
  976. }
  977. // This constraint is expected by logic in Controller.runTunnels().
  978. if config.PacketTunnelTunFileDescriptor > 0 && config.TunnelPoolSize != 1 {
  979. return errors.TraceNew("packet tunnel mode requires TunnelPoolSize to be 1")
  980. }
  981. // SessionID must be PSIPHON_API_CLIENT_SESSION_ID_LENGTH lowercase hex-encoded bytes.
  982. if config.SessionID == "" {
  983. sessionID, err := MakeSessionId()
  984. if err != nil {
  985. return errors.Trace(err)
  986. }
  987. config.SessionID = sessionID
  988. }
  989. if len(config.SessionID) != 2*protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH ||
  990. -1 != strings.IndexFunc(config.SessionID, func(c rune) bool {
  991. return !unicode.Is(unicode.ASCII_Hex_Digit, c) || unicode.IsUpper(c)
  992. }) {
  993. return errors.TraceNew("invalid SessionID")
  994. }
  995. config.params, err = parameters.NewParameters(
  996. func(err error) {
  997. NoticeWarning("Parameters getValue failed: %s", err)
  998. })
  999. if err != nil {
  1000. return errors.Trace(err)
  1001. }
  1002. if config.ObfuscatedSSHAlgorithms != nil &&
  1003. len(config.ObfuscatedSSHAlgorithms) != 4 {
  1004. // TODO: validate each algorithm?
  1005. return errors.TraceNew("invalid ObfuscatedSSHAlgorithms")
  1006. }
  1007. // parametersParameters.Set will validate the config fields applied to
  1008. // parametersParameters.
  1009. err = config.SetParameters("", false, nil)
  1010. if err != nil {
  1011. return errors.Trace(err)
  1012. }
  1013. // Calculate and set the dial parameters hash. After this point, related
  1014. // config fields must not change.
  1015. config.setDialParametersHash()
  1016. // Set defaults for dynamic config fields.
  1017. config.SetDynamicConfig(config.SponsorId, config.Authorizations)
  1018. // Initialize config.deviceBinder and config.config.networkIDGetter. These
  1019. // wrap config.DeviceBinder and config.NetworkIDGetter/NetworkID with
  1020. // loggers.
  1021. //
  1022. // New variables are set to avoid mutating input config fields.
  1023. // Internally, code must use config.deviceBinder and
  1024. // config.networkIDGetter and not the input/exported fields.
  1025. if config.DeviceBinder != nil {
  1026. config.deviceBinder = newLoggingDeviceBinder(config.DeviceBinder)
  1027. }
  1028. networkIDGetter := config.NetworkIDGetter
  1029. if networkIDGetter == nil {
  1030. // Limitation: unlike NetworkIDGetter, which calls back to platform APIs
  1031. // this method of network identification is not dynamic and will not reflect
  1032. // network changes that occur while running.
  1033. if config.NetworkID != "" {
  1034. networkIDGetter = newStaticNetworkGetter(config.NetworkID)
  1035. } else {
  1036. networkIDGetter = newStaticNetworkGetter("UNKNOWN")
  1037. }
  1038. }
  1039. config.networkIDGetter = newLoggingNetworkIDGetter(networkIDGetter)
  1040. // Initialize config.clientFeatures, which adds feature names on top of
  1041. // those specified by the host application in config.ClientFeatures.
  1042. config.clientFeatures = config.ClientFeatures
  1043. feature := "VPN"
  1044. if config.DeviceBinder != nil && !common.Contains(config.clientFeatures, feature) {
  1045. config.clientFeatures = append(config.clientFeatures, feature)
  1046. }
  1047. // Migrate from old config fields. This results in files being moved under
  1048. // a config specified data root directory.
  1049. if migrateFromLegacyFields && needMigration {
  1050. // If unset, set MigrateDataStoreDirectory to the previous default value for
  1051. // DataStoreDirectory to ensure that datastore files are migrated.
  1052. if config.MigrateDataStoreDirectory == "" {
  1053. wd, err := os.Getwd()
  1054. if err != nil {
  1055. return errors.Trace(err)
  1056. }
  1057. NoticeInfo("MigrateDataStoreDirectory unset, using working directory")
  1058. config.MigrateDataStoreDirectory = wd
  1059. }
  1060. // Move files that exist at legacy file paths under the data root
  1061. // directory.
  1062. migrations, err := migrationsFromLegacyFilePaths(config)
  1063. if err != nil {
  1064. return errors.Trace(err)
  1065. }
  1066. // Do migrations
  1067. successfulMigrations := 0
  1068. for _, migration := range migrations {
  1069. err := DoFileMigration(migration)
  1070. if err != nil {
  1071. NoticeWarning("Config migration: %s", errors.Trace(err))
  1072. } else {
  1073. successfulMigrations += 1
  1074. }
  1075. }
  1076. NoticeInfo(fmt.Sprintf(
  1077. "Config migration: %d/%d legacy files successfully migrated",
  1078. successfulMigrations, len(migrations)))
  1079. // Remove OSL directory if empty
  1080. if config.MigrateObfuscatedServerListDownloadDirectory != "" {
  1081. files, err := ioutil.ReadDir(config.MigrateObfuscatedServerListDownloadDirectory)
  1082. if err != nil {
  1083. NoticeWarning(
  1084. "Error reading OSL directory: %s",
  1085. errors.Trace(common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory)))
  1086. } else if len(files) == 0 {
  1087. err := os.Remove(config.MigrateObfuscatedServerListDownloadDirectory)
  1088. if err != nil {
  1089. NoticeWarning(
  1090. "Error deleting empty OSL directory: %s",
  1091. errors.Trace(common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory)))
  1092. }
  1093. }
  1094. }
  1095. f, err := os.Create(migrationCompleteFilePath)
  1096. if err != nil {
  1097. NoticeWarning(
  1098. "Config migration: failed to create migration completed file with error %s",
  1099. errors.Trace(common.RedactFilePathsError(err, migrationCompleteFilePath)))
  1100. } else {
  1101. NoticeInfo("Config migration: completed")
  1102. f.Close()
  1103. }
  1104. }
  1105. config.committed = true
  1106. return nil
  1107. }
  1108. // GetParameters returns the current parameters.Parameters.
  1109. func (config *Config) GetParameters() *parameters.Parameters {
  1110. return config.params
  1111. }
  1112. // SetParameters resets the parameters.Parameters to the default values,
  1113. // applies any config file values, and then applies the input parameters (from
  1114. // tactics, etc.)
  1115. //
  1116. // Set skipOnError to false when initially applying only config values, as
  1117. // this will validate the values and should fail. Set skipOnError to true when
  1118. // applying tactics to ignore invalid or unknown parameter values from tactics.
  1119. //
  1120. // In the case of applying tactics, do not call Config.parameters.Set
  1121. // directly as this will not first apply config values.
  1122. //
  1123. // If there is an error, the existing Config.parameters are left
  1124. // entirely unmodified.
  1125. func (config *Config) SetParameters(tag string, skipOnError bool, applyParameters map[string]interface{}) error {
  1126. setParameters := []map[string]interface{}{config.makeConfigParameters()}
  1127. if applyParameters != nil {
  1128. setParameters = append(setParameters, applyParameters)
  1129. }
  1130. counts, err := config.params.Set(tag, skipOnError, setParameters...)
  1131. if err != nil {
  1132. return errors.Trace(err)
  1133. }
  1134. NoticeInfo("applied %v parameters with tag '%s'", counts, tag)
  1135. // Emit certain individual parameter values for quick reference in diagnostics.
  1136. p := config.params.Get()
  1137. NoticeInfo(
  1138. "NetworkLatencyMultiplier Min/Max/Lambda: %f/%f/%f",
  1139. p.Float(parameters.NetworkLatencyMultiplierMin),
  1140. p.Float(parameters.NetworkLatencyMultiplierMax),
  1141. p.Float(parameters.NetworkLatencyMultiplierLambda))
  1142. // Application Parameters are feature flags/config info, delivered as Client
  1143. // Parameters via tactics/etc., to be communicated to the outer application.
  1144. // Emit these now, as notices.
  1145. if p.WeightedCoinFlip(parameters.ApplicationParametersProbability) {
  1146. NoticeApplicationParameters(p.KeyValues(parameters.ApplicationParameters))
  1147. }
  1148. return nil
  1149. }
  1150. // SetResolver sets the current resolver.
  1151. func (config *Config) SetResolver(resolver *resolver.Resolver) {
  1152. config.resolverMutex.Lock()
  1153. defer config.resolverMutex.Unlock()
  1154. config.resolver = resolver
  1155. }
  1156. // GetResolver returns the current resolver. May return nil.
  1157. func (config *Config) GetResolver() *resolver.Resolver {
  1158. config.resolverMutex.Lock()
  1159. defer config.resolverMutex.Unlock()
  1160. return config.resolver
  1161. }
  1162. // SetDynamicConfig sets the current client sponsor ID and authorizations.
  1163. // Invalid values for sponsor ID are ignored. The caller must not modify the
  1164. // input authorizations slice.
  1165. func (config *Config) SetDynamicConfig(sponsorID string, authorizations []string) {
  1166. config.dynamicConfigMutex.Lock()
  1167. defer config.dynamicConfigMutex.Unlock()
  1168. if sponsorID != "" {
  1169. config.sponsorID = sponsorID
  1170. }
  1171. config.authorizations = authorizations
  1172. }
  1173. // GetSponsorID returns the current client sponsor ID.
  1174. func (config *Config) GetSponsorID() string {
  1175. config.dynamicConfigMutex.Lock()
  1176. defer config.dynamicConfigMutex.Unlock()
  1177. return config.sponsorID
  1178. }
  1179. // IsSplitTunnelEnabled indicates if split tunnel mode is enabled, either for
  1180. // the client's own country, a specified list of countries, or both.
  1181. func (config *Config) IsSplitTunnelEnabled() bool {
  1182. return config.SplitTunnelOwnRegion || len(config.SplitTunnelRegions) > 0
  1183. }
  1184. // GetAuthorizations returns the current client authorizations.
  1185. // The caller must not modify the returned slice.
  1186. func (config *Config) GetAuthorizations() []string {
  1187. config.dynamicConfigMutex.Lock()
  1188. defer config.dynamicConfigMutex.Unlock()
  1189. return config.authorizations
  1190. }
  1191. // GetPsiphonDataDirectory returns the directory under which all persistent
  1192. // files should be stored. This directory is created under
  1193. // config.DataRootDirectory. The motivation for an additional directory is that
  1194. // config.DataRootDirectory defaults to the current working directory, which may
  1195. // include non-tunnel-core files that should be excluded from directory-spanning
  1196. // operations (e.g. excluding all tunnel-core files from backup).
  1197. func (config *Config) GetPsiphonDataDirectory() string {
  1198. return filepath.Join(config.DataRootDirectory, PsiphonDataDirectoryName)
  1199. }
  1200. // GetHomePageFilename the path where the homepage notices file will be created.
  1201. func (config *Config) GetHomePageFilename() string {
  1202. return filepath.Join(config.GetPsiphonDataDirectory(), HomepageFilename)
  1203. }
  1204. // GetNoticesFilename returns the path where the notices file will be created.
  1205. // When the file is rotated it will be moved to config.GetOldNoticesFilename().
  1206. func (config *Config) GetNoticesFilename() string {
  1207. return filepath.Join(config.GetPsiphonDataDirectory(), NoticesFilename)
  1208. }
  1209. // GetOldNoticeFilename returns the path where the rotated notices file will be
  1210. // created.
  1211. func (config *Config) GetOldNoticesFilename() string {
  1212. return filepath.Join(config.GetPsiphonDataDirectory(), OldNoticesFilename)
  1213. }
  1214. // GetDataStoreDirectory returns the directory in which the persistent database
  1215. // will be stored. Created in Config.Commit(). The persistent database contains
  1216. // information such as server entries.
  1217. func (config *Config) GetDataStoreDirectory() string {
  1218. return filepath.Join(config.GetPsiphonDataDirectory(), "datastore")
  1219. }
  1220. // GetObfuscatedServerListDownloadDirectory returns the directory in which
  1221. // obfuscated remote server list downloads will be stored. Created in
  1222. // Config.Commit().
  1223. func (config *Config) GetObfuscatedServerListDownloadDirectory() string {
  1224. return filepath.Join(config.GetPsiphonDataDirectory(), "osl")
  1225. }
  1226. // GetRemoteServerListDownloadFilename returns the filename where the remote
  1227. // server list download will be stored. Data is stored in co-located files
  1228. // (RemoteServerListDownloadFilename.part*) to allow for resumable downloading.
  1229. func (config *Config) GetRemoteServerListDownloadFilename() string {
  1230. return filepath.Join(config.GetPsiphonDataDirectory(), "remote_server_list")
  1231. }
  1232. // GetUpgradeDownloadFilename specifies the filename where upgrade downloads
  1233. // will be stored. This filename is valid when UpgradeDownloadURLs
  1234. // (or UpgradeDownloadUrl) is specified. Data is stored in co-located files
  1235. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  1236. func (config *Config) GetUpgradeDownloadFilename() string {
  1237. return filepath.Join(config.GetPsiphonDataDirectory(), UpgradeDownloadFilename)
  1238. }
  1239. // UseUpstreamProxy indicates if an upstream proxy has been
  1240. // configured.
  1241. func (config *Config) UseUpstreamProxy() bool {
  1242. return config.UpstreamProxyURL != ""
  1243. }
  1244. // GetNetworkID returns the current network ID. When NetworkIDGetter
  1245. // is set, this calls into the host application; otherwise, a default
  1246. // value is returned.
  1247. func (config *Config) GetNetworkID() string {
  1248. return config.networkIDGetter.GetNetworkID()
  1249. }
  1250. func (config *Config) makeConfigParameters() map[string]interface{} {
  1251. // Build set of config values to apply to parameters.
  1252. //
  1253. // Note: names of some config fields such as
  1254. // StaggerConnectionWorkersMilliseconds and LimitMeekBufferSizes have
  1255. // changed in the parameters. The existing config fields are retained for
  1256. // backwards compatibility.
  1257. applyParameters := make(map[string]interface{})
  1258. // To support platform clients that configure NetworkLatencyMultiplier, set
  1259. // the NetworkLatencyMultiplierMin/NetworkLatencyMultiplierMax range to the
  1260. // specified value. Also set the older NetworkLatencyMultiplier tactic, since
  1261. // that will be used in the case of replaying with dial parameters persisted
  1262. // by an older client version.
  1263. if config.NetworkLatencyMultiplier > 0.0 {
  1264. applyParameters[parameters.NetworkLatencyMultiplier] = config.NetworkLatencyMultiplier
  1265. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplier
  1266. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplier
  1267. }
  1268. if config.NetworkLatencyMultiplierMin > 0.0 {
  1269. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplierMin
  1270. }
  1271. if config.NetworkLatencyMultiplierMax > 0.0 {
  1272. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplierMax
  1273. }
  1274. if config.NetworkLatencyMultiplierLambda > 0.0 {
  1275. applyParameters[parameters.NetworkLatencyMultiplierLambda] = config.NetworkLatencyMultiplierLambda
  1276. }
  1277. if len(config.LimitTunnelProtocols) > 0 {
  1278. applyParameters[parameters.LimitTunnelProtocols] = protocol.TunnelProtocols(config.LimitTunnelProtocols)
  1279. }
  1280. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  1281. applyParameters[parameters.InitialLimitTunnelProtocols] = protocol.TunnelProtocols(config.InitialLimitTunnelProtocols)
  1282. applyParameters[parameters.InitialLimitTunnelProtocolsCandidateCount] = config.InitialLimitTunnelProtocolsCandidateCount
  1283. }
  1284. if len(config.LimitTLSProfiles) > 0 {
  1285. applyParameters[parameters.LimitTLSProfiles] = protocol.TunnelProtocols(config.LimitTLSProfiles)
  1286. }
  1287. if len(config.LimitQUICVersions) > 0 {
  1288. applyParameters[parameters.LimitQUICVersions] = protocol.QUICVersions(config.LimitQUICVersions)
  1289. }
  1290. if config.EstablishTunnelTimeoutSeconds != nil {
  1291. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  1292. }
  1293. if config.EstablishTunnelServerAffinityGracePeriodMilliseconds != nil {
  1294. applyParameters[parameters.EstablishTunnelServerAffinityGracePeriod] = fmt.Sprintf("%dms", *config.EstablishTunnelServerAffinityGracePeriodMilliseconds)
  1295. }
  1296. if config.EstablishTunnelPausePeriodSeconds != nil {
  1297. applyParameters[parameters.EstablishTunnelPausePeriod] = fmt.Sprintf("%ds", *config.EstablishTunnelPausePeriodSeconds)
  1298. }
  1299. if config.ConnectionWorkerPoolSize != 0 {
  1300. applyParameters[parameters.ConnectionWorkerPoolSize] = config.ConnectionWorkerPoolSize
  1301. }
  1302. if config.TunnelPoolSize != 0 {
  1303. applyParameters[parameters.TunnelPoolSize] = config.TunnelPoolSize
  1304. }
  1305. if config.StaggerConnectionWorkersMilliseconds > 0 {
  1306. applyParameters[parameters.StaggerConnectionWorkersPeriod] = fmt.Sprintf("%dms", config.StaggerConnectionWorkersMilliseconds)
  1307. }
  1308. if config.LimitIntensiveConnectionWorkers > 0 {
  1309. applyParameters[parameters.LimitIntensiveConnectionWorkers] = config.LimitIntensiveConnectionWorkers
  1310. }
  1311. applyParameters[parameters.MeekLimitBufferSizes] = config.LimitMeekBufferSizes
  1312. applyParameters[parameters.IgnoreHandshakeStatsRegexps] = config.IgnoreHandshakeStatsRegexps
  1313. if config.EstablishTunnelTimeoutSeconds != nil {
  1314. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  1315. }
  1316. if config.FetchRemoteServerListRetryPeriodMilliseconds != nil {
  1317. applyParameters[parameters.FetchRemoteServerListRetryPeriod] = fmt.Sprintf("%dms", *config.FetchRemoteServerListRetryPeriodMilliseconds)
  1318. }
  1319. if config.FetchUpgradeRetryPeriodMilliseconds != nil {
  1320. applyParameters[parameters.FetchUpgradeRetryPeriod] = fmt.Sprintf("%dms", *config.FetchUpgradeRetryPeriodMilliseconds)
  1321. }
  1322. if !config.DisableRemoteServerListFetcher {
  1323. if config.RemoteServerListURLs != nil {
  1324. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  1325. applyParameters[parameters.RemoteServerListURLs] = config.RemoteServerListURLs
  1326. }
  1327. if config.ObfuscatedServerListRootURLs != nil {
  1328. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  1329. applyParameters[parameters.ObfuscatedServerListRootURLs] = config.ObfuscatedServerListRootURLs
  1330. }
  1331. }
  1332. if config.UpgradeDownloadURLs != nil {
  1333. applyParameters[parameters.UpgradeDownloadClientVersionHeader] = config.UpgradeDownloadClientVersionHeader
  1334. applyParameters[parameters.UpgradeDownloadURLs] = config.UpgradeDownloadURLs
  1335. }
  1336. if len(config.FeedbackUploadURLs) > 0 {
  1337. applyParameters[parameters.FeedbackUploadURLs] = config.FeedbackUploadURLs
  1338. }
  1339. if config.FeedbackEncryptionPublicKey != "" {
  1340. applyParameters[parameters.FeedbackEncryptionPublicKey] = config.FeedbackEncryptionPublicKey
  1341. }
  1342. applyParameters[parameters.TunnelRateLimits] = config.RateLimits
  1343. if config.TransformHostNameProbability != nil {
  1344. applyParameters[parameters.TransformHostNameProbability] = *config.TransformHostNameProbability
  1345. }
  1346. if config.FragmentorProbability != nil {
  1347. applyParameters[parameters.FragmentorProbability] = *config.FragmentorProbability
  1348. }
  1349. if len(config.FragmentorLimitProtocols) > 0 {
  1350. applyParameters[parameters.FragmentorLimitProtocols] = protocol.TunnelProtocols(config.FragmentorLimitProtocols)
  1351. }
  1352. if config.FragmentorMinTotalBytes != nil {
  1353. applyParameters[parameters.FragmentorMinTotalBytes] = *config.FragmentorMinTotalBytes
  1354. }
  1355. if config.FragmentorMaxTotalBytes != nil {
  1356. applyParameters[parameters.FragmentorMaxTotalBytes] = *config.FragmentorMaxTotalBytes
  1357. }
  1358. if config.FragmentorMinWriteBytes != nil {
  1359. applyParameters[parameters.FragmentorMinWriteBytes] = *config.FragmentorMinWriteBytes
  1360. }
  1361. if config.FragmentorMaxWriteBytes != nil {
  1362. applyParameters[parameters.FragmentorMaxWriteBytes] = *config.FragmentorMaxWriteBytes
  1363. }
  1364. if config.FragmentorMinDelayMicroseconds != nil {
  1365. applyParameters[parameters.FragmentorMinDelay] = fmt.Sprintf("%dus", *config.FragmentorMinDelayMicroseconds)
  1366. }
  1367. if config.FragmentorMaxDelayMicroseconds != nil {
  1368. applyParameters[parameters.FragmentorMaxDelay] = fmt.Sprintf("%dus", *config.FragmentorMaxDelayMicroseconds)
  1369. }
  1370. if config.MeekTrafficShapingProbability != nil {
  1371. applyParameters[parameters.MeekTrafficShapingProbability] = *config.MeekTrafficShapingProbability
  1372. }
  1373. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  1374. applyParameters[parameters.MeekTrafficShapingLimitProtocols] = protocol.TunnelProtocols(config.MeekTrafficShapingLimitProtocols)
  1375. }
  1376. if config.MeekMinTLSPadding != nil {
  1377. applyParameters[parameters.MeekMinTLSPadding] = *config.MeekMinTLSPadding
  1378. }
  1379. if config.MeekMaxTLSPadding != nil {
  1380. applyParameters[parameters.MeekMaxTLSPadding] = *config.MeekMaxTLSPadding
  1381. }
  1382. if config.MeekMinLimitRequestPayloadLength != nil {
  1383. applyParameters[parameters.MeekMinLimitRequestPayloadLength] = *config.MeekMinLimitRequestPayloadLength
  1384. }
  1385. if config.MeekMaxLimitRequestPayloadLength != nil {
  1386. applyParameters[parameters.MeekMaxLimitRequestPayloadLength] = *config.MeekMaxLimitRequestPayloadLength
  1387. }
  1388. if config.MeekRedialTLSProbability != nil {
  1389. applyParameters[parameters.MeekRedialTLSProbability] = *config.MeekRedialTLSProbability
  1390. }
  1391. if config.MeekAlternateCookieNameProbability != nil {
  1392. applyParameters[parameters.MeekAlternateCookieNameProbability] = *config.MeekAlternateCookieNameProbability
  1393. }
  1394. if config.MeekAlternateContentTypeProbability != nil {
  1395. applyParameters[parameters.MeekAlternateContentTypeProbability] = *config.MeekAlternateContentTypeProbability
  1396. }
  1397. if config.ObfuscatedSSHMinPadding != nil {
  1398. applyParameters[parameters.ObfuscatedSSHMinPadding] = *config.ObfuscatedSSHMinPadding
  1399. }
  1400. if config.ObfuscatedSSHMaxPadding != nil {
  1401. applyParameters[parameters.ObfuscatedSSHMaxPadding] = *config.ObfuscatedSSHMaxPadding
  1402. }
  1403. if config.LivenessTestMinUpstreamBytes != nil {
  1404. applyParameters[parameters.LivenessTestMinUpstreamBytes] = *config.LivenessTestMinUpstreamBytes
  1405. }
  1406. if config.LivenessTestMaxUpstreamBytes != nil {
  1407. applyParameters[parameters.LivenessTestMaxUpstreamBytes] = *config.LivenessTestMaxUpstreamBytes
  1408. }
  1409. if config.LivenessTestMinDownstreamBytes != nil {
  1410. applyParameters[parameters.LivenessTestMinDownstreamBytes] = *config.LivenessTestMinDownstreamBytes
  1411. }
  1412. if config.LivenessTestMaxDownstreamBytes != nil {
  1413. applyParameters[parameters.LivenessTestMaxDownstreamBytes] = *config.LivenessTestMaxDownstreamBytes
  1414. }
  1415. if config.ReplayCandidateCount != nil {
  1416. applyParameters[parameters.ReplayCandidateCount] = *config.ReplayCandidateCount
  1417. }
  1418. if config.ReplayDialParametersTTLSeconds != nil {
  1419. applyParameters[parameters.ReplayDialParametersTTL] = fmt.Sprintf("%ds", *config.ReplayDialParametersTTLSeconds)
  1420. }
  1421. if config.ReplayTargetUpstreamBytes != nil {
  1422. applyParameters[parameters.ReplayTargetUpstreamBytes] = *config.ReplayTargetUpstreamBytes
  1423. }
  1424. if config.ReplayTargetDownstreamBytes != nil {
  1425. applyParameters[parameters.ReplayTargetDownstreamBytes] = *config.ReplayTargetDownstreamBytes
  1426. }
  1427. if config.ReplayTargetTunnelDurationSeconds != nil {
  1428. applyParameters[parameters.ReplayTargetTunnelDuration] = fmt.Sprintf("%ds", *config.ReplayTargetTunnelDurationSeconds)
  1429. }
  1430. if config.ReplayLaterRoundMoveToFrontProbability != nil {
  1431. applyParameters[parameters.ReplayLaterRoundMoveToFrontProbability] = *config.ReplayLaterRoundMoveToFrontProbability
  1432. }
  1433. if config.ReplayRetainFailedProbability != nil {
  1434. applyParameters[parameters.ReplayRetainFailedProbability] = *config.ReplayRetainFailedProbability
  1435. }
  1436. if config.UseOnlyCustomTLSProfiles != nil {
  1437. applyParameters[parameters.UseOnlyCustomTLSProfiles] = *config.UseOnlyCustomTLSProfiles
  1438. }
  1439. if len(config.CustomTLSProfiles) > 0 {
  1440. applyParameters[parameters.CustomTLSProfiles] = config.CustomTLSProfiles
  1441. }
  1442. if config.SelectRandomizedTLSProfileProbability != nil {
  1443. applyParameters[parameters.SelectRandomizedTLSProfileProbability] = *config.SelectRandomizedTLSProfileProbability
  1444. }
  1445. if config.NoDefaultTLSSessionIDProbability != nil {
  1446. applyParameters[parameters.NoDefaultTLSSessionIDProbability] = *config.NoDefaultTLSSessionIDProbability
  1447. }
  1448. if len(config.DisableFrontingProviderTLSProfiles) > 0 {
  1449. applyParameters[parameters.DisableFrontingProviderTLSProfiles] = config.DisableFrontingProviderTLSProfiles
  1450. }
  1451. if config.ClientBurstUpstreamTargetBytes != nil {
  1452. applyParameters[parameters.ClientBurstUpstreamTargetBytes] = *config.ClientBurstUpstreamTargetBytes
  1453. }
  1454. if config.ClientBurstUpstreamDeadlineMilliseconds != nil {
  1455. applyParameters[parameters.ClientBurstUpstreamDeadline] = fmt.Sprintf("%dms", *config.ClientBurstUpstreamDeadlineMilliseconds)
  1456. }
  1457. if config.ClientBurstDownstreamTargetBytes != nil {
  1458. applyParameters[parameters.ClientBurstDownstreamTargetBytes] = *config.ClientBurstDownstreamTargetBytes
  1459. }
  1460. if config.ClientBurstDownstreamDeadlineMilliseconds != nil {
  1461. applyParameters[parameters.ClientBurstDownstreamDeadline] = fmt.Sprintf("%dms", *config.ClientBurstDownstreamDeadlineMilliseconds)
  1462. }
  1463. if config.ApplicationParameters != nil {
  1464. applyParameters[parameters.ApplicationParameters] = config.ApplicationParameters
  1465. }
  1466. if config.CustomHostNameRegexes != nil {
  1467. applyParameters[parameters.CustomHostNameRegexes] = parameters.RegexStrings(config.CustomHostNameRegexes)
  1468. }
  1469. if config.CustomHostNameProbability != nil {
  1470. applyParameters[parameters.CustomHostNameProbability] = *config.CustomHostNameProbability
  1471. }
  1472. if config.CustomHostNameLimitProtocols != nil {
  1473. applyParameters[parameters.CustomHostNameLimitProtocols] = protocol.TunnelProtocols(config.CustomHostNameLimitProtocols)
  1474. }
  1475. if config.ConjureCachedRegistrationTTLSeconds != nil {
  1476. applyParameters[parameters.ConjureCachedRegistrationTTL] = fmt.Sprintf("%ds", *config.ConjureCachedRegistrationTTLSeconds)
  1477. }
  1478. if config.ConjureAPIRegistrarBidirectionalURL != "" {
  1479. applyParameters[parameters.ConjureAPIRegistrarBidirectionalURL] = config.ConjureAPIRegistrarBidirectionalURL
  1480. }
  1481. if len(config.ConjureAPIRegistrarFrontingSpecs) > 0 {
  1482. applyParameters[parameters.ConjureAPIRegistrarFrontingSpecs] = config.ConjureAPIRegistrarFrontingSpecs
  1483. }
  1484. if config.ConjureAPIRegistrarMinDelayMilliseconds != nil {
  1485. applyParameters[parameters.ConjureAPIRegistrarMinDelay] = fmt.Sprintf("%dms", *config.ConjureAPIRegistrarMinDelayMilliseconds)
  1486. }
  1487. if config.ConjureAPIRegistrarMaxDelayMilliseconds != nil {
  1488. applyParameters[parameters.ConjureAPIRegistrarMaxDelay] = fmt.Sprintf("%dms", *config.ConjureAPIRegistrarMaxDelayMilliseconds)
  1489. }
  1490. if config.ConjureDecoyRegistrarProbability != nil {
  1491. applyParameters[parameters.ConjureDecoyRegistrarProbability] = *config.ConjureDecoyRegistrarProbability
  1492. }
  1493. if config.ConjureDecoyRegistrarWidth != nil {
  1494. applyParameters[parameters.ConjureDecoyRegistrarWidth] = *config.ConjureDecoyRegistrarWidth
  1495. }
  1496. if config.ConjureDecoyRegistrarMinDelayMilliseconds != nil {
  1497. applyParameters[parameters.ConjureDecoyRegistrarMinDelay] = fmt.Sprintf("%dms", *config.ConjureDecoyRegistrarMinDelayMilliseconds)
  1498. }
  1499. if config.ConjureDecoyRegistrarMaxDelayMilliseconds != nil {
  1500. applyParameters[parameters.ConjureDecoyRegistrarMaxDelay] = fmt.Sprintf("%dms", *config.ConjureDecoyRegistrarMaxDelayMilliseconds)
  1501. }
  1502. if config.HoldOffTunnelMinDurationMilliseconds != nil {
  1503. applyParameters[parameters.HoldOffTunnelMinDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMinDurationMilliseconds)
  1504. }
  1505. if config.HoldOffTunnelMaxDurationMilliseconds != nil {
  1506. applyParameters[parameters.HoldOffTunnelMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMaxDurationMilliseconds)
  1507. }
  1508. if len(config.HoldOffTunnelProtocols) > 0 {
  1509. applyParameters[parameters.HoldOffTunnelProtocols] = protocol.TunnelProtocols(config.HoldOffTunnelProtocols)
  1510. }
  1511. if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
  1512. applyParameters[parameters.HoldOffTunnelFrontingProviderIDs] = config.HoldOffTunnelFrontingProviderIDs
  1513. }
  1514. if config.HoldOffTunnelProbability != nil {
  1515. applyParameters[parameters.HoldOffTunnelProbability] = *config.HoldOffTunnelProbability
  1516. }
  1517. if len(config.RestrictFrontingProviderIDs) > 0 {
  1518. applyParameters[parameters.RestrictFrontingProviderIDs] = config.RestrictFrontingProviderIDs
  1519. }
  1520. if config.RestrictFrontingProviderIDsClientProbability != nil {
  1521. applyParameters[parameters.RestrictFrontingProviderIDsClientProbability] = *config.RestrictFrontingProviderIDsClientProbability
  1522. }
  1523. if config.UpstreamProxyAllowAllServerEntrySources != nil {
  1524. applyParameters[parameters.UpstreamProxyAllowAllServerEntrySources] = *config.UpstreamProxyAllowAllServerEntrySources
  1525. }
  1526. if len(config.LimitTunnelDialPortNumbers) > 0 {
  1527. applyParameters[parameters.LimitTunnelDialPortNumbers] = config.LimitTunnelDialPortNumbers
  1528. }
  1529. if config.QUICDisablePathMTUDiscoveryProbability != nil {
  1530. applyParameters[parameters.QUICDisableClientPathMTUDiscoveryProbability] = *config.QUICDisablePathMTUDiscoveryProbability
  1531. }
  1532. if config.DNSResolverAttemptsPerServer != nil {
  1533. applyParameters[parameters.DNSResolverAttemptsPerServer] = *config.DNSResolverAttemptsPerServer
  1534. }
  1535. if config.DNSResolverAttemptsPerPreferredServer != nil {
  1536. applyParameters[parameters.DNSResolverAttemptsPerPreferredServer] = *config.DNSResolverAttemptsPerPreferredServer
  1537. }
  1538. if config.DNSResolverRequestTimeoutMilliseconds != nil {
  1539. applyParameters[parameters.DNSResolverRequestTimeout] = fmt.Sprintf("%dms", *config.DNSResolverRequestTimeoutMilliseconds)
  1540. }
  1541. if config.DNSResolverAwaitTimeoutMilliseconds != nil {
  1542. applyParameters[parameters.DNSResolverAwaitTimeout] = fmt.Sprintf("%dms", *config.DNSResolverAwaitTimeoutMilliseconds)
  1543. }
  1544. if config.DNSResolverPreresolvedIPAddressProbability != nil {
  1545. applyParameters[parameters.DNSResolverPreresolvedIPAddressProbability] = *config.DNSResolverPreresolvedIPAddressProbability
  1546. }
  1547. if config.DNSResolverPreresolvedIPAddressCIDRs != nil {
  1548. applyParameters[parameters.DNSResolverPreresolvedIPAddressCIDRs] = config.DNSResolverPreresolvedIPAddressCIDRs
  1549. }
  1550. if config.DNSResolverAlternateServers != nil {
  1551. applyParameters[parameters.DNSResolverAlternateServers] = config.DNSResolverAlternateServers
  1552. }
  1553. if config.DNSResolverPreferredAlternateServers != nil {
  1554. applyParameters[parameters.DNSResolverPreferredAlternateServers] = config.DNSResolverPreferredAlternateServers
  1555. }
  1556. if config.DNSResolverPreferAlternateServerProbability != nil {
  1557. applyParameters[parameters.DNSResolverPreferAlternateServerProbability] = *config.DNSResolverPreferAlternateServerProbability
  1558. }
  1559. if config.DNSResolverProtocolTransformSpecs != nil {
  1560. applyParameters[parameters.DNSResolverProtocolTransformSpecs] = config.DNSResolverProtocolTransformSpecs
  1561. }
  1562. if config.DNSResolverProtocolTransformScopedSpecNames != nil {
  1563. applyParameters[parameters.DNSResolverProtocolTransformScopedSpecNames] = config.DNSResolverProtocolTransformScopedSpecNames
  1564. }
  1565. if config.DNSResolverProtocolTransformProbability != nil {
  1566. applyParameters[parameters.DNSResolverProtocolTransformProbability] = *config.DNSResolverProtocolTransformProbability
  1567. }
  1568. if config.DNSResolverIncludeEDNS0Probability != nil {
  1569. applyParameters[parameters.DNSResolverIncludeEDNS0Probability] = *config.DNSResolverIncludeEDNS0Probability
  1570. }
  1571. if config.DNSResolverCacheExtensionInitialTTLMilliseconds != nil {
  1572. applyParameters[parameters.DNSResolverCacheExtensionInitialTTL] = fmt.Sprintf("%dms", *config.DNSResolverCacheExtensionInitialTTLMilliseconds)
  1573. }
  1574. if config.DNSResolverCacheExtensionVerifiedTTLMilliseconds != nil {
  1575. applyParameters[parameters.DNSResolverCacheExtensionVerifiedTTL] = fmt.Sprintf("%dms", *config.DNSResolverCacheExtensionVerifiedTTLMilliseconds)
  1576. }
  1577. if config.DirectHTTPProtocolTransformSpecs != nil {
  1578. applyParameters[parameters.DirectHTTPProtocolTransformSpecs] = config.DirectHTTPProtocolTransformSpecs
  1579. }
  1580. if config.DirectHTTPProtocolTransformScopedSpecNames != nil {
  1581. applyParameters[parameters.DirectHTTPProtocolTransformScopedSpecNames] = config.DirectHTTPProtocolTransformScopedSpecNames
  1582. }
  1583. if config.DirectHTTPProtocolTransformProbability != nil {
  1584. applyParameters[parameters.DirectHTTPProtocolTransformProbability] = *config.DirectHTTPProtocolTransformProbability
  1585. }
  1586. if config.FrontedHTTPProtocolTransformSpecs != nil {
  1587. applyParameters[parameters.FrontedHTTPProtocolTransformSpecs] = config.FrontedHTTPProtocolTransformSpecs
  1588. }
  1589. if config.FrontedHTTPProtocolTransformScopedSpecNames != nil {
  1590. applyParameters[parameters.FrontedHTTPProtocolTransformScopedSpecNames] = config.FrontedHTTPProtocolTransformScopedSpecNames
  1591. }
  1592. if config.FrontedHTTPProtocolTransformProbability != nil {
  1593. applyParameters[parameters.FrontedHTTPProtocolTransformProbability] = *config.FrontedHTTPProtocolTransformProbability
  1594. }
  1595. // When adding new config dial parameters that may override tactics, also
  1596. // update setDialParametersHash.
  1597. return applyParameters
  1598. }
  1599. func (config *Config) setDialParametersHash() {
  1600. // Calculate and store a hash of the config values that may impact
  1601. // dial parameters. This hash is used as part of the dial parameters
  1602. // replay mechanism to detect when persisted dial parameters should
  1603. // be discarded due to conflicting config changes.
  1604. //
  1605. // With a couple of minor exceptions, configuring dial parameters via the
  1606. // config is intended for testing only, and so these parameters are expected
  1607. // to be present in test runs only. It remains an important case to discard
  1608. // replay dial parameters when test config parameters are varied.
  1609. //
  1610. // Hashing the parameter names detects some ambiguous hash cases, such as two
  1611. // consecutive int64 parameters, one omitted and one not, that are flipped.
  1612. // The serialization is not completely unambiguous, and the format is
  1613. // currently limited by legacy cases (not invalidating replay dial parameters
  1614. // for production clients is more important than invalidating for test runs).
  1615. // We cannot hash the entire config JSON as it contains non-dial parameter
  1616. // fields which may frequently change across runs.
  1617. //
  1618. // MD5 hash is used solely as a data checksum and not for any security
  1619. // purpose.
  1620. hash := md5.New()
  1621. if len(config.LimitTunnelProtocols) > 0 {
  1622. hash.Write([]byte("LimitTunnelProtocols"))
  1623. for _, protocol := range config.LimitTunnelProtocols {
  1624. hash.Write([]byte(protocol))
  1625. }
  1626. }
  1627. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  1628. hash.Write([]byte("InitialLimitTunnelProtocols"))
  1629. for _, protocol := range config.InitialLimitTunnelProtocols {
  1630. hash.Write([]byte(protocol))
  1631. }
  1632. binary.Write(hash, binary.LittleEndian, int64(config.InitialLimitTunnelProtocolsCandidateCount))
  1633. }
  1634. if len(config.LimitTLSProfiles) > 0 {
  1635. hash.Write([]byte("LimitTLSProfiles"))
  1636. for _, profile := range config.LimitTLSProfiles {
  1637. hash.Write([]byte(profile))
  1638. }
  1639. }
  1640. if len(config.LimitQUICVersions) > 0 {
  1641. hash.Write([]byte("LimitQUICVersions"))
  1642. for _, version := range config.LimitQUICVersions {
  1643. hash.Write([]byte(version))
  1644. }
  1645. }
  1646. // Whether a custom User-Agent is specified is a binary flag: when not set,
  1647. // the replay dial parameters value applies. When set, external
  1648. // considerations apply.
  1649. if _, ok := config.CustomHeaders["User-Agent"]; ok {
  1650. hash.Write([]byte("CustomHeaders User-Agent"))
  1651. hash.Write([]byte{1})
  1652. }
  1653. if config.UpstreamProxyURL != "" {
  1654. hash.Write([]byte("UpstreamProxyURL"))
  1655. hash.Write([]byte(config.UpstreamProxyURL))
  1656. }
  1657. if config.TransformHostNameProbability != nil {
  1658. hash.Write([]byte("TransformHostNameProbability"))
  1659. binary.Write(hash, binary.LittleEndian, *config.TransformHostNameProbability)
  1660. }
  1661. if config.FragmentorProbability != nil {
  1662. hash.Write([]byte("FragmentorProbability"))
  1663. binary.Write(hash, binary.LittleEndian, *config.FragmentorProbability)
  1664. }
  1665. if len(config.FragmentorLimitProtocols) > 0 {
  1666. hash.Write([]byte("FragmentorLimitProtocols"))
  1667. for _, protocol := range config.FragmentorLimitProtocols {
  1668. hash.Write([]byte(protocol))
  1669. }
  1670. }
  1671. if config.FragmentorMinTotalBytes != nil {
  1672. hash.Write([]byte("FragmentorMinTotalBytes"))
  1673. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinTotalBytes))
  1674. }
  1675. if config.FragmentorMaxTotalBytes != nil {
  1676. hash.Write([]byte("FragmentorMaxTotalBytes"))
  1677. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxTotalBytes))
  1678. }
  1679. if config.FragmentorMinWriteBytes != nil {
  1680. hash.Write([]byte("FragmentorMinWriteBytes"))
  1681. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinWriteBytes))
  1682. }
  1683. if config.FragmentorMaxWriteBytes != nil {
  1684. hash.Write([]byte("FragmentorMaxWriteBytes"))
  1685. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxWriteBytes))
  1686. }
  1687. if config.FragmentorMinDelayMicroseconds != nil {
  1688. hash.Write([]byte("FragmentorMinDelayMicroseconds"))
  1689. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinDelayMicroseconds))
  1690. }
  1691. if config.FragmentorMaxDelayMicroseconds != nil {
  1692. hash.Write([]byte("FragmentorMaxDelayMicroseconds"))
  1693. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxDelayMicroseconds))
  1694. }
  1695. if config.MeekTrafficShapingProbability != nil {
  1696. hash.Write([]byte("MeekTrafficShapingProbability"))
  1697. binary.Write(hash, binary.LittleEndian, int64(*config.MeekTrafficShapingProbability))
  1698. }
  1699. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  1700. hash.Write([]byte("MeekTrafficShapingLimitProtocols"))
  1701. for _, protocol := range config.MeekTrafficShapingLimitProtocols {
  1702. hash.Write([]byte(protocol))
  1703. }
  1704. }
  1705. if config.MeekMinLimitRequestPayloadLength != nil {
  1706. hash.Write([]byte("MeekMinLimitRequestPayloadLength"))
  1707. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMinLimitRequestPayloadLength))
  1708. }
  1709. if config.MeekMaxLimitRequestPayloadLength != nil {
  1710. hash.Write([]byte("MeekMaxLimitRequestPayloadLength"))
  1711. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMaxLimitRequestPayloadLength))
  1712. }
  1713. if config.MeekRedialTLSProbability != nil {
  1714. hash.Write([]byte("MeekRedialTLSProbability"))
  1715. binary.Write(hash, binary.LittleEndian, *config.MeekRedialTLSProbability)
  1716. }
  1717. if config.ObfuscatedSSHMinPadding != nil {
  1718. hash.Write([]byte("ObfuscatedSSHMinPadding"))
  1719. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMinPadding))
  1720. }
  1721. if config.ObfuscatedSSHMaxPadding != nil {
  1722. hash.Write([]byte("ObfuscatedSSHMaxPadding"))
  1723. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMaxPadding))
  1724. }
  1725. if config.LivenessTestMinUpstreamBytes != nil {
  1726. hash.Write([]byte("LivenessTestMinUpstreamBytes"))
  1727. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinUpstreamBytes))
  1728. }
  1729. if config.LivenessTestMaxUpstreamBytes != nil {
  1730. hash.Write([]byte("LivenessTestMaxUpstreamBytes"))
  1731. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxUpstreamBytes))
  1732. }
  1733. if config.LivenessTestMinDownstreamBytes != nil {
  1734. hash.Write([]byte("LivenessTestMinDownstreamBytes"))
  1735. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinDownstreamBytes))
  1736. }
  1737. if config.LivenessTestMaxDownstreamBytes != nil {
  1738. hash.Write([]byte("LivenessTestMaxDownstreamBytes"))
  1739. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxDownstreamBytes))
  1740. }
  1741. // Legacy case: these parameters are included in the hash unconditionally,
  1742. // and so will impact almost all production clients. These parameter names
  1743. // are not hashed since that would invalidate all replay dial parameters for
  1744. // existing clients whose hashes predate the inclusion of parameter names.
  1745. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMin)
  1746. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMax)
  1747. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierLambda)
  1748. if config.UseOnlyCustomTLSProfiles != nil {
  1749. hash.Write([]byte("UseOnlyCustomTLSProfiles"))
  1750. binary.Write(hash, binary.LittleEndian, *config.UseOnlyCustomTLSProfiles)
  1751. }
  1752. if len(config.CustomTLSProfiles) > 0 {
  1753. hash.Write([]byte("CustomTLSProfiles"))
  1754. for _, customTLSProfile := range config.CustomTLSProfiles {
  1755. encodedCustomTLSProofile, _ := json.Marshal(customTLSProfile)
  1756. hash.Write(encodedCustomTLSProofile)
  1757. }
  1758. }
  1759. if config.SelectRandomizedTLSProfileProbability != nil {
  1760. hash.Write([]byte("SelectRandomizedTLSProfileProbability"))
  1761. binary.Write(hash, binary.LittleEndian, *config.SelectRandomizedTLSProfileProbability)
  1762. }
  1763. if config.NoDefaultTLSSessionIDProbability != nil {
  1764. hash.Write([]byte("NoDefaultTLSSessionIDProbability"))
  1765. binary.Write(hash, binary.LittleEndian, *config.NoDefaultTLSSessionIDProbability)
  1766. }
  1767. if len(config.DisableFrontingProviderTLSProfiles) > 0 {
  1768. hash.Write([]byte("DisableFrontingProviderTLSProfiles"))
  1769. encodedDisableFrontingProviderTLSProfiles, _ :=
  1770. json.Marshal(config.DisableFrontingProviderTLSProfiles)
  1771. hash.Write(encodedDisableFrontingProviderTLSProfiles)
  1772. }
  1773. if len(config.CustomHostNameRegexes) > 0 {
  1774. hash.Write([]byte("CustomHostNameRegexes"))
  1775. for _, customHostNameRegex := range config.CustomHostNameRegexes {
  1776. hash.Write([]byte(customHostNameRegex))
  1777. }
  1778. }
  1779. if config.CustomHostNameProbability != nil {
  1780. hash.Write([]byte("CustomHostNameProbability"))
  1781. binary.Write(hash, binary.LittleEndian, *config.CustomHostNameProbability)
  1782. }
  1783. if len(config.CustomHostNameLimitProtocols) > 0 {
  1784. hash.Write([]byte("CustomHostNameLimitProtocols"))
  1785. for _, protocol := range config.CustomHostNameLimitProtocols {
  1786. hash.Write([]byte(protocol))
  1787. }
  1788. }
  1789. if config.ConjureCachedRegistrationTTLSeconds != nil {
  1790. hash.Write([]byte("ConjureCachedRegistrationTTLSeconds"))
  1791. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureCachedRegistrationTTLSeconds))
  1792. }
  1793. if config.ConjureAPIRegistrarBidirectionalURL != "" {
  1794. hash.Write([]byte("ConjureAPIRegistrarBidirectionalURL"))
  1795. hash.Write([]byte(config.ConjureAPIRegistrarBidirectionalURL))
  1796. }
  1797. if len(config.ConjureAPIRegistrarFrontingSpecs) > 0 {
  1798. hash.Write([]byte("ConjureAPIRegistrarFrontingSpecs"))
  1799. for _, frontingSpec := range config.ConjureAPIRegistrarFrontingSpecs {
  1800. encodedFrontSpec, _ := json.Marshal(frontingSpec)
  1801. hash.Write(encodedFrontSpec)
  1802. }
  1803. }
  1804. if config.ConjureAPIRegistrarMinDelayMilliseconds != nil {
  1805. hash.Write([]byte("ConjureAPIRegistrarMinDelayMilliseconds"))
  1806. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureAPIRegistrarMinDelayMilliseconds))
  1807. }
  1808. if config.ConjureAPIRegistrarMaxDelayMilliseconds != nil {
  1809. hash.Write([]byte("ConjureAPIRegistrarMaxDelayMilliseconds"))
  1810. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureAPIRegistrarMaxDelayMilliseconds))
  1811. }
  1812. if config.ConjureDecoyRegistrarWidth != nil {
  1813. hash.Write([]byte("ConjureDecoyRegistrarWidth"))
  1814. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarWidth))
  1815. }
  1816. if config.ConjureDecoyRegistrarMinDelayMilliseconds != nil {
  1817. hash.Write([]byte("ConjureDecoyRegistrarMinDelayMilliseconds"))
  1818. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarMinDelayMilliseconds))
  1819. }
  1820. if config.ConjureDecoyRegistrarMaxDelayMilliseconds != nil {
  1821. hash.Write([]byte("ConjureDecoyRegistrarMaxDelayMilliseconds"))
  1822. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarMaxDelayMilliseconds))
  1823. }
  1824. if config.HoldOffTunnelMinDurationMilliseconds != nil {
  1825. hash.Write([]byte("HoldOffTunnelMinDurationMilliseconds"))
  1826. binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMinDurationMilliseconds))
  1827. }
  1828. if config.HoldOffTunnelMaxDurationMilliseconds != nil {
  1829. hash.Write([]byte("HoldOffTunnelMaxDurationMilliseconds"))
  1830. binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMaxDurationMilliseconds))
  1831. }
  1832. if len(config.HoldOffTunnelProtocols) > 0 {
  1833. hash.Write([]byte("HoldOffTunnelProtocols"))
  1834. for _, protocol := range config.HoldOffTunnelProtocols {
  1835. hash.Write([]byte(protocol))
  1836. }
  1837. }
  1838. if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
  1839. hash.Write([]byte("HoldOffTunnelFrontingProviderIDs"))
  1840. for _, providerID := range config.HoldOffTunnelFrontingProviderIDs {
  1841. hash.Write([]byte(providerID))
  1842. }
  1843. }
  1844. if config.HoldOffTunnelProbability != nil {
  1845. hash.Write([]byte("HoldOffTunnelProbability"))
  1846. binary.Write(hash, binary.LittleEndian, *config.HoldOffTunnelProbability)
  1847. }
  1848. if len(config.RestrictFrontingProviderIDs) > 0 {
  1849. hash.Write([]byte("RestrictFrontingProviderIDs"))
  1850. for _, providerID := range config.RestrictFrontingProviderIDs {
  1851. hash.Write([]byte(providerID))
  1852. }
  1853. }
  1854. if config.RestrictFrontingProviderIDsClientProbability != nil {
  1855. hash.Write([]byte("RestrictFrontingProviderIDsClientProbability"))
  1856. binary.Write(hash, binary.LittleEndian, *config.RestrictFrontingProviderIDsClientProbability)
  1857. }
  1858. if config.UpstreamProxyAllowAllServerEntrySources != nil {
  1859. hash.Write([]byte("UpstreamProxyAllowAllServerEntrySources"))
  1860. binary.Write(hash, binary.LittleEndian, *config.UpstreamProxyAllowAllServerEntrySources)
  1861. }
  1862. if len(config.LimitTunnelDialPortNumbers) > 0 {
  1863. hash.Write([]byte("LimitTunnelDialPortNumbers"))
  1864. encodedLimitTunnelDialPortNumbers, _ :=
  1865. json.Marshal(config.LimitTunnelDialPortNumbers)
  1866. hash.Write(encodedLimitTunnelDialPortNumbers)
  1867. }
  1868. if config.QUICDisablePathMTUDiscoveryProbability != nil {
  1869. hash.Write([]byte("QUICDisablePathMTUDiscoveryProbability"))
  1870. binary.Write(hash, binary.LittleEndian, *config.QUICDisablePathMTUDiscoveryProbability)
  1871. }
  1872. if config.DNSResolverAttemptsPerServer != nil {
  1873. hash.Write([]byte("DNSResolverAttemptsPerServer"))
  1874. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverAttemptsPerServer))
  1875. }
  1876. if config.DNSResolverRequestTimeoutMilliseconds != nil {
  1877. hash.Write([]byte("DNSResolverRequestTimeoutMilliseconds"))
  1878. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverRequestTimeoutMilliseconds))
  1879. }
  1880. if config.DNSResolverAwaitTimeoutMilliseconds != nil {
  1881. hash.Write([]byte("DNSResolverAwaitTimeoutMilliseconds"))
  1882. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverAwaitTimeoutMilliseconds))
  1883. }
  1884. if config.DNSResolverPreresolvedIPAddressCIDRs != nil {
  1885. hash.Write([]byte("DNSResolverPreresolvedIPAddressCIDRs"))
  1886. encodedDNSResolverPreresolvedIPAddressCIDRs, _ :=
  1887. json.Marshal(config.DNSResolverPreresolvedIPAddressCIDRs)
  1888. hash.Write(encodedDNSResolverPreresolvedIPAddressCIDRs)
  1889. }
  1890. if config.DNSResolverPreresolvedIPAddressProbability != nil {
  1891. hash.Write([]byte("DNSResolverPreresolvedIPAddressProbability"))
  1892. binary.Write(hash, binary.LittleEndian, *config.DNSResolverPreresolvedIPAddressProbability)
  1893. }
  1894. if config.DNSResolverAlternateServers != nil {
  1895. hash.Write([]byte("DNSResolverAlternateServers"))
  1896. for _, server := range config.DNSResolverAlternateServers {
  1897. hash.Write([]byte(server))
  1898. }
  1899. }
  1900. if config.DNSResolverPreferAlternateServerProbability != nil {
  1901. hash.Write([]byte("DNSResolverPreferAlternateServerProbability"))
  1902. binary.Write(hash, binary.LittleEndian, *config.DNSResolverPreferAlternateServerProbability)
  1903. }
  1904. if config.DNSResolverProtocolTransformSpecs != nil {
  1905. hash.Write([]byte("DNSResolverProtocolTransformSpecs"))
  1906. encodedDNSResolverProtocolTransformSpecs, _ :=
  1907. json.Marshal(config.DNSResolverProtocolTransformSpecs)
  1908. hash.Write(encodedDNSResolverProtocolTransformSpecs)
  1909. }
  1910. if config.DNSResolverProtocolTransformScopedSpecNames != nil {
  1911. hash.Write([]byte(""))
  1912. encodedDNSResolverProtocolTransformScopedSpecNames, _ :=
  1913. json.Marshal(config.DNSResolverProtocolTransformScopedSpecNames)
  1914. hash.Write(encodedDNSResolverProtocolTransformScopedSpecNames)
  1915. }
  1916. if config.DNSResolverProtocolTransformProbability != nil {
  1917. hash.Write([]byte("DNSResolverProtocolTransformProbability"))
  1918. binary.Write(hash, binary.LittleEndian, *config.DNSResolverProtocolTransformProbability)
  1919. }
  1920. if config.DNSResolverIncludeEDNS0Probability != nil {
  1921. hash.Write([]byte("DNSResolverIncludeEDNS0Probability"))
  1922. binary.Write(hash, binary.LittleEndian, *config.DNSResolverIncludeEDNS0Probability)
  1923. }
  1924. if config.DNSResolverCacheExtensionInitialTTLMilliseconds != nil {
  1925. hash.Write([]byte("DNSResolverCacheExtensionInitialTTLMilliseconds"))
  1926. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverCacheExtensionInitialTTLMilliseconds))
  1927. }
  1928. if config.DNSResolverCacheExtensionVerifiedTTLMilliseconds != nil {
  1929. hash.Write([]byte("DNSResolverCacheExtensionVerifiedTTLMilliseconds"))
  1930. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverCacheExtensionVerifiedTTLMilliseconds))
  1931. }
  1932. if config.DirectHTTPProtocolTransformSpecs != nil {
  1933. hash.Write([]byte("DirectHTTPProtocolTransformSpecs"))
  1934. encodedDirectHTTPProtocolTransformSpecs, _ :=
  1935. json.Marshal(config.DirectHTTPProtocolTransformSpecs)
  1936. hash.Write(encodedDirectHTTPProtocolTransformSpecs)
  1937. }
  1938. if config.DirectHTTPProtocolTransformScopedSpecNames != nil {
  1939. hash.Write([]byte(""))
  1940. encodedDirectHTTPProtocolTransformScopedSpecNames, _ :=
  1941. json.Marshal(config.DirectHTTPProtocolTransformScopedSpecNames)
  1942. hash.Write(encodedDirectHTTPProtocolTransformScopedSpecNames)
  1943. }
  1944. if config.DirectHTTPProtocolTransformProbability != nil {
  1945. hash.Write([]byte("DirectHTTPProtocolTransformProbability"))
  1946. binary.Write(hash, binary.LittleEndian, *config.DirectHTTPProtocolTransformProbability)
  1947. }
  1948. if config.FrontedHTTPProtocolTransformSpecs != nil {
  1949. hash.Write([]byte("FrontedHTTPProtocolTransformSpecs"))
  1950. encodedFrontedHTTPProtocolTransformSpecs, _ :=
  1951. json.Marshal(config.FrontedHTTPProtocolTransformSpecs)
  1952. hash.Write(encodedFrontedHTTPProtocolTransformSpecs)
  1953. }
  1954. if config.FrontedHTTPProtocolTransformScopedSpecNames != nil {
  1955. hash.Write([]byte(""))
  1956. encodedFrontedHTTPProtocolTransformScopedSpecNames, _ :=
  1957. json.Marshal(config.FrontedHTTPProtocolTransformScopedSpecNames)
  1958. hash.Write(encodedFrontedHTTPProtocolTransformScopedSpecNames)
  1959. }
  1960. if config.FrontedHTTPProtocolTransformProbability != nil {
  1961. hash.Write([]byte("FrontedHTTPProtocolTransformProbability"))
  1962. binary.Write(hash, binary.LittleEndian, *config.FrontedHTTPProtocolTransformProbability)
  1963. }
  1964. config.dialParametersHash = hash.Sum(nil)
  1965. }
  1966. func promoteLegacyTransferURL(URL string) parameters.TransferURLs {
  1967. transferURLs := make(parameters.TransferURLs, 1)
  1968. transferURLs[0] = &parameters.TransferURL{
  1969. URL: base64.StdEncoding.EncodeToString([]byte(URL)),
  1970. SkipVerify: false,
  1971. OnlyAfterAttempts: 0,
  1972. }
  1973. return transferURLs
  1974. }
  1975. type loggingDeviceBinder struct {
  1976. d DeviceBinder
  1977. }
  1978. func newLoggingDeviceBinder(d DeviceBinder) *loggingDeviceBinder {
  1979. return &loggingDeviceBinder{d: d}
  1980. }
  1981. func (d *loggingDeviceBinder) BindToDevice(fileDescriptor int) (string, error) {
  1982. deviceInfo, err := d.d.BindToDevice(fileDescriptor)
  1983. if err == nil && deviceInfo != "" {
  1984. NoticeBindToDevice(deviceInfo)
  1985. }
  1986. return deviceInfo, err
  1987. }
  1988. type staticNetworkGetter struct {
  1989. networkID string
  1990. }
  1991. func newStaticNetworkGetter(networkID string) *staticNetworkGetter {
  1992. return &staticNetworkGetter{networkID: networkID}
  1993. }
  1994. func (n *staticNetworkGetter) GetNetworkID() string {
  1995. return n.networkID
  1996. }
  1997. type loggingNetworkIDGetter struct {
  1998. n NetworkIDGetter
  1999. }
  2000. func newLoggingNetworkIDGetter(n NetworkIDGetter) *loggingNetworkIDGetter {
  2001. return &loggingNetworkIDGetter{n: n}
  2002. }
  2003. func (n *loggingNetworkIDGetter) GetNetworkID() string {
  2004. networkID := n.n.GetNetworkID()
  2005. // All PII must appear after the initial "-"
  2006. // See: https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
  2007. logNetworkID := networkID
  2008. index := strings.Index(logNetworkID, "-")
  2009. if index != -1 {
  2010. logNetworkID = logNetworkID[:index]
  2011. }
  2012. if len(logNetworkID)+1 < len(networkID) {
  2013. // Indicate when additional network info was present after the first "-".
  2014. logNetworkID += "+[redacted]"
  2015. }
  2016. NoticeNetworkID(logNetworkID)
  2017. return networkID
  2018. }
  2019. // migrationsFromLegacyNoticeFilePaths returns the file migrations which must be
  2020. // performed to move notice files from legacy file paths, which were configured
  2021. // with the legacy config fields HomepageNoticesFilename and
  2022. // RotatingNoticesFilename, to the new file paths used by Psiphon which exist
  2023. // under the data root directory.
  2024. func migrationsFromLegacyNoticeFilePaths(config *Config) []FileMigration {
  2025. var noticeMigrations []FileMigration
  2026. if config.MigrateHomepageNoticesFilename != "" {
  2027. noticeMigrations = append(noticeMigrations, FileMigration{
  2028. Name: "hompage",
  2029. OldPath: config.MigrateHomepageNoticesFilename,
  2030. NewPath: config.GetHomePageFilename(),
  2031. })
  2032. }
  2033. if config.MigrateRotatingNoticesFilename != "" {
  2034. migrations := []FileMigration{
  2035. {
  2036. Name: "notices",
  2037. OldPath: config.MigrateRotatingNoticesFilename,
  2038. NewPath: config.GetNoticesFilename(),
  2039. IsDir: false,
  2040. },
  2041. {
  2042. Name: "notices.1",
  2043. OldPath: config.MigrateRotatingNoticesFilename + ".1",
  2044. NewPath: config.GetNoticesFilename() + ".1",
  2045. },
  2046. }
  2047. noticeMigrations = append(noticeMigrations, migrations...)
  2048. }
  2049. return noticeMigrations
  2050. }
  2051. // migrationsFromLegacyFilePaths returns the file migrations which must be
  2052. // performed to move files from legacy file paths, which were configured with
  2053. // legacy config fields, to the new file paths used by Psiphon which exist
  2054. // under the data root directory.
  2055. // Note: an attempt is made to redact any file paths from the returned error.
  2056. func migrationsFromLegacyFilePaths(config *Config) ([]FileMigration, error) {
  2057. migrations := []FileMigration{
  2058. {
  2059. Name: "psiphon.boltdb",
  2060. OldPath: filepath.Join(config.MigrateDataStoreDirectory, "psiphon.boltdb"),
  2061. NewPath: filepath.Join(config.GetDataStoreDirectory(), "psiphon.boltdb"),
  2062. },
  2063. {
  2064. Name: "psiphon.boltdb.lock",
  2065. OldPath: filepath.Join(config.MigrateDataStoreDirectory, "psiphon.boltdb.lock"),
  2066. NewPath: filepath.Join(config.GetDataStoreDirectory(), "psiphon.boltdb.lock"),
  2067. },
  2068. }
  2069. if config.MigrateRemoteServerListDownloadFilename != "" {
  2070. // Migrate remote server list files
  2071. rslMigrations := []FileMigration{
  2072. {
  2073. Name: "remote_server_list",
  2074. OldPath: config.MigrateRemoteServerListDownloadFilename,
  2075. NewPath: config.GetRemoteServerListDownloadFilename(),
  2076. },
  2077. {
  2078. Name: "remote_server_list.part",
  2079. OldPath: config.MigrateRemoteServerListDownloadFilename + ".part",
  2080. NewPath: config.GetRemoteServerListDownloadFilename() + ".part",
  2081. },
  2082. {
  2083. Name: "remote_server_list.part.etag",
  2084. OldPath: config.MigrateRemoteServerListDownloadFilename + ".part.etag",
  2085. NewPath: config.GetRemoteServerListDownloadFilename() + ".part.etag",
  2086. },
  2087. }
  2088. migrations = append(migrations, rslMigrations...)
  2089. }
  2090. if config.MigrateObfuscatedServerListDownloadDirectory != "" {
  2091. // Migrate OSL registry file and downloads
  2092. oslFileRegex, err := regexp.Compile(`^osl-.+$`)
  2093. if err != nil {
  2094. return nil, errors.TraceMsg(err, "failed to compile regex for osl files")
  2095. }
  2096. files, err := ioutil.ReadDir(config.MigrateObfuscatedServerListDownloadDirectory)
  2097. if err != nil {
  2098. NoticeWarning(
  2099. "Migration: failed to read OSL download directory with error %s",
  2100. common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory))
  2101. } else {
  2102. for _, file := range files {
  2103. if oslFileRegex.MatchString(file.Name()) {
  2104. fileMigration := FileMigration{
  2105. Name: "osl",
  2106. OldPath: filepath.Join(config.MigrateObfuscatedServerListDownloadDirectory, file.Name()),
  2107. NewPath: filepath.Join(config.GetObfuscatedServerListDownloadDirectory(), file.Name()),
  2108. }
  2109. migrations = append(migrations, fileMigration)
  2110. }
  2111. }
  2112. }
  2113. }
  2114. if config.MigrateUpgradeDownloadFilename != "" {
  2115. // Migrate downloaded upgrade files
  2116. oldUpgradeDownloadFilename := filepath.Base(config.MigrateUpgradeDownloadFilename)
  2117. // Create regex for:
  2118. // <old_upgrade_download_filename>
  2119. // <old_upgrade_download_filename>.<client_version_number>
  2120. // <old_upgrade_download_filename>.<client_version_number>.part
  2121. // <old_upgrade_download_filename>.<client_version_number>.part.etag
  2122. upgradeDownloadFileRegex, err := regexp.Compile(`^` + oldUpgradeDownloadFilename + `(\.\d+(\.part(\.etag)?)?)?$`)
  2123. if err != nil {
  2124. return nil, errors.TraceMsg(err, "failed to compile regex for upgrade files")
  2125. }
  2126. upgradeDownloadDir := filepath.Dir(config.MigrateUpgradeDownloadFilename)
  2127. files, err := ioutil.ReadDir(upgradeDownloadDir)
  2128. if err != nil {
  2129. NoticeWarning(
  2130. "Migration: failed to read upgrade download directory with error %s",
  2131. common.RedactFilePathsError(err, upgradeDownloadDir))
  2132. } else {
  2133. for _, file := range files {
  2134. if upgradeDownloadFileRegex.MatchString(file.Name()) {
  2135. oldFileSuffix := strings.TrimPrefix(file.Name(), oldUpgradeDownloadFilename)
  2136. fileMigration := FileMigration{
  2137. Name: "upgrade",
  2138. OldPath: filepath.Join(upgradeDownloadDir, file.Name()),
  2139. NewPath: config.GetUpgradeDownloadFilename() + oldFileSuffix,
  2140. }
  2141. migrations = append(migrations, fileMigration)
  2142. }
  2143. }
  2144. }
  2145. }
  2146. return migrations, nil
  2147. }