mc_add.go 1.9 KB

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