config.go 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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. // SessionID specifies a client session ID to use in the Psiphon API. The
  413. // session ID should be a randomly generated value that is used only for a
  414. // single session, which is defined as the period between a user starting
  415. // a Psiphon client and stopping the client.
  416. //
  417. // A session ID must be 32 hex digits (lower case). When blank, a random
  418. // session ID is automatically generated. Supply a session ID when a
  419. // single client session will cross multiple Controller instances.
  420. SessionID string
  421. // Authorizations is a list of encoded, signed access control
  422. // authorizations that the client has obtained and will present to the
  423. // server.
  424. Authorizations []string
  425. // ServerEntrySignaturePublicKey is a base64-encoded, ed25519 public
  426. // key value used to verify individual server entry signatures. This value
  427. // is supplied by and depends on the Psiphon Network, and is typically
  428. // embedded in the client binary.
  429. ServerEntrySignaturePublicKey string
  430. // ExchangeObfuscationKey is a base64-encoded, NaCl secretbox key used to
  431. // obfuscate server info exchanges between clients.
  432. // Required for the exchange functionality.
  433. ExchangeObfuscationKey string
  434. // MigrateHomepageNoticesFilename migrates a homepage file from the path
  435. // previously configured with setNoticeFiles to the new path for homepage
  436. // files under the data root directory. The file specified by this config
  437. // value will be moved to config.GetHomePageFilename().
  438. //
  439. // Note: see comment for config.Commit() for a description of how file
  440. // migrations are performed.
  441. //
  442. // If not set, no migration operation will be performed.
  443. MigrateHomepageNoticesFilename string
  444. // MigrateRotatingNoticesFilename migrates notice files from the path
  445. // previously configured with setNoticeFiles to the new path for notice
  446. // files under the data root directory.
  447. //
  448. // MigrateRotatingNoticesFilename will be moved to
  449. // config.GetNoticesFilename().
  450. //
  451. // MigrateRotatingNoticesFilename.1 will be moved to
  452. // config.GetOldNoticesFilename().
  453. //
  454. // Note: see comment for config.Commit() for a description of how file
  455. // migrations are performed.
  456. //
  457. // If not set, no migration operation will be performed.
  458. MigrateRotatingNoticesFilename string
  459. // MigrateDataStoreDirectory indicates the location of the datastore
  460. // directory, as previously configured with the deprecated
  461. // DataStoreDirectory config field. Datastore files found in the specified
  462. // directory will be moved under the data root directory.
  463. //
  464. // Note: see comment for config.Commit() for a description of how file
  465. // migrations are performed.
  466. MigrateDataStoreDirectory string
  467. // MigrateRemoteServerListDownloadFilename indicates the location of
  468. // remote server list download files. The remote server list files found at
  469. // the specified path will be moved under the data root directory.
  470. //
  471. // Note: see comment for config.Commit() for a description of how file
  472. // migrations are performed.
  473. MigrateRemoteServerListDownloadFilename string
  474. // MigrateObfuscatedServerListDownloadDirectory indicates the location of
  475. // the obfuscated server list downloads directory, as previously configured
  476. // with ObfuscatedServerListDownloadDirectory. Obfuscated server list
  477. // download files found in the specified directory will be moved under the
  478. // data root directory.
  479. //
  480. // Warning: if the directory is empty after obfuscated server
  481. // list files are moved, then it will be deleted.
  482. //
  483. // Note: see comment for config.Commit() for a description of how file
  484. // migrations are performed.
  485. MigrateObfuscatedServerListDownloadDirectory string
  486. // MigrateUpgradeDownloadFilename indicates the location of downloaded
  487. // application upgrade files. Downloaded upgrade files found at the
  488. // specified path will be moved under the data root directory.
  489. //
  490. // Note: see comment for config.Commit() for a description of how file
  491. // migrations are performed.
  492. MigrateUpgradeDownloadFilename string
  493. //
  494. // The following parameters are deprecated.
  495. //
  496. // DataStoreDirectory is the directory in which to store the persistent
  497. // database, which contains information such as server entries. By
  498. // default, current working directory.
  499. //
  500. // Deprecated:
  501. // Use MigrateDataStoreDirectory. When MigrateDataStoreDirectory
  502. // is set, this parameter is ignored.
  503. //
  504. // DataStoreDirectory has been subsumed by the new data root directory,
  505. // which is configured with DataRootDirectory. If set, datastore files
  506. // found in the specified directory will be moved under the data root
  507. // directory.
  508. DataStoreDirectory string
  509. // RemoteServerListDownloadFilename specifies a target filename for
  510. // storing the remote server list download. Data is stored in co-located
  511. // files (RemoteServerListDownloadFilename.part*) to allow for resumable
  512. // downloading.
  513. //
  514. // Deprecated:
  515. // Use MigrateRemoteServerListDownloadFilename. When
  516. // MigrateRemoteServerListDownloadFilename is set, this parameter is
  517. // ignored.
  518. //
  519. // If set, remote server list download files found at the specified path
  520. // will be moved under the data root directory.
  521. RemoteServerListDownloadFilename string
  522. // ObfuscatedServerListDownloadDirectory specifies a target directory for
  523. // storing the obfuscated remote server list downloads. Data is stored in
  524. // co-located files (<OSL filename>.part*) to allow for resumable
  525. // downloading.
  526. //
  527. // Deprecated:
  528. // Use MigrateObfuscatedServerListDownloadDirectory. When
  529. // MigrateObfuscatedServerListDownloadDirectory is set, this parameter is
  530. // ignored.
  531. //
  532. // If set, obfuscated server list download files found at the specified path
  533. // will be moved under the data root directory.
  534. ObfuscatedServerListDownloadDirectory string
  535. // UpgradeDownloadFilename is the local target filename for an upgrade
  536. // download. This parameter is required when UpgradeDownloadURLs (or
  537. // UpgradeDownloadUrl) is specified. Data is stored in co-located files
  538. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  539. //
  540. // Deprecated:
  541. // Use MigrateUpgradeDownloadFilename. When MigrateUpgradeDownloadFilename
  542. // is set, this parameter is ignored.
  543. //
  544. // If set, upgrade download files found at the specified path will be moved
  545. // under the data root directory.
  546. UpgradeDownloadFilename string
  547. // TunnelProtocol indicates which protocol to use. For the default, "",
  548. // all protocols are used.
  549. //
  550. // Deprecated: Use LimitTunnelProtocols. When LimitTunnelProtocols is not
  551. // nil, this parameter is ignored.
  552. TunnelProtocol string
  553. // Deprecated: Use CustomHeaders. When CustomHeaders is not nil, this
  554. // parameter is ignored.
  555. UpstreamProxyCustomHeaders http.Header
  556. // RemoteServerListUrl is a URL which specifies a location to fetch out-
  557. // of-band server entries. This facility is used when a tunnel cannot be
  558. // established to known servers. This value is supplied by and depends on
  559. // the Psiphon Network, and is typically embedded in the client binary.
  560. //
  561. // Deprecated: Use RemoteServerListURLs. When RemoteServerListURLs is not
  562. // nil, this parameter is ignored.
  563. RemoteServerListUrl string
  564. // ObfuscatedServerListRootURL is a URL which specifies the root location
  565. // from which to fetch obfuscated server list files. This value is
  566. // supplied by and depends on the Psiphon Network, and is typically
  567. // embedded in the client binary.
  568. //
  569. // Deprecated: Use ObfuscatedServerListRootURLs. When
  570. // ObfuscatedServerListRootURLs is not nil, this parameter is ignored.
  571. ObfuscatedServerListRootURL string
  572. // UpgradeDownloadUrl specifies a URL from which to download a host client
  573. // upgrade file, when one is available. The core tunnel controller
  574. // provides a resumable download facility which downloads this resource
  575. // and emits a notice when complete. This value is supplied by and depends
  576. // on the Psiphon Network, and is typically embedded in the client binary.
  577. //
  578. // Deprecated: Use UpgradeDownloadURLs. When UpgradeDownloadURLs is not
  579. // nil, this parameter is ignored.
  580. UpgradeDownloadUrl string
  581. //
  582. // The following parameters are for testing purposes.
  583. //
  584. // TransformHostNameProbability is for testing purposes.
  585. TransformHostNameProbability *float64
  586. // FragmentorProbability and associated Fragmentor fields are for testing
  587. // purposes.
  588. FragmentorProbability *float64
  589. FragmentorLimitProtocols []string
  590. FragmentorMinTotalBytes *int
  591. FragmentorMaxTotalBytes *int
  592. FragmentorMinWriteBytes *int
  593. FragmentorMaxWriteBytes *int
  594. FragmentorMinDelayMicroseconds *int
  595. FragmentorMaxDelayMicroseconds *int
  596. // MeekTrafficShapingProbability and associated fields are for testing
  597. // purposes.
  598. MeekTrafficShapingProbability *float64
  599. MeekTrafficShapingLimitProtocols []string
  600. MeekMinTLSPadding *int
  601. MeekMaxTLSPadding *int
  602. MeekMinLimitRequestPayloadLength *int
  603. MeekMaxLimitRequestPayloadLength *int
  604. MeekRedialTLSProbability *float64
  605. MeekAlternateCookieNameProbability *float64
  606. MeekAlternateContentTypeProbability *float64
  607. // ObfuscatedSSHAlgorithms and associated ObfuscatedSSH fields are for
  608. // testing purposes. If specified, ObfuscatedSSHAlgorithms must have 4 SSH
  609. // KEX elements in order: the kex algorithm, cipher, MAC, and server host
  610. // key algorithm.
  611. ObfuscatedSSHAlgorithms []string
  612. ObfuscatedSSHMinPadding *int
  613. ObfuscatedSSHMaxPadding *int
  614. // LivenessTestMinUpstreamBytes and other LivenessTest fields are for
  615. // testing purposes.
  616. LivenessTestMinUpstreamBytes *int
  617. LivenessTestMaxUpstreamBytes *int
  618. LivenessTestMinDownstreamBytes *int
  619. LivenessTestMaxDownstreamBytes *int
  620. // ReplayCandidateCount and other Replay fields are for testing purposes.
  621. ReplayCandidateCount *int
  622. ReplayDialParametersTTLSeconds *int
  623. ReplayTargetUpstreamBytes *int
  624. ReplayTargetDownstreamBytes *int
  625. ReplayTargetTunnelDurationSeconds *int
  626. ReplayLaterRoundMoveToFrontProbability *float64
  627. ReplayRetainFailedProbability *float64
  628. // NetworkLatencyMultiplierMin and other NetworkLatencyMultiplier fields are
  629. // for testing purposes.
  630. NetworkLatencyMultiplierMin float64
  631. NetworkLatencyMultiplierMax float64
  632. NetworkLatencyMultiplierLambda float64
  633. // UseOnlyCustomTLSProfiles and other TLS configuration fields are for
  634. // testing purposes.
  635. UseOnlyCustomTLSProfiles *bool
  636. CustomTLSProfiles protocol.CustomTLSProfiles
  637. SelectRandomizedTLSProfileProbability *float64
  638. NoDefaultTLSSessionIDProbability *float64
  639. DisableFrontingProviderTLSProfiles protocol.LabeledTLSProfiles
  640. // ClientBurstUpstreamTargetBytes and other burst metric fields are for
  641. // testing purposes.
  642. ClientBurstUpstreamTargetBytes *int
  643. ClientBurstUpstreamDeadlineMilliseconds *int
  644. ClientBurstDownstreamTargetBytes *int
  645. ClientBurstDownstreamDeadlineMilliseconds *int
  646. // ApplicationParameters is for testing purposes.
  647. ApplicationParameters parameters.KeyValues
  648. // CustomHostNameRegexes and other custom host name fields are for testing
  649. // purposes.
  650. CustomHostNameRegexes []string
  651. CustomHostNameProbability *float64
  652. CustomHostNameLimitProtocols []string
  653. // ConjureCachedRegistrationTTLSeconds and other Conjure fields are for
  654. // testing purposes.
  655. ConjureCachedRegistrationTTLSeconds *int
  656. ConjureAPIRegistrarBidirectionalURL string
  657. ConjureAPIRegistrarFrontingSpecs parameters.FrontingSpecs
  658. ConjureAPIRegistrarMinDelayMilliseconds *int
  659. ConjureAPIRegistrarMaxDelayMilliseconds *int
  660. ConjureDecoyRegistrarProbability *float64
  661. ConjureDecoyRegistrarWidth *int
  662. ConjureDecoyRegistrarMinDelayMilliseconds *int
  663. ConjureDecoyRegistrarMaxDelayMilliseconds *int
  664. // HoldOffTunnelMinDurationMilliseconds and other HoldOffTunnel fields are
  665. // for testing purposes.
  666. HoldOffTunnelMinDurationMilliseconds *int
  667. HoldOffTunnelMaxDurationMilliseconds *int
  668. HoldOffTunnelProtocols []string
  669. HoldOffTunnelFrontingProviderIDs []string
  670. HoldOffTunnelProbability *float64
  671. // RestrictFrontingProviderIDs and other RestrictFrontingProviderIDs fields
  672. // are for testing purposes.
  673. RestrictFrontingProviderIDs []string
  674. RestrictFrontingProviderIDsClientProbability *float64
  675. // UpstreamProxyAllowAllServerEntrySources is for testing purposes.
  676. UpstreamProxyAllowAllServerEntrySources *bool
  677. // LimitTunnelDialPortNumbers is for testing purposes.
  678. LimitTunnelDialPortNumbers parameters.TunnelProtocolPortLists
  679. // QUICDisablePathMTUDiscoveryProbability is for testing purposes.
  680. QUICDisablePathMTUDiscoveryProbability *float64
  681. // DNSResolverAttemptsPerServer and other DNSResolver fields are for
  682. // testing purposes.
  683. DNSResolverAttemptsPerServer *int
  684. DNSResolverAttemptsPerPreferredServer *int
  685. DNSResolverRequestTimeoutMilliseconds *int
  686. DNSResolverAwaitTimeoutMilliseconds *int
  687. DNSResolverPreresolvedIPAddressCIDRs parameters.LabeledCIDRs
  688. DNSResolverPreresolvedIPAddressProbability *float64
  689. DNSResolverAlternateServers []string
  690. DNSResolverPreferredAlternateServers []string
  691. DNSResolverPreferAlternateServerProbability *float64
  692. DNSResolverProtocolTransformSpecs transforms.Specs
  693. DNSResolverProtocolTransformScopedSpecNames transforms.ScopedSpecNames
  694. DNSResolverProtocolTransformProbability *float64
  695. DNSResolverIncludeEDNS0Probability *float64
  696. DNSResolverCacheExtensionInitialTTLMilliseconds *int
  697. DNSResolverCacheExtensionVerifiedTTLMilliseconds *int
  698. // params is the active parameters.Parameters with defaults, config values,
  699. // and, optionally, tactics applied.
  700. //
  701. // New tactics must be applied by calling Config.SetParameters; calling
  702. // params.Set directly will fail to add config values.
  703. params *parameters.Parameters
  704. dialParametersHash []byte
  705. dynamicConfigMutex sync.Mutex
  706. sponsorID string
  707. authorizations []string
  708. deviceBinder DeviceBinder
  709. networkIDGetter NetworkIDGetter
  710. clientFeatures []string
  711. resolverMutex sync.Mutex
  712. resolver *resolver.Resolver
  713. committed bool
  714. loadTimestamp string
  715. }
  716. // Config field which specifies if notice files should be used and at which
  717. // frequency and size they should be rotated.
  718. //
  719. // If either RotatingFileSize or RotatingSyncFrequency are <= 0, default values
  720. // are used.
  721. //
  722. // See comment for setNoticeFiles in notice.go for further details.
  723. type UseNoticeFiles struct {
  724. RotatingFileSize int
  725. RotatingSyncFrequency int
  726. }
  727. // LoadConfig parses a JSON format Psiphon config JSON string and returns a
  728. // Config struct populated with config values.
  729. //
  730. // The Config struct may then be programmatically populated with additional
  731. // values, including callbacks such as DeviceBinder.
  732. //
  733. // Before using the Config, Commit must be called, which will perform further
  734. // validation and initialize internal data structures.
  735. func LoadConfig(configJson []byte) (*Config, error) {
  736. var config Config
  737. err := json.Unmarshal(configJson, &config)
  738. if err != nil {
  739. return nil, errors.Trace(err)
  740. }
  741. config.loadTimestamp = common.TruncateTimestampToHour(
  742. common.GetCurrentTimestamp())
  743. return &config, nil
  744. }
  745. // IsCommitted checks if Commit was called.
  746. func (config *Config) IsCommitted() bool {
  747. return config.committed
  748. }
  749. // Commit validates Config fields finalizes initialization.
  750. //
  751. // Config fields should not be set after calling Config, as any changes may
  752. // not be reflected in internal data structures.
  753. //
  754. // If migrateFromLegacyFields is set to true, then an attempt to migrate from
  755. // legacy fields is made.
  756. //
  757. // Migration from legacy fields:
  758. // Config fields of the naming Migrate* (e.g. MigrateDataStoreDirectory) specify
  759. // a file migration operation which should be performed. These fields correspond
  760. // to deprecated fields, which previously could be used to specify where Psiphon
  761. // stored different sets of persistent files (e.g. MigrateDataStoreDirectory
  762. // corresponds to the deprecated field DataStoreDirectory).
  763. //
  764. // Psiphon now stores all persistent data under the configurable
  765. // DataRootDirectory (see Config.DataRootDirectory). The deprecated fields, and
  766. // corresponding Migrate* fields, are now used to specify the file or directory
  767. // path where, or under which, persistent files and directories created by
  768. // previous versions of Psiphon exist, so they can be moved under the
  769. // DataRootDirectory.
  770. //
  771. // For each migration operation:
  772. // - In the case of directories that could have defaulted to the current working
  773. // directory, persistent files and directories created by Psiphon are
  774. // precisely targeted to avoid moving files which were not created by Psiphon.
  775. // - If no file is found at the specified path, or an error is encountered while
  776. // migrating the file, then an error is logged and execution continues
  777. // normally.
  778. //
  779. // A sentinel file which signals that file migration has been completed, and
  780. // should not be attempted again, is created under DataRootDirectory after one
  781. // full pass through Commit(), regardless of whether file migration succeeds or
  782. // fails. It is better to not endlessly retry file migrations on each Commit()
  783. // because file system errors are expected to be rare and persistent files will
  784. // be re-populated over time.
  785. func (config *Config) Commit(migrateFromLegacyFields bool) error {
  786. // Do SetEmitDiagnosticNotices first, to ensure config file errors are
  787. // emitted.
  788. if config.EmitDiagnosticNotices {
  789. SetEmitDiagnosticNotices(
  790. true, config.EmitDiagnosticNetworkParameters)
  791. }
  792. // Migrate and set notice files before any operations that may emit an
  793. // error. This is to ensure config file errors are written to file when
  794. // notice files are configured with config.UseNoticeFiles.
  795. //
  796. // Note:
  797. // Errors encountered while configuring the data directory cannot be written
  798. // to notice files. This is because notices files are created within the
  799. // data directory.
  800. if config.DataRootDirectory == "" {
  801. wd, err := os.Getwd()
  802. if err != nil {
  803. return errors.Trace(common.RedactFilePathsError(err))
  804. }
  805. config.DataRootDirectory = wd
  806. }
  807. // Create root directory
  808. dataDirectoryPath := config.GetPsiphonDataDirectory()
  809. if !common.FileExists(dataDirectoryPath) {
  810. err := os.Mkdir(dataDirectoryPath, os.ModePerm)
  811. if err != nil {
  812. return errors.Tracef(
  813. "failed to create datastore directory with error: %s",
  814. common.RedactFilePathsError(err, dataDirectoryPath))
  815. }
  816. }
  817. // Check if the migration from legacy config fields has already been
  818. // completed. See the Migrate* config fields for more details.
  819. migrationCompleteFilePath := filepath.Join(config.GetPsiphonDataDirectory(), "migration_complete")
  820. needMigration := !common.FileExists(migrationCompleteFilePath)
  821. // Collect notices to emit them after notice files are set
  822. var noticeMigrationAlertMsgs []string
  823. var noticeMigrationInfoMsgs []string
  824. // Migrate notices first to ensure notice files are used for notices if
  825. // UseNoticeFiles is set.
  826. homepageFilePath := config.GetHomePageFilename()
  827. noticesFilePath := config.GetNoticesFilename()
  828. if migrateFromLegacyFields {
  829. if needMigration {
  830. // Move notice files that exist at legacy file paths under the data root
  831. // directory.
  832. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, "Config migration: need migration")
  833. noticeMigrations := migrationsFromLegacyNoticeFilePaths(config)
  834. successfulMigrations := 0
  835. for _, migration := range noticeMigrations {
  836. err := DoFileMigration(migration)
  837. if err != nil {
  838. alertMsg := fmt.Sprintf("Config migration: %s", errors.Trace(err))
  839. noticeMigrationAlertMsgs = append(noticeMigrationAlertMsgs, alertMsg)
  840. } else {
  841. successfulMigrations += 1
  842. }
  843. }
  844. infoMsg := fmt.Sprintf("Config migration: %d/%d notice files successfully migrated", successfulMigrations, len(noticeMigrations))
  845. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, infoMsg)
  846. } else {
  847. noticeMigrationInfoMsgs = append(noticeMigrationInfoMsgs, "Config migration: migration already completed")
  848. }
  849. }
  850. if config.UseNoticeFiles != nil {
  851. setNoticeFiles(
  852. homepageFilePath,
  853. noticesFilePath,
  854. config.UseNoticeFiles.RotatingFileSize,
  855. config.UseNoticeFiles.RotatingSyncFrequency)
  856. }
  857. // Emit notices now that notice files are set if configured
  858. for _, msg := range noticeMigrationAlertMsgs {
  859. NoticeWarning(msg)
  860. }
  861. for _, msg := range noticeMigrationInfoMsgs {
  862. NoticeInfo(msg)
  863. }
  864. // Promote legacy fields.
  865. if config.CustomHeaders == nil {
  866. config.CustomHeaders = config.UpstreamProxyCustomHeaders
  867. config.UpstreamProxyCustomHeaders = nil
  868. }
  869. if config.RemoteServerListUrl != "" && config.RemoteServerListURLs == nil {
  870. config.RemoteServerListURLs = promoteLegacyTransferURL(config.RemoteServerListUrl)
  871. }
  872. if config.ObfuscatedServerListRootURL != "" && config.ObfuscatedServerListRootURLs == nil {
  873. config.ObfuscatedServerListRootURLs = promoteLegacyTransferURL(config.ObfuscatedServerListRootURL)
  874. }
  875. if config.UpgradeDownloadUrl != "" && config.UpgradeDownloadURLs == nil {
  876. config.UpgradeDownloadURLs = promoteLegacyTransferURL(config.UpgradeDownloadUrl)
  877. }
  878. if config.TunnelProtocol != "" && len(config.LimitTunnelProtocols) == 0 {
  879. config.LimitTunnelProtocols = []string{config.TunnelProtocol}
  880. }
  881. if config.DataStoreDirectory != "" && config.MigrateDataStoreDirectory == "" {
  882. config.MigrateDataStoreDirectory = config.DataStoreDirectory
  883. }
  884. if config.RemoteServerListDownloadFilename != "" && config.MigrateRemoteServerListDownloadFilename == "" {
  885. config.MigrateRemoteServerListDownloadFilename = config.RemoteServerListDownloadFilename
  886. }
  887. if config.ObfuscatedServerListDownloadDirectory != "" && config.MigrateObfuscatedServerListDownloadDirectory == "" {
  888. config.MigrateObfuscatedServerListDownloadDirectory = config.ObfuscatedServerListDownloadDirectory
  889. }
  890. if config.UpgradeDownloadFilename != "" && config.MigrateUpgradeDownloadFilename == "" {
  891. config.MigrateUpgradeDownloadFilename = config.UpgradeDownloadFilename
  892. }
  893. // Supply default values.
  894. // Create datastore directory.
  895. dataStoreDirectoryPath := config.GetDataStoreDirectory()
  896. if !common.FileExists(dataStoreDirectoryPath) {
  897. err := os.Mkdir(dataStoreDirectoryPath, os.ModePerm)
  898. if err != nil {
  899. return errors.Tracef(
  900. "failed to create datastore directory with error: %s",
  901. common.RedactFilePathsError(err, dataStoreDirectoryPath))
  902. }
  903. }
  904. // Create OSL directory.
  905. oslDirectoryPath := config.GetObfuscatedServerListDownloadDirectory()
  906. if !common.FileExists(oslDirectoryPath) {
  907. err := os.Mkdir(oslDirectoryPath, os.ModePerm)
  908. if err != nil {
  909. return errors.Tracef(
  910. "failed to create osl directory with error: %s",
  911. common.RedactFilePathsError(err, oslDirectoryPath))
  912. }
  913. }
  914. if config.ClientVersion == "" {
  915. config.ClientVersion = "0"
  916. }
  917. if config.TunnelPoolSize == 0 {
  918. config.TunnelPoolSize = TUNNEL_POOL_SIZE
  919. }
  920. // Validate config fields.
  921. if !common.FileExists(config.DataRootDirectory) {
  922. return errors.TraceNew("DataRootDirectory does not exist")
  923. }
  924. if config.PropagationChannelId == "" {
  925. return errors.TraceNew("propagation channel ID is missing from the configuration file")
  926. }
  927. if config.SponsorId == "" {
  928. return errors.TraceNew("sponsor ID is missing from the configuration file")
  929. }
  930. _, err := strconv.Atoi(config.ClientVersion)
  931. if err != nil {
  932. return errors.Tracef("invalid client version: %s", err)
  933. }
  934. if !common.Contains(
  935. []string{"", protocol.PSIPHON_SSH_API_PROTOCOL, protocol.PSIPHON_WEB_API_PROTOCOL},
  936. config.TargetApiProtocol) {
  937. return errors.TraceNew("invalid TargetApiProtocol")
  938. }
  939. if !config.DisableRemoteServerListFetcher {
  940. if config.RemoteServerListURLs != nil {
  941. if config.RemoteServerListSignaturePublicKey == "" {
  942. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  943. }
  944. }
  945. if config.ObfuscatedServerListRootURLs != nil {
  946. if config.RemoteServerListSignaturePublicKey == "" {
  947. return errors.TraceNew("missing RemoteServerListSignaturePublicKey")
  948. }
  949. }
  950. }
  951. if config.UpgradeDownloadURLs != nil {
  952. if config.UpgradeDownloadClientVersionHeader == "" {
  953. return errors.TraceNew("missing UpgradeDownloadClientVersionHeader")
  954. }
  955. }
  956. if config.FeedbackUploadURLs != nil {
  957. if config.FeedbackEncryptionPublicKey == "" {
  958. return errors.TraceNew("missing FeedbackEncryptionPublicKey")
  959. }
  960. }
  961. // This constraint is expected by logic in Controller.runTunnels().
  962. if config.PacketTunnelTunFileDescriptor > 0 && config.TunnelPoolSize != 1 {
  963. return errors.TraceNew("packet tunnel mode requires TunnelPoolSize to be 1")
  964. }
  965. // SessionID must be PSIPHON_API_CLIENT_SESSION_ID_LENGTH lowercase hex-encoded bytes.
  966. if config.SessionID == "" {
  967. sessionID, err := MakeSessionId()
  968. if err != nil {
  969. return errors.Trace(err)
  970. }
  971. config.SessionID = sessionID
  972. }
  973. if len(config.SessionID) != 2*protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH ||
  974. -1 != strings.IndexFunc(config.SessionID, func(c rune) bool {
  975. return !unicode.Is(unicode.ASCII_Hex_Digit, c) || unicode.IsUpper(c)
  976. }) {
  977. return errors.TraceNew("invalid SessionID")
  978. }
  979. config.params, err = parameters.NewParameters(
  980. func(err error) {
  981. NoticeWarning("Parameters getValue failed: %s", err)
  982. })
  983. if err != nil {
  984. return errors.Trace(err)
  985. }
  986. if config.ObfuscatedSSHAlgorithms != nil &&
  987. len(config.ObfuscatedSSHAlgorithms) != 4 {
  988. // TODO: validate each algorithm?
  989. return errors.TraceNew("invalid ObfuscatedSSHAlgorithms")
  990. }
  991. // parametersParameters.Set will validate the config fields applied to
  992. // parametersParameters.
  993. err = config.SetParameters("", false, nil)
  994. if err != nil {
  995. return errors.Trace(err)
  996. }
  997. // Calculate and set the dial parameters hash. After this point, related
  998. // config fields must not change.
  999. config.setDialParametersHash()
  1000. // Set defaults for dynamic config fields.
  1001. config.SetDynamicConfig(config.SponsorId, config.Authorizations)
  1002. // Initialize config.deviceBinder and config.config.networkIDGetter. These
  1003. // wrap config.DeviceBinder and config.NetworkIDGetter/NetworkID with
  1004. // loggers.
  1005. //
  1006. // New variables are set to avoid mutating input config fields.
  1007. // Internally, code must use config.deviceBinder and
  1008. // config.networkIDGetter and not the input/exported fields.
  1009. if config.DeviceBinder != nil {
  1010. config.deviceBinder = newLoggingDeviceBinder(config.DeviceBinder)
  1011. }
  1012. networkIDGetter := config.NetworkIDGetter
  1013. if networkIDGetter == nil {
  1014. // Limitation: unlike NetworkIDGetter, which calls back to platform APIs
  1015. // this method of network identification is not dynamic and will not reflect
  1016. // network changes that occur while running.
  1017. if config.NetworkID != "" {
  1018. networkIDGetter = newStaticNetworkGetter(config.NetworkID)
  1019. } else {
  1020. networkIDGetter = newStaticNetworkGetter("UNKNOWN")
  1021. }
  1022. }
  1023. config.networkIDGetter = newLoggingNetworkIDGetter(networkIDGetter)
  1024. // Initialize config.clientFeatures, which adds feature names on top of
  1025. // those specified by the host application in config.ClientFeatures.
  1026. config.clientFeatures = config.ClientFeatures
  1027. feature := "VPN"
  1028. if config.DeviceBinder != nil && !common.Contains(config.clientFeatures, feature) {
  1029. config.clientFeatures = append(config.clientFeatures, feature)
  1030. }
  1031. // Migrate from old config fields. This results in files being moved under
  1032. // a config specified data root directory.
  1033. if migrateFromLegacyFields && needMigration {
  1034. // If unset, set MigrateDataStoreDirectory to the previous default value for
  1035. // DataStoreDirectory to ensure that datastore files are migrated.
  1036. if config.MigrateDataStoreDirectory == "" {
  1037. wd, err := os.Getwd()
  1038. if err != nil {
  1039. return errors.Trace(err)
  1040. }
  1041. NoticeInfo("MigrateDataStoreDirectory unset, using working directory")
  1042. config.MigrateDataStoreDirectory = wd
  1043. }
  1044. // Move files that exist at legacy file paths under the data root
  1045. // directory.
  1046. migrations, err := migrationsFromLegacyFilePaths(config)
  1047. if err != nil {
  1048. return errors.Trace(err)
  1049. }
  1050. // Do migrations
  1051. successfulMigrations := 0
  1052. for _, migration := range migrations {
  1053. err := DoFileMigration(migration)
  1054. if err != nil {
  1055. NoticeWarning("Config migration: %s", errors.Trace(err))
  1056. } else {
  1057. successfulMigrations += 1
  1058. }
  1059. }
  1060. NoticeInfo(fmt.Sprintf(
  1061. "Config migration: %d/%d legacy files successfully migrated",
  1062. successfulMigrations, len(migrations)))
  1063. // Remove OSL directory if empty
  1064. if config.MigrateObfuscatedServerListDownloadDirectory != "" {
  1065. files, err := ioutil.ReadDir(config.MigrateObfuscatedServerListDownloadDirectory)
  1066. if err != nil {
  1067. NoticeWarning(
  1068. "Error reading OSL directory: %s",
  1069. errors.Trace(common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory)))
  1070. } else if len(files) == 0 {
  1071. err := os.Remove(config.MigrateObfuscatedServerListDownloadDirectory)
  1072. if err != nil {
  1073. NoticeWarning(
  1074. "Error deleting empty OSL directory: %s",
  1075. errors.Trace(common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory)))
  1076. }
  1077. }
  1078. }
  1079. f, err := os.Create(migrationCompleteFilePath)
  1080. if err != nil {
  1081. NoticeWarning(
  1082. "Config migration: failed to create migration completed file with error %s",
  1083. errors.Trace(common.RedactFilePathsError(err, migrationCompleteFilePath)))
  1084. } else {
  1085. NoticeInfo("Config migration: completed")
  1086. f.Close()
  1087. }
  1088. }
  1089. config.committed = true
  1090. return nil
  1091. }
  1092. // GetParameters returns the current parameters.Parameters.
  1093. func (config *Config) GetParameters() *parameters.Parameters {
  1094. return config.params
  1095. }
  1096. // SetParameters resets the parameters.Parameters to the default values,
  1097. // applies any config file values, and then applies the input parameters (from
  1098. // tactics, etc.)
  1099. //
  1100. // Set skipOnError to false when initially applying only config values, as
  1101. // this will validate the values and should fail. Set skipOnError to true when
  1102. // applying tactics to ignore invalid or unknown parameter values from tactics.
  1103. //
  1104. // In the case of applying tactics, do not call Config.parameters.Set
  1105. // directly as this will not first apply config values.
  1106. //
  1107. // If there is an error, the existing Config.parameters are left
  1108. // entirely unmodified.
  1109. func (config *Config) SetParameters(tag string, skipOnError bool, applyParameters map[string]interface{}) error {
  1110. setParameters := []map[string]interface{}{config.makeConfigParameters()}
  1111. if applyParameters != nil {
  1112. setParameters = append(setParameters, applyParameters)
  1113. }
  1114. counts, err := config.params.Set(tag, skipOnError, setParameters...)
  1115. if err != nil {
  1116. return errors.Trace(err)
  1117. }
  1118. NoticeInfo("applied %v parameters with tag '%s'", counts, tag)
  1119. // Emit certain individual parameter values for quick reference in diagnostics.
  1120. p := config.params.Get()
  1121. NoticeInfo(
  1122. "NetworkLatencyMultiplier Min/Max/Lambda: %f/%f/%f",
  1123. p.Float(parameters.NetworkLatencyMultiplierMin),
  1124. p.Float(parameters.NetworkLatencyMultiplierMax),
  1125. p.Float(parameters.NetworkLatencyMultiplierLambda))
  1126. // Application Parameters are feature flags/config info, delivered as Client
  1127. // Parameters via tactics/etc., to be communicated to the outer application.
  1128. // Emit these now, as notices.
  1129. if p.WeightedCoinFlip(parameters.ApplicationParametersProbability) {
  1130. NoticeApplicationParameters(p.KeyValues(parameters.ApplicationParameters))
  1131. }
  1132. return nil
  1133. }
  1134. // SetResolver sets the current resolver.
  1135. func (config *Config) SetResolver(resolver *resolver.Resolver) {
  1136. config.resolverMutex.Lock()
  1137. defer config.resolverMutex.Unlock()
  1138. config.resolver = resolver
  1139. }
  1140. // GetResolver returns the current resolver. May return nil.
  1141. func (config *Config) GetResolver() *resolver.Resolver {
  1142. config.resolverMutex.Lock()
  1143. defer config.resolverMutex.Unlock()
  1144. return config.resolver
  1145. }
  1146. // SetDynamicConfig sets the current client sponsor ID and authorizations.
  1147. // Invalid values for sponsor ID are ignored. The caller must not modify the
  1148. // input authorizations slice.
  1149. func (config *Config) SetDynamicConfig(sponsorID string, authorizations []string) {
  1150. config.dynamicConfigMutex.Lock()
  1151. defer config.dynamicConfigMutex.Unlock()
  1152. if sponsorID != "" {
  1153. config.sponsorID = sponsorID
  1154. }
  1155. config.authorizations = authorizations
  1156. }
  1157. // GetSponsorID returns the current client sponsor ID.
  1158. func (config *Config) GetSponsorID() string {
  1159. config.dynamicConfigMutex.Lock()
  1160. defer config.dynamicConfigMutex.Unlock()
  1161. return config.sponsorID
  1162. }
  1163. // IsSplitTunnelEnabled indicates if split tunnel mode is enabled, either for
  1164. // the client's own country, a specified list of countries, or both.
  1165. func (config *Config) IsSplitTunnelEnabled() bool {
  1166. return config.SplitTunnelOwnRegion || len(config.SplitTunnelRegions) > 0
  1167. }
  1168. // GetAuthorizations returns the current client authorizations.
  1169. // The caller must not modify the returned slice.
  1170. func (config *Config) GetAuthorizations() []string {
  1171. config.dynamicConfigMutex.Lock()
  1172. defer config.dynamicConfigMutex.Unlock()
  1173. return config.authorizations
  1174. }
  1175. // GetPsiphonDataDirectory returns the directory under which all persistent
  1176. // files should be stored. This directory is created under
  1177. // config.DataRootDirectory. The motivation for an additional directory is that
  1178. // config.DataRootDirectory defaults to the current working directory, which may
  1179. // include non-tunnel-core files that should be excluded from directory-spanning
  1180. // operations (e.g. excluding all tunnel-core files from backup).
  1181. func (config *Config) GetPsiphonDataDirectory() string {
  1182. return filepath.Join(config.DataRootDirectory, PsiphonDataDirectoryName)
  1183. }
  1184. // GetHomePageFilename the path where the homepage notices file will be created.
  1185. func (config *Config) GetHomePageFilename() string {
  1186. return filepath.Join(config.GetPsiphonDataDirectory(), HomepageFilename)
  1187. }
  1188. // GetNoticesFilename returns the path where the notices file will be created.
  1189. // When the file is rotated it will be moved to config.GetOldNoticesFilename().
  1190. func (config *Config) GetNoticesFilename() string {
  1191. return filepath.Join(config.GetPsiphonDataDirectory(), NoticesFilename)
  1192. }
  1193. // GetOldNoticeFilename returns the path where the rotated notices file will be
  1194. // created.
  1195. func (config *Config) GetOldNoticesFilename() string {
  1196. return filepath.Join(config.GetPsiphonDataDirectory(), OldNoticesFilename)
  1197. }
  1198. // GetDataStoreDirectory returns the directory in which the persistent database
  1199. // will be stored. Created in Config.Commit(). The persistent database contains
  1200. // information such as server entries.
  1201. func (config *Config) GetDataStoreDirectory() string {
  1202. return filepath.Join(config.GetPsiphonDataDirectory(), "datastore")
  1203. }
  1204. // GetObfuscatedServerListDownloadDirectory returns the directory in which
  1205. // obfuscated remote server list downloads will be stored. Created in
  1206. // Config.Commit().
  1207. func (config *Config) GetObfuscatedServerListDownloadDirectory() string {
  1208. return filepath.Join(config.GetPsiphonDataDirectory(), "osl")
  1209. }
  1210. // GetRemoteServerListDownloadFilename returns the filename where the remote
  1211. // server list download will be stored. Data is stored in co-located files
  1212. // (RemoteServerListDownloadFilename.part*) to allow for resumable downloading.
  1213. func (config *Config) GetRemoteServerListDownloadFilename() string {
  1214. return filepath.Join(config.GetPsiphonDataDirectory(), "remote_server_list")
  1215. }
  1216. // GetUpgradeDownloadFilename specifies the filename where upgrade downloads
  1217. // will be stored. This filename is valid when UpgradeDownloadURLs
  1218. // (or UpgradeDownloadUrl) is specified. Data is stored in co-located files
  1219. // (UpgradeDownloadFilename.part*) to allow for resumable downloading.
  1220. func (config *Config) GetUpgradeDownloadFilename() string {
  1221. return filepath.Join(config.GetPsiphonDataDirectory(), UpgradeDownloadFilename)
  1222. }
  1223. // UseUpstreamProxy indicates if an upstream proxy has been
  1224. // configured.
  1225. func (config *Config) UseUpstreamProxy() bool {
  1226. return config.UpstreamProxyURL != ""
  1227. }
  1228. // GetNetworkID returns the current network ID. When NetworkIDGetter
  1229. // is set, this calls into the host application; otherwise, a default
  1230. // value is returned.
  1231. func (config *Config) GetNetworkID() string {
  1232. return config.networkIDGetter.GetNetworkID()
  1233. }
  1234. func (config *Config) makeConfigParameters() map[string]interface{} {
  1235. // Build set of config values to apply to parameters.
  1236. //
  1237. // Note: names of some config fields such as
  1238. // StaggerConnectionWorkersMilliseconds and LimitMeekBufferSizes have
  1239. // changed in the parameters. The existing config fields are retained for
  1240. // backwards compatibility.
  1241. applyParameters := make(map[string]interface{})
  1242. // To support platform clients that configure NetworkLatencyMultiplier, set
  1243. // the NetworkLatencyMultiplierMin/NetworkLatencyMultiplierMax range to the
  1244. // specified value. Also set the older NetworkLatencyMultiplier tactic, since
  1245. // that will be used in the case of replaying with dial parameters persisted
  1246. // by an older client version.
  1247. if config.NetworkLatencyMultiplier > 0.0 {
  1248. applyParameters[parameters.NetworkLatencyMultiplier] = config.NetworkLatencyMultiplier
  1249. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplier
  1250. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplier
  1251. }
  1252. if config.NetworkLatencyMultiplierMin > 0.0 {
  1253. applyParameters[parameters.NetworkLatencyMultiplierMin] = config.NetworkLatencyMultiplierMin
  1254. }
  1255. if config.NetworkLatencyMultiplierMax > 0.0 {
  1256. applyParameters[parameters.NetworkLatencyMultiplierMax] = config.NetworkLatencyMultiplierMax
  1257. }
  1258. if config.NetworkLatencyMultiplierLambda > 0.0 {
  1259. applyParameters[parameters.NetworkLatencyMultiplierLambda] = config.NetworkLatencyMultiplierLambda
  1260. }
  1261. if len(config.LimitTunnelProtocols) > 0 {
  1262. applyParameters[parameters.LimitTunnelProtocols] = protocol.TunnelProtocols(config.LimitTunnelProtocols)
  1263. }
  1264. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  1265. applyParameters[parameters.InitialLimitTunnelProtocols] = protocol.TunnelProtocols(config.InitialLimitTunnelProtocols)
  1266. applyParameters[parameters.InitialLimitTunnelProtocolsCandidateCount] = config.InitialLimitTunnelProtocolsCandidateCount
  1267. }
  1268. if len(config.LimitTLSProfiles) > 0 {
  1269. applyParameters[parameters.LimitTLSProfiles] = protocol.TunnelProtocols(config.LimitTLSProfiles)
  1270. }
  1271. if len(config.LimitQUICVersions) > 0 {
  1272. applyParameters[parameters.LimitQUICVersions] = protocol.QUICVersions(config.LimitQUICVersions)
  1273. }
  1274. if config.EstablishTunnelTimeoutSeconds != nil {
  1275. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  1276. }
  1277. if config.EstablishTunnelServerAffinityGracePeriodMilliseconds != nil {
  1278. applyParameters[parameters.EstablishTunnelServerAffinityGracePeriod] = fmt.Sprintf("%dms", *config.EstablishTunnelServerAffinityGracePeriodMilliseconds)
  1279. }
  1280. if config.EstablishTunnelPausePeriodSeconds != nil {
  1281. applyParameters[parameters.EstablishTunnelPausePeriod] = fmt.Sprintf("%ds", *config.EstablishTunnelPausePeriodSeconds)
  1282. }
  1283. if config.ConnectionWorkerPoolSize != 0 {
  1284. applyParameters[parameters.ConnectionWorkerPoolSize] = config.ConnectionWorkerPoolSize
  1285. }
  1286. if config.TunnelPoolSize != 0 {
  1287. applyParameters[parameters.TunnelPoolSize] = config.TunnelPoolSize
  1288. }
  1289. if config.StaggerConnectionWorkersMilliseconds > 0 {
  1290. applyParameters[parameters.StaggerConnectionWorkersPeriod] = fmt.Sprintf("%dms", config.StaggerConnectionWorkersMilliseconds)
  1291. }
  1292. if config.LimitIntensiveConnectionWorkers > 0 {
  1293. applyParameters[parameters.LimitIntensiveConnectionWorkers] = config.LimitIntensiveConnectionWorkers
  1294. }
  1295. applyParameters[parameters.MeekLimitBufferSizes] = config.LimitMeekBufferSizes
  1296. applyParameters[parameters.IgnoreHandshakeStatsRegexps] = config.IgnoreHandshakeStatsRegexps
  1297. if config.EstablishTunnelTimeoutSeconds != nil {
  1298. applyParameters[parameters.EstablishTunnelTimeout] = fmt.Sprintf("%ds", *config.EstablishTunnelTimeoutSeconds)
  1299. }
  1300. if config.FetchRemoteServerListRetryPeriodMilliseconds != nil {
  1301. applyParameters[parameters.FetchRemoteServerListRetryPeriod] = fmt.Sprintf("%dms", *config.FetchRemoteServerListRetryPeriodMilliseconds)
  1302. }
  1303. if config.FetchUpgradeRetryPeriodMilliseconds != nil {
  1304. applyParameters[parameters.FetchUpgradeRetryPeriod] = fmt.Sprintf("%dms", *config.FetchUpgradeRetryPeriodMilliseconds)
  1305. }
  1306. if !config.DisableRemoteServerListFetcher {
  1307. if config.RemoteServerListURLs != nil {
  1308. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  1309. applyParameters[parameters.RemoteServerListURLs] = config.RemoteServerListURLs
  1310. }
  1311. if config.ObfuscatedServerListRootURLs != nil {
  1312. applyParameters[parameters.RemoteServerListSignaturePublicKey] = config.RemoteServerListSignaturePublicKey
  1313. applyParameters[parameters.ObfuscatedServerListRootURLs] = config.ObfuscatedServerListRootURLs
  1314. }
  1315. }
  1316. if config.UpgradeDownloadURLs != nil {
  1317. applyParameters[parameters.UpgradeDownloadClientVersionHeader] = config.UpgradeDownloadClientVersionHeader
  1318. applyParameters[parameters.UpgradeDownloadURLs] = config.UpgradeDownloadURLs
  1319. }
  1320. if len(config.FeedbackUploadURLs) > 0 {
  1321. applyParameters[parameters.FeedbackUploadURLs] = config.FeedbackUploadURLs
  1322. }
  1323. if config.FeedbackEncryptionPublicKey != "" {
  1324. applyParameters[parameters.FeedbackEncryptionPublicKey] = config.FeedbackEncryptionPublicKey
  1325. }
  1326. applyParameters[parameters.TunnelRateLimits] = config.RateLimits
  1327. if config.TransformHostNameProbability != nil {
  1328. applyParameters[parameters.TransformHostNameProbability] = *config.TransformHostNameProbability
  1329. }
  1330. if config.FragmentorProbability != nil {
  1331. applyParameters[parameters.FragmentorProbability] = *config.FragmentorProbability
  1332. }
  1333. if len(config.FragmentorLimitProtocols) > 0 {
  1334. applyParameters[parameters.FragmentorLimitProtocols] = protocol.TunnelProtocols(config.FragmentorLimitProtocols)
  1335. }
  1336. if config.FragmentorMinTotalBytes != nil {
  1337. applyParameters[parameters.FragmentorMinTotalBytes] = *config.FragmentorMinTotalBytes
  1338. }
  1339. if config.FragmentorMaxTotalBytes != nil {
  1340. applyParameters[parameters.FragmentorMaxTotalBytes] = *config.FragmentorMaxTotalBytes
  1341. }
  1342. if config.FragmentorMinWriteBytes != nil {
  1343. applyParameters[parameters.FragmentorMinWriteBytes] = *config.FragmentorMinWriteBytes
  1344. }
  1345. if config.FragmentorMaxWriteBytes != nil {
  1346. applyParameters[parameters.FragmentorMaxWriteBytes] = *config.FragmentorMaxWriteBytes
  1347. }
  1348. if config.FragmentorMinDelayMicroseconds != nil {
  1349. applyParameters[parameters.FragmentorMinDelay] = fmt.Sprintf("%dus", *config.FragmentorMinDelayMicroseconds)
  1350. }
  1351. if config.FragmentorMaxDelayMicroseconds != nil {
  1352. applyParameters[parameters.FragmentorMaxDelay] = fmt.Sprintf("%dus", *config.FragmentorMaxDelayMicroseconds)
  1353. }
  1354. if config.MeekTrafficShapingProbability != nil {
  1355. applyParameters[parameters.MeekTrafficShapingProbability] = *config.MeekTrafficShapingProbability
  1356. }
  1357. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  1358. applyParameters[parameters.MeekTrafficShapingLimitProtocols] = protocol.TunnelProtocols(config.MeekTrafficShapingLimitProtocols)
  1359. }
  1360. if config.MeekMinTLSPadding != nil {
  1361. applyParameters[parameters.MeekMinTLSPadding] = *config.MeekMinTLSPadding
  1362. }
  1363. if config.MeekMaxTLSPadding != nil {
  1364. applyParameters[parameters.MeekMaxTLSPadding] = *config.MeekMaxTLSPadding
  1365. }
  1366. if config.MeekMinLimitRequestPayloadLength != nil {
  1367. applyParameters[parameters.MeekMinLimitRequestPayloadLength] = *config.MeekMinLimitRequestPayloadLength
  1368. }
  1369. if config.MeekMaxLimitRequestPayloadLength != nil {
  1370. applyParameters[parameters.MeekMaxLimitRequestPayloadLength] = *config.MeekMaxLimitRequestPayloadLength
  1371. }
  1372. if config.MeekRedialTLSProbability != nil {
  1373. applyParameters[parameters.MeekRedialTLSProbability] = *config.MeekRedialTLSProbability
  1374. }
  1375. if config.MeekAlternateCookieNameProbability != nil {
  1376. applyParameters[parameters.MeekAlternateCookieNameProbability] = *config.MeekAlternateCookieNameProbability
  1377. }
  1378. if config.MeekAlternateContentTypeProbability != nil {
  1379. applyParameters[parameters.MeekAlternateContentTypeProbability] = *config.MeekAlternateContentTypeProbability
  1380. }
  1381. if config.ObfuscatedSSHMinPadding != nil {
  1382. applyParameters[parameters.ObfuscatedSSHMinPadding] = *config.ObfuscatedSSHMinPadding
  1383. }
  1384. if config.ObfuscatedSSHMaxPadding != nil {
  1385. applyParameters[parameters.ObfuscatedSSHMaxPadding] = *config.ObfuscatedSSHMaxPadding
  1386. }
  1387. if config.LivenessTestMinUpstreamBytes != nil {
  1388. applyParameters[parameters.LivenessTestMinUpstreamBytes] = *config.LivenessTestMinUpstreamBytes
  1389. }
  1390. if config.LivenessTestMaxUpstreamBytes != nil {
  1391. applyParameters[parameters.LivenessTestMaxUpstreamBytes] = *config.LivenessTestMaxUpstreamBytes
  1392. }
  1393. if config.LivenessTestMinDownstreamBytes != nil {
  1394. applyParameters[parameters.LivenessTestMinDownstreamBytes] = *config.LivenessTestMinDownstreamBytes
  1395. }
  1396. if config.LivenessTestMaxDownstreamBytes != nil {
  1397. applyParameters[parameters.LivenessTestMaxDownstreamBytes] = *config.LivenessTestMaxDownstreamBytes
  1398. }
  1399. if config.ReplayCandidateCount != nil {
  1400. applyParameters[parameters.ReplayCandidateCount] = *config.ReplayCandidateCount
  1401. }
  1402. if config.ReplayDialParametersTTLSeconds != nil {
  1403. applyParameters[parameters.ReplayDialParametersTTL] = fmt.Sprintf("%ds", *config.ReplayDialParametersTTLSeconds)
  1404. }
  1405. if config.ReplayTargetUpstreamBytes != nil {
  1406. applyParameters[parameters.ReplayTargetUpstreamBytes] = *config.ReplayTargetUpstreamBytes
  1407. }
  1408. if config.ReplayTargetDownstreamBytes != nil {
  1409. applyParameters[parameters.ReplayTargetDownstreamBytes] = *config.ReplayTargetDownstreamBytes
  1410. }
  1411. if config.ReplayTargetTunnelDurationSeconds != nil {
  1412. applyParameters[parameters.ReplayTargetTunnelDuration] = fmt.Sprintf("%ds", *config.ReplayTargetTunnelDurationSeconds)
  1413. }
  1414. if config.ReplayLaterRoundMoveToFrontProbability != nil {
  1415. applyParameters[parameters.ReplayLaterRoundMoveToFrontProbability] = *config.ReplayLaterRoundMoveToFrontProbability
  1416. }
  1417. if config.ReplayRetainFailedProbability != nil {
  1418. applyParameters[parameters.ReplayRetainFailedProbability] = *config.ReplayRetainFailedProbability
  1419. }
  1420. if config.UseOnlyCustomTLSProfiles != nil {
  1421. applyParameters[parameters.UseOnlyCustomTLSProfiles] = *config.UseOnlyCustomTLSProfiles
  1422. }
  1423. if len(config.CustomTLSProfiles) > 0 {
  1424. applyParameters[parameters.CustomTLSProfiles] = config.CustomTLSProfiles
  1425. }
  1426. if config.SelectRandomizedTLSProfileProbability != nil {
  1427. applyParameters[parameters.SelectRandomizedTLSProfileProbability] = *config.SelectRandomizedTLSProfileProbability
  1428. }
  1429. if config.NoDefaultTLSSessionIDProbability != nil {
  1430. applyParameters[parameters.NoDefaultTLSSessionIDProbability] = *config.NoDefaultTLSSessionIDProbability
  1431. }
  1432. if len(config.DisableFrontingProviderTLSProfiles) > 0 {
  1433. applyParameters[parameters.DisableFrontingProviderTLSProfiles] = config.DisableFrontingProviderTLSProfiles
  1434. }
  1435. if config.ClientBurstUpstreamTargetBytes != nil {
  1436. applyParameters[parameters.ClientBurstUpstreamTargetBytes] = *config.ClientBurstUpstreamTargetBytes
  1437. }
  1438. if config.ClientBurstUpstreamDeadlineMilliseconds != nil {
  1439. applyParameters[parameters.ClientBurstUpstreamDeadline] = fmt.Sprintf("%dms", *config.ClientBurstUpstreamDeadlineMilliseconds)
  1440. }
  1441. if config.ClientBurstDownstreamTargetBytes != nil {
  1442. applyParameters[parameters.ClientBurstDownstreamTargetBytes] = *config.ClientBurstDownstreamTargetBytes
  1443. }
  1444. if config.ClientBurstDownstreamDeadlineMilliseconds != nil {
  1445. applyParameters[parameters.ClientBurstDownstreamDeadline] = fmt.Sprintf("%dms", *config.ClientBurstDownstreamDeadlineMilliseconds)
  1446. }
  1447. if config.ApplicationParameters != nil {
  1448. applyParameters[parameters.ApplicationParameters] = config.ApplicationParameters
  1449. }
  1450. if config.CustomHostNameRegexes != nil {
  1451. applyParameters[parameters.CustomHostNameRegexes] = parameters.RegexStrings(config.CustomHostNameRegexes)
  1452. }
  1453. if config.CustomHostNameProbability != nil {
  1454. applyParameters[parameters.CustomHostNameProbability] = *config.CustomHostNameProbability
  1455. }
  1456. if config.CustomHostNameLimitProtocols != nil {
  1457. applyParameters[parameters.CustomHostNameLimitProtocols] = protocol.TunnelProtocols(config.CustomHostNameLimitProtocols)
  1458. }
  1459. if config.ConjureCachedRegistrationTTLSeconds != nil {
  1460. applyParameters[parameters.ConjureCachedRegistrationTTL] = fmt.Sprintf("%ds", *config.ConjureCachedRegistrationTTLSeconds)
  1461. }
  1462. if config.ConjureAPIRegistrarBidirectionalURL != "" {
  1463. applyParameters[parameters.ConjureAPIRegistrarBidirectionalURL] = config.ConjureAPIRegistrarBidirectionalURL
  1464. }
  1465. if len(config.ConjureAPIRegistrarFrontingSpecs) > 0 {
  1466. applyParameters[parameters.ConjureAPIRegistrarFrontingSpecs] = config.ConjureAPIRegistrarFrontingSpecs
  1467. }
  1468. if config.ConjureAPIRegistrarMinDelayMilliseconds != nil {
  1469. applyParameters[parameters.ConjureAPIRegistrarMinDelay] = fmt.Sprintf("%dms", *config.ConjureAPIRegistrarMinDelayMilliseconds)
  1470. }
  1471. if config.ConjureAPIRegistrarMaxDelayMilliseconds != nil {
  1472. applyParameters[parameters.ConjureAPIRegistrarMaxDelay] = fmt.Sprintf("%dms", *config.ConjureAPIRegistrarMaxDelayMilliseconds)
  1473. }
  1474. if config.ConjureDecoyRegistrarProbability != nil {
  1475. applyParameters[parameters.ConjureDecoyRegistrarProbability] = *config.ConjureDecoyRegistrarProbability
  1476. }
  1477. if config.ConjureDecoyRegistrarWidth != nil {
  1478. applyParameters[parameters.ConjureDecoyRegistrarWidth] = *config.ConjureDecoyRegistrarWidth
  1479. }
  1480. if config.ConjureDecoyRegistrarMinDelayMilliseconds != nil {
  1481. applyParameters[parameters.ConjureDecoyRegistrarMinDelay] = fmt.Sprintf("%dms", *config.ConjureDecoyRegistrarMinDelayMilliseconds)
  1482. }
  1483. if config.ConjureDecoyRegistrarMaxDelayMilliseconds != nil {
  1484. applyParameters[parameters.ConjureDecoyRegistrarMaxDelay] = fmt.Sprintf("%dms", *config.ConjureDecoyRegistrarMaxDelayMilliseconds)
  1485. }
  1486. if config.HoldOffTunnelMinDurationMilliseconds != nil {
  1487. applyParameters[parameters.HoldOffTunnelMinDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMinDurationMilliseconds)
  1488. }
  1489. if config.HoldOffTunnelMaxDurationMilliseconds != nil {
  1490. applyParameters[parameters.HoldOffTunnelMaxDuration] = fmt.Sprintf("%dms", *config.HoldOffTunnelMaxDurationMilliseconds)
  1491. }
  1492. if len(config.HoldOffTunnelProtocols) > 0 {
  1493. applyParameters[parameters.HoldOffTunnelProtocols] = protocol.TunnelProtocols(config.HoldOffTunnelProtocols)
  1494. }
  1495. if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
  1496. applyParameters[parameters.HoldOffTunnelFrontingProviderIDs] = config.HoldOffTunnelFrontingProviderIDs
  1497. }
  1498. if config.HoldOffTunnelProbability != nil {
  1499. applyParameters[parameters.HoldOffTunnelProbability] = *config.HoldOffTunnelProbability
  1500. }
  1501. if len(config.RestrictFrontingProviderIDs) > 0 {
  1502. applyParameters[parameters.RestrictFrontingProviderIDs] = config.RestrictFrontingProviderIDs
  1503. }
  1504. if config.RestrictFrontingProviderIDsClientProbability != nil {
  1505. applyParameters[parameters.RestrictFrontingProviderIDsClientProbability] = *config.RestrictFrontingProviderIDsClientProbability
  1506. }
  1507. if config.UpstreamProxyAllowAllServerEntrySources != nil {
  1508. applyParameters[parameters.UpstreamProxyAllowAllServerEntrySources] = *config.UpstreamProxyAllowAllServerEntrySources
  1509. }
  1510. if len(config.LimitTunnelDialPortNumbers) > 0 {
  1511. applyParameters[parameters.LimitTunnelDialPortNumbers] = config.LimitTunnelDialPortNumbers
  1512. }
  1513. if config.QUICDisablePathMTUDiscoveryProbability != nil {
  1514. applyParameters[parameters.QUICDisableClientPathMTUDiscoveryProbability] = *config.QUICDisablePathMTUDiscoveryProbability
  1515. }
  1516. if config.DNSResolverAttemptsPerServer != nil {
  1517. applyParameters[parameters.DNSResolverAttemptsPerServer] = *config.DNSResolverAttemptsPerServer
  1518. }
  1519. if config.DNSResolverAttemptsPerPreferredServer != nil {
  1520. applyParameters[parameters.DNSResolverAttemptsPerPreferredServer] = *config.DNSResolverAttemptsPerPreferredServer
  1521. }
  1522. if config.DNSResolverRequestTimeoutMilliseconds != nil {
  1523. applyParameters[parameters.DNSResolverRequestTimeout] = fmt.Sprintf("%dms", *config.DNSResolverRequestTimeoutMilliseconds)
  1524. }
  1525. if config.DNSResolverAwaitTimeoutMilliseconds != nil {
  1526. applyParameters[parameters.DNSResolverAwaitTimeout] = fmt.Sprintf("%dms", *config.DNSResolverAwaitTimeoutMilliseconds)
  1527. }
  1528. if config.DNSResolverPreresolvedIPAddressProbability != nil {
  1529. applyParameters[parameters.DNSResolverPreresolvedIPAddressProbability] = *config.DNSResolverPreresolvedIPAddressProbability
  1530. }
  1531. if config.DNSResolverPreresolvedIPAddressCIDRs != nil {
  1532. applyParameters[parameters.DNSResolverPreresolvedIPAddressCIDRs] = config.DNSResolverPreresolvedIPAddressCIDRs
  1533. }
  1534. if config.DNSResolverAlternateServers != nil {
  1535. applyParameters[parameters.DNSResolverAlternateServers] = config.DNSResolverAlternateServers
  1536. }
  1537. if config.DNSResolverPreferredAlternateServers != nil {
  1538. applyParameters[parameters.DNSResolverPreferredAlternateServers] = config.DNSResolverPreferredAlternateServers
  1539. }
  1540. if config.DNSResolverPreferAlternateServerProbability != nil {
  1541. applyParameters[parameters.DNSResolverPreferAlternateServerProbability] = *config.DNSResolverPreferAlternateServerProbability
  1542. }
  1543. if config.DNSResolverProtocolTransformSpecs != nil {
  1544. applyParameters[parameters.DNSResolverProtocolTransformSpecs] = config.DNSResolverProtocolTransformSpecs
  1545. }
  1546. if config.DNSResolverProtocolTransformScopedSpecNames != nil {
  1547. applyParameters[parameters.DNSResolverProtocolTransformScopedSpecNames] = config.DNSResolverProtocolTransformScopedSpecNames
  1548. }
  1549. if config.DNSResolverProtocolTransformProbability != nil {
  1550. applyParameters[parameters.DNSResolverProtocolTransformProbability] = *config.DNSResolverProtocolTransformProbability
  1551. }
  1552. if config.DNSResolverIncludeEDNS0Probability != nil {
  1553. applyParameters[parameters.DNSResolverIncludeEDNS0Probability] = *config.DNSResolverIncludeEDNS0Probability
  1554. }
  1555. if config.DNSResolverCacheExtensionInitialTTLMilliseconds != nil {
  1556. applyParameters[parameters.DNSResolverCacheExtensionInitialTTL] = fmt.Sprintf("%dms", *config.DNSResolverCacheExtensionInitialTTLMilliseconds)
  1557. }
  1558. if config.DNSResolverCacheExtensionVerifiedTTLMilliseconds != nil {
  1559. applyParameters[parameters.DNSResolverCacheExtensionVerifiedTTL] = fmt.Sprintf("%dms", *config.DNSResolverCacheExtensionVerifiedTTLMilliseconds)
  1560. }
  1561. // When adding new config dial parameters that may override tactics, also
  1562. // update setDialParametersHash.
  1563. return applyParameters
  1564. }
  1565. func (config *Config) setDialParametersHash() {
  1566. // Calculate and store a hash of the config values that may impact
  1567. // dial parameters. This hash is used as part of the dial parameters
  1568. // replay mechanism to detect when persisted dial parameters should
  1569. // be discarded due to conflicting config changes.
  1570. //
  1571. // With a couple of minor exceptions, configuring dial parameters via the
  1572. // config is intended for testing only, and so these parameters are expected
  1573. // to be present in test runs only. It remains an important case to discard
  1574. // replay dial parameters when test config parameters are varied.
  1575. //
  1576. // Hashing the parameter names detects some ambiguous hash cases, such as two
  1577. // consecutive int64 parameters, one omitted and one not, that are flipped.
  1578. // The serialization is not completely unambiguous, and the format is
  1579. // currently limited by legacy cases (not invalidating replay dial parameters
  1580. // for production clients is more important than invalidating for test runs).
  1581. // We cannot hash the entire config JSON as it contains non-dial parameter
  1582. // fields which may frequently change across runs.
  1583. //
  1584. // MD5 hash is used solely as a data checksum and not for any security
  1585. // purpose.
  1586. hash := md5.New()
  1587. if len(config.LimitTunnelProtocols) > 0 {
  1588. hash.Write([]byte("LimitTunnelProtocols"))
  1589. for _, protocol := range config.LimitTunnelProtocols {
  1590. hash.Write([]byte(protocol))
  1591. }
  1592. }
  1593. if len(config.InitialLimitTunnelProtocols) > 0 && config.InitialLimitTunnelProtocolsCandidateCount > 0 {
  1594. hash.Write([]byte("InitialLimitTunnelProtocols"))
  1595. for _, protocol := range config.InitialLimitTunnelProtocols {
  1596. hash.Write([]byte(protocol))
  1597. }
  1598. binary.Write(hash, binary.LittleEndian, int64(config.InitialLimitTunnelProtocolsCandidateCount))
  1599. }
  1600. if len(config.LimitTLSProfiles) > 0 {
  1601. hash.Write([]byte("LimitTLSProfiles"))
  1602. for _, profile := range config.LimitTLSProfiles {
  1603. hash.Write([]byte(profile))
  1604. }
  1605. }
  1606. if len(config.LimitQUICVersions) > 0 {
  1607. hash.Write([]byte("LimitQUICVersions"))
  1608. for _, version := range config.LimitQUICVersions {
  1609. hash.Write([]byte(version))
  1610. }
  1611. }
  1612. // Whether a custom User-Agent is specified is a binary flag: when not set,
  1613. // the replay dial parameters value applies. When set, external
  1614. // considerations apply.
  1615. if _, ok := config.CustomHeaders["User-Agent"]; ok {
  1616. hash.Write([]byte("CustomHeaders User-Agent"))
  1617. hash.Write([]byte{1})
  1618. }
  1619. if config.UpstreamProxyURL != "" {
  1620. hash.Write([]byte("UpstreamProxyURL"))
  1621. hash.Write([]byte(config.UpstreamProxyURL))
  1622. }
  1623. if config.TransformHostNameProbability != nil {
  1624. hash.Write([]byte("TransformHostNameProbability"))
  1625. binary.Write(hash, binary.LittleEndian, *config.TransformHostNameProbability)
  1626. }
  1627. if config.FragmentorProbability != nil {
  1628. hash.Write([]byte("FragmentorProbability"))
  1629. binary.Write(hash, binary.LittleEndian, *config.FragmentorProbability)
  1630. }
  1631. if len(config.FragmentorLimitProtocols) > 0 {
  1632. hash.Write([]byte("FragmentorLimitProtocols"))
  1633. for _, protocol := range config.FragmentorLimitProtocols {
  1634. hash.Write([]byte(protocol))
  1635. }
  1636. }
  1637. if config.FragmentorMinTotalBytes != nil {
  1638. hash.Write([]byte("FragmentorMinTotalBytes"))
  1639. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinTotalBytes))
  1640. }
  1641. if config.FragmentorMaxTotalBytes != nil {
  1642. hash.Write([]byte("FragmentorMaxTotalBytes"))
  1643. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxTotalBytes))
  1644. }
  1645. if config.FragmentorMinWriteBytes != nil {
  1646. hash.Write([]byte("FragmentorMinWriteBytes"))
  1647. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinWriteBytes))
  1648. }
  1649. if config.FragmentorMaxWriteBytes != nil {
  1650. hash.Write([]byte("FragmentorMaxWriteBytes"))
  1651. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxWriteBytes))
  1652. }
  1653. if config.FragmentorMinDelayMicroseconds != nil {
  1654. hash.Write([]byte("FragmentorMinDelayMicroseconds"))
  1655. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMinDelayMicroseconds))
  1656. }
  1657. if config.FragmentorMaxDelayMicroseconds != nil {
  1658. hash.Write([]byte("FragmentorMaxDelayMicroseconds"))
  1659. binary.Write(hash, binary.LittleEndian, int64(*config.FragmentorMaxDelayMicroseconds))
  1660. }
  1661. if config.MeekTrafficShapingProbability != nil {
  1662. hash.Write([]byte("MeekTrafficShapingProbability"))
  1663. binary.Write(hash, binary.LittleEndian, int64(*config.MeekTrafficShapingProbability))
  1664. }
  1665. if len(config.MeekTrafficShapingLimitProtocols) > 0 {
  1666. hash.Write([]byte("MeekTrafficShapingLimitProtocols"))
  1667. for _, protocol := range config.MeekTrafficShapingLimitProtocols {
  1668. hash.Write([]byte(protocol))
  1669. }
  1670. }
  1671. if config.MeekMinLimitRequestPayloadLength != nil {
  1672. hash.Write([]byte("MeekMinLimitRequestPayloadLength"))
  1673. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMinLimitRequestPayloadLength))
  1674. }
  1675. if config.MeekMaxLimitRequestPayloadLength != nil {
  1676. hash.Write([]byte("MeekMaxLimitRequestPayloadLength"))
  1677. binary.Write(hash, binary.LittleEndian, int64(*config.MeekMaxLimitRequestPayloadLength))
  1678. }
  1679. if config.MeekRedialTLSProbability != nil {
  1680. hash.Write([]byte("MeekRedialTLSProbability"))
  1681. binary.Write(hash, binary.LittleEndian, *config.MeekRedialTLSProbability)
  1682. }
  1683. if config.ObfuscatedSSHMinPadding != nil {
  1684. hash.Write([]byte("ObfuscatedSSHMinPadding"))
  1685. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMinPadding))
  1686. }
  1687. if config.ObfuscatedSSHMaxPadding != nil {
  1688. hash.Write([]byte("ObfuscatedSSHMaxPadding"))
  1689. binary.Write(hash, binary.LittleEndian, int64(*config.ObfuscatedSSHMaxPadding))
  1690. }
  1691. if config.LivenessTestMinUpstreamBytes != nil {
  1692. hash.Write([]byte("LivenessTestMinUpstreamBytes"))
  1693. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinUpstreamBytes))
  1694. }
  1695. if config.LivenessTestMaxUpstreamBytes != nil {
  1696. hash.Write([]byte("LivenessTestMaxUpstreamBytes"))
  1697. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxUpstreamBytes))
  1698. }
  1699. if config.LivenessTestMinDownstreamBytes != nil {
  1700. hash.Write([]byte("LivenessTestMinDownstreamBytes"))
  1701. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMinDownstreamBytes))
  1702. }
  1703. if config.LivenessTestMaxDownstreamBytes != nil {
  1704. hash.Write([]byte("LivenessTestMaxDownstreamBytes"))
  1705. binary.Write(hash, binary.LittleEndian, int64(*config.LivenessTestMaxDownstreamBytes))
  1706. }
  1707. // Legacy case: these parameters are included in the hash unconditionally,
  1708. // and so will impact almost all production clients. These parameter names
  1709. // are not hashed since that would invalidate all replay dial parameters for
  1710. // existing clients whose hashes predate the inclusion of parameter names.
  1711. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMin)
  1712. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierMax)
  1713. binary.Write(hash, binary.LittleEndian, config.NetworkLatencyMultiplierLambda)
  1714. if config.UseOnlyCustomTLSProfiles != nil {
  1715. hash.Write([]byte("UseOnlyCustomTLSProfiles"))
  1716. binary.Write(hash, binary.LittleEndian, *config.UseOnlyCustomTLSProfiles)
  1717. }
  1718. if len(config.CustomTLSProfiles) > 0 {
  1719. hash.Write([]byte("CustomTLSProfiles"))
  1720. for _, customTLSProfile := range config.CustomTLSProfiles {
  1721. encodedCustomTLSProofile, _ := json.Marshal(customTLSProfile)
  1722. hash.Write(encodedCustomTLSProofile)
  1723. }
  1724. }
  1725. if config.SelectRandomizedTLSProfileProbability != nil {
  1726. hash.Write([]byte("SelectRandomizedTLSProfileProbability"))
  1727. binary.Write(hash, binary.LittleEndian, *config.SelectRandomizedTLSProfileProbability)
  1728. }
  1729. if config.NoDefaultTLSSessionIDProbability != nil {
  1730. hash.Write([]byte("NoDefaultTLSSessionIDProbability"))
  1731. binary.Write(hash, binary.LittleEndian, *config.NoDefaultTLSSessionIDProbability)
  1732. }
  1733. if len(config.DisableFrontingProviderTLSProfiles) > 0 {
  1734. hash.Write([]byte("DisableFrontingProviderTLSProfiles"))
  1735. encodedDisableFrontingProviderTLSProfiles, _ :=
  1736. json.Marshal(config.DisableFrontingProviderTLSProfiles)
  1737. hash.Write(encodedDisableFrontingProviderTLSProfiles)
  1738. }
  1739. if len(config.CustomHostNameRegexes) > 0 {
  1740. hash.Write([]byte("CustomHostNameRegexes"))
  1741. for _, customHostNameRegex := range config.CustomHostNameRegexes {
  1742. hash.Write([]byte(customHostNameRegex))
  1743. }
  1744. }
  1745. if config.CustomHostNameProbability != nil {
  1746. hash.Write([]byte("CustomHostNameProbability"))
  1747. binary.Write(hash, binary.LittleEndian, *config.CustomHostNameProbability)
  1748. }
  1749. if len(config.CustomHostNameLimitProtocols) > 0 {
  1750. hash.Write([]byte("CustomHostNameLimitProtocols"))
  1751. for _, protocol := range config.CustomHostNameLimitProtocols {
  1752. hash.Write([]byte(protocol))
  1753. }
  1754. }
  1755. if config.ConjureCachedRegistrationTTLSeconds != nil {
  1756. hash.Write([]byte("ConjureCachedRegistrationTTLSeconds"))
  1757. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureCachedRegistrationTTLSeconds))
  1758. }
  1759. if config.ConjureAPIRegistrarBidirectionalURL != "" {
  1760. hash.Write([]byte("ConjureAPIRegistrarBidirectionalURL"))
  1761. hash.Write([]byte(config.ConjureAPIRegistrarBidirectionalURL))
  1762. }
  1763. if len(config.ConjureAPIRegistrarFrontingSpecs) > 0 {
  1764. hash.Write([]byte("ConjureAPIRegistrarFrontingSpecs"))
  1765. for _, frontingSpec := range config.ConjureAPIRegistrarFrontingSpecs {
  1766. encodedFrontSpec, _ := json.Marshal(frontingSpec)
  1767. hash.Write(encodedFrontSpec)
  1768. }
  1769. }
  1770. if config.ConjureAPIRegistrarMinDelayMilliseconds != nil {
  1771. hash.Write([]byte("ConjureAPIRegistrarMinDelayMilliseconds"))
  1772. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureAPIRegistrarMinDelayMilliseconds))
  1773. }
  1774. if config.ConjureAPIRegistrarMaxDelayMilliseconds != nil {
  1775. hash.Write([]byte("ConjureAPIRegistrarMaxDelayMilliseconds"))
  1776. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureAPIRegistrarMaxDelayMilliseconds))
  1777. }
  1778. if config.ConjureDecoyRegistrarWidth != nil {
  1779. hash.Write([]byte("ConjureDecoyRegistrarWidth"))
  1780. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarWidth))
  1781. }
  1782. if config.ConjureDecoyRegistrarMinDelayMilliseconds != nil {
  1783. hash.Write([]byte("ConjureDecoyRegistrarMinDelayMilliseconds"))
  1784. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarMinDelayMilliseconds))
  1785. }
  1786. if config.ConjureDecoyRegistrarMaxDelayMilliseconds != nil {
  1787. hash.Write([]byte("ConjureDecoyRegistrarMaxDelayMilliseconds"))
  1788. binary.Write(hash, binary.LittleEndian, int64(*config.ConjureDecoyRegistrarMaxDelayMilliseconds))
  1789. }
  1790. if config.HoldOffTunnelMinDurationMilliseconds != nil {
  1791. hash.Write([]byte("HoldOffTunnelMinDurationMilliseconds"))
  1792. binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMinDurationMilliseconds))
  1793. }
  1794. if config.HoldOffTunnelMaxDurationMilliseconds != nil {
  1795. hash.Write([]byte("HoldOffTunnelMaxDurationMilliseconds"))
  1796. binary.Write(hash, binary.LittleEndian, int64(*config.HoldOffTunnelMaxDurationMilliseconds))
  1797. }
  1798. if len(config.HoldOffTunnelProtocols) > 0 {
  1799. hash.Write([]byte("HoldOffTunnelProtocols"))
  1800. for _, protocol := range config.HoldOffTunnelProtocols {
  1801. hash.Write([]byte(protocol))
  1802. }
  1803. }
  1804. if len(config.HoldOffTunnelFrontingProviderIDs) > 0 {
  1805. hash.Write([]byte("HoldOffTunnelFrontingProviderIDs"))
  1806. for _, providerID := range config.HoldOffTunnelFrontingProviderIDs {
  1807. hash.Write([]byte(providerID))
  1808. }
  1809. }
  1810. if config.HoldOffTunnelProbability != nil {
  1811. hash.Write([]byte("HoldOffTunnelProbability"))
  1812. binary.Write(hash, binary.LittleEndian, *config.HoldOffTunnelProbability)
  1813. }
  1814. if len(config.RestrictFrontingProviderIDs) > 0 {
  1815. hash.Write([]byte("RestrictFrontingProviderIDs"))
  1816. for _, providerID := range config.RestrictFrontingProviderIDs {
  1817. hash.Write([]byte(providerID))
  1818. }
  1819. }
  1820. if config.RestrictFrontingProviderIDsClientProbability != nil {
  1821. hash.Write([]byte("RestrictFrontingProviderIDsClientProbability"))
  1822. binary.Write(hash, binary.LittleEndian, *config.RestrictFrontingProviderIDsClientProbability)
  1823. }
  1824. if config.UpstreamProxyAllowAllServerEntrySources != nil {
  1825. hash.Write([]byte("UpstreamProxyAllowAllServerEntrySources"))
  1826. binary.Write(hash, binary.LittleEndian, *config.UpstreamProxyAllowAllServerEntrySources)
  1827. }
  1828. if len(config.LimitTunnelDialPortNumbers) > 0 {
  1829. hash.Write([]byte("LimitTunnelDialPortNumbers"))
  1830. encodedLimitTunnelDialPortNumbers, _ :=
  1831. json.Marshal(config.LimitTunnelDialPortNumbers)
  1832. hash.Write(encodedLimitTunnelDialPortNumbers)
  1833. }
  1834. if config.QUICDisablePathMTUDiscoveryProbability != nil {
  1835. hash.Write([]byte("QUICDisablePathMTUDiscoveryProbability"))
  1836. binary.Write(hash, binary.LittleEndian, *config.QUICDisablePathMTUDiscoveryProbability)
  1837. }
  1838. if config.DNSResolverAttemptsPerServer != nil {
  1839. hash.Write([]byte("DNSResolverAttemptsPerServer"))
  1840. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverAttemptsPerServer))
  1841. }
  1842. if config.DNSResolverRequestTimeoutMilliseconds != nil {
  1843. hash.Write([]byte("DNSResolverRequestTimeoutMilliseconds"))
  1844. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverRequestTimeoutMilliseconds))
  1845. }
  1846. if config.DNSResolverAwaitTimeoutMilliseconds != nil {
  1847. hash.Write([]byte("DNSResolverAwaitTimeoutMilliseconds"))
  1848. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverAwaitTimeoutMilliseconds))
  1849. }
  1850. if config.DNSResolverPreresolvedIPAddressCIDRs != nil {
  1851. hash.Write([]byte("DNSResolverPreresolvedIPAddressCIDRs"))
  1852. encodedDNSResolverPreresolvedIPAddressCIDRs, _ :=
  1853. json.Marshal(config.DNSResolverPreresolvedIPAddressCIDRs)
  1854. hash.Write(encodedDNSResolverPreresolvedIPAddressCIDRs)
  1855. }
  1856. if config.DNSResolverPreresolvedIPAddressProbability != nil {
  1857. hash.Write([]byte("DNSResolverPreresolvedIPAddressProbability"))
  1858. binary.Write(hash, binary.LittleEndian, *config.DNSResolverPreresolvedIPAddressProbability)
  1859. }
  1860. if config.DNSResolverAlternateServers != nil {
  1861. hash.Write([]byte("DNSResolverAlternateServers"))
  1862. for _, server := range config.DNSResolverAlternateServers {
  1863. hash.Write([]byte(server))
  1864. }
  1865. }
  1866. if config.DNSResolverPreferAlternateServerProbability != nil {
  1867. hash.Write([]byte("DNSResolverPreferAlternateServerProbability"))
  1868. binary.Write(hash, binary.LittleEndian, *config.DNSResolverPreferAlternateServerProbability)
  1869. }
  1870. if config.DNSResolverProtocolTransformSpecs != nil {
  1871. hash.Write([]byte("DNSResolverProtocolTransformSpecs"))
  1872. encodedDNSResolverProtocolTransformSpecs, _ :=
  1873. json.Marshal(config.DNSResolverProtocolTransformSpecs)
  1874. hash.Write(encodedDNSResolverProtocolTransformSpecs)
  1875. }
  1876. if config.DNSResolverProtocolTransformScopedSpecNames != nil {
  1877. hash.Write([]byte(""))
  1878. encodedDNSResolverProtocolTransformScopedSpecNames, _ :=
  1879. json.Marshal(config.DNSResolverProtocolTransformScopedSpecNames)
  1880. hash.Write(encodedDNSResolverProtocolTransformScopedSpecNames)
  1881. }
  1882. if config.DNSResolverProtocolTransformProbability != nil {
  1883. hash.Write([]byte("DNSResolverProtocolTransformProbability"))
  1884. binary.Write(hash, binary.LittleEndian, *config.DNSResolverProtocolTransformProbability)
  1885. }
  1886. if config.DNSResolverIncludeEDNS0Probability != nil {
  1887. hash.Write([]byte("DNSResolverIncludeEDNS0Probability"))
  1888. binary.Write(hash, binary.LittleEndian, *config.DNSResolverIncludeEDNS0Probability)
  1889. }
  1890. if config.DNSResolverCacheExtensionInitialTTLMilliseconds != nil {
  1891. hash.Write([]byte("DNSResolverCacheExtensionInitialTTLMilliseconds"))
  1892. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverCacheExtensionInitialTTLMilliseconds))
  1893. }
  1894. if config.DNSResolverCacheExtensionVerifiedTTLMilliseconds != nil {
  1895. hash.Write([]byte("DNSResolverCacheExtensionVerifiedTTLMilliseconds"))
  1896. binary.Write(hash, binary.LittleEndian, int64(*config.DNSResolverCacheExtensionVerifiedTTLMilliseconds))
  1897. }
  1898. config.dialParametersHash = hash.Sum(nil)
  1899. }
  1900. func promoteLegacyTransferURL(URL string) parameters.TransferURLs {
  1901. transferURLs := make(parameters.TransferURLs, 1)
  1902. transferURLs[0] = &parameters.TransferURL{
  1903. URL: base64.StdEncoding.EncodeToString([]byte(URL)),
  1904. SkipVerify: false,
  1905. OnlyAfterAttempts: 0,
  1906. }
  1907. return transferURLs
  1908. }
  1909. type loggingDeviceBinder struct {
  1910. d DeviceBinder
  1911. }
  1912. func newLoggingDeviceBinder(d DeviceBinder) *loggingDeviceBinder {
  1913. return &loggingDeviceBinder{d: d}
  1914. }
  1915. func (d *loggingDeviceBinder) BindToDevice(fileDescriptor int) (string, error) {
  1916. deviceInfo, err := d.d.BindToDevice(fileDescriptor)
  1917. if err == nil && deviceInfo != "" {
  1918. NoticeBindToDevice(deviceInfo)
  1919. }
  1920. return deviceInfo, err
  1921. }
  1922. type staticNetworkGetter struct {
  1923. networkID string
  1924. }
  1925. func newStaticNetworkGetter(networkID string) *staticNetworkGetter {
  1926. return &staticNetworkGetter{networkID: networkID}
  1927. }
  1928. func (n *staticNetworkGetter) GetNetworkID() string {
  1929. return n.networkID
  1930. }
  1931. type loggingNetworkIDGetter struct {
  1932. n NetworkIDGetter
  1933. }
  1934. func newLoggingNetworkIDGetter(n NetworkIDGetter) *loggingNetworkIDGetter {
  1935. return &loggingNetworkIDGetter{n: n}
  1936. }
  1937. func (n *loggingNetworkIDGetter) GetNetworkID() string {
  1938. networkID := n.n.GetNetworkID()
  1939. // All PII must appear after the initial "-"
  1940. // See: https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
  1941. logNetworkID := networkID
  1942. index := strings.Index(logNetworkID, "-")
  1943. if index != -1 {
  1944. logNetworkID = logNetworkID[:index]
  1945. }
  1946. if len(logNetworkID)+1 < len(networkID) {
  1947. // Indicate when additional network info was present after the first "-".
  1948. logNetworkID += "+[redacted]"
  1949. }
  1950. NoticeNetworkID(logNetworkID)
  1951. return networkID
  1952. }
  1953. // migrationsFromLegacyNoticeFilePaths returns the file migrations which must be
  1954. // performed to move notice files from legacy file paths, which were configured
  1955. // with the legacy config fields HomepageNoticesFilename and
  1956. // RotatingNoticesFilename, to the new file paths used by Psiphon which exist
  1957. // under the data root directory.
  1958. func migrationsFromLegacyNoticeFilePaths(config *Config) []FileMigration {
  1959. var noticeMigrations []FileMigration
  1960. if config.MigrateHomepageNoticesFilename != "" {
  1961. noticeMigrations = append(noticeMigrations, FileMigration{
  1962. Name: "hompage",
  1963. OldPath: config.MigrateHomepageNoticesFilename,
  1964. NewPath: config.GetHomePageFilename(),
  1965. })
  1966. }
  1967. if config.MigrateRotatingNoticesFilename != "" {
  1968. migrations := []FileMigration{
  1969. {
  1970. Name: "notices",
  1971. OldPath: config.MigrateRotatingNoticesFilename,
  1972. NewPath: config.GetNoticesFilename(),
  1973. IsDir: false,
  1974. },
  1975. {
  1976. Name: "notices.1",
  1977. OldPath: config.MigrateRotatingNoticesFilename + ".1",
  1978. NewPath: config.GetNoticesFilename() + ".1",
  1979. },
  1980. }
  1981. noticeMigrations = append(noticeMigrations, migrations...)
  1982. }
  1983. return noticeMigrations
  1984. }
  1985. // migrationsFromLegacyFilePaths returns the file migrations which must be
  1986. // performed to move files from legacy file paths, which were configured with
  1987. // legacy config fields, to the new file paths used by Psiphon which exist
  1988. // under the data root directory.
  1989. // Note: an attempt is made to redact any file paths from the returned error.
  1990. func migrationsFromLegacyFilePaths(config *Config) ([]FileMigration, error) {
  1991. migrations := []FileMigration{
  1992. {
  1993. Name: "psiphon.boltdb",
  1994. OldPath: filepath.Join(config.MigrateDataStoreDirectory, "psiphon.boltdb"),
  1995. NewPath: filepath.Join(config.GetDataStoreDirectory(), "psiphon.boltdb"),
  1996. },
  1997. {
  1998. Name: "psiphon.boltdb.lock",
  1999. OldPath: filepath.Join(config.MigrateDataStoreDirectory, "psiphon.boltdb.lock"),
  2000. NewPath: filepath.Join(config.GetDataStoreDirectory(), "psiphon.boltdb.lock"),
  2001. },
  2002. }
  2003. if config.MigrateRemoteServerListDownloadFilename != "" {
  2004. // Migrate remote server list files
  2005. rslMigrations := []FileMigration{
  2006. {
  2007. Name: "remote_server_list",
  2008. OldPath: config.MigrateRemoteServerListDownloadFilename,
  2009. NewPath: config.GetRemoteServerListDownloadFilename(),
  2010. },
  2011. {
  2012. Name: "remote_server_list.part",
  2013. OldPath: config.MigrateRemoteServerListDownloadFilename + ".part",
  2014. NewPath: config.GetRemoteServerListDownloadFilename() + ".part",
  2015. },
  2016. {
  2017. Name: "remote_server_list.part.etag",
  2018. OldPath: config.MigrateRemoteServerListDownloadFilename + ".part.etag",
  2019. NewPath: config.GetRemoteServerListDownloadFilename() + ".part.etag",
  2020. },
  2021. }
  2022. migrations = append(migrations, rslMigrations...)
  2023. }
  2024. if config.MigrateObfuscatedServerListDownloadDirectory != "" {
  2025. // Migrate OSL registry file and downloads
  2026. oslFileRegex, err := regexp.Compile(`^osl-.+$`)
  2027. if err != nil {
  2028. return nil, errors.TraceMsg(err, "failed to compile regex for osl files")
  2029. }
  2030. files, err := ioutil.ReadDir(config.MigrateObfuscatedServerListDownloadDirectory)
  2031. if err != nil {
  2032. NoticeWarning(
  2033. "Migration: failed to read OSL download directory with error %s",
  2034. common.RedactFilePathsError(err, config.MigrateObfuscatedServerListDownloadDirectory))
  2035. } else {
  2036. for _, file := range files {
  2037. if oslFileRegex.MatchString(file.Name()) {
  2038. fileMigration := FileMigration{
  2039. Name: "osl",
  2040. OldPath: filepath.Join(config.MigrateObfuscatedServerListDownloadDirectory, file.Name()),
  2041. NewPath: filepath.Join(config.GetObfuscatedServerListDownloadDirectory(), file.Name()),
  2042. }
  2043. migrations = append(migrations, fileMigration)
  2044. }
  2045. }
  2046. }
  2047. }
  2048. if config.MigrateUpgradeDownloadFilename != "" {
  2049. // Migrate downloaded upgrade files
  2050. oldUpgradeDownloadFilename := filepath.Base(config.MigrateUpgradeDownloadFilename)
  2051. // Create regex for:
  2052. // <old_upgrade_download_filename>
  2053. // <old_upgrade_download_filename>.<client_version_number>
  2054. // <old_upgrade_download_filename>.<client_version_number>.part
  2055. // <old_upgrade_download_filename>.<client_version_number>.part.etag
  2056. upgradeDownloadFileRegex, err := regexp.Compile(`^` + oldUpgradeDownloadFilename + `(\.\d+(\.part(\.etag)?)?)?$`)
  2057. if err != nil {
  2058. return nil, errors.TraceMsg(err, "failed to compile regex for upgrade files")
  2059. }
  2060. upgradeDownloadDir := filepath.Dir(config.MigrateUpgradeDownloadFilename)
  2061. files, err := ioutil.ReadDir(upgradeDownloadDir)
  2062. if err != nil {
  2063. NoticeWarning(
  2064. "Migration: failed to read upgrade download directory with error %s",
  2065. common.RedactFilePathsError(err, upgradeDownloadDir))
  2066. } else {
  2067. for _, file := range files {
  2068. if upgradeDownloadFileRegex.MatchString(file.Name()) {
  2069. oldFileSuffix := strings.TrimPrefix(file.Name(), oldUpgradeDownloadFilename)
  2070. fileMigration := FileMigration{
  2071. Name: "upgrade",
  2072. OldPath: filepath.Join(upgradeDownloadDir, file.Name()),
  2073. NewPath: config.GetUpgradeDownloadFilename() + oldFileSuffix,
  2074. }
  2075. migrations = append(migrations, fileMigration)
  2076. }
  2077. }
  2078. }
  2079. }
  2080. return migrations, nil
  2081. }