change conf to argsset and flag for oauth

This commit is contained in:
bel
2019-10-22 04:52:04 +00:00
parent e20ba5361d
commit bc11dd7f82
10 changed files with 398 additions and 250 deletions

View File

@@ -5,7 +5,9 @@ import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"local/oauth2/oauth2client"
"local/rproxy3/config"
"local/rproxy3/storage"
"local/rproxy3/storage/packable"
@@ -20,6 +22,7 @@ import (
)
const nsRouting = "routing"
const nsBOAuthZ = "oauth"
type listenerScheme int
@@ -49,12 +52,13 @@ type Server struct {
limiter *rate.Limiter
}
func (s *Server) Route(src, dst string) error {
log.Printf("Adding route %q -> %q...\n", src, dst)
u, err := url.Parse(dst)
func (s *Server) Route(src string, dst config.Proxy) error {
log.Printf("Adding route %q -> %v...\n", src, dst)
u, err := url.Parse(dst.To)
if err != nil {
return err
}
s.db.Set(nsBOAuthZ, src, packable.NewString(fmt.Sprint(dst.BOAuthZ)))
return s.db.Set(nsRouting, src, packable.NewURL(u))
}
@@ -103,7 +107,6 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rusr, rpwd, ok := config.GetAuth()
if ok {
//usr, pwd := getProxyAuth(r)
usr, pwd, ok := r.BasicAuth()
if !ok || rusr != usr || rpwd != pwd {
w.WriteHeader(http.StatusUnauthorized)
@@ -111,6 +114,17 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
return
}
}
ok, err := s.lookupBOAuthZ(mapKey(r.Host))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if boauthz, useoauth := config.GetBOAuthZ(); ok && useoauth {
err := oauth2client.Authenticate(boauthz, w, r)
if err != nil {
return
}
}
foo(w, r)
}
}