Debian Codegraph @

bus/connectivity/webchecker.go

raw
1 /*
2 Copyright 2013-2015 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // webchecker checks whether we're actually connected by doing an http
18 // GET to the Ubuntu connectivity check URL,
19 // http://start.ubuntu.com/connectivity-check.html
20 //
21 // We could make it be https to make extra doubly sure, but it's expensive
22 // overkill for the majority of cases.
23 package connectivity
24
25 import (
26 "bytes"
27 "crypto/md5"
28 "crypto/tls"
29 "fmt"
30 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
31 "io"
32 "net/http"
33 "time"
34 )
35
36 const ResponseLimit = 1024
37
38 // how much web would a webchecker check
39
40 type Webchecker interface {
41 // Webcheck checks whether retrieving the URL works, and if its
42 // contents match the target. If so, then it sends true; if anything
43 // fails, it sends false.
44 Webcheck(chan<- bool)
45 // Close idle connections.
46 Close()
47 }
48
49 type webchecker struct {
50 log logger.Logger
51 url string
52 targetMD5 string
53 targetMatch string
54 cli *http.Client
55 }
56
57 // Build a webchecker for the given URL, that should match the target MD5.
58 func NewWebchecker(url, targetMD5, targetMatch string, timeout time.Duration, log logger.Logger) Webchecker {
59 cli := &http.Client{
60 Timeout: timeout,
61 Transport: &http.Transport{
62 // ignore errors since the system clock might not be
63 // set correctly on startup
64 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
65 TLSHandshakeTimeout: timeout,
66 },
67 }
68 return &webchecker{log, url, targetMD5, targetMatch, cli}
69 }
70
71 // ensure webchecker implements Webchecker
72 var _ Webchecker = &webchecker{}
73
74 func (wb *webchecker) Webcheck(ch chan<- bool) {
75 response, err := wb.cli.Get(wb.url)
76 if err != nil {
77 wb.log.Errorf("while GETting %s: %v", wb.url, err)
78 ch <- false
79 return
80 }
81 defer response.Body.Close()
82
83 hash := md5.New()
84 var buf bytes.Buffer
85 var w io.Writer
86 if len(wb.targetMatch) > 0 {
87 w = &buf
88 } else {
89 w = hash
90 }
91 _, err = io.CopyN(w, response.Body, ResponseLimit)
92 if err != io.EOF {
93 if err == nil {
94 wb.log.Errorf("reading %s, but response body is larger than 1k.", wb.url)
95 } else {
96 wb.log.Errorf("reading %s, expecting EOF, got: %v", wb.url, err)
97 }
98 ch <- false
99 return
100 }
101 if len(wb.targetMatch) > 0 {
102 if bytes.Contains(buf.Bytes(), []byte(wb.targetMatch)) {
103 wb.log.Debugf("connectivity check passed.")
104 ch <- true
105 } else {
106 wb.log.Debugf("connectivity check failed: string not found.")
107 ch <- false
108 }
109 return
110 }
111 sum := fmt.Sprintf("%x", hash.Sum(nil))
112 if sum == wb.targetMD5 {
113 wb.log.Debugf("connectivity check passed.")
114 ch <- true
115 } else {
116 wb.log.Debugf("connectivity check failed: content mismatch.")
117 ch <- false
118 }
119 }
120
121 func (wb *webchecker) Close() {
122 wb.cli.Transport.(*http.Transport).CloseIdleConnections()
123 }