-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvgproxy.go
More file actions
189 lines (150 loc) · 4.67 KB
/
vgproxy.go
File metadata and controls
189 lines (150 loc) · 4.67 KB
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
package main
import (
"net/http"
"io"
"log"
"flag"
"bufio"
"strings"
"encoding/base64"
"bytes"
"time"
"sync"
"strconv"
// rolling file logging
"github.com/natefinch/lumberjack"
// optional gzip
"github.com/NYTimes/gziphandler"
)
const Path = "/csv"
var (
url = flag.String("url", "", "remote csv url to connect to (default: none)")
port = flag.Int("port", 80, "port to bind HTTP to")
addr = flag.String("addr", "", "addr to bind HTTP and HTTPS to (default: none i.e. 0.0.0.0)")
sleep = flag.Int("sleep", 600, "seconds between refresh of cached csv")
tlsCert = flag.String("cert", "", "cert file for HTTPS on port 443 (default: none)")
tlsKey = flag.String("key", "", "key file for HTTPS on port 443 (default: none)")
gzip = flag.Bool("gzip", false, "enable gzip support (default: disabled)")
csvBody *string
csvDate *string
csvLength *string
bodyLock = sync.RWMutex{}
)
func setBody(body *string) {
now := time.Now().UTC().Format(time.RFC3339)
length := strconv.Itoa(len(*body))
bodyLock.Lock()
csvBody = body
csvDate = &now
csvLength = &length
bodyLock.Unlock()
}
func getBody() (body, length, date *string) {
bodyLock.RLock()
body = csvBody
date = csvDate
length = csvLength
bodyLock.RUnlock()
return
}
func loadBody() bool {
log.Print("loadBody() starting download")
resp, err := http.Get(*url)
if err != nil {
log.Printf("loadBody() request to url failed %v", err)
return false
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Printf("loadBody() request to url failed %d", resp.StatusCode)
return false
}
// remove all comments from ovpn config files to reduce size
csvScanner := bufio.NewScanner(resp.Body)
body := ""
for csvScanner.Scan() {
line := csvScanner.Text()
if (len(line) > 0 && line[0] != '#') {
i := strings.LastIndex(line, ",")
if (i > 0) {
certRaw, err := base64.StdEncoding.DecodeString(line[i+1:])
if err != nil {
log.Printf("loadBody() base decode failed %v", err)
return false
}
certScanner := bufio.NewScanner(bytes.NewReader(certRaw))
cleanCert := ""
for certScanner.Scan() {
certLine := certScanner.Text()
if (len(certLine) > 0 && certLine[0] != ';' && certLine[0] != '#') {
cleanCert += certLine + "\n"
}
}
line = line[:i+1] + base64.StdEncoding.EncodeToString([]byte(cleanCert))
}
}
body += line + "\n"
}
if err := csvScanner.Err(); err != nil {
log.Printf("loadBody() reading error: %v", err)
} else if len(body) <= 0 {
log.Print("loadBody() body is nil or empty")
} else {
log.Printf("loadBody() successfully finished: now %d bytes", len(body))
setBody(&body)
return true
}
return false
}
func refreshLoop() {
timing := time.Tick(time.Duration(*sleep) * time.Second)
for _ = range timing {
go loadBody()
}
}
func getCSV(w http.ResponseWriter, r *http.Request) {
log.Printf("getCSV() answering request: %s", r.RemoteAddr)
body, length, date := getBody()
// custom header in response to see age of csv
w.Header().Set("X-Source-Date", *date)
w.Header().Set("Content-Length", *length)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
// answer request
io.WriteString(w, *body)
}
func main() {
// parse arguments
flag.Parse()
if len(*url) == 0 {
flag.PrintDefaults()
return
}
// init logging
log.SetOutput(&lumberjack.Logger{
Filename: "vg" + strconv.Itoa(*port) + ".log",
MaxSize: 10, // MB
MaxBackups: 10,
MaxAge: 21, // days
})
// load csv before starting server
for ! loadBody() {
time.Sleep(1 * time.Second)
}
// start refresh
go refreshLoop()
if *gzip {
// optional GZip
log.Print("main() enable gzip support")
handler := gziphandler.GzipHandler(http.HandlerFunc(getCSV))
http.Handle(Path, handler)
} else {
http.HandleFunc(Path, getCSV)
}
if len(*tlsCert) > 0 || len(*tlsKey) > 0 {
// optional HTTPS
log.Print("main() enable HTTPS support")
go func() { log.Fatal(http.ListenAndServeTLS(*addr + ":443", *tlsCert, *tlsKey, nil)) }()
}
// HTTP
log.Fatal(http.ListenAndServe(*addr + ":" + strconv.Itoa(*port), nil))
}