-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.go
71 lines (65 loc) · 1.44 KB
/
firewall.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package panw
import (
"crypto/tls"
"fmt"
"path/filepath"
"time"
"github.com/go-resty/resty/v2"
)
type Firewall struct {
Name string
client *resty.Client
Device Device
}
func (fw *Firewall) Check() (bool, error) {
req := fw.client.R().SetQueryParams(map[string]string{
"type": "op",
"cmd": "<show><clock></clock></show>",
})
res, err := req.Get("/api")
if err != nil {
return false, err
}
if res.IsError() {
return false, fmt.Errorf(res.Status())
}
return true, nil
}
func (fw *Firewall) ExportDeviceState(dir string) (string, error) {
fw.client.SetOutputDirectory(dir)
now := time.Now()
outFile := filepath.Join(dir,
fmt.Sprintf(
"%s_%s_devicestate.tgz",
now.Format("20060102"),
fw.Device.Hostname,
))
req := fw.client.R().SetOutput(outFile).SetQueryParams(map[string]string{
"type": "export",
"category": "device-state",
})
res, err := req.Get("/api")
if err != nil {
return "", err
}
if res.IsError() {
return "", fmt.Errorf(res.Status())
}
return outFile, nil
}
func NewFirewall(device Device, username, password string) (*Firewall, error) {
client := resty.New()
client.SetBasicAuth(username, password)
client.SetBaseURL(fmt.Sprintf("https://%s", device.IPAddress))
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
fw := &Firewall{
Name: device.Hostname,
Device: device,
client: client,
}
_, err := fw.Check()
if err != nil {
return nil, err
}
return fw, nil
}