diff --git a/internal/config/config.go b/internal/config/config.go index f96df4a..efc44c9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -62,6 +62,10 @@ type Uplink struct { // for all other queries. Address3 string `yaml:"address3,omitempty"` + // Address4 contains address of the uplink server in form IP:PORT + // for PNG queries. + Address4 string `yaml:"address4,omitempty"` + // Timeout for upstream queries. Timeout int `yaml:"timeout,omitempty"` @@ -152,6 +156,7 @@ func Default() *Config { Address1: "127.0.0.1:9002", Address2: "127.0.0.1:9002", Address3: "127.0.0.1:9002", + Address4: "127.0.0.1:9002", Timeout: 30, PrefetchInterval: 300, }, diff --git a/internal/processor/j1.go b/internal/processor/j1.go index f6afce7..bf23ff6 100644 --- a/internal/processor/j1.go +++ b/internal/processor/j1.go @@ -9,7 +9,7 @@ import ( "time" ) -func getAny(req *http.Request, tr1, tr2, tr3 *http.Transport) (*ResponseWithHeader, error) { +func getAny(req *http.Request, tr1, tr2, tr3, tr4 *http.Transport) (*ResponseWithHeader, error) { uri := strings.ReplaceAll(req.URL.RequestURI(), "%", "%25") u, err := url.Parse(uri) @@ -28,6 +28,10 @@ func getAny(req *http.Request, tr1, tr2, tr3 *http.Transport) (*ResponseWithHead // log.Println(req.URL.Query()) // log.Println() + if checkURLForPNG(req) { + return getDefault(req, tr4) + } + return getDefault(req, tr3) } @@ -87,3 +91,8 @@ func getUpstream(req *http.Request, transport *http.Transport) (*ResponseWithHea StatusCode: res.StatusCode, }, nil } + +func checkURLForPNG(r *http.Request) bool { + url := r.URL.String() + return strings.Contains(url, ".png") && !strings.Contains(url, "/files/") +} diff --git a/internal/processor/processor.go b/internal/processor/processor.go index e9bf886..9bc7e65 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -60,6 +60,7 @@ type RequestProcessor struct { upstreamTransport1 *http.Transport upstreamTransport2 *http.Transport upstreamTransport3 *http.Transport + upstreamTransport4 *http.Transport config *config.Config geoIPCache *geoip.Cache geoLocation *geoloc.Cache @@ -93,6 +94,11 @@ func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) { return dialer.DialContext(ctx, network, config.Uplink.Address3) }, } + transport4 := &http.Transport{ + DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { + return dialer.DialContext(ctx, network, config.Uplink.Address4) + }, + } geoCache, err := geoip.NewCache(config) if err != nil { @@ -110,6 +116,7 @@ func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) { upstreamTransport1: transport1, upstreamTransport2: transport2, upstreamTransport3: transport3, + upstreamTransport4: transport4, config: config, geoIPCache: geoCache, geoLocation: geoLocation, @@ -156,7 +163,7 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*ResponseWithHeader if dontCache(r) { rp.stats.Inc("uncached") - return getAny(r, rp.upstreamTransport1, rp.upstreamTransport2, rp.upstreamTransport3) + return getAny(r, rp.upstreamTransport1, rp.upstreamTransport2, rp.upstreamTransport3, rp.upstreamTransport4) } // processing cached request @@ -247,7 +254,7 @@ func (rp *RequestProcessor) processUncachedRequest(r *http.Request) (*ResponseWi rp.stats.Inc("geoip") } - response, err = getAny(r, rp.upstreamTransport1, rp.upstreamTransport2, rp.upstreamTransport3) + response, err = getAny(r, rp.upstreamTransport1, rp.upstreamTransport2, rp.upstreamTransport3, rp.upstreamTransport4) if err != nil { return nil, err }