From 81317510450e9d2f3357134563bd75876ef5760c Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sat, 27 Jan 2024 23:20:54 -0800 Subject: [PATCH 1/3] [workflows] on-push publishes to the development package. --- .github/workflows/on-push.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/on-push.yml b/.github/workflows/on-push.yml index 1773edf..bd84919 100644 --- a/.github/workflows/on-push.yml +++ b/.github/workflows/on-push.yml @@ -41,11 +41,11 @@ jobs: uses: docker/metadata-action@v5 with: images: | - ${{ github.repository }} - ghcr.io/${{ github.repository }} + ${{ github.repository }}-development + ghcr.io/${{ github.repository }}-development tags: | type=raw,value=dev-{{date 'X'}} - type=raw,value=development + type=raw,value=latest type=ref,event=branch type=edge,branch=main - name: Build and push ${{ github.repository }}:${{ steps.git.outputs.image_tag }} From 0cc52eee460c9ee9c5f495c1cebda4f8789e14b1 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sat, 27 Jan 2024 23:26:39 -0800 Subject: [PATCH 2/3] Prometheus roundup the trapped time to the interval. In the old way, if the connection is broken less than an interval, the trapped time won't be reported. With this fix, the prometheus should report the same value as the log. --- main.go | 11 ++++++----- metrics/metrics.go | 9 +++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 64b996e..c9d0353 100644 --- a/main.go +++ b/main.go @@ -42,23 +42,24 @@ func startSending(maxClients int64, bannerMaxLength int64, records chan<- metric bytesSent, err := c.Send(bannerMaxLength) remoteIpAddr := c.RemoteIpAddr() localPort := c.LocalPort() + millisecondsSpent := c.MillisecondsSinceLast() if err != nil { c.Close() records <- metrics.RecordEntry{ - RecordType: metrics.RecordEntryTypeStop, - IpAddr: remoteIpAddr, - LocalPort: localPort, + RecordType: metrics.RecordEntryTypeStop, + IpAddr: remoteIpAddr, + LocalPort: localPort, + MillisecondsSpent: millisecondsSpent, } return } - millisecondsSpent := c.MillisecondsSinceLast() clients <- c records <- metrics.RecordEntry{ RecordType: metrics.RecordEntryTypeSend, IpAddr: remoteIpAddr, LocalPort: localPort, - BytesSent: bytesSent, MillisecondsSpent: millisecondsSpent, + BytesSent: bytesSent, } }() } diff --git a/metrics/metrics.go b/metrics/metrics.go index fc6773b..c754360 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -106,8 +106,8 @@ type RecordEntry struct { RecordType int IpAddr string LocalPort string - BytesSent int MillisecondsSpent int64 + BytesSent int } func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry { @@ -140,10 +140,15 @@ func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUns clientSeconds.With(prometheus.Labels{ "ip": r.IpAddr, "local_port": r.LocalPort}).Add(secondsSpent) - totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent)) totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent) + totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent)) pq.Update(r.IpAddr, time.Now()) case RecordEntryTypeStop: + secondsSpent := float64(r.MillisecondsSpent) / 1000 + clientSeconds.With(prometheus.Labels{ + "ip": r.IpAddr, + "local_port": r.LocalPort}).Add(secondsSpent) + totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent) totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc() pq.Update(r.IpAddr, time.Now()) case RecordEntryTypeClean: From c309a8fc587b21c3769fa8bf9324587d8b685a68 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sun, 28 Jan 2024 21:52:12 -0800 Subject: [PATCH 3/3] [readme] remove sudo --- README.md | 2 +- examples/docker-maxmind/README.md | 4 ++-- examples/docker-simple/README.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a6c82fd..2ae2942 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ go build . Alternatively, you can use the [docker image](https://hub.docker.com/r/shizunge/endlessh-go): ``` -sudo docker run -d -p 2222:2222 shizunge/endlessh-go -logtostderr -v=1 +docker run -d -p 2222:2222 shizunge/endlessh-go -logtostderr -v=1 ``` It listens to port `2222` by default. diff --git a/examples/docker-maxmind/README.md b/examples/docker-maxmind/README.md index 3dff00d..656dbdb 100644 --- a/examples/docker-maxmind/README.md +++ b/examples/docker-maxmind/README.md @@ -5,12 +5,12 @@ This is an example how to setup endlessh-go with the Maxmind GeoIP Database usin To start the stack, in the _examples_ folder, run: ``` -sudo docker-compose up -d +docker-compose up -d ``` The GeoIP Database will be saved in a mounted volume in: `./geo-data`. And the endlessh-go container will use this database to do the location lookups. This example exposes the following ports. Except the SSH port, you should not expose other ports to public without protections (not included in this example) in production. -- **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `sudo docker logs endlessh`. +- **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `docker logs endlessh`. - **2112**: The Prometheus metrics exported by endlessh-go. Go to [http://localhost:2112/metrics](http://localhost:2112/metrics) in your web browser to view the metrics. diff --git a/examples/docker-simple/README.md b/examples/docker-simple/README.md index 18d9d9c..89c93b2 100644 --- a/examples/docker-simple/README.md +++ b/examples/docker-simple/README.md @@ -9,12 +9,12 @@ This is an example how to setup endlessh-go, Prometheus, and Grafana using [dock To start the stack, in the *examples* folder, run: ``` -sudo docker-compose up -d +docker-compose up -d ``` This example exposes the following ports. Except the SSH port, you should not expose other ports to public without protections (not included in this example) in production. -* **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `sudo docker logs endlessh`. +* **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `docker logs endlessh`. * **2112**: The Prometheus metrics exported by endlessh-go. Go to [http://localhost:2112/metrics](http://localhost:2112/metrics) in your web browser to view the metrics. * **9090**: Prometheus web interface. Go to [http://localhost:9090](http://localhost:9090) in your web browser for Prometheus. You can check whether the target of endlessh-go is up (Click Status, then Targets). * **3000**: Grafana. Go to [http://localhost:3000](http://localhost:3000) in your web browser for Grafana. Use username *examples* and password *examples* to login.