From 7ee5658cbc87190ba41921350770277cbe72e921 Mon Sep 17 00:00:00 2001
From: Erik Brakkee 
Date: Sun, 21 Jul 2024 18:51:30 +0200
Subject: [PATCH] added proxycomamnd to be used directly by ssh
---
 Dockerfile                 |  2 +-
 cmd/agent/agent.go         |  6 +++---
 cmd/convergeproxy/proxy.go | 40 ++++++++++++++++++++++++++++++++++++++
 static/index.html          | 19 +++++++++++-------
 4 files changed, 56 insertions(+), 11 deletions(-)
 create mode 100644 cmd/convergeproxy/proxy.go
diff --git a/Dockerfile b/Dockerfile
index 3892211..b777fdd 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,7 +11,7 @@ RUN go build -ldflags "-linkmode 'external' -extldflags '-static'" -o bin ./cmd/
 FROM scratch
 
 COPY --from=builder /opt/converge/bin/converge /opt/converge/bin/
-COPY --from=builder /opt/converge/bin/agent /opt/converge/bin/tcptows /opt/converge/docs/
+COPY --from=builder /opt/converge/bin/agent /opt/converge/bin/tcptows /opt/converge/bin/convergeproxy /opt/converge/docs/
 COPY --from=builder /opt/converge/static/ /opt/converge/docs/
 
 ENTRYPOINT ["/opt/converge/bin/converge", "/opt/converge/docs" ]
diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go
index 38d2c10..2d96d69 100755
--- a/cmd/agent/agent.go
+++ b/cmd/agent/agent.go
@@ -154,9 +154,9 @@ func (f ReaderFunc) Read(p []byte) (n int, err error) {
 func main() {
 	wsURL := os.Args[1]
 
-	advanceWarningTime := 1 * time.Minute
-	agentExpriryTime := 2 * time.Minute
-	tickerInterval := 10 * time.Second
+	advanceWarningTime := 10 * time.Minute
+	agentExpriryTime := 30 * time.Minute
+	tickerInterval := 60 * time.Second
 	agent.ConfigureAgent(advanceWarningTime, agentExpriryTime, tickerInterval)
 
 	conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
diff --git a/cmd/convergeproxy/proxy.go b/cmd/convergeproxy/proxy.go
new file mode 100644
index 0000000..1b6d249
--- /dev/null
+++ b/cmd/convergeproxy/proxy.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+	"cidebug/pkg/iowrappers"
+	"cidebug/pkg/websocketutil"
+	"github.com/gorilla/websocket"
+	"log"
+	"net"
+	"os"
+)
+
+func closeConnection(conn net.Conn) {
+	if tcpConn, ok := conn.(*net.TCPConn); ok {
+		tcpConn.SetLinger(0)
+	}
+	_ = conn.Close()
+}
+
+type Stdio struct{}
+
+func (stdio Stdio) Read(b []byte) (n int, err error) {
+	return os.Stdin.Read(b)
+}
+func (stdio Stdio) Write(b []byte) (n int, err error) {
+	return os.Stdout.Write(b)
+}
+
+func main() {
+	wsURL := os.Args[1]
+
+	_wsConn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
+	if err != nil {
+		log.Println("WebSocket connection error:", err)
+		panic(err)
+	}
+	wsConn := websocketutil.NewWebSocketConn(_wsConn)
+	defer wsConn.Close()
+
+	iowrappers.SynchronizeStreams(wsConn, Stdio{})
+}
diff --git a/static/index.html b/static/index.html
index 306dad7..7c51013 100644
--- a/static/index.html
+++ b/static/index.html
@@ -40,7 +40,10 @@
         to establish a connection to the CI job through converge.
     
 
-    Local clients
+    Local clients: with a local TCP forwarding proxy
+
+    This option is less convenient than the proxy command because it requires two separate
+    commands to execute.
 
     
         Local clients can connect using regular ssh and sftp commands through a tunnel that
@@ -51,14 +54,14 @@
 
     
     # for HTTP hosted server
-    curl http://HOST:PORT/docs/wstotcp > wstotcp
-    chmod 755 wstotcp
-    ./wstotcp 10000 ws://HOST:PORT/client/ID
+    curl http://HOST:PORT/docs/tcptows > tcptows
+    chmod 755 tcptows
+    ./tcptows 10000 ws://HOST:PORT/client/ID
 
     # for HTTPS hosted server
-    curl https://HOST:PORT/docs/wstotcp > wstotcp
-    chmod 755 wstotcp
-    ./wstotcp 10000 wss://HOST:PORT/client/ID
+    curl https://HOST:PORT/docs/tcptows > tcptows
+    chmod 755 tcptows
+    ./tcptows 10000 wss://HOST:PORT/client/ID
 
 
     
@@ -86,6 +89,8 @@
         
tcptows: TCP to WS tunnel for allowing regular
             SSH and SFTP clients to connect to converge.
         
+        convergeproxy: SSH proxy command that can be directly used by ssh
+