-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (131 loc) · 3.22 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
156
157
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/dav-m85/xbellum/store"
"github.com/dav-m85/xbellum/vfs"
"github.com/dav-m85/xbellum/xbel"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/webdav"
)
var secret string = os.Getenv("SECRET")
var root string = os.Getenv("ROOT")
func main() {
var dead bool
flag.BoolVar(&dead, "c", false, "check for dead")
flag.Parse()
if root == "" {
root = "./data"
}
args := flag.Args()
st := store.NewStore(root)
if len(args) == 0 {
args = append(args, "")
}
switch args[0] {
default:
log.Fatalln("Usage: go main.go server|dedup|check")
case "server":
wh := webdav.Handler{
FileSystem: vfs.NewVFS(st), // os.FS cannot be used here :(
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, e error) {
log.Printf("%s %s ERR:%s", r.Method, r.URL, e)
},
}
listener, err := net.Listen("tcp", ":8082")
if err != nil {
log.Fatal(err)
}
serve := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
// Gets the correct user for this request.
_, password, ok := r.BasicAuth()
if !ok {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
if !checkPassword(secret, password) {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
if r.URL.Path == "/info" {
st.ServeHTTP(w, r)
} else {
wh.ServeHTTP(w, r)
}
}
log.Printf("Serving on %s", listener.Addr())
if err := http.Serve(listener, Server(serve)); err != nil {
log.Print("shutting server", err)
}
case "dedup":
buf, _ := st.Get()
x, err := xbel.Parse(buf)
if err != nil {
log.Fatal(err)
}
hrefs := make(map[string]struct{})
nx := xbel.Walk(x, func(b *xbel.Bookmark) bool {
if _, exists := hrefs[b.Href]; exists {
log.Println("Duplicate " + b.Href)
return false
}
hrefs[b.Href] = struct{}{}
return true
})
b := bytes.NewBuffer([]byte{})
xbel.Write(b, nx)
err = st.Set(b.Bytes())
if err != nil {
log.Fatal(err)
}
case "check":
buf, _ := st.Get()
x, err := xbel.Parse(buf)
if err != nil {
log.Fatal(err)
}
xbel.Walk(x, func(b *xbel.Bookmark) bool {
// Lets findout if still ok
// if false && strings.HasPrefix(b.Href, "https://example.com") {
fmt.Println("Checking " + b.Href)
resp, err := http.Get(b.Href)
// handle the error if there is one
if err != nil {
panic(err)
}
// do this now so it won't be forgotten
defer resp.Body.Close()
// reads html as a slice of bytes
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// show the HTML code as a string %s
if strings.Contains(string(html), "Post Not Found") {
fmt.Println("DEAD")
b.Title = "[DEAD] " + b.Title
}
return true
})
}
}
type Server func(w http.ResponseWriter, r *http.Request)
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s(w, r)
}
func checkPassword(saved, input string) bool {
if strings.HasPrefix(saved, "{bcrypt}") {
savedPassword := strings.TrimPrefix(saved, "{bcrypt}")
return bcrypt.CompareHashAndPassword([]byte(savedPassword), []byte(input)) == nil
}
return saved == input
}