mirror of
https://github.com/simon987/Architeuthis.git
synced 2025-12-19 03:09:03 +00:00
Big refactor/rewrite: dynamic proxies, kill/revive proxies, all internal state stored in redis
This commit is contained in:
93
util.go
Normal file
93
util.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/ryanuber/go-glob"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func normalizeHost(host string) string {
|
||||
|
||||
col := strings.LastIndex(host, ":")
|
||||
if col > 0 {
|
||||
host = host[:col]
|
||||
}
|
||||
|
||||
return "." + host
|
||||
}
|
||||
|
||||
func parseOptions(header *http.Header) RequestOptions {
|
||||
|
||||
opts := RequestOptions{}
|
||||
|
||||
cfParam := header.Get("X-Architeuthis-CF-Bypass")
|
||||
if cfParam != "" {
|
||||
header.Del("X-Architeuthis-CF-Bypass")
|
||||
opts.DoCloudflareBypass = true
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
func getConfigsMatchingHost(sHost string) []*HostConfig {
|
||||
|
||||
configs := make([]*HostConfig, 0)
|
||||
|
||||
for _, conf := range config.Hosts {
|
||||
if glob.Glob(conf.Host, sHost) {
|
||||
configs = append(configs, conf)
|
||||
}
|
||||
}
|
||||
|
||||
return configs
|
||||
}
|
||||
|
||||
func applyHeaders(r *http.Request, configs []*HostConfig) *http.Request {
|
||||
|
||||
for _, conf := range configs {
|
||||
for k, v := range conf.Headers {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func computeRules(requestCtx *RequestCtx, responseCtx ResponseCtx) (dontRetry, forceRetry bool, shouldRetry bool) {
|
||||
dontRetry = false
|
||||
forceRetry = false
|
||||
shouldRetry = false
|
||||
|
||||
for _, conf := range requestCtx.configs {
|
||||
for _, rule := range conf.Rules {
|
||||
if rule.Matches(&responseCtx) {
|
||||
switch rule.Action {
|
||||
case DontRetry:
|
||||
dontRetry = true
|
||||
case ForceRetry:
|
||||
forceRetry = true
|
||||
case ShouldRetry:
|
||||
shouldRetry = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func cloneRequest(r *http.Request) *http.Request {
|
||||
|
||||
proxyReq := &http.Request{
|
||||
Method: r.Method,
|
||||
URL: r.URL,
|
||||
Proto: "HTTP/1.1",
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
Header: r.Header,
|
||||
Body: r.Body,
|
||||
Host: r.URL.Host,
|
||||
}
|
||||
|
||||
return proxyReq
|
||||
}
|
||||
Reference in New Issue
Block a user