mc_add.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package main
  2. import (
  3. "net/http"
  4. "regexp"
  5. "strconv"
  6. "time"
  7. )
  8. //TODO: implement duplicate entry check.
  9. func AddBanIP(w http.ResponseWriter, r *http.Request) {
  10. IP, _ := mv(r, "ip", false)
  11. Created := currentTime()
  12. Source := "ArOZ Minecraft Terminal"
  13. Expires := "forever"
  14. Reason, _ := mv(r, "reason", false)
  15. Config.WriteBannedIP(IP, Created, Source, Expires, Reason)
  16. sendTextResponse(w, "OK")
  17. }
  18. func AddBanPlayer(w http.ResponseWriter, r *http.Request) {
  19. UUID, _ := mv(r, "uuid", false)
  20. Name, _ := mv(r, "name", false)
  21. Created := currentTime()
  22. Source := "ArOZ Minecraft Terminal"
  23. Expires := "forever"
  24. Reason, _ := mv(r, "reason", false)
  25. if IsValidUUID(UUID) {
  26. Config.WriteBannedPlayer(UUID, Name, Created, Source, Expires, Reason)
  27. sendTextResponse(w, "OK")
  28. } else {
  29. sendTextResponse(w, "Incorrect UUID.")
  30. }
  31. }
  32. func AddOps(w http.ResponseWriter, r *http.Request) {
  33. UUID, _ := mv(r, "uuid", false)
  34. Name, _ := mv(r, "name", false)
  35. Level, _ := mv(r, "level", false)
  36. LevelI, _ := strconv.Atoi(Level)
  37. BypassesPlayerLimit, _ := mv(r, "bypass", false)
  38. BypassesPlayerLimitB, _ := strconv.ParseBool(BypassesPlayerLimit)
  39. if IsValidUUID(UUID) {
  40. Config.WriteOps(UUID, Name, LevelI, BypassesPlayerLimitB)
  41. sendTextResponse(w, "OK")
  42. } else {
  43. sendTextResponse(w, "Incorrect UUID.")
  44. }
  45. }
  46. func AddWhitelist(w http.ResponseWriter, r *http.Request) {
  47. UUID, _ := mv(r, "uuid", false)
  48. Name, _ := mv(r, "name", false)
  49. if IsValidUUID(UUID) {
  50. Config.WriteWhitelist(UUID, Name)
  51. sendTextResponse(w, "OK")
  52. } else {
  53. sendTextResponse(w, "Incorrect UUID.")
  54. }
  55. }
  56. func currentTime() string {
  57. t := time.Now()
  58. return t.Format("2006-01-02 15:04:05 -0700")
  59. }
  60. //vaild UUID
  61. //https://stackoverflow.com/questions/25051675/how-to-validate-uuid-v4-in-go
  62. func IsValidUUID(uuid string) bool {
  63. r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  64. return r.MatchString(uuid)
  65. }