mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
support setting proxy to request exchange rates or map data
This commit is contained in:
@@ -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"
|
# 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
|
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
|
# For "tomtom" only, TomTom map API key, please visit https://developer.tomtom.com/how-to-get-tomtom-api-key
|
||||||
tomtom_map_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)
|
# Requesting exchange rates data timeout (0 - 4294967295 milliseconds), default is 10000 (10 seconds)
|
||||||
request_timeout = 10000
|
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
|
# Set to true skip tls verification when request exchange rates data
|
||||||
skip_tls_verify = false
|
skip_tls_verify = false
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||||
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
||||||
|
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExchangeRatesApi represents exchange rate api
|
// ExchangeRatesApi represents exchange rate api
|
||||||
@@ -34,6 +35,7 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.Context) (any, *err
|
|||||||
uid := c.GetCurrentUid()
|
uid := c.GetCurrentUid()
|
||||||
|
|
||||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
utils.SetProxyUrl(transport, settings.Container.Current.ExchangeRatesProxy)
|
||||||
|
|
||||||
if settings.Container.Current.ExchangeRatesSkipTLSVerify {
|
if settings.Container.Current.ExchangeRatesSkipTLSVerify {
|
||||||
transport.TLSClientConfig = &tls.Config{
|
transport.TLSClientConfig = &tls.Config{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||||
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
"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
|
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
|
return nil, errs.ErrParameterInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
utils.SetProxyUrl(transport, settings.Container.Current.MapProxy)
|
||||||
|
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
imageRawUrl := targetUrl
|
imageRawUrl := targetUrl
|
||||||
imageRawUrl = strings.Replace(imageRawUrl, "{z}", zoomLevel, -1)
|
imageRawUrl = strings.Replace(imageRawUrl, "{z}", zoomLevel, -1)
|
||||||
@@ -86,5 +90,8 @@ func (p *MapImageProxy) MapTileImageProxyHandler(c *core.Context) (*httputil.Rev
|
|||||||
req.Host = imageUrl.Host
|
req.Host = imageUrl.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
return &httputil.ReverseProxy{Director: director}, nil
|
return &httputil.ReverseProxy{
|
||||||
|
Transport: transport,
|
||||||
|
Director: director,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,6 +222,8 @@ type Config struct {
|
|||||||
|
|
||||||
// Map
|
// Map
|
||||||
MapProvider string
|
MapProvider string
|
||||||
|
EnableMapDataFetchProxy bool
|
||||||
|
MapProxy string
|
||||||
TomTomMapAPIKey string
|
TomTomMapAPIKey string
|
||||||
GoogleMapAPIKey string
|
GoogleMapAPIKey string
|
||||||
BaiduMapAK string
|
BaiduMapAK string
|
||||||
@@ -233,11 +235,11 @@ type Config struct {
|
|||||||
CustomMapTileServerMinZoomLevel uint8
|
CustomMapTileServerMinZoomLevel uint8
|
||||||
CustomMapTileServerMaxZoomLevel uint8
|
CustomMapTileServerMaxZoomLevel uint8
|
||||||
CustomMapTileServerDefaultZoomLevel uint8
|
CustomMapTileServerDefaultZoomLevel uint8
|
||||||
EnableMapDataFetchProxy bool
|
|
||||||
|
|
||||||
// Exchange Rates
|
// Exchange Rates
|
||||||
ExchangeRatesDataSource string
|
ExchangeRatesDataSource string
|
||||||
ExchangeRatesRequestTimeout uint32
|
ExchangeRatesRequestTimeout uint32
|
||||||
|
ExchangeRatesProxy string
|
||||||
ExchangeRatesSkipTLSVerify bool
|
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.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.TomTomMapAPIKey = getConfigItemStringValue(configFile, sectionName, "tomtom_map_api_key")
|
||||||
config.GoogleMapAPIKey = getConfigItemStringValue(configFile, sectionName, "google_map_api_key")
|
config.GoogleMapAPIKey = getConfigItemStringValue(configFile, sectionName, "google_map_api_key")
|
||||||
config.BaiduMapAK = getConfigItemStringValue(configFile, sectionName, "baidu_map_ak")
|
config.BaiduMapAK = getConfigItemStringValue(configFile, sectionName, "baidu_map_ak")
|
||||||
@@ -605,6 +608,7 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio
|
|||||||
return errs.ErrInvalidExchangeRatesDataSource
|
return errs.ErrInvalidExchangeRatesDataSource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.ExchangeRatesProxy = getConfigItemStringValue(configFile, sectionName, "proxy", "system")
|
||||||
config.ExchangeRatesRequestTimeout = getConfigItemUint32Value(configFile, sectionName, "request_timeout", defaultExchangeRatesDataRequestTimeout)
|
config.ExchangeRatesRequestTimeout = getConfigItemUint32Value(configFile, sectionName, "request_timeout", defaultExchangeRatesDataRequestTimeout)
|
||||||
config.ExchangeRatesSkipTLSVerify = getConfigItemBoolValue(configFile, sectionName, "skip_tls_verify", false)
|
config.ExchangeRatesSkipTLSVerify = getConfigItemBoolValue(configFile, sectionName, "skip_tls_verify", false)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user