/
ufm.go
170 lines (157 loc) · 4.31 KB
/
ufm.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ufm
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
//"time"
"os"
)
func DoNothing() {
fmt.Println("nothing.")
os.Exit(0)
}
// need a client object that executes http requests returns json
type UfmClient struct {
Insecure bool
Username string
Password string
Endpoint string
CurrentCookie *http.Cookie
}
func (u *UfmClient) writeCookieFile(cookieFile string) {
// write out current cookie to cookie file
bytes, err := json.Marshal(u.CurrentCookie)
if err != nil {
fmt.Println("couldn't retrieve current cookie: ", err)
}
fmt.Println("writing out cookies.")
err = ioutil.WriteFile(cookieFile, bytes, 0644)
if err != nil {
fmt.Println("couldn't write out cookiefile: ", err)
}
}
func GetClient(username string, password string, endpoint string, insecure bool, cookieFile string) (*UfmClient, error) {
u := &UfmClient{
Username: username,
Password: password,
Endpoint: endpoint,
Insecure: insecure,
//AccessCookies: []*http.Cookie{},
CurrentCookie: nil,
}
// Load cookies file check expiry
// never expires ??
bytes, err := ioutil.ReadFile(cookieFile)
if err == nil && len(bytes) > 0 {
err = json.Unmarshal(bytes, &u.CurrentCookie)
if err != nil {
return nil, err
//}
//if ! time.Now().Before(u.CurrentCookie.Expires) {
// fmt.Println(time.Now(), u.CurrentCookie.Expires)
// err := u.Auth(insecure)
// if err != nil {
// return nil, err
// }
// u.writeCookieFile(cookieFile)
}
//fmt.Println("succeeded using existing cookie.")
} else {
err := u.Auth()
if err != nil {
return nil, err
}
u.writeCookieFile(cookieFile)
}
return u, nil
}
func (u *UfmClient) Auth() error {
// Check if cookies file exists and has content
// UFM uses cookie after user/pass auth
path := "/dologin"
form := url.Values{
"httpd_username": {u.Username},
"httpd_password": {u.Password},
}
//form := url.Values{}
//form.Add("httpd_username", u.Username)
//form.Add("httpd_password", u.Password)
fmt.Println("form:", form)
req, err := http.NewRequest("POST", u.Endpoint+path, strings.NewReader(form.Encode()))
fmt.Println(req.FormValue("httpd_username"))
fmt.Println(req.FormValue("httpd_password"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return err
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: u.Insecure},
}
checkRedirect := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
client := &http.Client{Transport: tr, CheckRedirect: checkRedirect}
fmt.Println("sending request to", u.Endpoint+path)
//fmt.Println("req", req)
resp, err := client.Do(req)
if err != nil {
return err
}
//bodyBytes, _ := io.ReadAll(resp.Body)
//fmt.Println(string(bodyBytes))
//fmt.Println("req: ", req)
//fmt.Println("resp: ", resp)
u.CurrentCookie = resp.Cookies()[0]
//fmt.Println("resp.cookies:", resp.Cookies())
//fmt.Println("u cookies:", u.AccessCookies)
//fmt.Println("current cookie:", u.CurrentCookie)
return nil
}
// raw get with queries
func (u *UfmClient) Get(path string, queries []string) (*http.Response, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: u.Insecure},
}
req, err := http.NewRequest("GET", u.Endpoint+path, nil)
req.AddCookie(u.CurrentCookie)
q := req.URL.Query()
for _, query := range queries {
//fmt.Fprintln(os.Stderr, "processing query:", query)
a := strings.Split(query, "=")[0]
b := strings.Split(query, "=")[1]
q.Add(a, b)
}
req.URL.RawQuery = q.Encode()
//fmt.Println("req:", req)
if err != nil {
return nil, err
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
//fmt.Fprintln(os.Stderr, "req:", req)
//bodyBytes, _ := io.ReadAll(resp.Body)
//jsonData, _ := json.Marshal(bodyBytes)
//fmt.Println("resp:", string(bodyBytes))
return resp, err
}
// raw put
func (u *UfmClient) Post(path string, data io.Reader) (*http.Response, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: u.Insecure},
}
req, err := http.NewRequest("POST", u.Endpoint+path, data)
req.AddCookie(u.CurrentCookie)
//fmt.Println("req:", req)
if err != nil {
return nil, err
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
//fmt.Fprintln(os.Stderr, "req:", req)
return resp, err
}