tuns

created pr with 34.1 on 2024-11-13T02:18:51Z · by c8ef7d19
added 34.2 on 2024-11-14T03:06:46Z · by c8ef7d19
1: 4bd8050 = 1: 4bd8050 feat: metric drain
-: ------- > 2: ea49338 chore: add userId to metric drain visit
added 34.3 on 2024-11-14T03:22:31Z · by c8ef7d19
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 on 2024-11-23T03:33:38Z · by c8ef7d19
cmds
checkout latest patchset:
ssh pr.pico.sh print 34 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print 34.[rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 34
set PR to open (enables RSS notifications):
ssh pr.pico.sh pr open 34
set PR to draft (stops RSS notifications):
ssh pr.pico.sh pr draft 34
+4 -0 Makefile #
......@@ -19,3 +19,7 @@ docs-prod: ssg
1919 dev:
2020 go run main.go --http-address localhost:3000 --domain testing.ssi.sh
2121 .PHONY: dev
22+
23+fmt:
24+ go fmt ./...
25+.PHONY: fmt
+7 -2 cmd/sish.go #
......@@ -17,6 +17,7 @@ import (
1717 "github.com/spf13/viper"
1818 "gopkg.in/natefinch/lumberjack.v2"
1919
20+ "github.com/picosh/pubsub"
2021 pipeLogger "github.com/picosh/pubsub/log"
2122 picoUtils "github.com/picosh/utils"
2223 )
......@@ -206,17 +207,21 @@ func initConfig() {
206207 slog.NewTextHandler(multiWriter, &slog.HandlerOptions{AddSource: true, Level: logLevel}),
207208 )
208209
209- realLogger, err := pipeLogger.SendLogRegister(logger, &pipeLogger.PubSubConnectionInfo{
210+ info := &pubsub.RemoteClientInfo{
210211 RemoteHost: picoUtils.GetEnv("PICO_PIPE_ENDPOINT", "send.pico.sh:22"),
211212 KeyLocation: picoUtils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"),
212213 KeyPassphrase: picoUtils.GetEnv("PICO_PIPE_PASSPHRASE", ""),
213214 RemoteHostname: picoUtils.GetEnv("PICO_PIPE_REMOTE_HOST", "send.pico.sh"),
214215 RemoteUser: picoUtils.GetEnv("PICO_PIPE_USER", "pico"),
215- }, 100)
216+ }
217+ realLogger, err := pipeLogger.SendLogRegister(logger, info, 100)
216218 if err != nil {
217219 slog.Error("unable to set real logger", slog.Any("error", err))
218220 }
219221
222+ metricDrain := pubsub.NewRemoteClientWriter(info, logger, 100)
223+ go metricDrain.KeepAlive("pub metric-drain -b=false")
224+
220225 trueLogger := logger
221226
222227 if realLogger != nil {
+2 -0 go.mod #
......@@ -2,6 +2,8 @@ module github.com/antoniomika/sish
22
33 go 1.23.1
44
5+replace github.com/picosh/pubsub => /home/erock/dev/pico/pubsub
6+
57 require (
68 github.com/ScaleFT/sshkeys v1.2.0
79 github.com/antoniomika/multilistener v0.0.0-20240307222635-f0dc097d8acc
+28 -0 httpmuxer/httpmuxer.go #
......@@ -31,6 +31,16 @@ import (
3131 "github.com/gin-gonic/gin"
3232 )
3333
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+
3444 // Start initializes the HTTP service.
3545 func Start(state *utils.State) {
3646 releaseMode := gin.ReleaseMode
......@@ -106,6 +116,24 @@ func Start(state *utils.State) {
106116 param.ErrorMessage,
107117 )
108118
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+
109137 slog.Info(logLine)
110138
111139 if viper.GetBool("log-to-client") && param.Keys["httpHolder"] != nil {
+13 -0 sshmuxer/sshmuxer.go #
......@@ -16,6 +16,8 @@ import (
1616 "github.com/antoniomika/sish/httpmuxer"
1717 "github.com/antoniomika/sish/utils"
1818 "github.com/antoniomika/syncmap"
19+ "github.com/picosh/pubsub"
20+ picoUtils "github.com/picosh/utils"
1921 "github.com/pires/go-proxyproto"
2022 "github.com/spf13/viper"
2123 "golang.org/x/crypto/ssh"
......@@ -70,10 +72,21 @@ func Start() {
7072
7173 utils.WatchKeys()
7274
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+
7385 state := utils.NewState()
7486 state.Ports.HTTPPort = httpPort
7587 state.Ports.HTTPSPort = httpsPort
7688 state.Ports.SSHPort = sshPort
89+ state.MetricDrain = metricDrain
7790
7891 state.Console.State = state
7992
+10 -0 utils/conn.go #
......@@ -99,6 +99,16 @@ func (s *SSHConnection) User() string {
9999 return user
100100 }
101101
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+
102112 // TeeConn represents a simple net.Conn interface for SNI Processing.
103113 type TeeConn struct {
104114 Conn net.Conn
+1 -0 utils/state.go #
......@@ -231,6 +231,7 @@ type State struct {
231231 ActiveConnections prometheus.GaugeFunc
232232 ActiveTunnels *prometheus.GaugeVec
233233 ConnectionTime *prometheus.GaugeVec
234+ MetricDrain io.Writer
234235 }
235236
236237 // NewState returns a new State struct.
+6 -0 utils/utils.go #
......@@ -597,7 +597,13 @@ func checkAuthenticationKeyRequest(authUrl string, authKey []byte, addr net.Addr
597597 return false, extensionsInfo, nil
598598 }
599599
600+ userId, ok := userData["id"].(string)
601+ if !ok {
602+ return false, extensionsInfo, nil
603+ }
604+
600605 extensionsInfo["user"] = userName
606+ extensionsInfo["userId"] = userId
601607 }
602608
603609 return true, extensionsInfo, nil
Back to top