Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38f19408c2 | ||
|
|
f28211e722 | ||
|
|
ef3abbbf07 | ||
|
|
af240639cb | ||
|
|
c623792c2f | ||
|
|
cebb518e05 | ||
|
|
177e0d88da | ||
|
|
9b0bccd9ca | ||
|
|
1af274dc1d | ||
|
|
ec1e0cdf2e |
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -24,6 +25,11 @@ func parseProxy(s string) (string, Proxy) {
|
|||||||
return key, p
|
return key, p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAuthelia() (string, bool) {
|
||||||
|
authelia := conf.Get("authelia").GetString()
|
||||||
|
return authelia, authelia != ""
|
||||||
|
}
|
||||||
|
|
||||||
func GetBOAuthZ() (string, bool) {
|
func GetBOAuthZ() (string, bool) {
|
||||||
boauthz := conf.Get("oauth").GetString()
|
boauthz := conf.Get("oauth").GetString()
|
||||||
return boauthz, boauthz != ""
|
return boauthz, boauthz != ""
|
||||||
@@ -35,6 +41,10 @@ func GetAuth() (string, string, bool) {
|
|||||||
return user, pass, user != "" && pass != ""
|
return user, pass, user != "" && pass != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTrim() string {
|
||||||
|
return conf.Get("trim").GetString()
|
||||||
|
}
|
||||||
|
|
||||||
func GetPort() string {
|
func GetPort() string {
|
||||||
port := conf.Get("p").GetInt()
|
port := conf.Get("p").GetInt()
|
||||||
return ":" + fmt.Sprint(port)
|
return ":" + fmt.Sprint(port)
|
||||||
@@ -78,3 +88,27 @@ func GetTimeout() time.Duration {
|
|||||||
timeout := conf.Get("timeout").GetDuration()
|
timeout := conf.Get("timeout").GetDuration()
|
||||||
return timeout
|
return timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCORS(key string) bool {
|
||||||
|
cors := conf.GetString("cors")
|
||||||
|
var m map[string]bool
|
||||||
|
if err := json.Unmarshal([]byte(cors), &m); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := m[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNoPath(key string) bool {
|
||||||
|
nopath := conf.GetString("nopath")
|
||||||
|
var m map[string]bool
|
||||||
|
if err := json.Unmarshal([]byte(nopath), &m); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := m[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCompression() bool {
|
||||||
|
return conf.GetBool("compression")
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/args"
|
"local/args"
|
||||||
|
"local/logb"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -26,6 +27,7 @@ func Refresh() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
conf = as
|
conf = as
|
||||||
|
logb.Set(logb.LevelFromString(as.GetString("level")))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,12 +44,18 @@ func parseArgs() (*args.ArgSet, error) {
|
|||||||
as.Append(args.INT, "ap", "alt port for always http service", 51556)
|
as.Append(args.INT, "ap", "alt port for always http service", 51556)
|
||||||
as.Append(args.INT, "r", "rate per second for requests", 100)
|
as.Append(args.INT, "r", "rate per second for requests", 100)
|
||||||
as.Append(args.INT, "b", "burst requests", 100)
|
as.Append(args.INT, "b", "burst requests", 100)
|
||||||
|
as.Append(args.BOOL, "compress", "enable compression", true)
|
||||||
as.Append(args.STRING, "crt", "path to crt for ssl", "")
|
as.Append(args.STRING, "crt", "path to crt for ssl", "")
|
||||||
as.Append(args.STRING, "key", "path to key for ssl", "")
|
as.Append(args.STRING, "key", "path to key for ssl", "")
|
||||||
|
as.Append(args.STRING, "trim", "path prefix to trim, like '/abc' to change '/abc/def' to '/def'", "")
|
||||||
as.Append(args.STRING, "tcp", "address for tcp only tunnel", "")
|
as.Append(args.STRING, "tcp", "address for tcp only tunnel", "")
|
||||||
as.Append(args.DURATION, "timeout", "timeout for tunnel", time.Minute)
|
as.Append(args.DURATION, "timeout", "timeout for tunnel", time.Minute)
|
||||||
as.Append(args.STRING, "proxy", "double-comma separated (+ if oauth)from,scheme://to.tld:port,oauth,,", "")
|
as.Append(args.STRING, "proxy", "double-comma separated (+ if auth)from,scheme://to.tld:port,,", "")
|
||||||
as.Append(args.STRING, "oauth", "url for boauthz", "")
|
as.Append(args.STRING, "oauth", "url for boauthz", "")
|
||||||
|
as.Append(args.STRING, "authelia", "url for authelia", "")
|
||||||
|
as.Append(args.STRING, "cors", "json dict key:true for keys to set CORS permissive headers, like {\"from\":true}", "{}")
|
||||||
|
as.Append(args.STRING, "nopath", "json dict key:true for keys to remove all path info from forwarded request, like -cors", "{}")
|
||||||
|
as.Append(args.STRING, "level", "log level", "info")
|
||||||
|
|
||||||
err := as.Parse()
|
err := as.Parse()
|
||||||
return as, err
|
return as, err
|
||||||
|
|||||||
@@ -11,10 +11,13 @@ func New() *Server {
|
|||||||
port := config.GetPort()
|
port := config.GetPort()
|
||||||
altport := config.GetAltPort()
|
altport := config.GetAltPort()
|
||||||
r, b := config.GetRate()
|
r, b := config.GetRate()
|
||||||
return &Server{
|
server := &Server{
|
||||||
db: storage.NewMap(),
|
db: storage.NewMap(),
|
||||||
addr: port,
|
addr: port,
|
||||||
altaddr: altport,
|
altaddr: altport,
|
||||||
limiter: rate.NewLimiter(rate.Limit(r), b),
|
limiter: rate.NewLimiter(rate.Limit(r), b),
|
||||||
}
|
}
|
||||||
|
_, server.auth.BOAuthZ = config.GetBOAuthZ()
|
||||||
|
_, server.auth.Authelia = config.GetAuthelia()
|
||||||
|
return server
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"io"
|
"io"
|
||||||
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -25,6 +26,7 @@ type rewrite struct {
|
|||||||
|
|
||||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||||
newURL, err := s.lookup(mapKey(r.Host))
|
newURL, err := s.lookup(mapKey(r.Host))
|
||||||
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, config.GetTrim())
|
||||||
var transport http.RoundTripper
|
var transport http.RoundTripper
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
transport = &redirPurge{
|
transport = &redirPurge{
|
||||||
@@ -37,7 +39,7 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Printf("unknown host lookup %q", r.Host)
|
log.Printf("unknown host lookup %q", r.Host)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Host = newURL.Host
|
//r.Host = newURL.Host
|
||||||
proxy := httputil.NewSingleHostReverseProxy(newURL)
|
proxy := httputil.NewSingleHostReverseProxy(newURL)
|
||||||
proxy.Transport = transport
|
proxy.Transport = transport
|
||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
@@ -49,7 +51,7 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
|||||||
return v.URL(), err
|
return v.URL(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) lookupBOAuthZ(host string) (bool, error) {
|
func (s *Server) lookupAuth(host string) (bool, error) {
|
||||||
v := packable.NewString()
|
v := packable.NewString()
|
||||||
err := s.db.Get(nsBOAuthZ, host, v)
|
err := s.db.Get(nsBOAuthZ, host, v)
|
||||||
return v.String() == "true", err
|
return v.String() == "true", err
|
||||||
@@ -69,6 +71,8 @@ func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
|||||||
if loc := resp.Header.Get("Location"); loc != "" {
|
if loc := resp.Header.Get("Location"); loc != "" {
|
||||||
resp.Header.Set("Location", strings.Replace(loc, rp.targetHost, rp.proxyHost, 1))
|
resp.Header.Set("Location", strings.Replace(loc, rp.targetHost, rp.proxyHost, 1))
|
||||||
}
|
}
|
||||||
|
// google floc https://paramdeo.com/blog/opting-your-website-out-of-googles-floc-network
|
||||||
|
resp.Header.Set("Permissions-Policy", "interest-cohort=()")
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
151
server/server.go
151
server/server.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"local/logb"
|
||||||
"local/oauth2/oauth2client"
|
"local/oauth2/oauth2client"
|
||||||
"local/rproxy3/config"
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage"
|
"local/rproxy3/storage"
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -51,6 +53,10 @@ type Server struct {
|
|||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
limiter *rate.Limiter
|
limiter *rate.Limiter
|
||||||
|
auth struct {
|
||||||
|
BOAuthZ bool
|
||||||
|
Authelia bool
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Route(src string, dst config.Proxy) error {
|
func (s *Server) Route(src string, dst config.Proxy) error {
|
||||||
@@ -98,8 +104,106 @@ func (s *Server) Run() error {
|
|||||||
return errors.New("did not load server")
|
return errors.New("did not load server")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
func (s *Server) doAuthelia(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
authelia, ok := config.GetAuthelia()
|
||||||
|
if !ok {
|
||||||
|
panic("howd i get here")
|
||||||
|
}
|
||||||
|
url, err := url.Parse(authelia)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("bad config for authelia url: %v", err))
|
||||||
|
}
|
||||||
|
url.Path = "/api/verify"
|
||||||
|
logb.Verbosef("authelia @ %s", url.String())
|
||||||
|
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
r2 := r.Clone(r.Context())
|
||||||
|
if r2.URL.Host == "" {
|
||||||
|
r2.URL.Host = r2.Host
|
||||||
|
}
|
||||||
|
if r2.URL.Scheme == "" {
|
||||||
|
r2.URL.Scheme = "https"
|
||||||
|
}
|
||||||
|
for _, httpreq := range []*http.Request{r, req} {
|
||||||
|
for k, v := range map[string]string{
|
||||||
|
"X-Original-Url": r2.URL.String(),
|
||||||
|
"X-Forwarded-Proto": r2.URL.Scheme,
|
||||||
|
"X-Forwarded-Host": r2.URL.Host,
|
||||||
|
"X-Forwarded-Uri": r2.URL.String(),
|
||||||
|
} {
|
||||||
|
if _, ok := httpreq.Header[k]; !ok {
|
||||||
|
logb.Verbosef("authelia header setting %s:%s", k, v)
|
||||||
|
httpreq.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cookie, err := r.Cookie("authelia_session"); err == nil {
|
||||||
|
logb.Verbosef("authelia session found in cookies; %+v", cookie)
|
||||||
|
req.AddCookie(cookie)
|
||||||
|
}
|
||||||
|
c := &http.Client{
|
||||||
|
Timeout: time.Minute,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
autheliaKey := mapKey(req.Host)
|
||||||
|
logb.Verbosef("request to %s is authelia %s? %v", r.Host, autheliaKey, strings.HasPrefix(r.Host, autheliaKey))
|
||||||
|
if strings.HasPrefix(r.Host, autheliaKey) {
|
||||||
|
logb.Debugf("no authelia for %s because it has prefix %s", r.Host, autheliaKey)
|
||||||
|
foo(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logb.Debugf(
|
||||||
|
"authelia: %+v, %+v \n\t-> \n\t(%d) %+v, %+v",
|
||||||
|
req,
|
||||||
|
req.Cookies(),
|
||||||
|
resp.StatusCode,
|
||||||
|
resp.Header,
|
||||||
|
resp.Cookies(),
|
||||||
|
)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
for k := range resp.Header {
|
||||||
|
if strings.HasPrefix(k, "Remote-") {
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: k,
|
||||||
|
Value: resp.Header.Get(k),
|
||||||
|
Path: "/",
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
Expires: time.Now().Add(24 * time.Hour * 30),
|
||||||
|
}
|
||||||
|
logb.Verbosef("setting authelia cookie in response: %+v", cookie)
|
||||||
|
http.SetCookie(w, cookie)
|
||||||
|
logb.Verbosef("setting authelia cookie in request: %+v", cookie)
|
||||||
|
r.AddCookie(cookie)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foo(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url.Path = ""
|
||||||
|
q := url.Query()
|
||||||
|
q.Set("rd", r2.URL.String())
|
||||||
|
url.RawQuery = q.Encode()
|
||||||
|
logb.Verbosef("authelia status %d, rd'ing %s", resp.StatusCode, url.String())
|
||||||
|
http.Redirect(w, r, url.String(), http.StatusFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) doBOAuthZ(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
key := mapKey(r.Host)
|
||||||
rusr, rpwd, ok := config.GetAuth()
|
rusr, rpwd, ok := config.GetAuth()
|
||||||
if ok {
|
if ok {
|
||||||
usr, pwd, ok := r.BasicAuth()
|
usr, pwd, ok := r.BasicAuth()
|
||||||
@@ -109,8 +213,7 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
key := mapKey(r.Host)
|
ok, err := s.lookupAuth(key)
|
||||||
ok, err := s.lookupBOAuthZ(key)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -121,6 +224,9 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if config.GetNoPath(key) && path.Ext(r.URL.Path) == "" {
|
||||||
|
r.URL.Path = "/"
|
||||||
|
}
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,7 +269,19 @@ func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
w.WriteHeader(http.StatusTooManyRequests)
|
w.WriteHeader(http.StatusTooManyRequests)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.doAuth(foo)(w, r)
|
w, did := s.doCORS(w, r)
|
||||||
|
if did {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.auth.BOAuthZ {
|
||||||
|
logb.Verbosef("doing boauthz for request to %s", r.URL.String())
|
||||||
|
s.doBOAuthZ(foo)(w, r)
|
||||||
|
} else if s.auth.Authelia {
|
||||||
|
logb.Verbosef("doing authelia for request to %s", r.URL.String())
|
||||||
|
s.doAuthelia(foo)(w, r)
|
||||||
|
} else {
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,6 +289,31 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
s.Pre(s.Proxy)(w, r)
|
s.Pre(s.Proxy)(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type corsResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb corsResponseWriter) WriteHeader(code int) {
|
||||||
|
cb.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
cb.Header().Set("Access-Control-Allow-Headers", "X-Auth-Token, content-type, Content-Type")
|
||||||
|
cb.ResponseWriter.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) doCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, bool) {
|
||||||
|
key := mapKey(r.Host)
|
||||||
|
if !config.GetCORS(key) {
|
||||||
|
return w, false
|
||||||
|
}
|
||||||
|
w = corsResponseWriter{ResponseWriter: w}
|
||||||
|
if r.Method != "OPTIONS" {
|
||||||
|
return w, false
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Length", "0")
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, TRACE, PATCH, HEAD, DELETE")
|
||||||
|
return w, true
|
||||||
|
}
|
||||||
|
|
||||||
func getProxyAuth(r *http.Request) (string, string) {
|
func getProxyAuth(r *http.Request) (string, string) {
|
||||||
proxyAuthHeader := r.Header.Get("Proxy-Authorization")
|
proxyAuthHeader := r.Header.Get("Proxy-Authorization")
|
||||||
proxyAuthB64 := strings.TrimPrefix(proxyAuthHeader, "Basic ")
|
proxyAuthB64 := strings.TrimPrefix(proxyAuthHeader, "Basic ")
|
||||||
|
|||||||
Reference in New Issue
Block a user