aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 07e15b12d3418b1c3c3c699497fd4a9caa32139e (plain)
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
package main

import (
	"bytes"
	"crypto/sha1"
	"encoding/base64"
	"encoding/json"
	"flag"
	"html/template"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
)

var (
	tls     = flag.String("t", ":8443", "[ip]:port to tls-listen to")
	nontls  = flag.String("l", "", "optional, non-tls [ip]:port to listen to")
	config  = flag.String("cfg", "tools.json", "config file with tool-definitions")
	cert    = flag.String("cert", "cert.pem", "certitifate")
	key     = flag.String("key", "key.pem", "key")
	savedir = flag.String("d", "saved", "direcotry to save the pics.")
)

type Tool struct {
	Name          string
	Cmd           string
	Args          []string
	NeedsFile     bool
	ContentType   string
	Suffix        string
	Description   string
	Documentation map[string]string
	Example       string
	BgColor       string
}

func (t *Tool) execute(in io.Reader, w http.ResponseWriter) {
	var args []string

	if t.NeedsFile {
		tmpf, e := ioutil.TempFile(".", "*"+t.Suffix)
		if e != nil {
			log.Printf("couldn't create tmp-file: %v\n", e)
			http.Error(w, e.Error(), http.StatusInternalServerError)
			return
		} else if _, e = io.Copy(tmpf, in); e != nil {
			log.Printf("couldn't write to tmp-file: %v\n", e)
			http.Error(w, e.Error(), http.StatusInternalServerError)
			return
		}
		defer os.Remove(tmpf.Name())
		// log.Printf("using tempfile: %q\n", tmpf.Name())

		args = []string{}
		args = append(args, t.Args...)
		args = append(args, tmpf.Name())
	} else {
		args = t.Args
	}

	var (
		cmd      = exec.Command(t.Cmd, args...)
		err, buf = &bytes.Buffer{}, &bytes.Buffer{}
	)

	if !t.NeedsFile {
		cmd.Stdin = in
	}
	cmd.Stderr = err
	cmd.Stdout = buf

	if e := cmd.Run(); e == nil {
		if t.ContentType != "" {
			w.Header().Add("Content-Type", t.ContentType)
			io.Copy(base64.NewEncoder(base64.StdEncoding, w), buf)
		} else {
			io.Copy(w, buf)
		}
	} else {
		log.Printf("%s returned error\n", t.Name)
		http.Error(w, err.String(), http.StatusBadRequest)
	}
}

func (t *Tool) compile() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		t.execute(r.Body, w)
	}
}

func (t *Tool) svg() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		name := filepath.Base(r.URL.Path)
		if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil {
			log.Println(err)
			http.Error(w, "couldn't open file", http.StatusBadRequest)
		} else {
			defer file.Close()
			t.execute(file, w)
		}
	}
}

const maxSnippetSize = 64 * 1024

func (t *Tool) save() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		body, err := ioutil.ReadAll(io.LimitReader(r.Body, maxSnippetSize))
		if err != nil {
			log.Println(err)
			http.Error(w, "body too large", http.StatusBadRequest)
			return
		}
		r.Body.Close()

		// Create a filename taking the first 10 characters of the sha1-sum of
		// content.  Based on code from the golang-playground.
		h := sha1.New()
		io.Copy(h, bytes.NewBuffer(body))
		sum := h.Sum(nil)
		b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
		base64.URLEncoding.Encode(b, sum)
		name := string(b)[:10]

		// Write snippet to file.  TODO: Shall we return error if file exists?
		if file, err := os.Create(filepath.Join(*savedir, name+t.Suffix)); err != nil {
			log.Println(err)
			http.Error(w, "couldn't create file", http.StatusInternalServerError)
		} else if err = file.Chmod(0644); err != nil {
			log.Println(err)
			http.Error(w, "couldn't setup file", http.StatusInternalServerError)
		} else if _, err = io.Copy(file, bytes.NewBuffer(body)); err != nil {
			log.Println(err)
			http.Error(w, "couldn't write file", http.StatusInternalServerError)
		} else if err = file.Close(); err != nil {
			log.Println(err)
			http.Error(w, "couldn't close file", http.StatusInternalServerError)
		}
		w.Write([]byte(name))
	}
}

func (t *Tool) load() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		name := filepath.Base(r.URL.Path)
		if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil {
			log.Println(err)
			http.Error(w, "couldn't open file", http.StatusBadRequest)
		} else {
			defer file.Close()
			io.Copy(w, file)
		}
	}
}

func (t *Tool) index(tmpl *template.Template, data interface{}) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		err := tmpl.ExecuteTemplate(w, "index.html", map[string]interface{}{"Tools": data, "Cur": t})
		if err != nil {
			log.Printf("error executing template for %s: %v", t.Name, err)
		}
	}
}

func main() {
	var tools = []*Tool{}
	var tmpl *template.Template

	flag.Parse()
	if cfg, err := os.Open(*config); err != nil {
		log.Fatal(err)
	} else if err = json.NewDecoder(cfg).Decode(&tools); err != nil {
		log.Fatalf("error loading %s: %v\n", *config, err)
	} else if tmpl, err = template.ParseFiles("index.html"); err != nil {
		log.Fatalf("error parsing index.html: %v", err)
	}

	for _, tool := range tools {
		pre := "/" + tool.Name + "/"
		http.HandleFunc(pre+"c", tool.compile())
		http.HandleFunc(pre+"s", tool.save())
		http.HandleFunc(pre+"l/", tool.load())
		http.HandleFunc(pre+"svg/", tool.svg())
		http.HandleFunc(pre, tool.index(tmpl, tools))
		log.Println("handler for", pre, "registered")
	}
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/"+tools[0].Name, http.StatusFound)
	})

	if len(*nontls) > 0 {
		log.Println("listening non-tls on", *nontls)
		log.Fatal(http.ListenAndServe(*nontls, nil))
	}
	log.Println("listening tls on", *tls)
	log.Fatal(http.ListenAndServeTLS(*tls, *cert, *key, nil))
}