diff options
Diffstat (limited to 'phlox')
-rw-r--r-- | phlox/go.mod | 16 | ||||
-rw-r--r-- | phlox/go.sum | 20 | ||||
-rw-r--r-- | phlox/login.go | 119 | ||||
-rw-r--r-- | phlox/main.go | 178 |
4 files changed, 0 insertions, 333 deletions
diff --git a/phlox/go.mod b/phlox/go.mod deleted file mode 100644 index 6ea0cc1..0000000 --- a/phlox/go.mod +++ /dev/null @@ -1,16 +0,0 @@ -module sanine.net/git/phlox/phlox - -go 1.19 - -replace sanine.net/git/phlox/db => ../db - -require ( - github.com/sirupsen/logrus v1.9.0 - sanine.net/git/phlox/db v0.0.0-00010101000000-000000000000 -) - -require ( - github.com/mattn/go-sqlite3 v1.14.16 // indirect - golang.org/x/crypto v0.8.0 // indirect - golang.org/x/sys v0.7.0 // indirect -) diff --git a/phlox/go.sum b/phlox/go.sum deleted file mode 100644 index b99b4dc..0000000 --- a/phlox/go.sum +++ /dev/null @@ -1,20 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= -github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= -golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/phlox/login.go b/phlox/login.go deleted file mode 100644 index 9dd82f1..0000000 --- a/phlox/login.go +++ /dev/null @@ -1,119 +0,0 @@ -package main - -import ( - "fmt" - "strings" - "net/http" - "text/template" - log "github.com/sirupsen/logrus" - db "sanine.net/git/phlox/db" -) - - -func LoginUser(username, password string) (bool, db.Session, error) { - p := &P - auth, user, err := p.AuthenticateUser(username, password) - if err != nil { - return false, db.Session{}, err - } - if auth == false { - return false, db.Session{}, nil - } - // auth was successful! - session, err := p.CreateSession(user) - if err != nil { - return false, db.Session{}, err - } - return true, session, nil -} - - -func LoginPostHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - username := r.Form.Get("username") - password := strings.TrimSpace(r.Form.Get("password")) - log.Infof("username: %v\tpassword: '%v'", username, password) - redirect := r.Form.Get("redirect") - - auth, session, err := LoginUser(username, password) - if err != nil { - // respond with error page - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, "an error was encountered when processing the request") - log.Error(err) - return - } - - if auth == false { - // not allowed! - w.WriteHeader(http.StatusUnauthorized) - fmt.Fprintf(w, "bad username or password") - log.Errorf("failed login for %v", username) - return - } - - http.SetCookie(w, &http.Cookie{ - Name: "phlox-session-id", - Value: session.Id, - SameSite: http.SameSiteLaxMode, - }) - - w.Header().Add("Location", redirect) - w.WriteHeader(http.StatusTemporaryRedirect) -} - - -var page *template.Template - -type Page struct { - Title string - Body string -} - - -func InitLogin() { - var err error - page, err = template.New("").Parse(` -<!doctype html> -<html> - <head> - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <title>Login</title> - </head> - <body> - {{ .Body }} - </body> -</html> -`) - - if err != nil { - log.Fatal(err) - } - - http.HandleFunc("/login", func (w http.ResponseWriter, r *http.Request) { - if r.Method == "POST" { - LoginPostHandler(w, r) - } else { - LoginGetHandler(w, r) - } - }) -} - - -func LoginGetHandler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - page.Execute(w, Page{ - Title: "Login", - Body: ` - <form method="post"> - <label for="user">Username</label> - <input type="text" id="user" name="username"> - <br> - <label for="pass">Password</label> - <input type="text" id="pass" name="password"> - <br> - <input type="submit" value="Submit"> - `, - }) -} diff --git a/phlox/main.go b/phlox/main.go deleted file mode 100644 index da52654..0000000 --- a/phlox/main.go +++ /dev/null @@ -1,178 +0,0 @@ -package main - -import ( - "fmt" - "io" - "time" - "strings" - "errors" - "flag" - "net" - "net/http" - "net/url" - log "github.com/sirupsen/logrus" - db "sanine.net/git/phlox/db" -) - - -var P db.Phlox - - -func main() { - p := &P - var dbfile string - flag.StringVar(&dbfile, "db", "/etc/phlox/phlox.conf", "path to the configuration db") - flag.Parse() - - err := p.Open(dbfile) - if err != nil { - log.Fatal(err) - } - defer p.Close() - - addr, err := p.GetHostAddress() - if err != nil { - log.Fatal(err) - } - - endpoints, err := p.AllEndpoints() - if err != nil { - log.Fatal(err) - } - log.Info("configuring reverse proxy...") - for _, endpoint := range endpoints { - configureEndpoint(endpoint.Path, endpoint.Address) - } - - InitLogin() - - c := time.Tick(5 * time.Minute) - go (func() { - p := &P - for ;; { - _ = <-c // wait for 5 minutes - p.CleanSessions(time.Hour) - } - })() - - log.Infof("serving on %v", addr) - log.Fatal(http.ListenAndServe(addr, nil)) -} - - -type Endpoint struct { - Path string - Origin *url.URL -} - - -func configureEndpoint(path, address string) { - log.Infof("proxying endpoint %v to %v", path, address) - origin, err := url.Parse(address) - if err != nil { - log.Fatal(err) - } - - end := Endpoint{ - Path: path, - Origin: origin, - } - - http.HandleFunc(path + "/", func(w http.ResponseWriter, req *http.Request) { - log.Infof("REQ: %v", req.URL.Path) - proxy(w, req, end) - }) -} - - -func proxy(w http.ResponseWriter, req *http.Request, end Endpoint) { - p := &P - cookie, err := req.Cookie("phlox-session-id") - if errors.Is(err, http.ErrNoCookie) { - // not logged in - w.Header().Set("Location", "/login") - w.WriteHeader(http.StatusTemporaryRedirect) - return - } - - // check cookie - check, err := p.CheckSessionId(cookie.Value) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, "internal server error") - return - } - if !check { - // not logged in - w.Header().Set("Location", "/login") - w.WriteHeader(http.StatusTemporaryRedirect) - return - } - // update modified time - p.TouchSessionId(cookie.Value) - - response := proxyRequest(w, req, end) - if response != nil { - proxyResponse(w, response) - } -} - - -func proxyRequest(w http.ResponseWriter, req *http.Request, end Endpoint) *http.Response { - // configure host address - req.Host = end.Origin.Host - req.URL.Host = end.Origin.Host - - // strip proxy endpoint path from request path - req.URL.Path = strings.TrimPrefix(req.URL.Path, end.Path) - - // set X-Forwarded-For - forwardedFor, _, _ := net.SplitHostPort(req.RemoteAddr) - req.Header.Set("X-Forwarded-For", forwardedFor) - - // misc request cleanups - req.URL.Scheme = end.Origin.Scheme - req.RequestURI = "" - - // make request - response, err := http.DefaultClient.Do(req) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintf(w, "%v", err) - log.Error(err) - return nil - } - - return response -} - - -func proxyResponse(w http.ResponseWriter, response *http.Response) { - // copy header - for key, values := range response.Header { - for _, value := range values { - w.Header().Add(key, value) - } - } - - // get trailer keys - trailerKeys := []string{} - for key := range response.Trailer { - trailerKeys = append(trailerKeys, key) - } - if (len(trailerKeys) > 0) { - w.Header().Set("Trailer", strings.Join(trailerKeys, ",")) - } - - w.WriteHeader(response.StatusCode) - - // copy body to client - io.Copy(w, response.Body) - - // write trailers - for key, values := range response.Trailer { - for _, value := range values { - w.Header().Set(key, value) - } - } -} |