56 lines
941 B
Go
56 lines
941 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type Auth struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type Instance struct {
|
|
Url *url.URL
|
|
Jar *cookiejar.Jar
|
|
}
|
|
|
|
func newJar(hosts *Instance) bool {
|
|
for _, e := range hosts.Jar.Cookies(hosts.Url) {
|
|
if strings.Contains(e.Name, "AIROS_") {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func auth(e *Instance) {
|
|
var auth Auth
|
|
auth.Username = cfg.Username
|
|
auth.Password = cfg.Password
|
|
|
|
body, err := json.Marshal(&auth)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
resp, err := http.Post(fmt.Sprintf("https://%s/api/auth", e.Url.Host), "application/json", bytes.NewReader(body))
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
if resp.StatusCode > 201 {
|
|
log.Println("Auth failed")
|
|
}
|
|
|
|
e.Jar.SetCookies(e.Url, resp.Cookies())
|
|
|
|
log.Println("Got new cookies for:", e.Url.Host, resp.Cookies())
|
|
}
|