-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
90 lines (79 loc) · 1.89 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"strings"
"time"
"golang.org/x/crypto/ssh"
)
func sshServer() {
config := &ssh.ServerConfig{
NoClientAuth: true,
}
privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
log.Fatal("failed to read private key")
}
privateKey, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("failed to parse private key")
}
config.AddHostKey(privateKey)
listener, err := net.Listen("tcp", "0.0.0.0:22")
if err != nil {
log.Printf("failed to listen on 22 (listening on 2200 instead): %s", err)
listener, err = net.Listen("tcp", "0.0.0.0:2200")
if err != nil {
log.Fatal("failed to listen on 2200: %s", err)
}
}
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("failed to accept ssh connection: %s", err)
continue
}
go func(conn net.Conn) {
defer conn.Close()
sshConn, chans, _, err := ssh.NewServerConn(conn, config)
if err != nil {
log.Printf("failed to handshake: %s", err)
return
}
log.Printf("New SSH connection from %s '%s'", sshConn.RemoteAddr(), sshConn.ClientVersion())
for newChannel := range chans {
channel, _, err := newChannel.Accept()
if err != nil {
log.Printf("could not accept channel: %s", err)
continue
}
go func(channel ssh.Channel) {
b := make([]byte, 256)
for {
n, err := channel.Read(b)
if err != nil || (n == 1 && (b[0] == 3 || b[0] == 4)) {
channel.Close()
break
}
}
}(channel)
for {
_, err := fmt.Fprintf(channel, strings.Replace(Taco, "\n", "\r\n", -1))
if err != nil {
break
}
time.Sleep(time.Second)
_, err = fmt.Fprintf(channel, "\033[8A" + strings.Replace(Taco2, "\n", "\r\n", -1))
if err != nil {
break
}
time.Sleep(time.Second)
fmt.Fprintf(channel, "\033[8A")
}
channel.Close()
}
}(conn)
}
}