mineauth/_hooks/mcm/mcm.go

68 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"github.com/peterbourgon/ff/v3"
)
type profile struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
}
func (p *profile) UUID() string {
return fmt.Sprintf("%s-%s-%s-%s-%s", p.ID[:8], p.ID[8:12], p.ID[12:16], p.ID[16:20], p.ID[20:32])
}
func main() {
fs := flag.NewFlagSet("mcm", flag.ExitOnError)
baseFlag := fs.String("base", "", "Base URL for MCM API")
tokenFlag := fs.String("token", "", "MCM API Token")
if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("MCM_REGISTER"),
); err != nil {
panic(err)
}
var profile profile
if err := json.NewDecoder(os.Stdin).Decode(&profile); err != nil {
panic(err)
}
baseURL := strings.TrimSuffix(*baseFlag, "/")
u := fmt.Sprintf("%s/plugin/register", baseURL)
resp, err := http.PostForm(u, url.Values{
"api": []string{*tokenFlag},
"uuid": []string{profile.UUID()},
"username": []string{profile.Name},
"ip": []string{profile.IP},
})
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
panic("invalid response from MCM")
}
s := struct {
Status bool `json:"status"`
Message string `json:"message"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println(s.Message)
}