-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (115 loc) · 3.47 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/muesli/cache2go"
"willnorris.com/go/imageproxy"
)
// LinkpreviewAPI endpoint
var LinkpreviewAPI = "https://api.linkpreview.net"
var linkpreviewKey = ""
// Cache engine for the app
var Cache *cache2go.CacheTable
// CachedResponse struct
type CachedResponse struct {
Body []byte
Status int
}
type lpreq struct {
Key string `json:"key"`
Q string `json:"q"`
Fields string `json:"fields"`
}
func init() {
Cache = cache2go.Cache("myCache")
}
func main() {
fmt.Println("LinkPreview Proxy Server v2.0.0")
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
addr := os.Getenv("ADDR")
linkpreviewKey = os.Getenv("LINK_PREVIEW_KEY")
http.HandleFunc("/linkpreview/", MWrefererCheck(os.Getenv("REFERER"), LinkpreviewProxyHandler))
http.HandleFunc("/imageproxy/", MWrefererCheck(os.Getenv("REFERER"), ImageProxyHandler))
if os.Getenv("SSL_CERT") != "" && os.Getenv("SSL_KEY") != "" {
fmt.Printf("Proxy secure server started on https://%s\n", addr)
if err := http.ListenAndServeTLS(addr, os.Getenv("SSL_CERT"), os.Getenv("SSL_KEY"), nil); err != nil {
panic(err)
}
}
fmt.Printf("Proxy server started on http://%s\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
panic(err)
}
}
// LinkpreviewProxyHandler is a Proxy for the API
func LinkpreviewProxyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
query := r.URL.Query().Get("q")
// keep results in cache for one day (based on the key scheme)
cacheKey := fmt.Sprintf("%s%d", query, time.Now().Day())
cached, err := Cache.Value(cacheKey)
if err == nil {
fmt.Println("Serving from Cache: " + query)
w.WriteHeader(cached.Data().(*CachedResponse).Status)
w.Write(cached.Data().(*CachedResponse).Body)
return
}
body, _ := json.Marshal(&lpreq{
Key: linkpreviewKey,
Q: query,
Fields: "title,description,image,url", // see https://docs.linkpreview.net/#query-parameters
})
fmt.Println("Requesting from the API: " + query)
client := &http.Client{}
req, _ := http.NewRequest("POST", LinkpreviewAPI, bytes.NewBuffer(body))
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
cr := CachedResponse{}
cr.Body, err = ioutil.ReadAll(resp.Body)
cr.Status = resp.StatusCode
if err != nil {
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
w.WriteHeader(cr.Status)
w.Write(cr.Body)
// add to cache and automatically expire unused after one day
Cache.Add(cacheKey, 24*time.Hour, &cr)
}
// ImageProxyHandler is a Proxy for serving images
// for advanced usage see https://github.com/willnorris/imageproxy
func ImageProxyHandler(w http.ResponseWriter, r *http.Request) {
p := imageproxy.NewProxy(nil, nil)
// convert query input to root path, imageproxy will be confused otherwise
r.URL.Path = "/" + r.URL.Query().Get("src")
p.ServeHTTP(w, r)
}
// MWrefererCheck is a Middleware that can protect agains unknown referrers
func MWrefererCheck(referer string, next http.HandlerFunc) http.HandlerFunc {
// skip if not configured
if referer == "" {
return next
}
return func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.Referer(), referer) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
next(w, r)
}
}