|
@@ -8,18 +8,55 @@ import (
|
|
"io"
|
|
"io"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "net/url"
|
|
"os"
|
|
"os"
|
|
"path"
|
|
"path"
|
|
|
|
|
|
|
|
+ "git.aetherial.dev/aeth/keiji/pkg/controller"
|
|
"git.aetherial.dev/aeth/keiji/pkg/helpers"
|
|
"git.aetherial.dev/aeth/keiji/pkg/helpers"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
)
|
|
|
|
|
|
-const DEFAULT_URL = "http://horus-ctn01.void:10277"
|
|
|
|
-
|
|
|
|
// authenticate and get the cookie needed to make updates
|
|
// authenticate and get the cookie needed to make updates
|
|
-func auth() string {
|
|
|
|
- return ""
|
|
|
|
|
|
+func auth(url, username, password string) *http.Cookie {
|
|
|
|
+ client := http.Client{}
|
|
|
|
+ b, _ := json.Marshal(helpers.Credentials{Username: username, Password: password})
|
|
|
|
+ req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
|
+ resp, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("auth failed: ", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ if resp.StatusCode > 200 {
|
|
|
|
+ msg, _ := io.ReadAll(resp.Body)
|
|
|
|
+ log.Fatal("Invalid credentials or server error: ", string(msg), "\n Status code: ", resp.StatusCode)
|
|
|
|
+ }
|
|
|
|
+ cookies := resp.Cookies()
|
|
|
|
+ for i := range cookies {
|
|
|
|
+ if cookies[i].Name == controller.AUTH_COOKIE_NAME {
|
|
|
|
+ return cookies[i]
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ log.Fatal("Auth cookie not found.")
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// prepare the auth cookie
|
|
|
|
+func prepareCookie(address string) *http.Cookie {
|
|
|
|
+
|
|
|
|
+ parsedAddr, err := url.Parse(address)
|
|
|
|
+ dn := parsedAddr.Hostname()
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal("unparseable address: ", address, " error: ", err)
|
|
|
|
+ }
|
|
|
|
+ var preparedCookie *http.Cookie
|
|
|
|
+ if cookie == "" {
|
|
|
|
+ log.Fatal("Cookie cannot be empty.")
|
|
|
|
+ } else {
|
|
|
|
+ preparedCookie = &http.Cookie{Value: cookie, Name: controller.AUTH_COOKIE_NAME, Domain: dn}
|
|
|
|
+ }
|
|
|
|
+ return preparedCookie
|
|
}
|
|
}
|
|
|
|
|
|
var pngFile string
|
|
var pngFile string
|
|
@@ -27,6 +64,8 @@ var redirect string
|
|
var text string
|
|
var text string
|
|
var col string
|
|
var col string
|
|
var cmd string
|
|
var cmd string
|
|
|
|
+var address string
|
|
|
|
+var cookie string
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
|
|
|
|
@@ -34,11 +73,18 @@ func main() {
|
|
flag.StringVar(&redirect, "redirect", "", "the website that the navbar will redirect to")
|
|
flag.StringVar(&redirect, "redirect", "", "the website that the navbar will redirect to")
|
|
flag.StringVar(&text, "text", "", "the text to display on the menu item")
|
|
flag.StringVar(&text, "text", "", "the text to display on the menu item")
|
|
flag.StringVar(&col, "col", "", "the column to add/populate the admin table item under")
|
|
flag.StringVar(&col, "col", "", "the column to add/populate the admin table item under")
|
|
- flag.StringVar(&cmd, "cmd", "", "the 'command' for the seed program to use, currently supports options 'admin', 'menu', and 'png")
|
|
|
|
|
|
+ flag.StringVar(&cmd, "cmd", "", "the 'command' for the seed program to use, currently supports options 'admin', 'menu', and 'asset', 'nav'")
|
|
|
|
+ flag.StringVar(&address, "address", "https://aetherial.dev", "override the url to contact.")
|
|
|
|
+ flag.StringVar(&cookie, "cookie", "", "pass a cookie to bypass direct authentication")
|
|
flag.Parse()
|
|
flag.Parse()
|
|
|
|
|
|
client := http.Client{}
|
|
client := http.Client{}
|
|
|
|
+
|
|
switch cmd {
|
|
switch cmd {
|
|
|
|
+ case "auth":
|
|
|
|
+ cookie := auth(fmt.Sprintf("%s/login", address), os.Getenv("KEIJI_USERNAME"), os.Getenv("KEIJI_PASSWORD"))
|
|
|
|
+ fmt.Println(cookie.Value)
|
|
|
|
+
|
|
case "asset":
|
|
case "asset":
|
|
b, err := os.ReadFile(pngFile)
|
|
b, err := os.ReadFile(pngFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -50,7 +96,8 @@ func main() {
|
|
Data: b,
|
|
Data: b,
|
|
}
|
|
}
|
|
data, _ := json.Marshal(item)
|
|
data, _ := json.Marshal(item)
|
|
- req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/asset", DEFAULT_URL), bytes.NewReader(data))
|
|
|
|
|
|
+ req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/asset", address), bytes.NewReader(data))
|
|
|
|
+ req.AddCookie(prepareCookie(address))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -66,7 +113,7 @@ func main() {
|
|
fmt.Println("navigation bar item upload successfully.")
|
|
fmt.Println("navigation bar item upload successfully.")
|
|
os.Exit(0)
|
|
os.Exit(0)
|
|
|
|
|
|
- case "png":
|
|
|
|
|
|
+ case "nav":
|
|
fmt.Println(string(pngFile))
|
|
fmt.Println(string(pngFile))
|
|
b, err := os.ReadFile(pngFile)
|
|
b, err := os.ReadFile(pngFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -80,7 +127,8 @@ func main() {
|
|
Png: b,
|
|
Png: b,
|
|
}
|
|
}
|
|
data, _ := json.Marshal(item)
|
|
data, _ := json.Marshal(item)
|
|
- req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/navbar", DEFAULT_URL), bytes.NewReader(data))
|
|
|
|
|
|
+ req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/navbar", address), bytes.NewReader(data))
|
|
|
|
+ req.AddCookie(prepareCookie(address))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -93,11 +141,12 @@ func main() {
|
|
fmt.Println("There was an error performing the desired request: ", string(b))
|
|
fmt.Println("There was an error performing the desired request: ", string(b))
|
|
os.Exit(2)
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
- fmt.Println("navigation bar item upload successfully.")
|
|
|
|
|
|
+ fmt.Println("png item upload successfully.")
|
|
os.Exit(0)
|
|
os.Exit(0)
|
|
case "menu":
|
|
case "menu":
|
|
b, _ := json.Marshal(helpers.MenuLinkPair{LinkText: text, MenuLink: redirect})
|
|
b, _ := json.Marshal(helpers.MenuLinkPair{LinkText: text, MenuLink: redirect})
|
|
- req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/menu", DEFAULT_URL), bytes.NewReader(b))
|
|
|
|
|
|
+ req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/menu", address), bytes.NewReader(b))
|
|
|
|
+ req.AddCookie(prepareCookie(address))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -117,7 +166,8 @@ func main() {
|
|
adminPage := helpers.AdminPage{Tables: tables}
|
|
adminPage := helpers.AdminPage{Tables: tables}
|
|
adminPage.Tables[col] = append(adminPage.Tables[col], helpers.TableData{Link: redirect, DisplayName: text})
|
|
adminPage.Tables[col] = append(adminPage.Tables[col], helpers.TableData{Link: redirect, DisplayName: text})
|
|
b, _ := json.Marshal(adminPage)
|
|
b, _ := json.Marshal(adminPage)
|
|
- req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/panel", DEFAULT_URL), bytes.NewReader(b))
|
|
|
|
|
|
+ req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/panel", address), bytes.NewReader(b))
|
|
|
|
+ req.AddCookie(prepareCookie(address))
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -130,6 +180,8 @@ func main() {
|
|
fmt.Println("There was an error performing the desired request: ", string(b))
|
|
fmt.Println("There was an error performing the desired request: ", string(b))
|
|
os.Exit(4)
|
|
os.Exit(4)
|
|
}
|
|
}
|
|
|
|
+ fmt.Println("admin item added successfully.")
|
|
|
|
+ os.Exit(0)
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|