-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
155 lines (131 loc) · 3.35 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"runtime/debug"
"strconv"
"syscall"
"time"
)
var client http.Client
var orgSrvURL string
var quality = 90
var version = ""
func main() {
var ver bool
flag.BoolVar(&ver, "version", false, "show version")
flag.Parse()
if ver {
fmt.Printf("oyaki %s\n", getVersion())
return
}
orgScheme := os.Getenv("OYAKI_ORIGIN_SCHEME")
orgHost := os.Getenv("OYAKI_ORIGIN_HOST")
if orgScheme == "" {
orgScheme = "https"
}
orgSrvURL = orgScheme + "://" + orgHost
if q := os.Getenv("OYAKI_QUALITY"); q != "" {
quality, _ = strconv.Atoi(q)
}
log.Printf("starting oyaki %s\n", getVersion())
http.HandleFunc("/", proxy)
http.ListenAndServe(":8080", nil)
}
func proxy(w http.ResponseWriter, r *http.Request) {
path := r.URL.RequestURI()
if path == "/" {
fmt.Fprintln(w, "Oyaki lives!")
return
}
orgURL, err := url.Parse(orgSrvURL + path)
if err != nil {
http.Error(w, "Invalid origin URL", http.StatusBadRequest)
log.Printf("Invalid origin URL. %v\n", err)
return
}
req, err := http.NewRequest("GET", orgURL.String(), nil)
if err != nil {
http.Error(w, "Request Failed", http.StatusInternalServerError)
log.Printf("Request Failed. %v\n", err)
return
}
req.Header.Set("User-Agent", "oyaki")
if r.Header.Get("If-Modified-Since") != "" {
req.Header.Set("If-Modified-Since", r.Header.Get("If-Modified-Since"))
}
xff := r.Header.Get("X-Forwarded-For")
if len(xff) > 1 {
req.Header.Set("X-Forwarded-For", xff)
}
orgRes, err := client.Do(req)
if err != nil {
http.Error(w, "Get origin failed", http.StatusBadGateway)
log.Printf("Get origin failed. %v\n", err)
return
}
defer orgRes.Body.Close()
if orgRes.StatusCode == http.StatusNotModified {
w.WriteHeader(http.StatusNotModified)
return
}
if orgRes.StatusCode != http.StatusOK {
http.Error(w, "Get origin failed", http.StatusBadGateway)
log.Printf("Get origin failed. %v\n", orgRes.Status)
return
}
ct := orgRes.Header.Get("Content-Type")
cl := orgRes.Header.Get("Content-Length")
if ct != "image/jpeg" {
w.Header().Set("Content-Type", ct)
if cl != "" {
w.Header().Set("Content-Length", cl)
}
_, err := io.Copy(w, orgRes.Body)
if err != nil {
// ignore already close client.
if !errors.Is(err, syscall.EPIPE) {
http.Error(w, "Read origin body failed", http.StatusInternalServerError)
log.Printf("Read origin body failed. %v\n", err)
}
}
return
}
buf, err := convert(orgRes.Body, quality)
if err != nil {
http.Error(w, "Image convert failed", http.StatusInternalServerError)
log.Printf("Image convert failed. %v\n", err)
return
}
defer buf.Reset()
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
if orgRes.Header.Get("Last-Modified") != "" {
w.Header().Set("Last-Modified", orgRes.Header.Get("Last-Modified"))
} else {
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
}
if _, err := io.Copy(w, buf); err != nil {
// ignore already close client.
if !errors.Is(err, syscall.EPIPE) {
http.Error(w, "Write responce failed", http.StatusInternalServerError)
log.Printf("Write responce failed. %v\n", err)
}
}
}
func getVersion() string {
if version != "" {
return version
}
i, ok := debug.ReadBuildInfo()
if !ok {
return "(unknown)"
}
return i.Main.Version
}