From d923430e964622544c7583a10c202ffb9b82db21 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Mon, 10 Feb 2025 23:28:51 +0100 Subject: [PATCH 1/4] Reenable PNG queries --- srv.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/srv.go b/srv.go index b07cbf9..94d2713 100644 --- a/srv.go +++ b/srv.go @@ -6,7 +6,6 @@ import ( "io" stdlog "log" "net/http" - "strings" "time" "github.com/alecthomas/kong" @@ -156,12 +155,6 @@ func mainHandler( log.Println(err) } - if checkURLForPNG(r) { - w.Write([]byte("PNG support temporary disabled")) - - return - } - response, err := rp.ProcessRequest(r) if err != nil { log.Println(err) @@ -259,8 +252,3 @@ func setLogLevel(logLevel string) error { return nil } - -func checkURLForPNG(r *http.Request) bool { - url := r.URL.String() - return strings.Contains(url, ".png") && !strings.Contains(url, "/files/") -} From 399d0c889097e2efb7f493beecd7d4ee0416762e Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Mon, 10 Feb 2025 23:35:28 +0100 Subject: [PATCH 2/4] Add an upstream channel for PNG queries --- internal/config/config.go | 5 +++++ internal/processor/j1.go | 11 ++++++++++- internal/processor/processor.go | 11 +++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) 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 } From 0a8a069ea1e371ae7f4955c2a58a54112ecc9daf Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Mon, 10 Feb 2025 23:35:51 +0100 Subject: [PATCH 3/4] Add a dedicated process for PNG processing --- config/services/services.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/services/services.yaml b/config/services/services.yaml index b9b7c8a..59affe7 100644 --- a/config/services/services.yaml +++ b/config/services/services.yaml @@ -30,3 +30,8 @@ services: command: "WTTRIN_SRV_PORT=9005 ve/bin/python3 bin/srv.py" workdir: "/wttr.in/wttr.in-v2-v2" port: 9005 + + - name: "filetype=png" + command: "WTTRIN_SRV_PORT=9006 ve/bin/python3 bin/srv.py" + workdir: "/wttr.in/wttr.in-v2-v2" + port: 9006 From 70554f498f303601e623c6f7d82d724aeb9ea23a Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Tue, 11 Feb 2025 21:42:26 +0100 Subject: [PATCH 4/4] Increase caching interval --- bin/proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/proxy.py b/bin/proxy.py index 4e90e1b..5dea3ce 100755 --- a/bin/proxy.py +++ b/bin/proxy.py @@ -110,7 +110,7 @@ def _cache_file(path, query): digest = hashlib.sha1(("%s %s" % (path, query)).encode("utf-8")).hexdigest() digest_number = ord(digest[0].upper()) - expiry_interval = 60 * (digest_number + 180) + expiry_interval = 60 * (digest_number + 300) timestamp = "%010d" % (int(time.time()) // expiry_interval * expiry_interval) filename = os.path.join(PROXY_CACHEDIR, timestamp, path, query)