config.go 86 KB

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