Skip to content
Snippets Groups Projects
Verified Commit f24c1b71 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Improve logging and connection handling

parent c47128a8
Branches main
Tags 0.1.1
No related merge requests found
...@@ -7,4 +7,5 @@ RUN go build -ldflags="-w -s" -o /go/bin/sshway ...@@ -7,4 +7,5 @@ RUN go build -ldflags="-w -s" -o /go/bin/sshway
FROM alpine:3.18 FROM alpine:3.18
COPY --from=builder /go/bin/sshway /bin/sshway COPY --from=builder /go/bin/sshway /bin/sshway
CMD ["/bin/sshway"] EXPOSE 2222
\ No newline at end of file CMD ["/bin/sshway"]
...@@ -67,17 +67,20 @@ func main() { ...@@ -67,17 +67,20 @@ func main() {
fmt.Printf("Listening on %s\n", localAddr) fmt.Printf("Listening on %s\n", localAddr)
defer server.Close() defer server.Close()
client_counter := 0
for { for {
connection, err := server.Accept() connection, err := server.Accept()
if err != nil { if err != nil {
log.Fatalf("Error accepting local client: %v\n", err) log.Fatalf("Error accepting local client: %v\n", err)
} }
fmt.Printf("client connected %s\n", connection.RemoteAddr().String()) log.Printf("%6d New connection from client: %s\n", client_counter, connection.RemoteAddr().String())
go handleClient(connection) go handleClient(client_counter, connection)
client_counter++
} }
} }
func handleClient(localSock net.Conn) { func handleClient(client_counter int, localSock net.Conn) {
defer localSock.Close() defer localSock.Close()
config := &ssh.ClientConfig{ config := &ssh.ClientConfig{
...@@ -92,21 +95,24 @@ func handleClient(localSock net.Conn) { ...@@ -92,21 +95,24 @@ func handleClient(localSock net.Conn) {
conn, err := ssh.Dial("tcp", sshHost, config) conn, err := ssh.Dial("tcp", sshHost, config)
if err != nil { if err != nil {
log.Printf("SSH connection failed: %v\n", err) log.Printf("%6d SSH connection failed: %v\n", client_counter, err)
return return
} }
defer conn.Close()
forwardClient, err := conn.Dial("tcp", remoteAddr) forwardClient, err := conn.Dial("tcp", remoteAddr)
if err != nil { if err != nil {
log.Printf("Port forward failed: %v\n", err) log.Printf("%6d Port forward failed: %v\n", client_counter, err)
conn.Close()
return return
} }
go func() { go func() {
io.Copy(localSock, forwardClient) io.Copy(forwardClient, localSock)
log.Printf("%6d Local client disconnected\n", client_counter)
conn.Close()
}() }()
io.Copy(forwardClient, localSock) io.Copy(localSock, forwardClient)
log.Printf("Disconnected: %v\n", localSock) log.Printf("%6d Remote socket closed for local client\n", client_counter)
conn.Close()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment