2022-04-19 23:33:29 +01:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
package tshttpproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/util/lineread"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These vars are overridden for tests.
|
|
|
|
var (
|
|
|
|
synologyProxyConfigPath = "/etc/proxy.conf"
|
|
|
|
|
|
|
|
openSynologyProxyConf = func() (io.ReadCloser, error) {
|
|
|
|
return os.Open(synologyProxyConfigPath)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var cache struct {
|
|
|
|
sync.Mutex
|
2022-04-28 19:34:36 +01:00
|
|
|
httpProxy *url.URL
|
|
|
|
httpsProxy *url.URL
|
|
|
|
updated time.Time
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func synologyProxyFromConfigCached(req *http.Request) (*url.URL, error) {
|
|
|
|
if req.URL == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.Lock()
|
|
|
|
defer cache.Unlock()
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
var err error
|
2022-04-19 23:33:29 +01:00
|
|
|
modtime := mtime(synologyProxyConfigPath)
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
if modtime != cache.updated {
|
|
|
|
cache.httpProxy, cache.httpsProxy, err = synologyProxiesFromConfig()
|
|
|
|
cache.updated = modtime
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
return cache.httpsProxy, err
|
|
|
|
}
|
|
|
|
return cache.httpProxy, err
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
func synologyProxiesFromConfig() (*url.URL, *url.URL, error) {
|
2022-04-19 23:33:29 +01:00
|
|
|
r, err := openSynologyProxyConf()
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2022-04-28 19:34:36 +01:00
|
|
|
return nil, nil, nil
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
2022-04-28 19:34:36 +01:00
|
|
|
return nil, nil, err
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
return parseSynologyConfig(r)
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
// parseSynologyConfig parses the Synology proxy configuration, and returns any
|
|
|
|
// http proxy, and any https proxy respectively, or an error if parsing fails.
|
|
|
|
func parseSynologyConfig(r io.Reader) (*url.URL, *url.URL, error) {
|
2022-04-19 23:33:29 +01:00
|
|
|
cfg := map[string]string{}
|
|
|
|
|
|
|
|
if err := lineread.Reader(r, func(line []byte) error {
|
|
|
|
// accept and skip over empty lines
|
|
|
|
line = bytes.TrimSpace(line)
|
|
|
|
if len(line) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value, ok := strings.Cut(string(line), "=")
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing \"=\" in proxy.conf line: %q", line)
|
|
|
|
}
|
|
|
|
cfg[string(key)] = string(value)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2022-04-28 19:34:36 +01:00
|
|
|
return nil, nil, err
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg["proxy_enabled"] != "yes" {
|
2022-04-28 19:34:36 +01:00
|
|
|
return nil, nil, nil
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
httpProxyURL := new(url.URL)
|
|
|
|
httpsProxyURL := new(url.URL)
|
2022-04-19 23:33:29 +01:00
|
|
|
if cfg["auth_enabled"] == "yes" {
|
2022-04-28 19:34:36 +01:00
|
|
|
httpProxyURL.User = url.UserPassword(cfg["proxy_user"], cfg["proxy_pwd"])
|
|
|
|
httpsProxyURL.User = url.UserPassword(cfg["proxy_user"], cfg["proxy_pwd"])
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
// As far as we are aware, synology does not support tls proxies.
|
|
|
|
httpProxyURL.Scheme = "http"
|
|
|
|
httpsProxyURL.Scheme = "http"
|
2022-04-19 23:33:29 +01:00
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
httpsProxyURL = addHostPort(httpsProxyURL, cfg["https_host"], cfg["https_port"])
|
|
|
|
httpProxyURL = addHostPort(httpProxyURL, cfg["http_host"], cfg["http_port"])
|
|
|
|
|
|
|
|
return httpProxyURL, httpsProxyURL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addHostPort adds to u the given host and port and returns the updated url, or
|
|
|
|
// if host is empty, it returns nil.
|
|
|
|
func addHostPort(u *url.URL, host, port string) *url.URL {
|
2022-04-19 23:33:29 +01:00
|
|
|
if host == "" {
|
2022-04-28 19:34:36 +01:00
|
|
|
return nil
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:34:36 +01:00
|
|
|
if port == "" {
|
|
|
|
u.Host = host
|
2022-04-19 23:33:29 +01:00
|
|
|
} else {
|
2022-04-28 19:34:36 +01:00
|
|
|
u.Host = net.JoinHostPort(host, port)
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
2022-04-28 19:34:36 +01:00
|
|
|
return u
|
2022-04-19 23:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-26 16:12:45 +01:00
|
|
|
// mtime stat's path and returns its modification time. If path does not exist,
|
2022-04-19 23:33:29 +01:00
|
|
|
// it returns the unix epoch.
|
|
|
|
func mtime(path string) time.Time {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
return fi.ModTime()
|
|
|
|
}
|