хозяйственная работа: некоторый младший щипает здесь и там
This commit is contained in:
parent
1d4bb2a118
commit
7f7d10aeee
7 changed files with 60 additions and 30 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue