config.go 81 KB

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