-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
91 lines (77 loc) · 1.4 KB
/
router.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
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// HTTP requests router
package main
import (
"strings"
)
//
// Request router
//
type Router struct {
froxy *Froxy // Back link to Froxy
}
//
// Router answer
//
type RouterAnswer int
const (
RouterBypass = RouterAnswer(iota)
RouterForward
RouterBlock
)
//
// RouterAnswer->string (for debugging)
//
func (a RouterAnswer) String() string {
switch a {
case RouterBypass:
return "bypass"
case RouterForward:
return "forward"
case RouterBlock:
return "block"
}
panic("internal error")
}
//
// Create new router
//
func NewRouter(froxy *Froxy) *Router {
return &Router{
froxy: froxy,
}
}
//
// Route the URL. Returns true if site must be routed via server,
// false if site must be accessed directly
//
func (r *Router) Route(host string) (answer RouterAnswer) {
sites := r.froxy.GetSites()
found := (*SiteParams)(nil)
for _, site := range sites {
if site.Host == host {
found = &site
break
}
if site.Rec &&
strings.HasSuffix(host, site.Host) &&
host[len(host)-len(site.Host)-1] == '.' {
// More specific match wins
if found == nil || len(found.Host) < len(site.Host) {
found = &site
}
}
}
if found != nil {
if found.Block {
return RouterBlock
} else {
return RouterForward
}
}
return RouterBypass
}