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
|
package web
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"adammathes.com/neko/api"
"adammathes.com/neko/config"
rice "github.com/GeertJohan/go.rice"
"golang.org/x/crypto/bcrypt"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
serveBoxedFile(w, r, "ui.html")
}
func imageProxyHandler(w http.ResponseWriter, r *http.Request) {
imgURL := r.URL.String()
decodedURL, err := base64.URLEncoding.DecodeString(imgURL)
// pseudo-caching
if r.Header.Get("If-None-Match") == string(decodedURL) {
w.WriteHeader(http.StatusNotModified)
return
}
if r.Header.Get("Etag") == string(decodedURL) {
w.WriteHeader(http.StatusNotModified)
return
}
// grab the img
c := &http.Client{
Timeout: 5 * time.Second,
}
request, err := http.NewRequest("GET", string(decodedURL), nil)
if err != nil {
http.Error(w, "failed to proxy image", 404)
return
}
userAgent := "neko RSS Reader Image Proxy +https://github.com/adammathes/neko"
request.Header.Set("User-Agent", userAgent)
resp, err := c.Do(request)
if err != nil {
http.Error(w, "failed to proxy image", 404)
return
}
bts, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "failed to read proxy image", 404)
return
}
w.Header().Set("ETag", string(decodedURL))
w.Header().Set("Cache-Control", "public")
w.Header().Set("Expires", time.Now().Add(48*time.Hour).Format(time.RFC1123))
w.Write(bts)
}
var AuthCookie = "auth"
var SecondsInAYear = 60 * 60 * 24 * 365
func loginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
serveBoxedFile(w, r, "login.html")
case "POST":
password := r.FormValue("password")
if password == config.Config.DigestPassword {
v, _ := bcrypt.GenerateFromPassword([]byte(password), 0)
c := http.Cookie{Name: AuthCookie, Value: string(v), Path: "/", MaxAge: SecondsInAYear, HttpOnly: false}
http.SetCookie(w, &c)
http.Redirect(w, r, "/", 307)
} else {
http.Error(w, "bad login", 401)
}
default:
http.Error(w, "nope", 500)
}
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
c := http.Cookie{Name: AuthCookie, MaxAge: 0, Path: "/", HttpOnly: false}
http.SetCookie(w, &c)
fmt.Fprintf(w, "you are logged out")
}
func Authenticated(r *http.Request) bool {
pc, err := r.Cookie("auth")
if err != nil {
return false
}
err = bcrypt.CompareHashAndPassword([]byte(pc.Value), []byte(config.Config.DigestPassword))
return err == nil
}
func AuthWrap(wrapped http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if Authenticated(r) {
wrapped(w, r)
} else {
http.Redirect(w, r, "/login/", 307)
}
}
}
func AuthWrapHandler(wrapped http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if Authenticated(r) {
wrapped.ServeHTTP(w, r)
} else {
http.Redirect(w, r, "/login/", 307)
}
})
}
func serveBoxedFile(w http.ResponseWriter, r *http.Request, filename string) {
box := rice.MustFindBox("../static")
ui, err := box.Open(filename)
if err != nil {
panic(err)
}
fi, _ := ui.Stat()
http.ServeContent(w, r, filename, fi.ModTime(), ui)
}
func Serve() {
box := rice.MustFindBox("../static")
staticFileServer := http.StripPrefix("/static/", http.FileServer(box.HTTPBox()))
http.Handle("/static/", staticFileServer)
// New REST API
apiRouter := api.NewRouter()
http.Handle("/api/", http.StripPrefix("/api", AuthWrapHandler(apiRouter)))
// Legacy routes for backward compatibility
http.HandleFunc("/stream/", AuthWrap(api.HandleStream))
http.HandleFunc("/item/", AuthWrap(api.HandleItem))
http.HandleFunc("/feed/", AuthWrap(api.HandleFeed))
http.HandleFunc("/tag/", AuthWrap(api.HandleCategory))
http.HandleFunc("/export/", AuthWrap(api.HandleExport))
http.HandleFunc("/crawl/", AuthWrap(api.HandleCrawl))
http.Handle("/image/", http.StripPrefix("/image/", AuthWrap(imageProxyHandler)))
http.HandleFunc("/login/", loginHandler)
http.HandleFunc("/logout/", logoutHandler)
http.HandleFunc("/", AuthWrap(indexHandler))
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.Config.Port), nil))
}
|