keiji-ctl.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "path"
  13. "git.aetherial.dev/aeth/keiji/pkg/controller"
  14. "git.aetherial.dev/aeth/keiji/pkg/helpers"
  15. _ "github.com/mattn/go-sqlite3"
  16. )
  17. // authenticate and get the cookie needed to make updates
  18. func auth(url, username, password string) *http.Cookie {
  19. client := http.Client{}
  20. b, _ := json.Marshal(helpers.Credentials{Username: username, Password: password})
  21. req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  22. req.Header.Add("Content-Type", "application/json")
  23. resp, err := client.Do(req)
  24. if err != nil {
  25. log.Fatal("auth failed: ", err)
  26. }
  27. defer resp.Body.Close()
  28. if resp.StatusCode > 200 {
  29. msg, _ := io.ReadAll(resp.Body)
  30. log.Fatal("Invalid credentials or server error: ", string(msg), "\n Status code: ", resp.StatusCode)
  31. }
  32. cookies := resp.Cookies()
  33. for i := range cookies {
  34. if cookies[i].Name == controller.AUTH_COOKIE_NAME {
  35. return cookies[i]
  36. }
  37. }
  38. log.Fatal("Auth cookie not found.")
  39. return nil
  40. }
  41. // prepare the auth cookie
  42. func prepareCookie(address string) *http.Cookie {
  43. parsedAddr, err := url.Parse(address)
  44. dn := parsedAddr.Hostname()
  45. if err != nil {
  46. log.Fatal("unparseable address: ", address, " error: ", err)
  47. }
  48. var preparedCookie *http.Cookie
  49. if cookie == "" {
  50. log.Fatal("Cookie cannot be empty.")
  51. } else {
  52. preparedCookie = &http.Cookie{Value: cookie, Name: controller.AUTH_COOKIE_NAME, Domain: dn}
  53. }
  54. return preparedCookie
  55. }
  56. var pngFile string
  57. var redirect string
  58. var text string
  59. var col string
  60. var cmd string
  61. var address string
  62. var cookie string
  63. func main() {
  64. flag.StringVar(&pngFile, "png", "", "The location of the PNG to upload")
  65. flag.StringVar(&redirect, "redirect", "", "the website that the navbar will redirect to")
  66. flag.StringVar(&text, "text", "", "the text to display on the menu item")
  67. flag.StringVar(&col, "col", "", "the column to add/populate the admin table item under")
  68. flag.StringVar(&cmd, "cmd", "", "the 'command' for the seed program to use, currently supports options 'admin', 'menu', and 'asset', 'nav'")
  69. flag.StringVar(&address, "address", "https://aetherial.dev", "override the url to contact.")
  70. flag.StringVar(&cookie, "cookie", "", "pass a cookie to bypass direct authentication")
  71. flag.Parse()
  72. client := http.Client{}
  73. switch cmd {
  74. case "auth":
  75. cookie := auth(fmt.Sprintf("%s/login", address), os.Getenv("KEIJI_USERNAME"), os.Getenv("KEIJI_PASSWORD"))
  76. fmt.Println(cookie.Value)
  77. case "asset":
  78. b, err := os.ReadFile(pngFile)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. _, fileName := path.Split(pngFile)
  83. item := helpers.Asset{
  84. Name: fileName,
  85. Data: b,
  86. }
  87. data, _ := json.Marshal(item)
  88. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/asset", address), bytes.NewReader(data))
  89. req.AddCookie(prepareCookie(address))
  90. req.Header.Add("Content-Type", "application/json")
  91. resp, err := client.Do(req)
  92. if err != nil {
  93. fmt.Println("There was an error performing the desired request: ", err.Error())
  94. os.Exit(1)
  95. }
  96. if resp.StatusCode > 200 {
  97. defer resp.Body.Close()
  98. b, _ := io.ReadAll(resp.Body)
  99. fmt.Println("There was an error performing the desired request: ", string(b))
  100. os.Exit(2)
  101. }
  102. fmt.Println("navigation bar item upload successfully.")
  103. os.Exit(0)
  104. case "nav":
  105. fmt.Println(string(pngFile))
  106. b, err := os.ReadFile(pngFile)
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. _, fileName := path.Split(pngFile)
  111. fmt.Println(fileName)
  112. item := helpers.NavBarItem{
  113. Link: fileName,
  114. Redirect: redirect,
  115. Png: b,
  116. }
  117. data, _ := json.Marshal(item)
  118. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/navbar", address), bytes.NewReader(data))
  119. req.AddCookie(prepareCookie(address))
  120. req.Header.Add("Content-Type", "application/json")
  121. resp, err := client.Do(req)
  122. if err != nil {
  123. fmt.Println("There was an error performing the desired request: ", err.Error())
  124. os.Exit(1)
  125. }
  126. if resp.StatusCode > 200 {
  127. defer resp.Body.Close()
  128. b, _ := io.ReadAll(resp.Body)
  129. fmt.Println("There was an error performing the desired request: ", string(b))
  130. os.Exit(2)
  131. }
  132. fmt.Println("png item upload successfully.")
  133. os.Exit(0)
  134. case "menu":
  135. b, _ := json.Marshal(helpers.MenuLinkPair{LinkText: text, MenuLink: redirect})
  136. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/menu", address), bytes.NewReader(b))
  137. req.AddCookie(prepareCookie(address))
  138. req.Header.Add("Content-Type", "application/json")
  139. resp, err := client.Do(req)
  140. if err != nil {
  141. fmt.Println("There was an error performing the desired request: ", err.Error())
  142. os.Exit(1)
  143. }
  144. if resp.StatusCode > 200 {
  145. defer resp.Body.Close()
  146. b, _ := io.ReadAll(resp.Body)
  147. fmt.Println("There was an error performing the desired request: ", string(b))
  148. os.Exit(3)
  149. }
  150. fmt.Println("menu item uploaded successfully.")
  151. os.Exit(0)
  152. case "admin":
  153. tables := make(map[string][]helpers.TableData)
  154. adminPage := helpers.AdminPage{Tables: tables}
  155. adminPage.Tables[col] = append(adminPage.Tables[col], helpers.TableData{Link: redirect, DisplayName: text})
  156. b, _ := json.Marshal(adminPage)
  157. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/panel", address), bytes.NewReader(b))
  158. req.AddCookie(prepareCookie(address))
  159. req.Header.Add("Content-Type", "application/json")
  160. resp, err := client.Do(req)
  161. if err != nil {
  162. fmt.Println("There was an error performing the desired request: ", err.Error())
  163. os.Exit(1)
  164. }
  165. if resp.StatusCode > 200 {
  166. defer resp.Body.Close()
  167. b, _ := io.ReadAll(resp.Body)
  168. fmt.Println("There was an error performing the desired request: ", string(b))
  169. os.Exit(4)
  170. }
  171. fmt.Println("admin item added successfully.")
  172. os.Exit(0)
  173. }
  174. }