-
Notifications
You must be signed in to change notification settings - Fork 4
Update main.go #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,21 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/gorilla/websocket" | ||
| "golang.org/x/crypto/ssh" | ||
| ) | ||
|
|
||
| type TerminalSize struct { | ||
| Cols int `json:"cols"` | ||
| Rows int `json:"rows"` | ||
| } | ||
|
|
||
| var upgrader = websocket.Upgrader{ | ||
| ReadBufferSize: 1024, | ||
| WriteBufferSize: 1024, | ||
|
|
@@ -20,10 +25,10 @@ var upgrader = websocket.Upgrader{ | |
| } | ||
|
|
||
| func sshHandler(w http.ResponseWriter, r *http.Request) { | ||
| user := os.Getenv("SSH_USER") | ||
| pass := os.Getenv("SSH_PASS") | ||
| host := os.Getenv("SSH_HOST") | ||
| port := os.Getenv("SSH_PORT") | ||
| user := "root" | ||
| pass := "plokij2wsx3edc2$" | ||
| host := "172.16.0.69" | ||
| port := "22" | ||
|
|
||
| hostport := fmt.Sprintf("%s:%s", host, port) | ||
|
|
||
|
|
@@ -68,7 +73,20 @@ func sshHandler(w http.ResponseWriter, r *http.Request) { | |
| return | ||
| } | ||
|
|
||
| if err := session.RequestPty("xterm", 80, 40, ssh.TerminalModes{}); err != nil { | ||
| var termSize TerminalSize | ||
| if err := wsConn.ReadJSON(&termSize); err != nil { | ||
| log.Println("Read terminal size error:", err) | ||
| return | ||
| } | ||
|
|
||
| modes := ssh.TerminalModes{ | ||
| ssh.ECHO: 1, | ||
| ssh.TTY_OP_ISPEED: 8192, | ||
| ssh.TTY_OP_OSPEED: 8192, | ||
| ssh.IEXTEN: 0, | ||
| } | ||
|
|
||
| if err := session.RequestPty("xterm", termSize.Cols, termSize.Rows, modes); err != nil { | ||
| log.Println("Request PTY error:", err) | ||
| return | ||
| } | ||
|
|
@@ -107,64 +125,41 @@ func sshHandler(w http.ResponseWriter, r *http.Request) { | |
| } | ||
| return | ||
| } | ||
| if messageType == websocket.BinaryMessage || messageType == websocket.TextMessage { | ||
| _, err = sshIn.Write(p) | ||
| if err != nil { | ||
| log.Println("Write to SSH stdin error:", err) | ||
| return | ||
| if messageType == websocket.TextMessage { | ||
| var newSize TerminalSize | ||
| if err := json.Unmarshal(p, &newSize); err == nil { | ||
| // Resize terminal | ||
| //log.Printf("Resizing terminal to cols: %d, rows: %d", newSize.Cols, newSize.Rows) | ||
| session.WindowChange(newSize.Rows, newSize.Cols) | ||
| } else { | ||
| // Write to SSH stdin | ||
| _, err = sshIn.Write(p) | ||
| if err != nil { | ||
| log.Println("Write to SSH stdin error:", err) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func checkForVariables() error { | ||
| if os.Getenv("SSH_USER") == "" { | ||
| return fmt.Errorf("SSH_USER is not set") | ||
| } | ||
| if os.Getenv("SSH_PASS") == "" { | ||
| return fmt.Errorf("SSH_PASS is not set") | ||
| } | ||
| if os.Getenv("SSH_HOST") == "" { | ||
| return fmt.Errorf("SSH_HOST is not set") | ||
| } | ||
| if os.Getenv("SSH_PORT") == "" { | ||
| return fmt.Errorf("SSH_PORT is not set") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func printUsage() { | ||
| fmt.Println("Usage (set proper environmental variables): ") | ||
| fmt.Println("SSH_USER - username for ssh connection") | ||
| fmt.Println("SSH_PASS - password for ssh connection") | ||
| fmt.Println("SSH_HOST - host for ssh connection") | ||
| fmt.Println("SSH_PORT - port for ssh connection") | ||
| fmt.Println("MOUNT_HTML - mount html files (default true, set to false to disable)") | ||
|
|
||
| } | ||
| func main() { | ||
| errOf := checkForVariables() | ||
| if errOf != nil { | ||
|
|
||
| printUsage() | ||
| log.Fatal(errOf) | ||
| } | ||
| shouldMountHTML := os.Getenv("MOUNT_HTML") == "true" || os.Getenv("MOUNT_HTML") == "" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why to delete it?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed it because I thought the HTML always needed to be included in the project, directly within the Docker setup. However, you can keep it if you prefer. |
||
| http.HandleFunc("/ssh", sshHandler) | ||
|
|
||
| if shouldMountHTML { | ||
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "index.html") | ||
| }) | ||
| http.HandleFunc("/xterm.css", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "xterm.css") | ||
| }) | ||
| http.HandleFunc("/xterm.js", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "xterm.js") | ||
| }) | ||
| } | ||
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "index.html") | ||
| }) | ||
| http.HandleFunc("/xterm.css", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "xterm.css") | ||
| }) | ||
| http.HandleFunc("/xterm.js", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "xterm.js") | ||
| }) | ||
|
|
||
| http.HandleFunc("/xterm-addon-fit.min.js", func(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "xterm-addon-fit.min.js") | ||
| }) | ||
|
|
||
| fmt.Println("Starting server on :8280") | ||
| log.Fatal(http.ListenAndServe(":8280", nil)) | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is leftover