-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinit.go
195 lines (178 loc) · 4.18 KB
/
init.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
package cdnfinder
//go:generate go-bindata -pkg resource -o resource/assets.go -nomemcopy assets/...
import (
"archive/tar"
"archive/zip"
"compress/bzip2"
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
"github.com/turbobytes/cdnfinder/resource"
)
var (
cdnmatches [][]string
resourcefinderjs = os.TempDir() + "/cdnfinder-resourcefinder.js"
phantomjsbin = ""
phantomdef = flag.String("phantomjsbin", "", "path to phantomjs, if blank tmp dir is used")
initialized = false
)
//Load cname chain
func populatecnamechain() {
raw, err := resource.Asset("assets/cnamechain.json")
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(raw, &cdnmatches)
if err != nil {
log.Fatal(err)
}
}
func assettofile(key, dst string) {
if _, err := os.Stat(dst); os.IsNotExist(err) {
raw, err := resource.Asset(key)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(dst, raw, 0644)
if err != nil {
log.Fatal("WriteFile", err)
}
} else {
log.Println(dst, " exists already...")
}
}
//Make sure resourcefinder.js is present somewhere and get a handle to it
func ensureresourcefinder() {
assettofile("assets/resourcefinder.js", resourcefinderjs)
}
func notsupported() {
log.Fatal("OS/ARCH not supported:", runtime.GOOS, runtime.GOARCH)
}
func installphantomjs(url, dst, fname string) {
log.Println("downloading", url)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if strings.HasSuffix(url, ".tar.bz2") {
extracttar(resp.Body, fname, dst)
}
if strings.HasSuffix(url, ".zip") {
extractzip(resp.Body, fname, dst)
}
}
func extractzip(r io.Reader, fname, dst string) {
tmpfile, err := ioutil.TempFile("", "phantomzip")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
_, err = io.Copy(tmpfile, r)
if err != nil {
log.Fatal(err)
}
tmpfile.Close()
zrd, err := zip.OpenReader(tmpfile.Name())
if err != nil {
log.Fatal(err)
}
for _, f := range zrd.File {
info := f.FileInfo()
if !info.IsDir() {
if strings.HasSuffix(f.Name, fname) {
log.Println(f.Name)
file, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
log.Fatal(err)
}
defer file.Close()
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(file, rc)
if err != nil {
log.Fatal(err)
}
}
}
}
}
func extracttar(r io.Reader, fname, dst string) {
trd := tar.NewReader(bzip2.NewReader(r))
for {
header, err := trd.Next()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
info := header.FileInfo()
if !info.IsDir() {
if strings.HasSuffix(header.Name, fname) {
file, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = io.Copy(file, trd)
if err != nil {
log.Fatal(err)
}
}
}
}
}
//Install phantomjs
func loadphantomjs() {
fname := ""
url := ""
switch runtime.GOOS {
case "windows":
fname = "phantomjs.exe"
url = "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-windows.zip"
case "linux":
fname = "phantomjs"
switch runtime.GOARCH {
case "amd64":
url = "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2"
case "386":
url = "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-i686.tar.bz2"
default:
notsupported()
}
case "darwin":
fname = "phantomjs"
url = "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip"
default:
notsupported()
}
if *phantomdef != "" {
phantomjsbin = *phantomdef
} else {
phantomjsbin = os.TempDir() + "/cdnfinder_2.1.1_" + fname
}
if _, err := os.Stat(phantomjsbin); os.IsNotExist(err) {
installphantomjs(url, phantomjsbin, fname)
} else {
log.Println(phantomjsbin, "is already installed")
}
}
// Init must be called once after the main package runs flag.Parse().
// The reason for doing this manually is to allow main package to set up its flags.
func Init() {
if initialized {
return
}
initialized = true
populatecnamechain()
ensureresourcefinder()
loadphantomjs()
}