/
main.go
226 lines (199 loc) · 5.62 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"crypto/sha256"
"net"
"os"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/client"
"github.com/namsral/flag"
"github.com/pkg/errors"
)
type imageMap struct {
sync.RWMutex
m map[string][]byte
}
func NewImageMap() *imageMap {
return &imageMap{
m: map[string][]byte{},
}
}
func (im *imageMap) Add(h string, d []byte) {
im.Lock()
im.m[h] = d
im.Unlock()
logrus.Debugf("added %s", h)
}
func (im *imageMap) Remove(h string) {
im.Lock()
delete(im.m, h)
im.Unlock()
logrus.Debugf("removed %s", h)
}
func (im *imageMap) Get(h string) ([]byte, bool) {
im.RLock()
bytes, ok := im.m[h]
im.RUnlock()
return bytes, ok
}
func main() {
fs := flag.NewFlagSetWithEnvPrefix(os.Args[0], "SVG2PNG", 0)
flagPort := fs.Int("port", 8544, "port to listen to")
flagTimeout := fs.Int("timeout", 30, "initial timeout")
flagURLs := fs.String("urls", "", "urls to chrome rdp (csv)")
flagHosts := fs.String("hosts", "", "hosts with running chrome rdp (csv)")
flagSelf := fs.String("self", "svg2png", "url under which chrome can reach this service (port is added automatically)")
fs.Parse(os.Args[1:])
if *flagHosts == "" && *flagURLs == "" {
*flagURLs = "http://localhost:9222/json"
}
selfURL := fmt.Sprintf("http://%s:%d/v1/svg-html/", *flagSelf, *flagPort)
logrus.SetLevel(logrus.DebugLevel)
chromes, err := createCDPClients(*flagURLs, *flagHosts, *flagTimeout)
if err != nil {
logrus.Fatal(err)
}
images := NewImageMap()
mux := http.NewServeMux()
mux.HandleFunc("/v1/svg-html/", htmlHandler)
mux.HandleFunc("/v1/svg-data/", dataHandler(images))
mux.HandleFunc("/v1/png", mainHandler(images, chromes, selfURL))
mux.HandleFunc("/healthz", healthzHandler)
logrus.Debugf("listening on :%d", *flagPort)
http.ListenAndServe(fmt.Sprintf(":%d", *flagPort), mux)
}
func fetchImages(url *url.URL, res *[]byte) chromedp.Tasks {
sel := `#svg`
return chromedp.Tasks{
chromedp.Navigate(url.String()),
//chromedp.Sleep(2000 * time.Millisecond),
chromedp.WaitVisible(sel, chromedp.ByID),
//chromedp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, chromedp.ByQuery),
chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByID),
//chromedp.CaptureScreenshot(res),
}
}
func createCDPClients(url, host string, timeout int) (chan *chromedp.CDP, error) {
if url != "" && host != "" {
return nil, fmt.Errorf("url and host parameters are mutually exclusive(u:'%s', h:'%s'", url, host)
}
var urls []string
switch {
case host != "":
urls = make([]string, 0, 5)
for _, h := range strings.Split(host, ",") {
addrs, err := net.LookupHost(h)
if err != nil {
return nil, errors.Wrapf(err, "could not resolve '%s'", h)
}
for _, a := range addrs {
urls = append(urls, fmt.Sprintf("http://%s:9222/json", a))
}
}
default: // url parameter has a default so will never be empty
urls = strings.Split(url, ",")
}
if len(urls) == 0 {
return nil, errors.New("at least one chrome instance must be reachable")
}
s := "s"
if len(urls) == 1 {
s = ""
}
logrus.Infof("using %d chrome instance%s as target%s", len(urls), s, s)
ctx := context.Background()
chromes := make(chan *chromedp.CDP, len(urls))
for _, u := range urls {
u = strings.TrimSpace(u)
start := time.Now()
var c *chromedp.CDP
var err error
for {
c, err = chromedp.New(ctx,
chromedp.WithTargets(client.New(client.URL(u)).WatchPageTargets(ctx)),
chromedp.WithErrorf(logrus.Errorf))
if err == nil ||
int(time.Now().Sub(start).Seconds()) > timeout {
break
}
logrus.Debugf("trying '%s' again after %s", u, err)
}
if err != nil {
return nil, err
}
chromes <- c
}
return chromes, nil
}
func htmlHandler(w http.ResponseWriter, r *http.Request) {
ch := r.URL.Path[len("/v1/svg-html/"):]
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<html><body><img id="svg" src="/v1/svg-data/` + ch + `" /></body></html>`))
}
func dataHandler(images *imageMap) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ch := r.URL.Path[len("/v1/svg-data/"):]
bytes, ok := images.Get(ch)
if !ok {
logrus.Warn(ch)
http.Error(w, "404 Image not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "image/svg+xml")
bw, err := w.Write(bytes)
if err != nil {
logrus.Warn(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if bw != len(bytes) {
logrus.Warnf("incomplete write %s: %d/%d", ch, bw, len(bytes))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func mainHandler(images *imageMap, chromes chan *chromedp.CDP, selfURL string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h := sha256.New()
h.Write([]byte(time.Now().UTC().String()))
body, err := ioutil.ReadAll(io.TeeReader(r.Body, h))
if err != nil {
logrus.Warn(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ch := fmt.Sprintf("%x.svg", h.Sum([]byte{}))
images.Add(ch, body)
defer images.Remove(ch)
imageURL, err := url.Parse(fmt.Sprintf("%s%s", selfURL, ch))
if err != nil {
logrus.Warn(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var res []byte
c := <-chromes
err = c.Run(r.Context(), fetchImages(imageURL, &res))
chromes <- c
if err != nil {
logrus.Warn(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(res)
}
}
func healthzHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK\n"))
}