summaryrefslogtreecommitdiff
path: root/termshare/internal
diff options
context:
space:
mode:
Diffstat (limited to 'termshare/internal')
-rw-r--r--termshare/internal/client.go_86
-rw-r--r--termshare/internal/daemon/daemon.go56
-rw-r--r--termshare/internal/forwarder.go_37
3 files changed, 179 insertions, 0 deletions
diff --git a/termshare/internal/client.go_ b/termshare/internal/client.go_
new file mode 100644
index 0000000..1845a15
--- /dev/null
+++ b/termshare/internal/client.go_
@@ -0,0 +1,86 @@
+package main
+
+import (
+ "fmt"
+ "golang.org/x/crypto/ssh"
+ "log"
+ "os"
+ // Uncomment to store output in variable
+ //"bytes"
+)
+
+func main() {
+
+ username := "root"
+ password := ""
+ hostname := "127.0.0.1"
+ port := "2222"
+
+ // SSH client config
+ config := &ssh.ClientConfig{
+ User: username,
+ Auth: []ssh.AuthMethod{
+ ssh.Password(password),
+ },
+ // Non-production only
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+
+ // Connect to host
+ client, err := ssh.Dial("tcp", hostname+":"+port, config)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer client.Close()
+
+ // Create sesssion
+ sess, err := client.NewSession()
+ if err != nil {
+ log.Fatal("Failed to create session: ", err)
+ }
+ defer sess.Close()
+
+ // StdinPipe for commands
+ stdin, err := sess.StdinPipe()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Uncomment to store output in variable
+ //var b bytes.Buffer
+ //sess.Stdout = &b
+ //sess.Stderr = &b
+
+ // Enable system stdout
+ // Comment these if you uncomment to store in variable
+ sess.Stdout = os.Stdout
+ sess.Stderr = os.Stderr
+
+ // Start remote shell
+ err = sess.Shell()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // send the commands
+ commands := []string{
+ "whoami",
+ "exit",
+ }
+ for _, cmd := range commands {
+ _, err = fmt.Fprintf(stdin, "%s\n", cmd)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+
+ // Wait for sess to finish
+ err = sess.Wait()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Uncomment to store in variable
+ //fmt.Println(b.String())
+
+}
diff --git a/termshare/internal/daemon/daemon.go b/termshare/internal/daemon/daemon.go
new file mode 100644
index 0000000..465cdff
--- /dev/null
+++ b/termshare/internal/daemon/daemon.go
@@ -0,0 +1,56 @@
+package daemon
+
+import (
+ "fmt"
+ "bufio"
+ "io"
+ "log"
+ "os"
+ "os/exec"
+ "syscall"
+ "strconv"
+ "unsafe"
+
+ "github.com/gliderlabs/ssh"
+ "github.com/creack/pty"
+)
+
+func setWinsize(f *os.File, w, h int) {
+ syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
+ uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
+}
+
+func RunDaemon(port string) {
+ ssh.Handle(func(s ssh.Session) {
+ cmd := exec.Command("bash")
+ ptyReq, winCh, isPty := s.Pty()
+ if isPty {
+ cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
+ f, err := pty.Start(cmd)
+ if err != nil {
+ panic(err)
+ }
+ go func() {
+ for win := range winCh {
+ setWinsize(f, win.Width, win.Height)
+ }
+ }()
+ go func() {
+ io.Copy(f, s) // stdin
+ }()
+ io.Copy(s, f) // stdout
+ cmd.Wait()
+ } else {
+ io.WriteString(s, "No PTY requested.\n")
+ reader := bufio.NewReader(s)
+ for {
+ input, _, _ := reader.ReadRune()
+ io.WriteString(os.Stdout, strconv.QuoteRune(input))
+ }
+ s.Exit(1)
+ }
+ })
+
+ log.Println("starting ssh server on port " + port + " ...")
+ log.Fatal(ssh.ListenAndServe(":" + port, nil))
+}
diff --git a/termshare/internal/forwarder.go_ b/termshare/internal/forwarder.go_
new file mode 100644
index 0000000..2ce866f
--- /dev/null
+++ b/termshare/internal/forwarder.go_
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "io"
+ "log"
+
+ "github.com/gliderlabs/ssh"
+)
+
+func main() {
+
+ log.Println("starting ssh server on port 2222...")
+
+ forwardHandler := &ssh.ForwardedTCPHandler{}
+
+ server := ssh.Server{
+ LocalPortForwardingCallback: ssh.LocalPortForwardingCallback(func(ctx ssh.Context, dhost string, dport uint32) bool {
+ log.Println("Accepted forward", dhost, dport)
+ return true
+ }),
+ Addr: ":2222",
+ Handler: ssh.Handler(func(s ssh.Session) {
+ io.WriteString(s, "Remote forwarding available...\n")
+ select {}
+ }),
+ ReversePortForwardingCallback: ssh.ReversePortForwardingCallback(func(ctx ssh.Context, host string, port uint32) bool {
+ log.Println("attempt to bind", host, port, "granted")
+ return true
+ }),
+ RequestHandlers: map[string]ssh.RequestHandler{
+ "tcpip-forward": forwardHandler.HandleSSHRequest,
+ "cancel-tcpip-forward": forwardHandler.HandleSSHRequest,
+ },
+ }
+
+ log.Fatal(server.ListenAndServe())
+}