honk/toys/gettoken.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

2019-11-07 22:50:52 +01:00
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
2022-02-09 23:11:55 +01:00
var debugMode = false
2019-11-07 22:50:52 +01:00
func main() {
server := ""
username := ""
password := ""
flag.StringVar(&server, "server", server, "server to connnect")
flag.StringVar(&username, "username", username, "username to use")
flag.StringVar(&password, "password", password, "password to use")
2022-02-09 23:11:55 +01:00
flag.BoolVar(&debugMode, "debug", debugMode, "debug mode")
2019-11-07 22:50:52 +01:00
flag.Parse()
if server == "" || username == "" || password == "" {
flag.Usage()
os.Exit(1)
}
form := make(url.Values)
form.Add("username", username)
form.Add("password", password)
form.Add("gettoken", "1")
loginurl := fmt.Sprintf("https://%s/dologin", server)
req, err := http.NewRequest("POST", loginurl, strings.NewReader(form.Encode()))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
2022-02-09 23:11:55 +01:00
client := http.DefaultClient
if debugMode {
client = debugClient
}
resp, err := client.Do(req)
2019-11-07 22:50:52 +01:00
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatalf("status: %d: %s", resp.StatusCode, answer)
}
fmt.Println(string(answer))
}