parser.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package config // import "miniflux.app/v2/internal/config"
  4. import (
  5. "bufio"
  6. "bytes"
  7. "crypto/rand"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "log/slog"
  12. "net/url"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type configParser struct {
  19. options *configOptions
  20. }
  21. func NewConfigParser() *configParser {
  22. return &configParser{
  23. options: NewConfigOptions(),
  24. }
  25. }
  26. func (cp *configParser) ParseEnvironmentVariables() (*configOptions, error) {
  27. if err := cp.parseLines(os.Environ()); err != nil {
  28. return nil, err
  29. }
  30. return cp.options, nil
  31. }
  32. func (cp *configParser) ParseFile(filename string) (*configOptions, error) {
  33. fp, err := os.Open(filename)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer fp.Close()
  38. if err := cp.parseLines(parseFileContent(fp)); err != nil {
  39. return nil, err
  40. }
  41. return cp.options, nil
  42. }
  43. func (cp *configParser) postParsing() error {
  44. // Parse basePath and rootURL based on BASE_URL
  45. baseURL := cp.options.options["BASE_URL"].parsedStringValue
  46. baseURL = strings.TrimSuffix(baseURL, "/")
  47. parsedURL, err := url.Parse(baseURL)
  48. if err != nil {
  49. return fmt.Errorf("invalid BASE_URL: %v", err)
  50. }
  51. scheme := strings.ToLower(parsedURL.Scheme)
  52. if scheme != "https" && scheme != "http" {
  53. return errors.New("BASE_URL scheme must be http or https")
  54. }
  55. cp.options.options["BASE_URL"].parsedStringValue = baseURL
  56. cp.options.basePath = parsedURL.Path
  57. parsedURL.Path = ""
  58. cp.options.rootURL = parsedURL.String()
  59. // Parse YouTube embed domain based on YOUTUBE_EMBED_URL_OVERRIDE
  60. youTubeEmbedURLOverride := cp.options.options["YOUTUBE_EMBED_URL_OVERRIDE"].parsedStringValue
  61. if youTubeEmbedURLOverride != "" {
  62. parsedYouTubeEmbedURL, err := url.Parse(youTubeEmbedURLOverride)
  63. if err != nil {
  64. return fmt.Errorf("invalid YOUTUBE_EMBED_URL_OVERRIDE: %v", err)
  65. }
  66. cp.options.youTubeEmbedDomain = parsedYouTubeEmbedURL.Hostname()
  67. }
  68. // Generate a media proxy private key if not set
  69. if len(cp.options.options["MEDIA_PROXY_PRIVATE_KEY"].parsedBytesValue) == 0 {
  70. randomKey := make([]byte, 16)
  71. rand.Read(randomKey)
  72. cp.options.options["MEDIA_PROXY_PRIVATE_KEY"].parsedBytesValue = randomKey
  73. }
  74. // Override LISTEN_ADDR with PORT if set (for compatibility reasons)
  75. if cp.options.Port() != "" {
  76. cp.options.options["LISTEN_ADDR"].parsedStringList = []string{":" + cp.options.Port()}
  77. cp.options.options["LISTEN_ADDR"].rawValue = ":" + cp.options.Port()
  78. }
  79. return nil
  80. }
  81. func (cp *configParser) parseLines(lines []string) error {
  82. for lineNum, line := range lines {
  83. key, value, ok := strings.Cut(line, "=")
  84. if !ok {
  85. return fmt.Errorf("unable to parse configuration, invalid format on line %d", lineNum)
  86. }
  87. key, value = strings.TrimSpace(key), strings.TrimSpace(value)
  88. if err := cp.parseLine(key, value); err != nil {
  89. return err
  90. }
  91. }
  92. if err := cp.postParsing(); err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func (cp *configParser) parseLine(key, value string) error {
  98. field, exists := cp.options.options[key]
  99. if !exists {
  100. if key == "FILTER_ENTRY_MAX_AGE_DAYS" {
  101. slog.Warn("Configuration option FILTER_ENTRY_MAX_AGE_DAYS is deprecated; use user filter rule max-age:<duration> instead")
  102. }
  103. // Ignore unknown configuration keys to avoid parsing unrelated environment variables.
  104. return nil
  105. }
  106. // Validate the option if a validator is provided
  107. if field.validator != nil {
  108. if err := field.validator(value); err != nil {
  109. return fmt.Errorf("invalid value for key %s: %v", key, err)
  110. }
  111. }
  112. // Convert the raw value based on its type
  113. switch field.valueType {
  114. case stringType:
  115. field.parsedStringValue = parseStringValue(value, field.parsedStringValue)
  116. field.rawValue = value
  117. case stringListType:
  118. field.parsedStringList = parseStringListValue(value, field.parsedStringList)
  119. field.rawValue = value
  120. case boolType:
  121. parsedValue, err := parseBoolValue(value, field.parsedBoolValue)
  122. if err != nil {
  123. return fmt.Errorf("invalid boolean value for key %s: %v", key, err)
  124. }
  125. field.parsedBoolValue = parsedValue
  126. field.rawValue = value
  127. case intType:
  128. field.parsedIntValue = parseIntValue(value, field.parsedIntValue)
  129. field.rawValue = value
  130. case int64Type:
  131. field.parsedInt64Value = ParsedInt64Value(value, field.parsedInt64Value)
  132. field.rawValue = value
  133. case secondType:
  134. field.parsedDuration = parseDurationValue(value, time.Second, field.parsedDuration)
  135. field.rawValue = value
  136. case minuteType:
  137. field.parsedDuration = parseDurationValue(value, time.Minute, field.parsedDuration)
  138. field.rawValue = value
  139. case hourType:
  140. field.parsedDuration = parseDurationValue(value, time.Hour, field.parsedDuration)
  141. field.rawValue = value
  142. case dayType:
  143. field.parsedDuration = parseDurationValue(value, time.Hour*24, field.parsedDuration)
  144. field.rawValue = value
  145. case urlType:
  146. parsedURL, err := parseURLValue(value, field.parsedURLValue)
  147. if err != nil {
  148. return fmt.Errorf("invalid URL for key %s: %v", key, err)
  149. }
  150. field.parsedURLValue = parsedURL
  151. field.rawValue = value
  152. case secretFileType:
  153. secretValue, err := readSecretFileValue(value)
  154. if err != nil {
  155. return fmt.Errorf("error reading secret file for key %s: %v", key, err)
  156. }
  157. if field.targetKey != "" {
  158. if targetField, ok := cp.options.options[field.targetKey]; ok {
  159. targetField.parsedStringValue = secretValue
  160. targetField.rawValue = secretValue
  161. }
  162. }
  163. field.rawValue = value
  164. case bytesType:
  165. if value != "" {
  166. field.parsedBytesValue = []byte(value)
  167. field.rawValue = value
  168. }
  169. }
  170. return nil
  171. }
  172. func parseStringValue(value string, fallback string) string {
  173. if value == "" {
  174. return fallback
  175. }
  176. return value
  177. }
  178. func parseBoolValue(value string, fallback bool) (bool, error) {
  179. if value == "" {
  180. return fallback, nil
  181. }
  182. value = strings.ToLower(value)
  183. if value == "1" || value == "yes" || value == "true" || value == "on" {
  184. return true, nil
  185. }
  186. if value == "0" || value == "no" || value == "false" || value == "off" {
  187. return false, nil
  188. }
  189. return false, fmt.Errorf("invalid boolean value: %q", value)
  190. }
  191. func parseIntValue(value string, fallback int) int {
  192. if value == "" {
  193. return fallback
  194. }
  195. v, err := strconv.Atoi(value)
  196. if err != nil {
  197. return fallback
  198. }
  199. return v
  200. }
  201. func ParsedInt64Value(value string, fallback int64) int64 {
  202. if value == "" {
  203. return fallback
  204. }
  205. v, err := strconv.ParseInt(value, 10, 64)
  206. if err != nil {
  207. return fallback
  208. }
  209. return v
  210. }
  211. func parseStringListValue(value string, fallback []string) []string {
  212. if value == "" {
  213. return fallback
  214. }
  215. var strList []string
  216. present := make(map[string]bool)
  217. for item := range strings.SplitSeq(value, ",") {
  218. if itemValue := strings.TrimSpace(item); itemValue != "" {
  219. if !present[itemValue] {
  220. present[itemValue] = true
  221. strList = append(strList, itemValue)
  222. }
  223. }
  224. }
  225. return strList
  226. }
  227. func parseDurationValue(value string, unit time.Duration, fallback time.Duration) time.Duration {
  228. if value == "" {
  229. return fallback
  230. }
  231. v, err := strconv.Atoi(value)
  232. if err != nil {
  233. return fallback
  234. }
  235. return time.Duration(v) * unit
  236. }
  237. func parseURLValue(value string, fallback *url.URL) (*url.URL, error) {
  238. if value == "" {
  239. return fallback, nil
  240. }
  241. parsedURL, err := url.Parse(value)
  242. if err != nil {
  243. return fallback, err
  244. }
  245. return parsedURL, nil
  246. }
  247. func readSecretFileValue(filename string) (string, error) {
  248. data, err := os.ReadFile(filename)
  249. if err != nil {
  250. return "", err
  251. }
  252. value := string(bytes.TrimSpace(data))
  253. if value == "" {
  254. return "", errors.New("secret file is empty")
  255. }
  256. return value, nil
  257. }
  258. func parseFileContent(r io.Reader) (lines []string) {
  259. scanner := bufio.NewScanner(r)
  260. for scanner.Scan() {
  261. line := strings.TrimSpace(scanner.Text())
  262. if !strings.HasPrefix(line, "#") && strings.Index(line, "=") > 0 {
  263. lines = append(lines, line)
  264. }
  265. }
  266. return lines
  267. }