feat: update Caddy config, add cache, improve response handling and logging
This commit is contained in:
parent
ef2f477fcd
commit
18828ca720
6 changed files with 162 additions and 96 deletions
191
proxy/main.go
191
proxy/main.go
|
@ -1,115 +1,144 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"bytes"
|
||||
"regexp"
|
||||
"strings"
|
||||
"compress/gzip"
|
||||
"net/url"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Rewrite: func(r *httputil.ProxyRequest) {
|
||||
host, _ := Yadro2Kernel(r.In.Host, true)
|
||||
url, err := url.Parse("https://" + host)
|
||||
|
||||
if err != nil {
|
||||
// TODO...
|
||||
}
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Rewrite: func(r *httputil.ProxyRequest) {
|
||||
host, _ := Yadro2Kernel(r.In.Host, true)
|
||||
url, _ := url.Parse("https://" + host)
|
||||
|
||||
r.SetURL(url)
|
||||
r.SetURL(url)
|
||||
|
||||
// We only support gzip decoding
|
||||
r.Out.Header.Set("Accept-Encoding", "gzip")
|
||||
},
|
||||
ModifyResponse: func(r *http.Response) error {
|
||||
// Disable security policy because of the domain restrictions.
|
||||
r.Header.Del("Content-Security-Policy")
|
||||
// We only support gzip decoding
|
||||
r.Out.Header.Set("Accept-Encoding", "gzip")
|
||||
},
|
||||
ModifyResponse: func(r *http.Response) error {
|
||||
// Disable security policy because of the domain restrictions.
|
||||
r.Header.Del("Content-Security-Policy")
|
||||
|
||||
// Skip non-html pages.
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "text/") {
|
||||
return nil
|
||||
}
|
||||
// Skip non-html pages.
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "text/") {
|
||||
return nil
|
||||
}
|
||||
r.Header.Set("Content-Type", contentType+";charset=utf-8")
|
||||
|
||||
// Read response body into data. If body is encoded, decode it.
|
||||
var data []byte
|
||||
// Read response body into data. If body is encoded, decode it.
|
||||
var data []byte
|
||||
|
||||
switch r.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
reader, _ := gzip.NewReader(r.Body)
|
||||
data, _ = io.ReadAll(reader)
|
||||
r.Body.Close()
|
||||
default:
|
||||
data, _ = io.ReadAll(r.Body)
|
||||
r.Body.Close()
|
||||
}
|
||||
switch r.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
reader, _ := gzip.NewReader(r.Body)
|
||||
data, _ = io.ReadAll(reader)
|
||||
r.Body.Close()
|
||||
default:
|
||||
data, _ = io.ReadAll(r.Body)
|
||||
r.Body.Close()
|
||||
}
|
||||
|
||||
// Modify the response.
|
||||
data = modifyResponse(data)
|
||||
// Rewrite 30x redirect location
|
||||
locHeader := r.Header.Get("Location")
|
||||
if locHeader != "" {
|
||||
re := regexp.MustCompile(`https?:\/\/[A-Za-z\-\.]*.?kernel\.org\/`)
|
||||
r.Header.Set("Location", string(re.ReplaceAllFunc([]byte(locHeader), func(original_raw []byte) []byte {
|
||||
kernel := strings.ToLower(string(original_raw))
|
||||
|
||||
// Remove headers that mess with body encoding and set the body.
|
||||
r.Header.Del("Content-Encoding")
|
||||
r.Header.Del("Content-Length")
|
||||
// r.Header.Set("Content-Type", "text/html; charset=windows-1251")
|
||||
kernel = strings.TrimPrefix(kernel, "https://")
|
||||
kernel = strings.TrimPrefix(kernel, "http://")
|
||||
kernel = strings.TrimPrefix(kernel, "www.")
|
||||
kernel = strings.TrimSuffix(kernel, "/")
|
||||
|
||||
r.Body = io.NopCloser(bytes.NewReader(data))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
yadro, exists := Kernel2Yadro(kernel)
|
||||
|
||||
http.ListenAndServe("localhost:8000", proxy)
|
||||
if !exists {
|
||||
fmt.Println("Missing:", kernel)
|
||||
return original_raw
|
||||
}
|
||||
|
||||
return []byte("http://" + yadro + "/") // TODO: https
|
||||
})))
|
||||
}
|
||||
|
||||
// Modify the response.
|
||||
data = modifyResponse(data)
|
||||
|
||||
// Remove headers that mess with body encoding and set the body.
|
||||
r.Header.Del("Content-Encoding")
|
||||
r.Header.Del("Content-Length")
|
||||
|
||||
r.Body = io.NopCloser(bytes.NewReader(data))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
http.ListenAndServe("0.0.0.0:80", proxy)
|
||||
}
|
||||
|
||||
func replaceDomains(response []byte) []byte {
|
||||
re := regexp.MustCompile(`(?i)[A-Za-z\-\.]*\.?kernel\.org`)
|
||||
response = re.ReplaceAllFunc(response, func(original_raw []byte) []byte {
|
||||
kernel := strings.ToLower(string(original_raw))
|
||||
re := regexp.MustCompile(`(?i)[A-Za-z\-\.]*\.?kernel\.org`)
|
||||
|
||||
// Strip `www.`
|
||||
kernel = strings.TrimPrefix(kernel, "www.")
|
||||
response = re.ReplaceAllFunc(response, func(original_raw []byte) []byte {
|
||||
kernel := strings.ToLower(string(original_raw))
|
||||
|
||||
yadro, exists := Kernel2Yadro(kernel)
|
||||
// Strip `www.`
|
||||
kernel = strings.TrimPrefix(kernel, "www.")
|
||||
|
||||
if !exists {
|
||||
return original_raw
|
||||
}
|
||||
yadro, exists := Kernel2Yadro(kernel)
|
||||
|
||||
return []byte(yadro)
|
||||
})
|
||||
if !exists {
|
||||
fmt.Println("Missing:", kernel)
|
||||
return original_raw
|
||||
}
|
||||
|
||||
return response
|
||||
return []byte(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 {
|
||||
enc := charmap.Windows1251.NewEncoder()
|
||||
cp1521, _ := enc.Bytes(response)
|
||||
// Don't try to translate empty body (30x, etc)
|
||||
if len(response) == 0 {
|
||||
return response
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest("POST", "http://localhost:2390/", bytes.NewReader(cp1521)) // TODO
|
||||
req, _ := http.NewRequest("POST", "http://caddy:9000/translate", bytes.NewReader(response))
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
fmt.Println(err)
|
||||
fmt.Println(resp.StatusCode)
|
||||
req.Header.Add("Content-Type", "text/html")
|
||||
|
||||
response, _ = io.ReadAll(resp.Body)
|
||||
dec, _ := charmap.Windows1251.NewDecoder().Bytes(response)
|
||||
// fmt.Println(len(dec))
|
||||
// fmt.Println(string(dec))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
resp.Body.Close()
|
||||
|
||||
return dec
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error in first", err)
|
||||
return []byte{0}
|
||||
}
|
||||
|
||||
response, _ = io.ReadAll(resp.Body)
|
||||
|
||||
resp.Body.Close()
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
func modifyResponse(response []byte) []byte {
|
||||
response = replaceDomains(response)
|
||||
response = translateWithPromtPuppies(response)
|
||||
return response
|
||||
response = translateWithPromtPuppies(response)
|
||||
response = replaceDomains(response)
|
||||
return response
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue