diff --git a/conf/ezbookkeeping.ini b/conf/ezbookkeeping.ini index 34b40fc7..4b071fd0 100644 --- a/conf/ezbookkeeping.ini +++ b/conf/ezbookkeeping.ini @@ -165,6 +165,9 @@ map_provider = openstreetmap # Set to true to use the ezbookkeeping server to proxy map data requests, for "openstreetmap", "openstreetmap_humanitarian", "opentopomap", "opnvkarte", "cyclosm", "cartodb", "tomtom" or "custom" map_data_fetch_proxy = false +# Proxy to request original map data when map_data_fetch_proxy is set to true, supports "system" (use system proxy), "none" (do not use proxy), or proxy URL which starts with "http://", "https://" or "socks5://", default is "system" +proxy = system + # For "tomtom" only, TomTom map API key, please visit https://developer.tomtom.com/how-to-get-tomtom-api-key tomtom_map_api_key = @@ -215,5 +218,8 @@ data_source = euro_central_bank # Requesting exchange rates data timeout (0 - 4294967295 milliseconds), default is 10000 (10 seconds) request_timeout = 10000 +# Proxy to request exchange rates data, supports "system" (use system proxy), "none" (do not use proxy), or proxy URL which starts with "http://", "https://" or "socks5://", default is "system" +proxy = system + # Set to true skip tls verification when request exchange rates data skip_tls_verify = false diff --git a/pkg/api/exchange_rates.go b/pkg/api/exchange_rates.go index 1550c838..af7d4daa 100644 --- a/pkg/api/exchange_rates.go +++ b/pkg/api/exchange_rates.go @@ -13,6 +13,7 @@ import ( "github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/settings" + "github.com/mayswind/ezbookkeeping/pkg/utils" ) // ExchangeRatesApi represents exchange rate api @@ -34,6 +35,7 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.Context) (any, *err uid := c.GetCurrentUid() transport := http.DefaultTransport.(*http.Transport).Clone() + utils.SetProxyUrl(transport, settings.Container.Current.ExchangeRatesProxy) if settings.Container.Current.ExchangeRatesSkipTLSVerify { transport.TLSClientConfig = &tls.Config{ diff --git a/pkg/api/map_image_proxies.go b/pkg/api/map_image_proxies.go index 09e315c6..cacad318 100644 --- a/pkg/api/map_image_proxies.go +++ b/pkg/api/map_image_proxies.go @@ -9,6 +9,7 @@ import ( "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/settings" + "github.com/mayswind/ezbookkeeping/pkg/utils" ) const openStreetMapTileImageUrlFormat = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" // https://tile.openstreetmap.org/{z}/{x}/{y}.png @@ -73,6 +74,9 @@ func (p *MapImageProxy) MapTileImageProxyHandler(c *core.Context) (*httputil.Rev return nil, errs.ErrParameterInvalid } + transport := http.DefaultTransport.(*http.Transport).Clone() + utils.SetProxyUrl(transport, settings.Container.Current.MapProxy) + director := func(req *http.Request) { imageRawUrl := targetUrl imageRawUrl = strings.Replace(imageRawUrl, "{z}", zoomLevel, -1) @@ -86,5 +90,8 @@ func (p *MapImageProxy) MapTileImageProxyHandler(c *core.Context) (*httputil.Rev req.Host = imageUrl.Host } - return &httputil.ReverseProxy{Director: director}, nil + return &httputil.ReverseProxy{ + Transport: transport, + Director: director, + }, nil } diff --git a/pkg/settings/setting.go b/pkg/settings/setting.go index 48db9d7b..1ef684c4 100644 --- a/pkg/settings/setting.go +++ b/pkg/settings/setting.go @@ -222,6 +222,8 @@ type Config struct { // Map MapProvider string + EnableMapDataFetchProxy bool + MapProxy string TomTomMapAPIKey string GoogleMapAPIKey string BaiduMapAK string @@ -233,11 +235,11 @@ type Config struct { CustomMapTileServerMinZoomLevel uint8 CustomMapTileServerMaxZoomLevel uint8 CustomMapTileServerDefaultZoomLevel uint8 - EnableMapDataFetchProxy bool // Exchange Rates ExchangeRatesDataSource string ExchangeRatesRequestTimeout uint32 + ExchangeRatesProxy string ExchangeRatesSkipTLSVerify bool } @@ -559,6 +561,7 @@ func loadMapConfiguration(config *Config, configFile *ini.File, sectionName stri } config.EnableMapDataFetchProxy = getConfigItemBoolValue(configFile, sectionName, "map_data_fetch_proxy", false) + config.MapProxy = getConfigItemStringValue(configFile, sectionName, "proxy", "system") config.TomTomMapAPIKey = getConfigItemStringValue(configFile, sectionName, "tomtom_map_api_key") config.GoogleMapAPIKey = getConfigItemStringValue(configFile, sectionName, "google_map_api_key") config.BaiduMapAK = getConfigItemStringValue(configFile, sectionName, "baidu_map_ak") @@ -605,6 +608,7 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio return errs.ErrInvalidExchangeRatesDataSource } + config.ExchangeRatesProxy = getConfigItemStringValue(configFile, sectionName, "proxy", "system") config.ExchangeRatesRequestTimeout = getConfigItemUint32Value(configFile, sectionName, "request_timeout", defaultExchangeRatesDataRequestTimeout) config.ExchangeRatesSkipTLSVerify = getConfigItemBoolValue(configFile, sectionName, "skip_tls_verify", false) diff --git a/pkg/utils/http.go b/pkg/utils/http.go new file mode 100644 index 00000000..cc052aa6 --- /dev/null +++ b/pkg/utils/http.go @@ -0,0 +1,18 @@ +package utils + +import ( + "net/http" + "net/url" +) + +// SetProxyUrl sets proxy url to http transport according to specified proxy setting +func SetProxyUrl(transport *http.Transport, proxy string) { + if proxy == "none" { + transport.Proxy = nil + } else if proxy != "system" { + proxy, _ := url.Parse(proxy) + transport.Proxy = http.ProxyURL(proxy) + } else { + transport.Proxy = http.ProxyFromEnvironment + } +}