tuns
created pr with
34.1
added 34.2
1: 4bd8050 = 1: 4bd8050 feat: metric drain
-: ------- > 2: ea49338 chore: add userId to metric drain visit
added 34.3
1: 4bd8050 = 1: 4bd8050 feat: metric drain
2: ea49338 = 2: ea49338 chore: add userId to metric drain visit
-: ------- > 3: 343f29e chore: update pubsub
changed status to
accepted
cmds
checkout latest patchset:
ssh pr.pico.sh print 34 | git am -3checkout any patchset in a patch request:
ssh pr.pico.sh print 34.[rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 34set PR to open (enables RSS notifications):
ssh pr.pico.sh pr open 34set PR to draft (stops RSS notifications):
ssh pr.pico.sh pr draft 34
Patchset
34.3
feat: metric drain
Eric Bower
2024-11-13T02:16:16ZThis change will enable http remote forwards to have site analytics enabled automatically.
Semantic diff summary
2 added,
7 modified,
0 signature changed,
0 removed
across 6 analyzed files
(2 files skipped: unsupported file type)
+4
-0
Makefile
#
+7
-2
cmd/sish.go
#
| ... | ... | @@ -206,17 +207,21 @@ func initConfig() { | |
| 206 | 207 | slog.NewTextHandler(multiWriter, &slog.HandlerOptions{AddSource: true, Level: logLevel}), | |
| 207 | 208 | ) | |
| 208 | 209 | ||
| 209 | - | realLogger, err := pipeLogger.SendLogRegister(logger, &pipeLogger.PubSubConnectionInfo{ | |
| 210 | + | info := &pubsub.RemoteClientInfo{ | |
| 210 | 211 | RemoteHost: picoUtils.GetEnv("PICO_PIPE_ENDPOINT", "send.pico.sh:22"), | |
| 211 | 212 | KeyLocation: picoUtils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"), | |
| 212 | 213 | KeyPassphrase: picoUtils.GetEnv("PICO_PIPE_PASSPHRASE", ""), | |
| 213 | 214 | RemoteHostname: picoUtils.GetEnv("PICO_PIPE_REMOTE_HOST", "send.pico.sh"), | |
| 214 | 215 | RemoteUser: picoUtils.GetEnv("PICO_PIPE_USER", "pico"), | |
| 215 | - | }, 100) | |
| 216 | + | } | |
| 217 | + | realLogger, err := pipeLogger.SendLogRegister(logger, info, 100) | |
| 216 | 218 | if err != nil { | |
| 217 | 219 | slog.Error("unable to set real logger", slog.Any("error", err)) | |
| 218 | 220 | } | |
| 219 | 221 | ||
| 222 | + | metricDrain := pubsub.NewRemoteClientWriter(info, logger, 100) | |
| 223 | + | go metricDrain.KeepAlive("pub metric-drain -b=false") | |
| 224 | + | ||
| 220 | 225 | trueLogger := logger | |
| 221 | 226 | ||
| 222 | 227 | if realLogger != nil { |
+2
-0
go.mod
#
+28
-0
httpmuxer/httpmuxer.go
#
| ... | ... | @@ -31,6 +31,16 @@ import ( | |
| 31 | 31 | "github.com/gin-gonic/gin" | |
| 32 | 32 | ) | |
| 33 | 33 | ||
| 34 | + | type MetricDrainVisit struct { | |
| 35 | + | UserID string `json:"user_id"` | |
| 36 | + | Host string `json:"host"` | |
| 37 | + | Path string `json:"path"` | |
| 38 | + | IpAddress string `json:"ip_adress"` | |
| 39 | + | UserAgent string `json:"user_agent"` | |
| 40 | + | Referer string `json:"referer"` | |
| 41 | + | Status int `json:"status"` | |
| 42 | + | } | |
| 43 | + | ||
| 34 | 44 | // Start initializes the HTTP service. | |
| 35 | 45 | func Start(state *utils.State) { | |
| 36 | 46 | releaseMode := gin.ReleaseMode |
| ... | ... | @@ -106,6 +116,24 @@ func Start(state *utils.State) { | |
| 106 | 116 | param.ErrorMessage, | |
| 107 | 117 | ) | |
| 108 | 118 | ||
| 119 | + | visit := MetricDrainVisit{ | |
| 120 | + | UserID: "", | |
| 121 | + | Host: param.Request.Host, | |
| 122 | + | IpAddress: param.ClientIP, | |
| 123 | + | Status: param.StatusCode, | |
| 124 | + | Path: originalURI, | |
| 125 | + | UserAgent: param.Request.UserAgent(), | |
| 126 | + | Referer: param.Request.Referer(), | |
| 127 | + | } | |
| 128 | + | data, err := json.Marshal(visit) | |
| 129 | + | if err != nil { | |
| 130 | + | slog.Error("could not json marshall visit record", "err", err) | |
| 131 | + | } | |
| 132 | + | _, err = state.MetricDrain.Write(data) | |
| 133 | + | if err != nil { | |
| 134 | + | slog.Error("could not send to visit to metric-drain", "err", err) | |
| 135 | + | } | |
| 136 | + | ||
| 109 | 137 | slog.Info(logLine) | |
| 110 | 138 | ||
| 111 | 139 | if viper.GetBool("log-to-client") && param.Keys["httpHolder"] != nil { |
+13
-0
sshmuxer/sshmuxer.go
#
| ... | ... | @@ -16,6 +16,8 @@ import ( | |
| 16 | 16 | "github.com/antoniomika/sish/httpmuxer" | |
| 17 | 17 | "github.com/antoniomika/sish/utils" | |
| 18 | 18 | "github.com/antoniomika/syncmap" | |
| 19 | + | "github.com/picosh/pubsub" | |
| 20 | + | picoUtils "github.com/picosh/utils" | |
| 19 | 21 | "github.com/pires/go-proxyproto" | |
| 20 | 22 | "github.com/spf13/viper" | |
| 21 | 23 | "golang.org/x/crypto/ssh" |
| ... | ... | @@ -70,10 +72,21 @@ func Start() { | |
| 70 | 72 | ||
| 71 | 73 | utils.WatchKeys() | |
| 72 | 74 | ||
| 75 | + | info := &pubsub.RemoteClientInfo{ | |
| 76 | + | RemoteHost: picoUtils.GetEnv("PICO_PIPE_ENDPOINT", "send.pico.sh:22"), | |
| 77 | + | KeyLocation: picoUtils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"), | |
| 78 | + | KeyPassphrase: picoUtils.GetEnv("PICO_PIPE_PASSPHRASE", ""), | |
| 79 | + | RemoteHostname: picoUtils.GetEnv("PICO_PIPE_REMOTE_HOST", "send.pico.sh"), | |
| 80 | + | RemoteUser: picoUtils.GetEnv("PICO_PIPE_USER", "pico"), | |
| 81 | + | } | |
| 82 | + | metricDrain := pubsub.NewRemoteClientWriter(info, slog.Default(), 100) | |
| 83 | + | go metricDrain.KeepAlive("pub metric-drain -b=false") | |
| 84 | + | ||
| 73 | 85 | state := utils.NewState() | |
| 74 | 86 | state.Ports.HTTPPort = httpPort | |
| 75 | 87 | state.Ports.HTTPSPort = httpsPort | |
| 76 | 88 | state.Ports.SSHPort = sshPort | |
| 89 | + | state.MetricDrain = metricDrain | |
| 77 | 90 | ||
| 78 | 91 | state.Console.State = state | |
| 79 | 92 |
+10
-0
utils/conn.go
#
| ... | ... | @@ -99,6 +99,16 @@ func (s *SSHConnection) User() string { | |
| 99 | 99 | return user | |
| 100 | 100 | } | |
| 101 | 101 | ||
| 102 | + | // User returns the user of the session, either using the extensions user or the passed user. | |
| 103 | + | func (s *SSHConnection) UserId() string { | |
| 104 | + | user, ok := s.SSHConn.Permissions.Extensions["userId"] | |
| 105 | + | if !ok { | |
| 106 | + | user = "" | |
| 107 | + | } | |
| 108 | + | ||
| 109 | + | return user | |
| 110 | + | } | |
| 111 | + | ||
| 102 | 112 | // TeeConn represents a simple net.Conn interface for SNI Processing. | |
| 103 | 113 | type TeeConn struct { | |
| 104 | 114 | Conn net.Conn |
+1
-0
utils/state.go
#
+6
-0
utils/utils.go
#
| ... | ... | @@ -597,7 +597,13 @@ func checkAuthenticationKeyRequest(authUrl string, authKey []byte, addr net.Addr | |
| 597 | 597 | return false, extensionsInfo, nil | |
| 598 | 598 | } | |
| 599 | 599 | ||
| 600 | + | userId, ok := userData["id"].(string) | |
| 601 | + | if !ok { | |
| 602 | + | return false, extensionsInfo, nil | |
| 603 | + | } | |
| 604 | + | ||
| 600 | 605 | extensionsInfo["user"] = userName | |
| 606 | + | extensionsInfo["userId"] = userId | |
| 601 | 607 | } | |
| 602 | 608 | ||
| 603 | 609 | return true, extensionsInfo, nil |