utils.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 GET paramter as boolean, accept 1 or true
  43. func GetBool(r *http.Request, key string) (bool, error) {
  44. x, err := GetPara(r, key)
  45. if err != nil {
  46. return false, err
  47. }
  48. x = strings.TrimSpace(x)
  49. if x == "1" || strings.ToLower(x) == "true" || strings.ToLower(x) == "on" {
  50. return true, nil
  51. } else if x == "0" || strings.ToLower(x) == "false" || strings.ToLower(x) == "off" {
  52. return false, nil
  53. }
  54. return false, errors.New("invalid boolean given")
  55. }
  56. // Get POST paramter
  57. func PostPara(r *http.Request, key string) (string, error) {
  58. r.ParseForm()
  59. x := r.Form.Get(key)
  60. if x == "" {
  61. return "", errors.New("invalid " + key + " given")
  62. } else {
  63. return x, nil
  64. }
  65. }
  66. // Get POST paramter as boolean, accept 1 or true
  67. func PostBool(r *http.Request, key string) (bool, error) {
  68. x, err := PostPara(r, key)
  69. if err != nil {
  70. return false, err
  71. }
  72. x = strings.TrimSpace(x)
  73. if x == "1" || strings.ToLower(x) == "true" || strings.ToLower(x) == "on" {
  74. return true, nil
  75. } else if x == "0" || strings.ToLower(x) == "false" || strings.ToLower(x) == "off" {
  76. return false, nil
  77. }
  78. return false, errors.New("invalid boolean given")
  79. }
  80. // Get POST paramter as int
  81. func PostInt(r *http.Request, key string) (int, error) {
  82. x, err := PostPara(r, key)
  83. if err != nil {
  84. return 0, err
  85. }
  86. x = strings.TrimSpace(x)
  87. rx, err := strconv.Atoi(x)
  88. if err != nil {
  89. return 0, err
  90. }
  91. return rx, nil
  92. }
  93. func FileExists(filename string) bool {
  94. _, err := os.Stat(filename)
  95. if os.IsNotExist(err) {
  96. return false
  97. }
  98. return true
  99. }
  100. func IsDir(path string) bool {
  101. if FileExists(path) == false {
  102. return false
  103. }
  104. fi, err := os.Stat(path)
  105. if err != nil {
  106. log.Fatal(err)
  107. return false
  108. }
  109. switch mode := fi.Mode(); {
  110. case mode.IsDir():
  111. return true
  112. case mode.IsRegular():
  113. return false
  114. }
  115. return false
  116. }
  117. func TimeToString(targetTime time.Time) string {
  118. return targetTime.Format("2006-01-02 15:04:05")
  119. }
  120. // Check if given string in a given slice
  121. func StringInArray(arr []string, str string) bool {
  122. for _, a := range arr {
  123. if a == str {
  124. return true
  125. }
  126. }
  127. return false
  128. }
  129. func StringInArrayIgnoreCase(arr []string, str string) bool {
  130. smallArray := []string{}
  131. for _, item := range arr {
  132. smallArray = append(smallArray, strings.ToLower(item))
  133. }
  134. return StringInArray(smallArray, strings.ToLower(str))
  135. }
  136. // Validate if the listening address is correct
  137. func ValidateListeningAddress(address string) bool {
  138. // Check if the address starts with a colon, indicating it's just a port
  139. if strings.HasPrefix(address, ":") {
  140. return true
  141. }
  142. // Split the address into host and port parts
  143. host, port, err := net.SplitHostPort(address)
  144. if err != nil {
  145. // Try to parse it as just a port
  146. if _, err := strconv.Atoi(address); err == nil {
  147. return false // It's just a port number
  148. }
  149. return false // It's an invalid address
  150. }
  151. // Check if the port part is a valid number
  152. if _, err := strconv.Atoi(port); err != nil {
  153. return false
  154. }
  155. // Check if the host part is a valid IP address or empty (indicating any IP)
  156. if host != "" {
  157. if net.ParseIP(host) == nil {
  158. return false
  159. }
  160. }
  161. return true
  162. }