-
Notifications
You must be signed in to change notification settings - Fork 17
/
launch.go
89 lines (64 loc) · 1.38 KB
/
launch.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
package main
import (
"github.com/danhigham/pty"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"os/exec"
"regexp"
"net"
)
func main() {
bin := "tmate"
cmd := exec.Command(bin)
log.Print("Starting tmate...")
f, err := pty.Start(cmd)
pty.Setsize(f, 1000, 1000)
row, col, _ := pty.Getsize(f)
log.Print(col)
log.Print(row)
if err != nil {
log.Print(err)
}
quit := make(chan int)
go func(r io.Reader) {
sessionRegex, _ := regexp.Compile(`Remote\ssession\:\sssh\s([^\.]+\.tmate.io)`)
// select {
// case <- quit:
// log.Print("Stopping tmate")
// return
// }
for {
buf := make([]byte, 1024)
_, err := r.Read(buf[:])
if err != nil {
return
}
matches := sessionRegex.FindSubmatch(buf)
if len(matches) > 0 {
log.Print(string(matches[1]))
}
}
}(f)
serverUrl, _ := url.Parse("http://127.0.0.1:8080")
reverseProxy := httputil.NewSingleHostReverseProxy(serverUrl)
http.Handle("/", reverseProxy)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
l, _ := net.Listen("tcp", ":" + os.Getenv("PORT"))
go func(){
for _ = range c {
// sig is a ^C, handle it
log.Print("Stopping tmate...")
close(quit)
l.Close()
f.Close()
}
}()
http.Serve(l, nil)
log.Print(err)
}