Skip to content

Commit

Permalink
fix: pgs tunnels
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 13, 2024
1 parent dcfb7ac commit 7372600
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions pgs/tunnel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgs

import (
"context"
"net/http"
"strings"

Expand All @@ -11,12 +12,26 @@ import (

type TunnelWebRouter struct {
*WebRouter
subdomain string
}

func (web *TunnelWebRouter) InitRouter() {
router := http.NewServeMux()
router.HandleFunc("GET /{fname...}", web.AssetRequest)
router.HandleFunc("GET /{$}", web.AssetRequest)
web.UserRouter = router
}

func (web *TunnelWebRouter) Perm(proj *db.Project) bool {
return true
}

func (web *TunnelWebRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, shared.CtxSubdomainKey{}, web.subdomain)
web.UserRouter.ServeHTTP(w, r.WithContext(ctx))
}

type CtxHttpBridge = func(ssh.Context) http.Handler

func getInfoFromUser(user string) (string, string) {
Expand Down Expand Up @@ -50,13 +65,17 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {

props, err := shared.GetProjectFromSubdomain(subdomain)
if err != nil {
log.Error(err.Error())
log.Error("could not get project from subdomain", "err", err.Error())
return http.HandlerFunc(shared.UnauthorizedHandler)
}

owner, err := dbh.FindUserForName(props.Username)
if err != nil {
log.Error(err.Error())
log.Error(
"could not find user from name",
"name", props.Username,
"err", err.Error(),
)
return http.HandlerFunc(shared.UnauthorizedHandler)
}
log = log.With(
Expand All @@ -65,7 +84,7 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {

project, err := dbh.FindProjectByName(owner.ID, props.ProjectName)
if err != nil {
log.Error(err.Error())
log.Error("could not get project by name", "project", props.ProjectName, "err", err.Error())
return http.HandlerFunc(shared.UnauthorizedHandler)
}

Expand All @@ -87,8 +106,9 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
}

ctx.Permissions().Extensions["user_id"] = requester.ID
publicKey, err := ssh.ParsePublicKey([]byte(pubkey))
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
if err != nil {
log.Error("could not parse public key", "pubkey", pubkey, "err", err)
return http.HandlerFunc(shared.UnauthorizedHandler)
}
if !HasProjectAccess(project, owner, requester, publicKey) {
Expand All @@ -104,11 +124,8 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
apiConfig.Dbpool,
apiConfig.Storage,
)
tunnelRouter := TunnelWebRouter{routes}
router := http.NewServeMux()
router.HandleFunc("GET /{fname}/{options}...", tunnelRouter.ImageRequest)
router.HandleFunc("GET /{fname}", tunnelRouter.AssetRequest)
router.HandleFunc("GET /{$}", tunnelRouter.AssetRequest)
return router
tunnelRouter := TunnelWebRouter{routes, subdomain}
tunnelRouter.initRouters()
return &tunnelRouter
}
}

0 comments on commit 7372600

Please sign in to comment.