utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package utils
  2. import (
  3. "errors"
  4. "log"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. /*
  13. Common
  14. Some commonly used functions in ArozOS
  15. */
  16. // Response related
  17. func SendTextResponse(w http.ResponseWriter, msg string) {
  18. w.Write([]byte(msg))
  19. }
  20. // Send JSON response, with an extra json header
  21. func SendJSONResponse(w http.ResponseWriter, json string) {
  22. w.Header().Set("Content-Type", "application/json")
  23. w.Write([]byte(json))
  24. }
  25. func SendErrorResponse(w http.ResponseWriter, errMsg string) {
  26. w.Header().Set("Content-Type", "application/json")
  27. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  28. }
  29. func SendOK(w http.ResponseWriter) {
  30. w.Header().Set("Content-Type", "application/json")
  31. w.Write([]byte("\"OK\""))
  32. }
  33. // Get GET parameter
  34. func GetPara(r *http.Request, key string) (string, error) {
  35. keys, ok := r.URL.Query()[key]
  36. if !ok || len(keys[0]) < 1 {
  37. return "", errors.New("invalid " + key + " given")
  38. } else {
  39. return keys[0], nil
  40. }
  41. }
  42. // Get POST paramter
  43. func PostPara(r *http.Request, key string) (string, error) {
  44. r.ParseForm()
  45. x := r.Form.Get(key)
  46. if x == "" {
  47. return "", errors.New("invalid " + key + " given")
  48. } else {
  49. return x, nil
  50. }
  51. }
  52. // Get POST paramter as boolean, accept 1 or true
  53. func PostBool(r *http.Request, key string) (bool, error) {
  54. x, err := PostPara(r, key)
  55. if err != nil {
  56. return false, err
  57. }
  58. x = strings.TrimSpace(x)
  59. if x == "1" || strings.ToLower(x) == "true" || strings.ToLower(x) == "on" {
  60. return true, nil
  61. } else if x == "0" || strings.ToLower(x) == "false" || strings.ToLower(x) == "off" {
  62. return false, nil
  63. }
  64. return false, errors.New("invalid boolean given")
  65. }
  66. // Get POST paramter as int
  67. func PostInt(r *http.Request, key string) (int, error) {
  68. x, err := PostPara(r, key)
  69. if err != nil {
  70. return 0, err
  71. }
  72. x = strings.TrimSpace(x)
  73. rx, err := strconv.Atoi(x)
  74. if err != nil {
  75. return 0, err
  76. }
  77. return rx, nil
  78. }
  79. func FileExists(filename string) bool {
  80. _, err := os.Stat(filename)
  81. if os.IsNotExist(err) {
  82. return false
  83. }
  84. return true
  85. }
  86. func IsDir(path string) bool {
  87. if FileExists(path) == false {
  88. return false
  89. }
  90. fi, err := os.Stat(path)
  91. if err != nil {
  92. log.Fatal(err)
  93. return false
  94. }
  95. switch mode := fi.Mode(); {
  96. case mode.IsDir():
  97. return true
  98. case mode.IsRegular():
  99. return false
  100. }
  101. return false
  102. }
  103. func TimeToString(targetTime time.Time) string {
  104. return targetTime.Format("2006-01-02 15:04:05")
  105. }
  106. // Check if given string in a given slice
  107. func StringInArray(arr []string, str string) bool {
  108. for _, a := range arr {
  109. if a == str {
  110. return true
  111. }
  112. }
  113. return false
  114. }
  115. func StringInArrayIgnoreCase(arr []string, str string) bool {
  116. smallArray := []string{}
  117. for _, item := range arr {
  118. smallArray = append(smallArray, strings.ToLower(item))
  119. }
  120. return StringInArray(smallArray, strings.ToLower(str))
  121. }
  122. // Validate if the listening address is correct
  123. func ValidateListeningAddress(address string) bool {
  124. // Check if the address starts with a colon, indicating it's just a port
  125. if strings.HasPrefix(address, ":") {
  126. return true
  127. }
  128. // Split the address into host and port parts
  129. host, port, err := net.SplitHostPort(address)
  130. if err != nil {
  131. // Try to parse it as just a port
  132. if _, err := strconv.Atoi(address); err == nil {
  133. return false // It's just a port number
  134. }
  135. return false // It's an invalid address
  136. }
  137. // Check if the port part is a valid number
  138. if _, err := strconv.Atoi(port); err != nil {
  139. return false
  140. }
  141. // Check if the host part is a valid IP address or empty (indicating any IP)
  142. if host != "" {
  143. if net.ParseIP(host) == nil {
  144. return false
  145. }
  146. }
  147. return true
  148. }