-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (52 loc) · 1.54 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
package main
import (
"github.com/elazarl/goproxy"
"github.com/lightsing/makehttps/config"
"github.com/lightsing/makehttps/proxy"
"github.com/lightsing/makehttps/rules"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
const name = "mhga"
var Version string
func main() {
// Only log the warning severity or above.
config := config.Init()
for _, rule := range config.Rules {
if err := rules.CheckRule(&rule); err == nil {
config.AvailableRules = append(config.AvailableRules, rule.Path)
} else {
log.Errorf("Rule check fail by (%s)", err)
}
}
/*if len(config.AvailableRules) == 0 {
panic("no available rules")
}*/
start := time.Now()
rules := rules.NewRuleSets()
for _, path := range config.AvailableRules {
rules.LoadRuleSets(path)
}
log.Infof("Load all rule in %s", time.Since(start))
server := goproxy.NewProxyHttpServer()
responseBuilder := proxy.NewResponseBuilder(Version)
server.OnRequest().DoFunc(
func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
start := time.Now()
if result, ok := rules.Apply(req.RequestURI); ok {
result := *result
log.Infof("[%s] Redirect %s to %s", time.Since(start), req.RequestURI, result)
var code int
if req.Method == "GET" {
code = http.StatusMovedPermanently
} else {
code = http.StatusTemporaryRedirect
}
return req, responseBuilder.Gen(req, result, code)
}
log.Infof("[%s] Nothing to do with %s", time.Since(start), req.RequestURI)
return req, nil
})
log.Fatal(http.ListenAndServe(config.Address, server))
}