diff --git a/.githooks/commit-msg b/.githooks/commit-msg index e53acec..52e7a30 100755 --- a/.githooks/commit-msg +++ b/.githooks/commit-msg @@ -1,9 +1,15 @@ #!/bin/bash +format() { + sed -e 's/^fix:/доза:/' \ + -e '/^# ------------------------ >8 ------------------------$/,$d' \ + -e '/^#/d' +} + translated="$(curl -s \ http://localhost:9000/translate \ -H 'Content-Type: text/html' \ - --data-binary @- -o- < <(sed 's,^fix:,доза:,' < "$1") | \ + --data-binary @- -o- < <(format < "$1") | \ tail -n +2 | \ perl -pe 's,(.*?),\1,g')" diff --git a/caddy/Caddyfile b/caddy/Caddyfile index 23fbf70..3be3842 100644 --- a/caddy/Caddyfile +++ b/caddy/Caddyfile @@ -14,6 +14,10 @@ http://ядро.орг, http://*.ядро.орг { # we don't want to flood the upstream from the same IP mode bypass ttl 30m + + timeout { + backend 10m + } } import Caddyfile.yadro proxy:80 @@ -23,6 +27,12 @@ http://ядро.орг, http://*.ядро.орг { reverse_proxy { dynamic a puppy 80 lb_policy least_conn + + transport http { + response_header_timeout 10m + read_timeout 10m + write_timeout 10m + } } cache { @@ -34,7 +44,7 @@ http://ядро.орг, http://*.ядро.орг { } timeout { - backend 1m + backend 10m } } } diff --git a/caddy/Caddyfile.yadro b/caddy/Caddyfile.yadro index c8244f5..c8a7c6d 100644 --- a/caddy/Caddyfile.yadro +++ b/caddy/Caddyfile.yadro @@ -75,6 +75,6 @@ handle @surely_static { redir @known_host https://{kernel}{uri} permanent } -# TOOO: cache by path for 30 minutes +# TODO: cache by path for 30 minutes reverse_proxy {args[0]} diff --git a/docker-compose.yml b/docker-compose.yml index b4f7eaa..45428e3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,6 @@ services: puppies: - networks: - - promty + networks: [promty] build: puppy hostname: puppy volumes: @@ -14,18 +13,20 @@ services: order: start-first proxy: - networks: - - promty + networks: [promty] container_name: proxy build: proxy + depends_on: + puppies: + condition: service_healthy caddy: - networks: - - promty + networks: [promty] container_name: caddy build: caddy volumes: - ./caddy:/etc/caddy + depends_on: [proxy] ports: - 80:80 - 9000:9000 diff --git a/proxy/Dockerfile b/proxy/Dockerfile index 52f9945..5e46401 100644 --- a/proxy/Dockerfile +++ b/proxy/Dockerfile @@ -1,10 +1,15 @@ FROM golang:1.23-alpine AS builder + WORKDIR /build COPY . . -RUN --mount=type=cache,target=/go/pkg/mod go build + +RUN --mount=type=cache,target=/go/pkg/mod go build; + FROM alpine AS runner + WORKDIR /app COPY --from=builder /build/proxy . + EXPOSE 80/tcp CMD ./proxy diff --git a/proxy/main.go b/proxy/main.go index 8a05bbf..7d01449 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -68,7 +68,7 @@ func main() { return original_raw } - return []byte("http://" + yadro + "/") // TODO: https + return []byte("https://" + yadro + "/") }))) } @@ -88,11 +88,15 @@ func main() { http.ListenAndServe("0.0.0.0:80", proxy) } -func replaceDomains(response []byte) []byte { +func replaceDomains(response string) string { re := regexp.MustCompile(`(?i)[A-Za-z\-\.]*\.?kernel\.org`) + response = strings.NewReplacer( + "%3F", "?", + "%26", "&", + ).Replace(response) - response = re.ReplaceAllFunc(response, func(original_raw []byte) []byte { - kernel := strings.ToLower(string(original_raw)) + response = re.ReplaceAllStringFunc(response, func(original_raw string) string { + kernel := strings.ToLower(original_raw) // Strip `www.` kernel = strings.TrimPrefix(kernel, "www.") @@ -104,42 +108,46 @@ func replaceDomains(response []byte) []byte { return original_raw } - return []byte(yadro) + return yadro }) - response = bytes.ReplaceAll(response, []byte("%3F"), []byte("?")) - response = bytes.ReplaceAll(response, []byte("%26"), []byte("&")) - response = bytes.ReplaceAll(response, []byte("https"), []byte("http")) // TODO: TEMP - return response } -func translateWithPromtPuppies(response []byte) []byte { +func translateWithPromtPuppies(response string) string { // Don't try to translate empty body (30x, etc) if len(response) == 0 { return response } - req, _ := http.NewRequest("POST", "http://caddy:9000/translate", bytes.NewReader(response)) + req, _ := http.NewRequest("POST", "http://caddy:9000/translate", strings.NewReader(response)) req.Header.Add("Content-Type", "text/html") resp, err := http.DefaultClient.Do(req) if err != nil { - fmt.Fprintln(os.Stderr, "Error in first", err) - return []byte{0} + fmt.Fprintln(os.Stderr, "Error in while translating", err) + return "" } - response, _ = io.ReadAll(resp.Body) - + var translated strings.Builder + io.Copy(&translated, resp.Body) resp.Body.Close() - return response + return translated.String() +} + +func enfunnify(response string) string { + return strings.NewReplacer( + "tarball", "tar ball", + ).Replace(response) } func modifyResponse(response []byte) []byte { - response = translateWithPromtPuppies(response) - response = replaceDomains(response) - return response + s := string(response) + s = enfunnify(s) + s = translateWithPromtPuppies(s) + s = replaceDomains(s) + return []byte(s) } diff --git a/puppy/Dockerfile b/puppy/Dockerfile index 300c1d9..b235ad9 100644 --- a/puppy/Dockerfile +++ b/puppy/Dockerfile @@ -37,7 +37,7 @@ COPY build/ $WINEPREFIX/drive_c WORKDIR /app COPY --from=builder /build/promt-puppy.exe /build/healthcheck . -HEALTHCHECK CMD ./healthcheck; +HEALTHCHECK --start-period=30s --start-interval=1s CMD ./healthcheck; EXPOSE 80/tcp VOLUME /cache