2
0

tshttpproxy_synology.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux
  4. package tshttpproxy
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "strings"
  14. "sync"
  15. "time"
  16. "tailscale.com/util/lineread"
  17. )
  18. // These vars are overridden for tests.
  19. var (
  20. synologyProxyConfigPath = "/etc/proxy.conf"
  21. openSynologyProxyConf = func() (io.ReadCloser, error) {
  22. return os.Open(synologyProxyConfigPath)
  23. }
  24. )
  25. var cache struct {
  26. sync.Mutex
  27. httpProxy *url.URL
  28. httpsProxy *url.URL
  29. updated time.Time
  30. }
  31. func synologyProxyFromConfigCached(req *http.Request) (*url.URL, error) {
  32. if req.URL == nil {
  33. return nil, nil
  34. }
  35. cache.Lock()
  36. defer cache.Unlock()
  37. var err error
  38. modtime := mtime(synologyProxyConfigPath)
  39. if modtime != cache.updated {
  40. cache.httpProxy, cache.httpsProxy, err = synologyProxiesFromConfig()
  41. cache.updated = modtime
  42. }
  43. if req.URL.Scheme == "https" {
  44. return cache.httpsProxy, err
  45. }
  46. return cache.httpProxy, err
  47. }
  48. func synologyProxiesFromConfig() (*url.URL, *url.URL, error) {
  49. r, err := openSynologyProxyConf()
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. return nil, nil, nil
  53. }
  54. return nil, nil, err
  55. }
  56. defer r.Close()
  57. return parseSynologyConfig(r)
  58. }
  59. // parseSynologyConfig parses the Synology proxy configuration, and returns any
  60. // http proxy, and any https proxy respectively, or an error if parsing fails.
  61. func parseSynologyConfig(r io.Reader) (*url.URL, *url.URL, error) {
  62. cfg := map[string]string{}
  63. if err := lineread.Reader(r, func(line []byte) error {
  64. // accept and skip over empty lines
  65. line = bytes.TrimSpace(line)
  66. if len(line) == 0 {
  67. return nil
  68. }
  69. key, value, ok := strings.Cut(string(line), "=")
  70. if !ok {
  71. return fmt.Errorf("missing \"=\" in proxy.conf line: %q", line)
  72. }
  73. cfg[string(key)] = string(value)
  74. return nil
  75. }); err != nil {
  76. return nil, nil, err
  77. }
  78. if cfg["proxy_enabled"] != "yes" {
  79. return nil, nil, nil
  80. }
  81. httpProxyURL := new(url.URL)
  82. httpsProxyURL := new(url.URL)
  83. if cfg["auth_enabled"] == "yes" {
  84. httpProxyURL.User = url.UserPassword(cfg["proxy_user"], cfg["proxy_pwd"])
  85. httpsProxyURL.User = url.UserPassword(cfg["proxy_user"], cfg["proxy_pwd"])
  86. }
  87. // As far as we are aware, synology does not support tls proxies.
  88. httpProxyURL.Scheme = "http"
  89. httpsProxyURL.Scheme = "http"
  90. httpsProxyURL = addHostPort(httpsProxyURL, cfg["https_host"], cfg["https_port"])
  91. httpProxyURL = addHostPort(httpProxyURL, cfg["http_host"], cfg["http_port"])
  92. return httpProxyURL, httpsProxyURL, nil
  93. }
  94. // addHostPort adds to u the given host and port and returns the updated url, or
  95. // if host is empty, it returns nil.
  96. func addHostPort(u *url.URL, host, port string) *url.URL {
  97. if host == "" {
  98. return nil
  99. }
  100. if port == "" {
  101. u.Host = host
  102. } else {
  103. u.Host = net.JoinHostPort(host, port)
  104. }
  105. return u
  106. }
  107. // mtime stat's path and returns its modification time. If path does not exist,
  108. // it returns the unix epoch.
  109. func mtime(path string) time.Time {
  110. fi, err := os.Stat(path)
  111. if err != nil {
  112. return time.Unix(0, 0)
  113. }
  114. return fi.ModTime()
  115. }