repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
701e8da
parent
c751d73
author
Eric Bower
date
2024-04-19 21:01:44 +0000 UTC
fix(pgs): analytics for private sites
5 files changed,  +34, -7
M Makefile
+4, -0
 1@@ -40,6 +40,10 @@ bp-bouncer: bp-setup
 2 	$(DOCKER_BUILDX_BUILD) -t ghcr.io/picosh/pico/bouncer:$(DOCKER_TAG) ./bouncer
 3 .PHONY: bp-bouncer
 4 
 5+bp-ssh-%: bp-setup
 6+	$(DOCKER_BUILDX_BUILD) --build-arg "APP=$*" -t "ghcr.io/picosh/pico/$*-ssh:$(DOCKER_TAG)" --target release-ssh .
 7+.PHONY: pgs-ssh
 8+
 9 bp-%: bp-setup
10 	$(DOCKER_BUILDX_BUILD) --build-arg "APP=$*" -t "ghcr.io/picosh/pico/$*-ssh:$(DOCKER_TAG)" --target release-ssh .
11 	$(DOCKER_BUILDX_BUILD) --build-arg "APP=$*" -t "ghcr.io/picosh/pico/$*-web:$(DOCKER_TAG)" --target release-web .
M pgs/ssh.go
+10, -6
 1@@ -12,6 +12,7 @@ import (
 2 	"github.com/charmbracelet/ssh"
 3 	"github.com/charmbracelet/wish"
 4 	bm "github.com/charmbracelet/wish/bubbletea"
 5+	"github.com/picosh/pico/db"
 6 	"github.com/picosh/pico/db/postgres"
 7 	uploadassets "github.com/picosh/pico/filehandlers/assets"
 8 	"github.com/picosh/pico/shared"
 9@@ -65,8 +66,8 @@ func StartSshServer() {
10 	promPort := shared.GetEnv("PGS_PROM_PORT", "9222")
11 	cfg := NewConfigSite()
12 	logger := cfg.Logger
13-	dbh := postgres.NewDB(cfg.DbURL, cfg.Logger)
14-	defer dbh.Close()
15+	dbpool := postgres.NewDB(cfg.DbURL, cfg.Logger)
16+	defer dbpool.Close()
17 
18 	var st storage.StorageServe
19 	var err error
20@@ -82,15 +83,18 @@ func StartSshServer() {
21 	}
22 
23 	handler := uploadassets.NewUploadAssetHandler(
24-		dbh,
25+		dbpool,
26 		cfg,
27 		st,
28 	)
29 
30+	ch := make(chan *db.AnalyticsVisits)
31+	go shared.AnalyticsCollect(ch, dbpool, logger)
32 	apiConfig := &shared.ApiConfig{
33-		Cfg:     cfg,
34-		Dbpool:  dbh,
35-		Storage: st,
36+		Cfg:            cfg,
37+		Dbpool:         dbpool,
38+		Storage:        st,
39+		AnalyticsQueue: ch,
40 	}
41 
42 	webTunnel := &ptun.WebTunnelHandler{
M pgs/tunnel.go
+3, -1
 1@@ -1,6 +1,7 @@
 2 package pgs
 3 
 4 import (
 5+	"context"
 6 	"encoding/json"
 7 	"net/http"
 8 	"strings"
 9@@ -122,7 +123,8 @@ func createHttpHandler(apiConfig *shared.ApiConfig) CtxHttpBridge {
10 
11 		subdomainRoutes := createSubdomainRoutes(allowPerm)
12 		routes = append(routes, subdomainRoutes...)
13-		finctx := apiConfig.CreateCtx(ctx, subdomain)
14+		finctx := apiConfig.CreateCtx(context.Background(), subdomain)
15+		finctx = context.WithValue(finctx, shared.CtxSshKey{}, ctx)
16 		httpHandler := shared.CreateServeBasic(routes, finctx)
17 		httpRouter := http.HandlerFunc(httpHandler)
18 		return httpRouter
M shared/analytics.go
+7, -0
 1@@ -95,6 +95,13 @@ func AnalyticsVisitFromRequest(r *http.Request, userID string, secret string) (*
 2 	if ipOrig == "" {
 3 		ipOrig = r.RemoteAddr
 4 	}
 5+	// probably means this is a web tunnel
 6+	if ipOrig == "" || ipOrig == "@" {
 7+		sshCtx, err := GetSshCtx(r)
 8+		if err == nil {
 9+			ipOrig = sshCtx.RemoteAddr().String()
10+		}
11+	}
12 	ipAddress, err := cleanIpAddress(ipOrig)
13 	if err != nil {
14 		return nil, err
M shared/router.go
+10, -0
 1@@ -10,6 +10,7 @@ import (
 2 	"regexp"
 3 	"strings"
 4 
 5+	"github.com/charmbracelet/ssh"
 6 	"github.com/picosh/pico/db"
 7 	"github.com/picosh/pico/shared/storage"
 8 )
 9@@ -147,6 +148,15 @@ type ctxLoggerKey struct{}
10 type ctxSubdomainKey struct{}
11 type ctxCfg struct{}
12 type ctxAnalyticsQueue struct{}
13+type CtxSshKey struct{}
14+
15+func GetSshCtx(r *http.Request) (ssh.Context, error) {
16+	payload, ok := r.Context().Value(CtxSshKey{}).(ssh.Context)
17+	if payload == nil || !ok {
18+		return payload, fmt.Errorf("sshCtx not set on `r.Context()` for connection")
19+	}
20+	return payload, nil
21+}
22 
23 func GetCfg(r *http.Request) *ConfigSite {
24 	return r.Context().Value(ctxCfg{}).(*ConfigSite)