From 0a478a5d654c612da203e85eeff25ae60e5ef13f Mon Sep 17 00:00:00 2001 From: Nil Date: Thu, 3 Feb 2022 20:29:12 -0500 Subject: [PATCH] initial commit --- README.md | 0 ftgapi.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ 3 files changed, 101 insertions(+) create mode 100644 README.md create mode 100644 ftgapi.go create mode 100644 go.mod diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/ftgapi.go b/ftgapi.go new file mode 100644 index 0000000..4db32e9 --- /dev/null +++ b/ftgapi.go @@ -0,0 +1,98 @@ +package ftgapi + +import ( + "bytes" + "crypto/tls" + "encoding/json" + "io/ioutil" + "log" + "net/http" + "time" +) + +func (f *FortigateAPI) query(method string, endpoint string, postData []byte) []byte { + + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + + spaceClient := http.Client{ + Timeout: time.Second * 2, // Timeout after 2 seconds + } + + req, err := http.NewRequest(method, f.URL+"/api/v2/"+endpoint+"/?access_token="+f.APIKey, bytes.NewBuffer(postData)) + if err != nil { + log.Fatal(err) + } + + // req.Header.Set("Authorization", "Basic "+bauth) + + res, getErr := spaceClient.Do(req) + if getErr != nil { + log.Fatal(getErr) + } + + if res.Body != nil { + defer res.Body.Close() + } + + body, readErr := ioutil.ReadAll(res.Body) + if readErr != nil { + log.Fatal(readErr) + } + + return body +} + +func (f *FortigateAPI) get(endpoint string) interface{} { + + var apirequest FTGAPIRequest + + response := f.query("GET", endpoint, nil) + + err := json.Unmarshal([]byte(response), &apirequest) + if err != nil { + log.Fatal(err) + } + + resultstr, _ := json.Marshal(apirequest.Results) + + //log.Println(string(response)) + + switch endpoint { + case "monitor/web-ui/state": + generic := FTGMonitorWebUIState{} + json.Unmarshal(resultstr, &generic) + return generic + } + + return nil +} + +func (f *FortigateAPI) GetMonitorWebUIState() FTGMonitorWebUIState { + return f.get("monitor/web-ui/state").(FTGMonitorWebUIState) +} + +type FTGMonitorWebUIState struct { + Model_name string + Model_number string + Hostname string + Model string + Model_subtype string + Model_level string +} + +type FTGAPIRequest struct { + Path string + Name string + Action string + Serial string + Version string + Build int + Status string + Http_status int + Results interface{} +} + +type FortigateAPI struct { + URL string + APIKey string +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3c6c222 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitx01.hn.yrw.io/nil/ftgapi + +go 1.15