Created
July 30, 2025 12:04
-
-
Save WilliamSampaio/b563a2424520c28bf826c152dfff1323 to your computer and use it in GitHub Desktop.
Websocket SSH Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="https://unpkg.com/xterm/lib/xterm.js"></script> | |
| <link rel="stylesheet" href="https://unpkg.com/xterm/css/xterm.css" /> | |
| <style> | |
| body, html { margin: 0; height: 100%; background: black; } | |
| #terminal { width: 100%; height: 100vh; } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="terminal"></div> | |
| <script> | |
| const term = new Terminal(); | |
| term.open(document.getElementById('terminal')); | |
| const ws = new WebSocket("ws://" + location.host + "/ssh"); | |
| ws.onmessage = (e) => term.write(e.data); | |
| term.onData(data => ws.send(data)); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // main.go | |
| package main | |
| import ( | |
| "golang.org/x/crypto/ssh" | |
| "github.com/gorilla/websocket" | |
| "log" | |
| "net/http" | |
| ) | |
| var upgrader = websocket.Upgrader{ | |
| CheckOrigin: func(r *http.Request) bool { | |
| return true | |
| }, | |
| } | |
| func sshWebSocketHandler(w http.ResponseWriter, r *http.Request) { | |
| // Atualize essas credenciais de acordo com seu ambiente | |
| sshConfig := &ssh.ClientConfig{ | |
| User: "server", | |
| Auth: []ssh.AuthMethod{ | |
| ssh.Password("systock"), | |
| }, | |
| HostKeyCallback: ssh.InsecureIgnoreHostKey(), | |
| } | |
| conn, err := upgrader.Upgrade(w, r, nil) | |
| if err != nil { | |
| log.Println("WebSocket upgrade error:", err) | |
| return | |
| } | |
| defer conn.Close() | |
| sshClient, err := ssh.Dial("tcp", "172.24.176.76:22", sshConfig) | |
| if err != nil { | |
| log.Println("SSH dial error:", err) | |
| return | |
| } | |
| defer sshClient.Close() | |
| sess, err := sshClient.NewSession() | |
| if err != nil { | |
| log.Println("SSH session error:", err) | |
| return | |
| } | |
| defer sess.Close() | |
| stdin, _ := sess.StdinPipe() | |
| stdout, _ := sess.StdoutPipe() | |
| sess.Stderr = sess.Stdout | |
| _ = sess.RequestPty("xterm", 40, 80, ssh.TerminalModes{}) | |
| _ = sess.Shell() | |
| // Leitor do SSH para WebSocket | |
| go func() { | |
| buf := make([]byte, 1024) | |
| for { | |
| n, err := stdout.Read(buf) | |
| if err != nil { | |
| break | |
| } | |
| conn.WriteMessage(websocket.TextMessage, buf[:n]) | |
| } | |
| }() | |
| // Escritor do WebSocket para SSH | |
| for { | |
| _, msg, err := conn.ReadMessage() | |
| if err != nil { | |
| break | |
| } | |
| stdin.Write(msg) | |
| } | |
| } | |
| func main() { | |
| http.HandleFunc("/ssh", sshWebSocketHandler) | |
| http.Handle("/", http.FileServer(http.Dir("./static"))) | |
| log.Println("Servidor iniciado em localhost:8777") | |
| http.ListenAndServe(":8777", nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment