forked from alex2108/syncthing-tray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_syncthing.go
53 lines (46 loc) · 1.04 KB
/
query_syncthing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"crypto/tls"
"log"
"time"
"net"
"net/http"
"io/ioutil"
)
func query_syncthing(url string) (string, error) {
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, time.Second*10)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(time.Second * 120))
return conn, nil
},
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.insecure,
},
ResponseHeaderTimeout: time.Second * 120,
},
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("X-API-Key", config.ApiKey)
response, err := client.Do(req)
if err != nil {
log.Printf("ERROR: %s\n", err)
return "", err
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if response.StatusCode == 401 {
log.Fatal("Invalid username or password")
}
if err != nil {
log.Printf("ERROR: %s\n", err)
return "", err
}
return string(contents), err
}
return "", err
}