From 278df0b1ad94f9824069731bcfae461e23989014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Tue, 8 Feb 2022 01:06:09 +0100 Subject: [PATCH] Initial commit --- client.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ main.go | 27 +++++++++++++++ status.go | 18 ++++++++++ 4 files changed, 146 insertions(+) create mode 100644 client.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 status.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..43d3ecc --- /dev/null +++ b/client.go @@ -0,0 +1,98 @@ +package main + +import ( + "encoding/json" + "encoding/xml" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "path/filepath" + "time" +) + +type Client struct { + baseURL string + httpClient *http.Client + apiKey string +} + +func readAPIKey() string { + configDir, err := os.UserConfigDir() + if err != nil { + log.Fatal(err) + } + + configFile := filepath.Join(configDir, "syncthing", "config.xml") + file, err := os.Open(configFile) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + b, err := ioutil.ReadAll(file) + if err != nil { + log.Fatal(err) + } + + type stConfig struct { + XMLName xml.Name `xml:"configuration"` + GUI struct { + APIKey string `xml:"apikey"` + } `xml:"gui"` + } + var config stConfig + + err = xml.Unmarshal(b, &config) + if err != nil { + log.Fatal(err) + } + + return config.GUI.APIKey +} + +func NewClient() *Client { + httpClient := &http.Client{ + Timeout: time.Second * 3, + } + + return &Client{httpClient: httpClient, apiKey: readAPIKey(), baseURL: "http://127.0.0.1:8384"} +} + +func (client *Client) sendRequest(req *http.Request) ([]byte, error) { + req.Header.Set("X-API-Key", client.apiKey) + + resp, err := client.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode >= 400 { + return nil, fmt.Errorf("api error") + } + + return b, nil +} + +func (client *Client) get(path string, resource interface{}) error { + url := client.baseURL + path + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return err + } + + b, err := client.sendRequest(req) + if err != nil { + return err + } + + return json.Unmarshal(b, &resource) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1370133 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.xenrox.net/~xenrox/syncthing-status + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e005217 --- /dev/null +++ b/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" +) + +type Status struct { + Local int `json:"local"` + Remote int `json:"remote"` +} + +func main() { + client := NewClient() + var status Status + + localStatus := client.localStatus() + status.Local = localStatus.Completion + + data, err := json.MarshalIndent(status, "", " ") + if err != nil { + log.Fatal(err) + } + + fmt.Printf("%s\n", data) +} diff --git a/status.go b/status.go new file mode 100644 index 0000000..15a3fc4 --- /dev/null +++ b/status.go @@ -0,0 +1,18 @@ +package main + +import "log" + +type LocalStatus struct { + Completion int `json:"completion"` +} + +func (client *Client) localStatus() *LocalStatus { + var status *LocalStatus + + err := client.get("/rest/db/completion", &status) + if err != nil { + log.Fatal(err) + } + + return status +} -- 2.44.0