1
0

utils.go 4.4 KB

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