forked from genshen/ssh-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge(ssh, sftp): Merge pull request genshen#2 from genshen/refactor-…
…ssh-io refactor ssh io
- Loading branch information
Showing
9 changed files
with
211 additions
and
227 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"log" | ||
"golang.org/x/crypto/ssh" | ||
"github.com/genshen/webConsole/src/utils" | ||
"github.com/genshen/webConsole/src/routers" | ||
) | ||
|
||
func main() { | ||
routers.Run() | ||
//setupSSH() | ||
} | ||
|
||
func setupSSH() { | ||
check := func(err error, msg string) { | ||
if err != nil { | ||
log.Fatalf("%s error: %v", msg, err) | ||
} | ||
} | ||
|
||
sshEntity := utils.SSH{ | ||
Node: utils.Node{ | ||
Host: "ssh.hpc.gensh.me", | ||
Port: 22, | ||
}, | ||
} | ||
err := sshEntity.Connect("genshen", "genshen1234") | ||
check(err, "connect") | ||
defer sshEntity.Client.Close() | ||
|
||
session, err := sshEntity.Client.NewSession() | ||
check(err, "new session") | ||
defer session.Close() | ||
|
||
session.Stdout = os.Stdout | ||
session.Stderr = os.Stderr | ||
session.Stdin = os.Stdin | ||
|
||
modes := ssh.TerminalModes{ | ||
ssh.ECHO: 0, | ||
ssh.TTY_OP_ISPEED: 14400, | ||
ssh.TTY_OP_OSPEED: 14400, | ||
} | ||
err = session.RequestPty("xterm", 25, 100, modes) | ||
check(err, "request pty") | ||
|
||
err = session.Shell() | ||
check(err, "start shell") | ||
|
||
err = session.Wait() | ||
check(err, "return") | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controllers | ||
|
||
import ( | ||
"bytes" | ||
"github.com/gorilla/websocket" | ||
"sync" | ||
) | ||
|
||
// copy data from WebSocket to ssh server | ||
// and copy data from ssh server to WebSocket | ||
|
||
// write data to WebSocket | ||
// the data comes from ssh server. | ||
type WebSocketBufferWriter struct { | ||
buffer bytes.Buffer | ||
mu sync.Mutex | ||
} | ||
|
||
// implement Write interface to write bytes from ssh server into bytes.Buffer. | ||
func (w *WebSocketBufferWriter) Write(p []byte) (int, error) { | ||
w.mu.Lock() | ||
defer w.mu.Unlock() | ||
return w.buffer.Write(p) | ||
} | ||
|
||
// flush all data in this buff into WebSocket. | ||
func (w *WebSocketBufferWriter) Flush(messageType int, ws *websocket.Conn) error { | ||
if w.buffer.Len() != 0 { | ||
err := ws.WriteMessage(messageType, []byte(w.buffer.Bytes())) | ||
if err != nil { | ||
return err | ||
} | ||
w.buffer.Reset() | ||
} | ||
return nil | ||
} |
Oops, something went wrong.