parser.go 7.5 KB

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