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
|
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2020 jet tsang zeon-git. All Rights Reserved.
*/
package main
import (
"bufio"
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/antchfx/htmlquery"
"github.com/valyala/fasthttp"
)
type ShortMap map[string]string
var listUrl string
var blockTxt string
var shortenMap map[string]string
var listen string
var reloadURI = "/reload"
func init() {
const (
urlUsage = "This program will get a list from the url you provide."
blockUsage = "html block to query."
defaultBlock = ""
defaultListen = "localhost:9530"
listenUsage = "TCP address to listen to"
)
flag.StringVar(&listUrl, "url", "https://pastebin.com/raw/9g4msZxC", urlUsage)
flag.StringVar(&listUrl, "u", "https://pastebin.com/raw/9g4msZxC", urlUsage+" (shorthand)")
flag.StringVar(&blockTxt, "block", defaultBlock, blockUsage)
flag.StringVar(&blockTxt, "b", defaultBlock, blockUsage+" (shorthand)")
flag.StringVar(&listen, "listen", defaultListen, listenUsage)
flag.StringVar(&listen, "l", defaultListen, listenUsage+" (shorthand)")
shortenMap = map[string]string{}
}
func main() {
flag.Parse()
go autoLoad()
log.Println("Listening to:", listen)
if err := fasthttp.ListenAndServe(listen, requestHandler); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
if string(ctx.RequestURI()) == reloadURI {
loadData()
fmt.Fprint(ctx, "ok")
ctx.SetStatusCode(fasthttp.StatusAccepted)
return
}
if relayUrl, exist := shortenMap[string(ctx.Path())]; exist {
var newRequest fasthttp.Request
ctx.Request.Header.CopyTo(&newRequest.Header)
newRequest.SetRequestURI(relayUrl)
newRequest.Header.SetBytesV("Host", newRequest.Host())
ctx.URI().QueryArgs().VisitAll(func(key []byte, value []byte) {
newRequest.URI().QueryArgs().AddBytesKV(key, value)
})
defer fasthttp.ReleaseRequest(&newRequest)
fasthttp.Do(&newRequest, &ctx.Response)
} else {
ctx.Error("err", 404)
}
}
func autoLoad() {
for {
loadData()
time.Sleep(time.Hour)
}
}
func loadData() {
log.Println("Loading data.")
var code string
if blockTxt != "" {
doc, err := htmlquery.LoadURL(listUrl)
if err != nil {
return
}
code = htmlquery.InnerText(htmlquery.FindOne(doc, blockTxt))
} else {
var dst []byte
_, byteCode, _ := fasthttp.Get(dst, string(listUrl))
code = string(byteCode)
}
scanner := bufio.NewScanner(strings.NewReader(code))
for scanner.Scan() {
maplist := strings.Split(scanner.Text(), "#")
shortenMap[maplist[0]] = maplist[1]
}
log.Println("urls mapped.")
}
|