parser.go 7.5 KB

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