-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (90 loc) · 3.12 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
// /!\ Lauch `go get golang.org/x/net/html` on a fresh installed server!
package main
import (
"os"
"io/ioutil"
"fmt"
"strings"
"net/http"
//"regexp"
"golang.org/x/net/html"
"net/url"
)
func errorOut(err error) {
fmt.Printf("ERROR: %s", err)
os.Exit(1)
}
func proxy(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
urlStr := r.Form.Get("u")
urlParsed, err := url.Parse(urlStr)
if err != nil {
fmt.Printf("WARN: %s, Possibly due to Favico web browser request\n", err)
return
}
w.Header().Set("X-Proxy", "Neko-San-Go-v1")
if r.Method == "GET" {
fmt.Printf("Requested %s with scheme [%s]\n", urlParsed.Host, urlParsed.Scheme)
response, err := http.Get(urlStr)
if err != nil {
fmt.Printf("WARN: %s, Possibly due to Favico web browser request\n", err)
return
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
errorOut(err)
}
DOMStr := string(contents[:])
doc, err := html.Parse(strings.NewReader(DOMStr))
if err != nil {
fmt.Printf("WARN: %s\n", err)
}
output := DOMStr
linkTab := []string{}
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && (n.Data == "a" || n.Data == "script" || n.Data == "link") {
for _, base := range n.Attr {
if base.Key == "href" {
linkTab = append(linkTab, base.Val)
fmt.Printf("Link A found: %s\n", base.Val)
} else if base.Key == "src" {
linkTab = append(linkTab, base.Val)
fmt.Printf("Link SRC found: %s\n", base.Val)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
for _, el := range linkTab {
if !strings.HasPrefix(el, "//") && !strings.HasPrefix(el, "http://") {
output = strings.Replace(output, el, "https://proxy.neko-san.fr/?u=http://" + urlParsed.Host + el, -1)
fmt.Printf("Replacement...\n")
}
}
fmt.Fprintf(w, "%s", output)
//html.Render(w, doc)
}
}
}
func main() {
http.HandleFunc("/", proxy)
http.ListenAndServe(":8008", nil)
}
/*
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
Path string
RawPath string // encoded path hint (Go 1.5 and later only; see EscapedPath method)
ForceQuery bool // append a query ('?') even if RawQuery is empty
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
}
*/