forked from dutchcoders/sshproxy
/
main.go
72 lines (55 loc) · 1.73 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"github.com/dutchcoders/sshproxy"
"golang.org/x/crypto/ssh"
)
func main() {
listen := flag.String("listen", ":8022", "listen address")
dest := flag.String("dest", ":22", "destination address")
key := flag.String("key", "conf/id_rsa", "rsa key to use")
flag.Parse()
privateBytes, err := ioutil.ReadFile(*key)
if err != nil {
panic("Failed to load private key")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
var sessions map[net.Addr]map[string]interface{} = make(map[net.Addr]map[string]interface{})
config := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
fmt.Printf("Login attempt: %s, user %s password: %s", c.RemoteAddr(), c.User(), string(pass))
sessions[c.RemoteAddr()] = map[string]interface{}{
"username": c.User(),
"password": string(pass),
}
clientConfig := &ssh.ClientConfig{}
clientConfig.User = c.User()
clientConfig.Auth = []ssh.AuthMethod{
ssh.Password(string(pass)),
}
client, err := ssh.Dial("tcp", *dest, clientConfig)
sessions[c.RemoteAddr()]["client"] = client
return nil, err
},
}
config.AddHostKey(private)
sshproxy.ListenAndServe(*listen, config, func(c ssh.ConnMetadata) (*ssh.Client, error) {
meta, _ := sessions[c.RemoteAddr()]
fmt.Println(meta)
client := meta["client"].(*ssh.Client)
fmt.Printf("Connection accepted from: %s", c.RemoteAddr())
return client, err
}, func(c ssh.ConnMetadata, r io.ReadCloser) (io.ReadCloser, error) {
return sshproxy.NewTypeWriterReadCloser(r), nil
}, func(c ssh.ConnMetadata) error {
fmt.Println("Connection closed.")
return nil
})
}