remoteServerList.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. "context"
  22. "encoding/hex"
  23. "fmt"
  24. "net/url"
  25. "os"
  26. "sync/atomic"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  33. )
  34. type RemoteServerListFetcher func(
  35. ctx context.Context, config *Config, attempt int, tunnel *Tunnel, untunneledDialConfig *DialConfig) error
  36. // FetchCommonRemoteServerList downloads the common remote server list from
  37. // config.RemoteServerListURLs. It validates its digital signature using the
  38. // public key config.RemoteServerListSignaturePublicKey and parses the
  39. // data field into ServerEntry records.
  40. // config.GetRemoteServerListDownloadFilename() is the location to store the
  41. // download. As the download is resumed after failure, this filename must
  42. // be unique and persistent.
  43. func FetchCommonRemoteServerList(
  44. ctx context.Context,
  45. config *Config,
  46. attempt int,
  47. tunnel *Tunnel,
  48. untunneledDialConfig *DialConfig) error {
  49. NoticeInfo("fetching common remote server list")
  50. p := config.GetParameters().Get()
  51. publicKey := p.String(parameters.RemoteServerListSignaturePublicKey)
  52. urls := p.TransferURLs(parameters.RemoteServerListURLs)
  53. downloadTimeout := p.Duration(parameters.FetchRemoteServerListTimeout)
  54. p.Close()
  55. downloadURL := urls.Select(attempt)
  56. canonicalURL := urls.CanonicalURL()
  57. newETag, downloadStatRecorder, err := downloadRemoteServerListFile(
  58. ctx,
  59. config,
  60. tunnel,
  61. untunneledDialConfig,
  62. downloadTimeout,
  63. downloadURL.URL,
  64. canonicalURL,
  65. downloadURL.SkipVerify || config.TransferURLsAlwaysSkipVerify,
  66. "",
  67. config.GetRemoteServerListDownloadFilename())
  68. if err != nil {
  69. return errors.Tracef("failed to download common remote server list: %s", errors.Trace(err))
  70. }
  71. authenticatedDownload := false
  72. if downloadStatRecorder != nil {
  73. defer func() { downloadStatRecorder(authenticatedDownload) }()
  74. }
  75. // When the resource is unchanged, skip.
  76. if newETag == "" {
  77. return nil
  78. }
  79. file, err := os.Open(config.GetRemoteServerListDownloadFilename())
  80. if err != nil {
  81. return errors.Tracef("failed to open common remote server list: %s", errors.Trace(err))
  82. }
  83. defer file.Close()
  84. serverListPayloadReader, err := common.NewAuthenticatedDataPackageReader(
  85. file, publicKey)
  86. if err != nil {
  87. return errors.Tracef("failed to read remote server list: %s", errors.Trace(err))
  88. }
  89. // NewAuthenticatedDataPackageReader authenticates the file before returning.
  90. authenticatedDownload = true
  91. err = StreamingStoreServerEntries(
  92. ctx,
  93. config,
  94. protocol.NewStreamingServerEntryDecoder(
  95. serverListPayloadReader,
  96. common.GetCurrentTimestamp(),
  97. protocol.SERVER_ENTRY_SOURCE_REMOTE),
  98. true)
  99. if err != nil {
  100. return errors.Tracef("failed to store common remote server list: %s", errors.Trace(err))
  101. }
  102. // Now that the server entries are successfully imported, store the response
  103. // ETag so we won't re-download this same data again.
  104. err = SetUrlETag(canonicalURL, newETag)
  105. if err != nil {
  106. NoticeWarning("failed to set ETag for common remote server list: %s", errors.Trace(err))
  107. // This fetch is still reported as a success, even if we can't store the etag
  108. }
  109. return nil
  110. }
  111. // FetchObfuscatedServerLists downloads the obfuscated remote server lists
  112. // from config.ObfuscatedServerListRootURLs.
  113. // It first downloads the OSL registry, and then downloads each seeded OSL
  114. // advertised in the registry. All downloads are resumable, ETags are used
  115. // to skip both an unchanged registry or unchanged OSL files, and when an
  116. // individual download fails, the fetch proceeds if it can.
  117. // Authenticated package digital signatures are validated using the
  118. // public key config.RemoteServerListSignaturePublicKey.
  119. // config.GetObfuscatedServerListDownloadDirectory() is the location to store
  120. // the downloaded files. As downloads are resumed after failure, this directory
  121. // must be unique and persistent.
  122. func FetchObfuscatedServerLists(
  123. ctx context.Context,
  124. config *Config,
  125. attempt int,
  126. tunnel *Tunnel,
  127. untunneledDialConfig *DialConfig) error {
  128. NoticeInfo("fetching obfuscated remote server lists")
  129. p := config.GetParameters().Get()
  130. publicKey := p.String(parameters.RemoteServerListSignaturePublicKey)
  131. urls := p.TransferURLs(parameters.ObfuscatedServerListRootURLs)
  132. downloadTimeout := p.Duration(parameters.FetchRemoteServerListTimeout)
  133. p.Close()
  134. rootURL := urls.Select(attempt)
  135. canonicalRootURL := urls.CanonicalURL()
  136. downloadURL := osl.GetOSLRegistryURL(rootURL.URL)
  137. canonicalURL := osl.GetOSLRegistryURL(canonicalRootURL)
  138. downloadFilename := osl.GetOSLRegistryFilename(config.GetObfuscatedServerListDownloadDirectory())
  139. cachedFilename := downloadFilename + ".cached"
  140. // If the cached registry is not present, we need to download or resume downloading
  141. // the registry, so clear the ETag to ensure that always happens.
  142. _, err := os.Stat(cachedFilename)
  143. if os.IsNotExist(err) {
  144. SetUrlETag(canonicalURL, "")
  145. }
  146. // failed is set if any operation fails and should trigger a retry. When the OSL registry
  147. // fails to download, any cached registry is used instead; when any single OSL fails
  148. // to download, the overall operation proceeds. So this flag records whether to report
  149. // failure at the end when downloading has proceeded after a failure.
  150. // TODO: should disk-full conditions not trigger retries?
  151. var failed bool
  152. // updateCache is set when modifed registry content is downloaded. Both the cached
  153. // file and the persisted ETag will be updated in this case. The update is deferred
  154. // until after the registry has been authenticated.
  155. updateCache := false
  156. registryFilename := cachedFilename
  157. newETag, downloadStatRecorder, err := downloadRemoteServerListFile(
  158. ctx,
  159. config,
  160. tunnel,
  161. untunneledDialConfig,
  162. downloadTimeout,
  163. downloadURL,
  164. canonicalURL,
  165. rootURL.SkipVerify,
  166. "",
  167. downloadFilename)
  168. if err != nil {
  169. failed = true
  170. NoticeWarning("failed to download obfuscated server list registry: %s", errors.Trace(err))
  171. // Proceed with any existing cached OSL registry.
  172. }
  173. authenticatedDownload := false
  174. if downloadStatRecorder != nil {
  175. defer func() { downloadStatRecorder(authenticatedDownload) }()
  176. }
  177. if newETag != "" {
  178. updateCache = true
  179. registryFilename = downloadFilename
  180. }
  181. // Prevent excessive notice noise in cases such as a general database
  182. // failure, as GetSLOK may be called thousands of times per fetch.
  183. emittedGetSLOKAlert := int32(0)
  184. lookupSLOKs := func(slokID []byte) []byte {
  185. // Lookup SLOKs in local datastore
  186. key, err := GetSLOK(slokID)
  187. if err != nil && atomic.CompareAndSwapInt32(&emittedGetSLOKAlert, 0, 1) {
  188. NoticeWarning("GetSLOK failed: %s", err)
  189. }
  190. return key
  191. }
  192. registryFile, err := os.Open(registryFilename)
  193. if err != nil {
  194. return errors.Tracef("failed to read obfuscated server list registry: %s", errors.Trace(err))
  195. }
  196. defer registryFile.Close()
  197. registryStreamer, err := osl.NewRegistryStreamer(
  198. registryFile,
  199. publicKey,
  200. lookupSLOKs)
  201. if err != nil {
  202. // TODO: delete file? redownload if corrupt?
  203. return errors.Tracef("failed to read obfuscated server list registry: %s", errors.Trace(err))
  204. }
  205. authenticatedDownload = true
  206. // NewRegistryStreamer authenticates the downloaded registry, so now it would be
  207. // ok to update the cache. However, we defer that until after processing so we
  208. // can close the file first before copying it, avoiding related complications on
  209. // platforms such as Windows.
  210. // Note: we proceed to check individual OSLs even if the directory is unchanged,
  211. // as the set of local SLOKs may have changed.
  212. for {
  213. oslFileSpec, err := registryStreamer.Next()
  214. if err != nil {
  215. failed = true
  216. NoticeWarning("failed to stream obfuscated server list registry: %s", errors.Trace(err))
  217. break
  218. }
  219. if oslFileSpec == nil {
  220. break
  221. }
  222. if !downloadOSLFileSpec(
  223. ctx,
  224. config,
  225. tunnel,
  226. untunneledDialConfig,
  227. downloadTimeout,
  228. rootURL.URL,
  229. canonicalRootURL,
  230. rootURL.SkipVerify,
  231. publicKey,
  232. lookupSLOKs,
  233. oslFileSpec) {
  234. // downloadOSLFileSpec emits notices with failure information. In the case
  235. // of a failure, set the retry flag but continue to process other OSL file
  236. // specs.
  237. failed = true
  238. }
  239. // Run a garbage collection to reclaim memory from the downloadOSLFileSpec
  240. // operation before processing the next file.
  241. DoGarbageCollection()
  242. }
  243. // Now that a new registry is downloaded, validated, and parsed, store
  244. // the response ETag so we won't re-download this same data again. First
  245. // close the file to avoid complications on platforms such as Windows.
  246. if updateCache {
  247. registryFile.Close()
  248. err := os.Rename(downloadFilename, cachedFilename)
  249. if err != nil {
  250. NoticeWarning("failed to set cached obfuscated server list registry: %s", errors.Trace(err))
  251. // This fetch is still reported as a success, even if we can't update the cache
  252. }
  253. err = SetUrlETag(canonicalURL, newETag)
  254. if err != nil {
  255. NoticeWarning("failed to set ETag for obfuscated server list registry: %s", errors.Trace(err))
  256. // This fetch is still reported as a success, even if we can't store the ETag
  257. }
  258. }
  259. if failed {
  260. return errors.TraceNew("one or more operations failed")
  261. }
  262. return nil
  263. }
  264. // downloadOSLFileSpec downloads, authenticates, and imports the OSL specified
  265. // by oslFileSpec. The return value indicates whether the operation succeeded.
  266. // Failure information is emitted in notices.
  267. func downloadOSLFileSpec(
  268. ctx context.Context,
  269. config *Config,
  270. tunnel *Tunnel,
  271. untunneledDialConfig *DialConfig,
  272. downloadTimeout time.Duration,
  273. rootURL string,
  274. canonicalRootURL string,
  275. skipVerify bool,
  276. publicKey string,
  277. lookupSLOKs func(slokID []byte) []byte,
  278. oslFileSpec *osl.OSLFileSpec) bool {
  279. downloadFilename := osl.GetOSLFilename(
  280. config.GetObfuscatedServerListDownloadDirectory(), oslFileSpec.ID)
  281. downloadURL := osl.GetOSLFileURL(rootURL, oslFileSpec.ID)
  282. canonicalURL := osl.GetOSLFileURL(canonicalRootURL, oslFileSpec.ID)
  283. hexID := hex.EncodeToString(oslFileSpec.ID)
  284. // Note: the MD5 checksum step assumes the remote server list host's ETag uses MD5
  285. // with a hex encoding. If this is not the case, the sourceETag should be left blank.
  286. sourceETag := fmt.Sprintf("\"%s\"", hex.EncodeToString(oslFileSpec.MD5Sum))
  287. newETag, downloadStatRecorder, err := downloadRemoteServerListFile(
  288. ctx,
  289. config,
  290. tunnel,
  291. untunneledDialConfig,
  292. downloadTimeout,
  293. downloadURL,
  294. canonicalURL,
  295. skipVerify,
  296. sourceETag,
  297. downloadFilename)
  298. if err != nil {
  299. NoticeWarning("failed to download obfuscated server list file (%s): %s", hexID, errors.Trace(err))
  300. return false
  301. }
  302. authenticatedDownload := false
  303. if downloadStatRecorder != nil {
  304. defer func() { downloadStatRecorder(authenticatedDownload) }()
  305. }
  306. // When the resource is unchanged, skip.
  307. if newETag == "" {
  308. return true
  309. }
  310. file, err := os.Open(downloadFilename)
  311. if err != nil {
  312. NoticeWarning("failed to open obfuscated server list file (%s): %s", hexID, errors.Trace(err))
  313. return false
  314. }
  315. defer file.Close()
  316. serverListPayloadReader, err := osl.NewOSLReader(
  317. file,
  318. oslFileSpec,
  319. lookupSLOKs,
  320. publicKey)
  321. if err != nil {
  322. NoticeWarning("failed to read obfuscated server list file (%s): %s", hexID, errors.Trace(err))
  323. return false
  324. }
  325. // NewOSLReader authenticates the file before returning.
  326. authenticatedDownload = true
  327. err = StreamingStoreServerEntries(
  328. ctx,
  329. config,
  330. protocol.NewStreamingServerEntryDecoder(
  331. serverListPayloadReader,
  332. common.GetCurrentTimestamp(),
  333. protocol.SERVER_ENTRY_SOURCE_OBFUSCATED),
  334. true)
  335. if err != nil {
  336. NoticeWarning("failed to store obfuscated server list file (%s): %s", hexID, errors.Trace(err))
  337. return false
  338. }
  339. // Now that the server entries are successfully imported, store the response
  340. // ETag so we won't re-download this same data again.
  341. err = SetUrlETag(canonicalURL, newETag)
  342. if err != nil {
  343. NoticeWarning("failed to set ETag for obfuscated server list file (%s): %s", hexID, errors.Trace(err))
  344. // This fetch is still reported as a success, even if we can't store the ETag
  345. return true
  346. }
  347. return true
  348. }
  349. // downloadRemoteServerListFile downloads the source URL to the destination
  350. // file, performing a resumable download. When the download completes and the
  351. // file content has changed, the new resource ETag is returned. Otherwise,
  352. // blank is returned. The caller is responsible for calling SetUrlETag once
  353. // the file content has been validated.
  354. //
  355. // The downloadStatReporter return value is a function that will invoke
  356. // RecordRemoteServerListStat to record a remote server list download event.
  357. // The caller must call this function if the return value is not nil,
  358. // providing a boolean argument indicating whether the download was
  359. // successfully authenticated.
  360. func downloadRemoteServerListFile(
  361. ctx context.Context,
  362. config *Config,
  363. tunnel *Tunnel,
  364. untunneledDialConfig *DialConfig,
  365. downloadTimeout time.Duration,
  366. sourceURL string,
  367. canonicalURL string,
  368. skipVerify bool,
  369. sourceETag string,
  370. destinationFilename string) (string, func(bool), error) {
  371. // All download URLs with the same canonicalURL
  372. // must have the same entity and ETag.
  373. lastETag, err := GetUrlETag(canonicalURL)
  374. if err != nil {
  375. return "", nil, errors.Trace(err)
  376. }
  377. // sourceETag, when specified, is prior knowledge of the
  378. // remote ETag that can be used to skip the request entirely.
  379. // This will be set in the case of OSL files, from the MD5Sum
  380. // values stored in the registry.
  381. if lastETag != "" && sourceETag == lastETag {
  382. // TODO: notice?
  383. return "", nil, nil
  384. }
  385. var cancelFunc context.CancelFunc
  386. ctx, cancelFunc = context.WithTimeout(ctx, downloadTimeout)
  387. defer cancelFunc()
  388. // MakeDownloadHttpClient will select either a tunneled
  389. // or untunneled configuration.
  390. httpClient, tunneled, err := MakeDownloadHTTPClient(
  391. ctx,
  392. config,
  393. tunnel,
  394. untunneledDialConfig,
  395. skipVerify)
  396. if err != nil {
  397. return "", nil, errors.Trace(err)
  398. }
  399. startTime := time.Now()
  400. bytes, responseETag, err := ResumeDownload(
  401. ctx,
  402. httpClient,
  403. sourceURL,
  404. MakePsiphonUserAgent(config),
  405. destinationFilename,
  406. lastETag)
  407. duration := time.Since(startTime)
  408. NoticeRemoteServerListResourceDownloadedBytes(sourceURL, bytes, duration)
  409. if err != nil {
  410. return "", nil, errors.Trace(err)
  411. }
  412. if responseETag == lastETag {
  413. return "", nil, nil
  414. }
  415. NoticeRemoteServerListResourceDownloaded(sourceURL)
  416. downloadStatRecorder := func(authenticated bool) {
  417. // Invoke DNS cache extension (if enabled in the resolver) now that
  418. // the download succeeded and the payload is authenticated. Only
  419. // extend when authenticated, as this demonstrates that any domain
  420. // name resolved to an endpoint that served a valid Psiphon remote
  421. // server list.
  422. //
  423. // TODO: when !skipVerify, invoke DNS cache extension earlier, in
  424. // ResumeDownload, after making the request but before downloading
  425. // the response body?
  426. resolver := config.GetResolver()
  427. url, err := url.Parse(sourceURL)
  428. if authenticated && resolver != nil && err == nil {
  429. resolver.VerifyCacheExtension(url.Hostname())
  430. }
  431. _ = RecordRemoteServerListStat(
  432. config, tunneled, sourceURL, responseETag, bytes, duration, authenticated)
  433. }
  434. return responseETag, downloadStatRecorder, nil
  435. }