net.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. // for HTTPSServer.ServeTLS:
  20. /*
  21. Copyright (c) 2012 The Go Authors. All rights reserved.
  22. Redistribution and use in source and binary forms, with or without
  23. modification, are permitted provided that the following conditions are
  24. met:
  25. * Redistributions of source code must retain the above copyright
  26. notice, this list of conditions and the following disclaimer.
  27. * Redistributions in binary form must reproduce the above
  28. copyright notice, this list of conditions and the following disclaimer
  29. in the documentation and/or other materials provided with the
  30. distribution.
  31. * Neither the name of Google Inc. nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package psiphon
  47. import (
  48. "crypto/tls"
  49. "crypto/x509"
  50. "errors"
  51. "fmt"
  52. "io"
  53. "io/ioutil"
  54. "net"
  55. "net/http"
  56. "net/url"
  57. "os"
  58. "reflect"
  59. "sync"
  60. "time"
  61. "github.com/Psiphon-Inc/dns"
  62. )
  63. const DNS_PORT = 53
  64. // DialConfig contains parameters to determine the behavior
  65. // of a Psiphon dialer (TCPDial, MeekDial, etc.)
  66. type DialConfig struct {
  67. // UpstreamProxyUrl specifies a proxy to connect through.
  68. // E.g., "http://proxyhost:8080"
  69. // "socks5://user:password@proxyhost:1080"
  70. // "socks4a://proxyhost:1080"
  71. // "http://NTDOMAIN\NTUser:password@proxyhost:3375"
  72. //
  73. // Certain tunnel protocols require HTTP CONNECT support
  74. // when a HTTP proxy is specified. If CONNECT is not
  75. // supported, those protocols will not connect.
  76. UpstreamProxyUrl string
  77. // UpstreamProxyCustomHeader is a set of additional arbitrary HTTP headers that are
  78. // added to all HTTP requests made through the upstream proxy specified by UpstreamProxyUrl
  79. // in case of HTTP proxy
  80. UpstreamProxyCustomHeaders http.Header
  81. ConnectTimeout time.Duration
  82. // PendingConns is used to track and interrupt dials in progress.
  83. // Dials may be interrupted using PendingConns.CloseAll(). Once instantiated,
  84. // a conn is added to pendingConns before the network connect begins and
  85. // removed from pendingConns once the connect succeeds or fails.
  86. // May be nil.
  87. PendingConns *Conns
  88. // BindToDevice parameters are used to exclude connections and
  89. // associated DNS requests from VPN routing.
  90. // When DeviceBinder is set, any underlying socket is
  91. // submitted to the device binding servicebefore connecting.
  92. // The service should bind the socket to a device so that it doesn't route
  93. // through a VPN interface. This service is also used to bind UDP sockets used
  94. // for DNS requests, in which case DnsServerGetter is used to get the
  95. // current active untunneled network DNS server.
  96. DeviceBinder DeviceBinder
  97. DnsServerGetter DnsServerGetter
  98. // UseIndistinguishableTLS specifies whether to try to use an
  99. // alternative stack for TLS. From a circumvention perspective,
  100. // Go's TLS has a distinct fingerprint that may be used for blocking.
  101. // Only applies to TLS connections.
  102. UseIndistinguishableTLS bool
  103. // TrustedCACertificatesFilename specifies a file containing trusted
  104. // CA certs. The file contents should be compatible with OpenSSL's
  105. // SSL_CTX_load_verify_locations.
  106. // Only applies to UseIndistinguishableTLS connections.
  107. TrustedCACertificatesFilename string
  108. // DeviceRegion is the reported region the host device is running in.
  109. // When set, this value may be used, pre-connection, to select performance
  110. // or circumvention optimization strategies for the given region.
  111. DeviceRegion string
  112. // ResolvedIPCallback, when set, is called with the IP address that was
  113. // dialed. This is either the specified IP address in the dial address,
  114. // or the resolved IP address in the case where the dial address is a
  115. // domain name.
  116. // The callback may be invoked by a concurrent goroutine.
  117. ResolvedIPCallback func(string)
  118. }
  119. // NetworkConnectivityChecker defines the interface to the external
  120. // HasNetworkConnectivity provider
  121. type NetworkConnectivityChecker interface {
  122. // TODO: change to bool return value once gobind supports that type
  123. HasNetworkConnectivity() int
  124. }
  125. // DeviceBinder defines the interface to the external BindToDevice provider
  126. type DeviceBinder interface {
  127. BindToDevice(fileDescriptor int) error
  128. }
  129. // DnsServerGetter defines the interface to the external GetDnsServer provider
  130. type DnsServerGetter interface {
  131. GetPrimaryDnsServer() string
  132. GetSecondaryDnsServer() string
  133. }
  134. // HostNameTransformer defines the interface for pluggable hostname
  135. // transformation circumvention strategies.
  136. type HostNameTransformer interface {
  137. TransformHostName(hostname string) (string, bool)
  138. }
  139. // IdentityHostNameTransformer is the default HostNameTransformer, which
  140. // returns the hostname unchanged.
  141. type IdentityHostNameTransformer struct{}
  142. func (IdentityHostNameTransformer) TransformHostName(hostname string) (string, bool) {
  143. return hostname, false
  144. }
  145. // TimeoutError implements the error interface
  146. type TimeoutError struct{}
  147. func (TimeoutError) Error() string { return "timed out" }
  148. func (TimeoutError) Timeout() bool { return true }
  149. func (TimeoutError) Temporary() bool { return true }
  150. // Dialer is a custom dialer compatible with http.Transport.Dial.
  151. type Dialer func(string, string) (net.Conn, error)
  152. // Conns is a synchronized list of Conns that is used to coordinate
  153. // interrupting a set of goroutines establishing connections, or
  154. // close a set of open connections, etc.
  155. // Once the list is closed, no more items may be added to the
  156. // list (unless it is reset).
  157. type Conns struct {
  158. mutex sync.Mutex
  159. isClosed bool
  160. conns map[net.Conn]bool
  161. }
  162. func (conns *Conns) Reset() {
  163. conns.mutex.Lock()
  164. defer conns.mutex.Unlock()
  165. conns.isClosed = false
  166. conns.conns = make(map[net.Conn]bool)
  167. }
  168. func (conns *Conns) Add(conn net.Conn) bool {
  169. conns.mutex.Lock()
  170. defer conns.mutex.Unlock()
  171. if conns.isClosed {
  172. return false
  173. }
  174. if conns.conns == nil {
  175. conns.conns = make(map[net.Conn]bool)
  176. }
  177. conns.conns[conn] = true
  178. return true
  179. }
  180. func (conns *Conns) Remove(conn net.Conn) {
  181. conns.mutex.Lock()
  182. defer conns.mutex.Unlock()
  183. delete(conns.conns, conn)
  184. }
  185. func (conns *Conns) CloseAll() {
  186. conns.mutex.Lock()
  187. defer conns.mutex.Unlock()
  188. conns.isClosed = true
  189. for conn, _ := range conns.conns {
  190. conn.Close()
  191. }
  192. conns.conns = make(map[net.Conn]bool)
  193. }
  194. // LocalProxyRelay sends to remoteConn bytes received from localConn,
  195. // and sends to localConn bytes received from remoteConn.
  196. func LocalProxyRelay(proxyType string, localConn, remoteConn net.Conn) {
  197. copyWaitGroup := new(sync.WaitGroup)
  198. copyWaitGroup.Add(1)
  199. go func() {
  200. defer copyWaitGroup.Done()
  201. _, err := io.Copy(localConn, remoteConn)
  202. if err != nil {
  203. err = fmt.Errorf("Relay failed: %s", ContextError(err))
  204. NoticeLocalProxyError(proxyType, err)
  205. }
  206. }()
  207. _, err := io.Copy(remoteConn, localConn)
  208. if err != nil {
  209. err = fmt.Errorf("Relay failed: %s", ContextError(err))
  210. NoticeLocalProxyError(proxyType, err)
  211. }
  212. copyWaitGroup.Wait()
  213. }
  214. // WaitForNetworkConnectivity uses a NetworkConnectivityChecker to
  215. // periodically check for network connectivity. It returns true if
  216. // no NetworkConnectivityChecker is provided (waiting is disabled)
  217. // or when NetworkConnectivityChecker.HasNetworkConnectivity()
  218. // indicates connectivity. It waits and polls the checker once a second.
  219. // If any stop is broadcast, false is returned immediately.
  220. func WaitForNetworkConnectivity(
  221. connectivityChecker NetworkConnectivityChecker, stopBroadcasts ...<-chan struct{}) bool {
  222. if connectivityChecker == nil || 1 == connectivityChecker.HasNetworkConnectivity() {
  223. return true
  224. }
  225. NoticeInfo("waiting for network connectivity")
  226. ticker := time.NewTicker(1 * time.Second)
  227. for {
  228. if 1 == connectivityChecker.HasNetworkConnectivity() {
  229. return true
  230. }
  231. selectCases := make([]reflect.SelectCase, 1+len(stopBroadcasts))
  232. selectCases[0] = reflect.SelectCase{
  233. Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ticker.C)}
  234. for i, stopBroadcast := range stopBroadcasts {
  235. selectCases[i+1] = reflect.SelectCase{
  236. Dir: reflect.SelectRecv, Chan: reflect.ValueOf(stopBroadcast)}
  237. }
  238. chosen, _, ok := reflect.Select(selectCases)
  239. if chosen == 0 && ok {
  240. // Ticker case, so check again
  241. } else {
  242. // Stop case
  243. return false
  244. }
  245. }
  246. }
  247. // ResolveIP uses a custom dns stack to make a DNS query over the
  248. // given TCP or UDP conn. This is used, e.g., when we need to ensure
  249. // that a DNS connection bypasses a VPN interface (BindToDevice) or
  250. // when we need to ensure that a DNS connection is tunneled.
  251. // Caller must set timeouts or interruptibility as required for conn.
  252. func ResolveIP(host string, conn net.Conn) (addrs []net.IP, ttls []time.Duration, err error) {
  253. // Send the DNS query
  254. dnsConn := &dns.Conn{Conn: conn}
  255. defer dnsConn.Close()
  256. query := new(dns.Msg)
  257. query.SetQuestion(dns.Fqdn(host), dns.TypeA)
  258. query.RecursionDesired = true
  259. dnsConn.WriteMsg(query)
  260. // Process the response
  261. response, err := dnsConn.ReadMsg()
  262. if err != nil {
  263. return nil, nil, ContextError(err)
  264. }
  265. addrs = make([]net.IP, 0)
  266. ttls = make([]time.Duration, 0)
  267. for _, answer := range response.Answer {
  268. if a, ok := answer.(*dns.A); ok {
  269. addrs = append(addrs, a.A)
  270. ttl := time.Duration(a.Hdr.Ttl) * time.Second
  271. ttls = append(ttls, ttl)
  272. }
  273. }
  274. return addrs, ttls, nil
  275. }
  276. // MakeUntunneledHttpsClient returns a net/http.Client which is
  277. // configured to use custom dialing features -- including BindToDevice,
  278. // UseIndistinguishableTLS, etc. -- for a specific HTTPS request URL.
  279. // If verifyLegacyCertificate is not nil, it's used for certificate
  280. // verification.
  281. // Because UseIndistinguishableTLS requires a hack to work with
  282. // net/http, MakeUntunneledHttpClient may return a modified request URL
  283. // to be used. Callers should always use this return value to make
  284. // requests, not the input value.
  285. func MakeUntunneledHttpsClient(
  286. dialConfig *DialConfig,
  287. verifyLegacyCertificate *x509.Certificate,
  288. requestUrl string,
  289. requestTimeout time.Duration) (*http.Client, string, error) {
  290. // Change the scheme to "http"; otherwise http.Transport will try to do
  291. // another TLS handshake inside the explicit TLS session. Also need to
  292. // force an explicit port, as the default for "http", 80, won't talk TLS.
  293. urlComponents, err := url.Parse(requestUrl)
  294. if err != nil {
  295. return nil, "", ContextError(err)
  296. }
  297. urlComponents.Scheme = "http"
  298. host, port, err := net.SplitHostPort(urlComponents.Host)
  299. if err != nil {
  300. // Assume there's no port
  301. host = urlComponents.Host
  302. port = ""
  303. }
  304. if port == "" {
  305. port = "443"
  306. }
  307. urlComponents.Host = net.JoinHostPort(host, port)
  308. // Note: IndistinguishableTLS mode doesn't support VerifyLegacyCertificate
  309. useIndistinguishableTLS := dialConfig.UseIndistinguishableTLS && verifyLegacyCertificate == nil
  310. dialer := NewCustomTLSDialer(
  311. // Note: when verifyLegacyCertificate is not nil, some
  312. // of the other CustomTLSConfig is overridden.
  313. &CustomTLSConfig{
  314. Dial: NewTCPDialer(dialConfig),
  315. VerifyLegacyCertificate: verifyLegacyCertificate,
  316. SNIServerName: host,
  317. SkipVerify: false,
  318. UseIndistinguishableTLS: useIndistinguishableTLS,
  319. TrustedCACertificatesFilename: dialConfig.TrustedCACertificatesFilename,
  320. })
  321. transport := &http.Transport{
  322. Dial: dialer,
  323. }
  324. httpClient := &http.Client{
  325. Timeout: requestTimeout,
  326. Transport: transport,
  327. }
  328. return httpClient, urlComponents.String(), nil
  329. }
  330. // MakeTunneledHttpClient returns a net/http.Client which is
  331. // configured to use custom dialing features including tunneled
  332. // dialing and, optionally, UseTrustedCACertificatesForStockTLS.
  333. // Unlike MakeUntunneledHttpsClient and makePsiphonHttpsClient,
  334. // This http.Client uses stock TLS and no scheme transformation
  335. // hack is required.
  336. func MakeTunneledHttpClient(
  337. config *Config,
  338. tunnel *Tunnel,
  339. requestTimeout time.Duration) (*http.Client, error) {
  340. tunneledDialer := func(_, addr string) (conn net.Conn, err error) {
  341. return tunnel.sshClient.Dial("tcp", addr)
  342. }
  343. transport := &http.Transport{
  344. Dial: tunneledDialer,
  345. }
  346. if config.UseTrustedCACertificatesForStockTLS {
  347. if config.TrustedCACertificatesFilename == "" {
  348. return nil, ContextError(errors.New(
  349. "UseTrustedCACertificatesForStockTLS requires TrustedCACertificatesFilename"))
  350. }
  351. rootCAs := x509.NewCertPool()
  352. certData, err := ioutil.ReadFile(config.TrustedCACertificatesFilename)
  353. if err != nil {
  354. return nil, ContextError(err)
  355. }
  356. rootCAs.AppendCertsFromPEM(certData)
  357. transport.TLSClientConfig = &tls.Config{RootCAs: rootCAs}
  358. }
  359. return &http.Client{
  360. Transport: transport,
  361. Timeout: requestTimeout,
  362. }, nil
  363. }
  364. // MakeDownloadHttpClient is a resusable helper that sets up a
  365. // http.Client for use either untunneled or through a tunnel.
  366. // See MakeUntunneledHttpsClient for a note about request URL
  367. // rewritting.
  368. func MakeDownloadHttpClient(
  369. config *Config,
  370. tunnel *Tunnel,
  371. untunneledDialConfig *DialConfig,
  372. requestUrl string,
  373. requestTimeout time.Duration) (*http.Client, string, error) {
  374. var httpClient *http.Client
  375. var err error
  376. if tunnel != nil {
  377. httpClient, err = MakeTunneledHttpClient(config, tunnel, requestTimeout)
  378. if err != nil {
  379. return nil, "", ContextError(err)
  380. }
  381. } else {
  382. httpClient, requestUrl, err = MakeUntunneledHttpsClient(
  383. untunneledDialConfig, nil, requestUrl, requestTimeout)
  384. if err != nil {
  385. return nil, "", ContextError(err)
  386. }
  387. }
  388. return httpClient, requestUrl, nil
  389. }
  390. // ResumeDownload is a resuable helper that downloads requestUrl via the
  391. // httpClient, storing the result in downloadFilename when the download is
  392. // complete. Intermediate, partial downloads state is stored in
  393. // downloadFilename.part and downloadFilename.part.etag.
  394. // Any existing downloadFilename file will be overwritten.
  395. //
  396. // In the case where the remote object has change while a partial download
  397. // is to be resumed, the partial state is reset and resumeDownload fails.
  398. // The caller must restart the download.
  399. //
  400. // When ifNoneMatchETag is specified, no download is made if the remote
  401. // object has the same ETag. ifNoneMatchETag has an effect only when no
  402. // partial download is in progress.
  403. //
  404. func ResumeDownload(
  405. httpClient *http.Client,
  406. requestUrl string,
  407. downloadFilename string,
  408. ifNoneMatchETag string) (int64, string, error) {
  409. partialFilename := fmt.Sprintf("%s.part", downloadFilename)
  410. partialETagFilename := fmt.Sprintf("%s.part.etag", downloadFilename)
  411. file, err := os.OpenFile(partialFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  412. if err != nil {
  413. return 0, "", ContextError(err)
  414. }
  415. defer file.Close()
  416. fileInfo, err := file.Stat()
  417. if err != nil {
  418. return 0, "", ContextError(err)
  419. }
  420. // A partial download should have an ETag which is to be sent with the
  421. // Range request to ensure that the source object is the same as the
  422. // one that is partially downloaded.
  423. var partialETag []byte
  424. if fileInfo.Size() > 0 {
  425. partialETag, err = ioutil.ReadFile(partialETagFilename)
  426. // When the ETag can't be loaded, delete the partial download. To keep the
  427. // code simple, there is no immediate, inline retry here, on the assumption
  428. // that the controller's upgradeDownloader will shortly call DownloadUpgrade
  429. // again.
  430. if err != nil {
  431. os.Remove(partialFilename)
  432. os.Remove(partialETagFilename)
  433. return 0, "", ContextError(
  434. fmt.Errorf("failed to load partial download ETag: %s", err))
  435. }
  436. }
  437. request, err := http.NewRequest("GET", requestUrl, nil)
  438. if err != nil {
  439. return 0, "", ContextError(err)
  440. }
  441. request.Header.Add("Range", fmt.Sprintf("bytes=%d-", fileInfo.Size()))
  442. if partialETag != nil {
  443. // Note: not using If-Range, since not all host servers support it.
  444. // Using If-Match means we need to check for status code 412 and reset
  445. // when the ETag has changed since the last partial download.
  446. request.Header.Add("If-Match", string(partialETag))
  447. } else if ifNoneMatchETag != "" {
  448. // Can't specify both If-Match and If-None-Match. Behavior is undefined.
  449. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
  450. // So for downloaders that store an ETag and wish to use that to prevent
  451. // redundant downloads, that ETag is sent as If-None-Match in the case
  452. // where a partial download is not in progress. When a partial download
  453. // is in progress, the partial ETag is sent as If-Match: either that's
  454. // a version that was never fully received, or it's no longer current in
  455. // which case the response will be StatusPreconditionFailed, the partial
  456. // download will be discarded, and then the next retry will use
  457. // If-None-Match.
  458. // Note: in this case, fileInfo.Size() == 0
  459. request.Header.Add("If-None-Match", ifNoneMatchETag)
  460. }
  461. response, err := httpClient.Do(request)
  462. // The resumeable download may ask for bytes past the resource range
  463. // since it doesn't store the "completed download" state. In this case,
  464. // the HTTP server returns 416. Otherwise, we expect 206. We may also
  465. // receive 412 on ETag mismatch.
  466. if err == nil &&
  467. (response.StatusCode != http.StatusPartialContent &&
  468. response.StatusCode != http.StatusRequestedRangeNotSatisfiable &&
  469. response.StatusCode != http.StatusPreconditionFailed &&
  470. response.StatusCode != http.StatusNotModified) {
  471. response.Body.Close()
  472. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  473. }
  474. if err != nil {
  475. return 0, "", ContextError(err)
  476. }
  477. defer response.Body.Close()
  478. responseETag := response.Header.Get("ETag")
  479. if response.StatusCode == http.StatusPreconditionFailed {
  480. // When the ETag no longer matches, delete the partial download. As above,
  481. // simply failing and relying on the caller's retry schedule.
  482. os.Remove(partialFilename)
  483. os.Remove(partialETagFilename)
  484. return 0, "", ContextError(errors.New("partial download ETag mismatch"))
  485. } else if response.StatusCode == http.StatusNotModified {
  486. // This status code is possible in the "If-None-Match" case. Don't leave
  487. // any partial download in progress. Caller should check that responseETag
  488. // matches ifNoneMatchETag.
  489. os.Remove(partialFilename)
  490. os.Remove(partialETagFilename)
  491. return 0, responseETag, nil
  492. }
  493. // Not making failure to write ETag file fatal, in case the entire download
  494. // succeeds in this one request.
  495. ioutil.WriteFile(partialETagFilename, []byte(responseETag), 0600)
  496. // A partial download occurs when this copy is interrupted. The io.Copy
  497. // will fail, leaving a partial download in place (.part and .part.etag).
  498. n, err := io.Copy(NewSyncFileWriter(file), response.Body)
  499. // From this point, n bytes are indicated as downloaded, even if there is
  500. // an error; the caller may use this to report partial download progress.
  501. if err != nil {
  502. return n, "", ContextError(err)
  503. }
  504. // Ensure the file is flushed to disk. The deferred close
  505. // will be a noop when this succeeds.
  506. err = file.Close()
  507. if err != nil {
  508. return n, "", ContextError(err)
  509. }
  510. // Remove if exists, to enable rename
  511. os.Remove(downloadFilename)
  512. err = os.Rename(partialFilename, downloadFilename)
  513. if err != nil {
  514. return n, "", ContextError(err)
  515. }
  516. os.Remove(partialETagFilename)
  517. return n, responseETag, nil
  518. }
  519. // IPAddressFromAddr is a helper which extracts an IP address
  520. // from a net.Addr or returns "" if there is no IP address.
  521. func IPAddressFromAddr(addr net.Addr) string {
  522. ipAddress := ""
  523. if addr != nil {
  524. host, _, err := net.SplitHostPort(addr.String())
  525. if err == nil {
  526. ipAddress = host
  527. }
  528. }
  529. return ipAddress
  530. }
  531. // HTTPSServer is a wrapper around http.Server which adds the
  532. // ServeTLS function.
  533. type HTTPSServer struct {
  534. http.Server
  535. }
  536. // ServeTLS is a offers the equivalent interface as http.Serve.
  537. // The http package has both ListenAndServe and ListenAndServeTLS higher-
  538. // level interfaces, but only Serve (not TLS) offers a lower-level interface that
  539. // allows the caller to keep a refererence to the Listener, allowing for external
  540. // shutdown. ListenAndServeTLS also requires the TLS cert and key to be in files
  541. // and we avoid that here.
  542. // tcpKeepAliveListener is used in http.ListenAndServeTLS but not exported,
  543. // so we use a copy from https://golang.org/src/net/http/server.go.
  544. func (server *HTTPSServer) ServeTLS(listener net.Listener) error {
  545. tlsListener := tls.NewListener(tcpKeepAliveListener{listener.(*net.TCPListener)}, server.TLSConfig)
  546. return server.Serve(tlsListener)
  547. }
  548. type tcpKeepAliveListener struct {
  549. *net.TCPListener
  550. }
  551. func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
  552. tc, err := ln.AcceptTCP()
  553. if err != nil {
  554. return
  555. }
  556. tc.SetKeepAlive(true)
  557. tc.SetKeepAlivePeriod(3 * time.Minute)
  558. return tc, nil
  559. }