/
launcher.go
84 lines (74 loc) · 2.36 KB
/
launcher.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
package tunnel
import (
"fmt"
"github.com/metal3d/idok/tunnel/go.crypto/ssh"
"github.com/metal3d/idok/utils"
"io"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"time"
)
// SshForward digs a tunnel to xbmc/kodi, then open a port and bind socket to
// the local http server
func SshHTTPForward(config *ssh.ClientConfig, file, dir string) {
// Setup sshClientConn (type *ssh.ClientConn)
sshClientConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", utils.GlobalConfig.Target, utils.GlobalConfig.Sshport), config)
if err != nil {
log.Fatal(err)
}
// Setup sshConn (type net.Conn)
// Because dropbear doesn't accept :0 port to open random port
// we do the randomisation ourself
rand.Seed(int64(time.Now().Nanosecond()))
dport := 10000 + rand.Intn(9999)
tries := 0
sshConn, err := sshClientConn.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", dport))
for err != nil && tries < 500 {
dport = 10000 + rand.Intn(9999)
sshConn, err = sshClientConn.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", dport))
tries++
}
log.Println("Listening port on raspberry: ", dport)
// send xbmc the file query
go utils.Send("http", "localhost", file, dport)
// now serve file
fullpath := filepath.Join(dir, file)
http.Serve(sshConn, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, fullpath)
}))
}
// SshForwardStdin reads stdin and stream this to distant socket
// through SSH tunnel
func SshForwardStdin(config *ssh.ClientConfig) {
// Setup sshClientConn (type *ssh.ClientConn)
sshClientConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", utils.GlobalConfig.Target, utils.GlobalConfig.Sshport), config)
if err != nil {
log.Fatal(err)
}
// Setup sshConn (type net.Conn)
// Because dropbear doesn't accept :0 port to open random port
// we do the randomisation ourself
rand.Seed(int64(time.Now().Nanosecond()))
dport := 10000 + rand.Intn(9999)
tries := 0
sshConn, err := sshClientConn.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", dport))
for err != nil && tries < 500 {
dport = 10000 + rand.Intn(9999)
sshConn, err = sshClientConn.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", dport))
tries++
}
log.Println("Listening port on the target: ", dport)
// send xbmc the file query
go utils.Send("tcp", "127.0.0.1", "", dport)
if err != nil {
log.Fatal(err)
}
c, err := sshConn.Accept()
if err != nil {
log.Fatal(err)
}
go io.Copy(c, os.Stdin)
}