forked from rystaf/mlmym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (131 loc) · 4.18 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
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"github.com/julienschmidt/httprouter"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
var version string
var watch = flag.Bool("w", false, "watch for file changes")
var addr = flag.String("addr", ":80", "http service address")
var md goldmark.Markdown
var templates map[string]*template.Template
type AddHeaderTransport struct {
T http.RoundTripper
ForwardFor string
}
func (adt *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", "Mlmym")
if adt.ForwardFor != "" {
req.Header.Add("X-Forwarded-For", adt.ForwardFor)
req.Header.Add("X-Real-IP", adt.ForwardFor)
}
return adt.T.RoundTrip(req)
}
func NewAddHeaderTransport(remoteAddr string) *AddHeaderTransport {
var forwardFor string
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
if ip := net.ParseIP(host); ip != nil {
if !ip.IsPrivate() {
forwardFor = ip.String()
}
}
}
return &AddHeaderTransport{
T: http.DefaultTransport,
ForwardFor: forwardFor,
}
}
func init() {
md = goldmark.New(goldmark.WithExtensions(
extension.Linkify,
extension.Table,
))
templates = make(map[string]*template.Template)
if !*watch {
for _, name := range []string{"index.html", "login.html", "frontpage.html", "root.html", "settings.html", "xhr.html", "create_comment.html", "block.html"} {
t := template.New(name).Funcs(funcMap)
glob, err := t.ParseGlob("templates/*")
if err != nil {
fmt.Println(err)
continue
}
templates[name] = glob
}
}
if os.Getenv("DEBUG") != "" {
test()
}
if data, err := os.ReadFile("VERSION"); err == nil {
version = string(data)
}
}
func test() {
links := [][]string{
[]string{"https://lemmy.local/u/dude", "/lemmy.local/u/dude", "/u/dude"},
[]string{"https://lemmy.local/u/[email protected]", "/lemmy.local/u/dude", "/u/dude"},
[]string{"/u/dude", "/lemmy.local/u/dude", "/u/dude"},
[]string{"/u/[email protected]", "/lemmy.local/u/dude", "/u/dude"},
[]string{"https://lemmy.world/c/dude", "/lemmy.local/c/[email protected]", "/c/[email protected]"},
[]string{"https://lemmy.world/u/dude", "/lemmy.local/u/[email protected]", "/u/[email protected]"},
[]string{"https://lemmy.world/u/[email protected]", "/lemmy.local/u/[email protected]", "/u/[email protected]"},
[]string{"https://lemmy.world/post/123", "/lemmy.local/post/[email protected]", "/post/[email protected]"},
[]string{"https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123"},
[]string{"/post/123", "/lemmy.local/post/123", "/post/123"},
[]string{"/comment/123", "/lemmy.local/comment/123", "/comment/123"},
[]string{"https://lemmy.local/comment/123", "/lemmy.local/comment/123", "/comment/123"},
}
for _, url := range links {
output := LemmyLinkRewrite(`href="`+url[0]+`"`, "lemmy.local", "")
success := (output == (`href="` + url[1] + `"`))
if !success {
fmt.Println("\n!!!! multi instance link rewrite failure !!!!")
fmt.Println(url)
fmt.Println(output)
fmt.Println("")
}
output = LemmyLinkRewrite(`href="`+url[0]+`"`, ".", "lemmy.local")
success = (output == (`href="` + url[2] + `"`))
if !success {
fmt.Println("\n!!!! single instance link rewrite failure !!!!")
fmt.Println(success, url)
fmt.Println(output)
fmt.Println("")
}
}
}
func RemoteAddr(r *http.Request) string {
if r.Header.Get("CF-Connecting-IP") != "" {
return r.Header.Get("CF-Connecting-IP")
}
return r.RemoteAddr
}
func middleware(n httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//remoteAddr := r.RemoteAddr
//if r.Header.Get("CF-Connecting-IP") != "" {
// remoteAddr = r.Header.Get("CF-Connecting-IP")
//}
//if ps.ByName("host") != "" && !IsLemmy(ps.ByName("host"), remoteAddr) {
// http.Redirect(w, r, "/", 301)
// return
//}
n(w, r, ps)
}
}
func main() {
flag.Parse()
log.Println("serve", *addr)
router := GetRouter()
err := http.ListenAndServe(*addr, router)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}