-
Notifications
You must be signed in to change notification settings - Fork 3
/
tech-talk.go
211 lines (170 loc) · 4.59 KB
/
tech-talk.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"embed"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"text/template"
"time"
socketio "github.com/googollee/go-socket.io"
)
//go:embed data www
var fs embed.FS
type TemplateValues struct {
Prefix string
Markdown string
}
const DEFAULT_HOST = "localhost"
const VERSION = "1.2.0"
var indexTemplate *template.Template
var socketServer *socketio.Server
var mdFilename string
var currentUser *user.User
var sshType *string
var sshHost *string
var key *string
var pass *string
var noBrowser *bool
var templatePath *string
// Checks if a file exists and can be accessed.
func check_access(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// Copy data from a reader (e.g. PTY or Stdin pipe) to the web socket.
func copyToSocket(r io.Reader, so socketio.Socket) {
for {
data := make([]byte, 512)
n, err := r.Read(data)
if err != nil {
log.Println(err)
break
}
if n > 0 {
so.Emit("output", string(data))
}
}
}
// Return an HTML page with the slideshow
func indexHandler(w http.ResponseWriter, r *http.Request) {
// If we aren't getting the index itself, serve static files in the
// same directory as the input Markdown slides file.
if r.URL.Path != "/" {
http.FileServer(http.Dir(filepath.Dir(mdFilename))).ServeHTTP(w, r)
return
}
var data TemplateValues
// Read the file on each request so that updates get applied when working
// on the slideshow.
var b []byte
if mdFilename != "" {
b, _ = ioutil.ReadFile(mdFilename)
data.Markdown = string(b)
} else {
b, _ = fs.ReadFile("data/example.md")
data.Markdown = string(b)
}
b, _ = fs.ReadFile("data/prefix.md")
data.Prefix = string(b)
if *templatePath != "" {
b, err := ioutil.ReadFile(*templatePath)
if err != nil {
panic(err)
}
indexTemplate = template.Must(template.New("index").Parse(string(b)))
}
w.Header().Add("Content-Type", "text/html")
indexTemplate.Execute(w, data)
}
// Create a new socket server to handle communication with a PTY shell.
// This allows you to run stuff in a terminal without ever leaving the
// slideshow.
func createSocketServer() {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
socketServer = server
socketServer.On("connection", func(so socketio.Socket) {
log.Printf("Terminal connected from %s\n", so.Request().RemoteAddr)
if *sshType == "internal" || CAN_USE_EXTERNAL == false {
log.Println("Using internal SSH client")
internalSSH(so)
} else {
externalSSH(so)
}
})
socketServer.On("error", func(so socketio.Socket, err error) {
log.Println("Error:", err)
})
}
func main() {
u, err := user.Current()
if err != nil {
log.Fatal("Couldn't get current user!")
}
currentUser = u
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: tech-talk [slides.md]\n")
flag.PrintDefaults()
os.Exit(2)
}
// Connection options
sshHost = flag.String("host", DEFAULT_HOST, "SSH connection [user@]`hostname`[:port]")
sshType = flag.String("ssh", "auto", "SSH `method` [auto, internal]")
// Auth options
keyDefault := ""
idRsaPath := path.Join(currentUser.HomeDir, ".ssh", "id_rsa")
if check_access(idRsaPath) {
keyDefault = idRsaPath
}
key = flag.String("key", keyDefault, "SSH private key `path` (for internal SSH)")
pass = flag.String("pass", "", "SSH `password` (for internal SSH)")
// Misc options
noBrowser = flag.Bool("n", false, "Do not automatically open browser")
version := flag.Bool("v", false, "Alias for --version")
flag.BoolVar(version, "version", false, "Print program version and exit")
templatePath = flag.String("template", "", "Path to custom HTML template")
flag.Parse()
args := flag.Args()
if *version {
fmt.Printf("tech-talk: %s\n", VERSION)
return
}
if len(args) > 0 {
if !check_access(args[0]) {
log.Fatalf("Cannot access %s", args[0])
}
mdFilename = args[0]
}
// Start web sockets
createSocketServer()
http.Handle("/wetty/socket.io/", socketServer)
// Setup web server
indexBytes, _ := fs.ReadFile("data/index.template")
indexTemplate = template.Must(template.New("index").Parse(string(indexBytes)))
http.HandleFunc("/", indexHandler)
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.FS(fs))))
s := &http.Server{
Addr: ":4000",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Server started on http://localhost:4000/")
if !*noBrowser && check_access("/usr/bin/open") {
c := exec.Command("/usr/bin/open", "http://localhost:4000")
c.Start()
}
log.Panic(s.ListenAndServe())
}