forked from firex-one/caddy-delay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teleport.go
162 lines (146 loc) · 4.24 KB
/
teleport.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package teleport
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/sirupsen/logrus"
"github.com/webteleport/ufo"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(Middleware{})
httpcaddyfile.RegisterHandlerDirective("teleport", parseCaddyfile)
logrus.Info("webteleport module inited")
}
type Middleware struct {
logger *zap.Logger
Station string `json:"duration,omitempty"`
KnockTimes string `json:"knock_time,omitempty"`
KnockURL string `json:"knock_url,omitempty"`
}
func attachReplacerContext(r *http.Request) *http.Request {
newCtx := context.WithValue(context.Background(), caddy.ReplacerCtxKey, caddy.NewEmptyReplacer())
newCtx = context.WithValue(newCtx, caddyhttp.ServerCtxKey, Server)
newCtx = context.WithValue(newCtx, "route_group", RouteGroup)
newCtx = context.WithValue(newCtx, caddyhttp.OriginalRequestCtxKey, OriginalRequest)
return r.WithContext(newCtx)
}
func (m *Middleware) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r2 := attachReplacerContext(r)
_ = r2.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
_ = r2.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server)
_ = r2.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
_ = r2.Context().Value("route_group").(map[string]struct{})
if Next == nil {
m.logger.Info("not found")
http.NotFoundHandler().ServeHTTP(w, r2)
return
}
Next.ServeHTTP(w, r2)
})
}
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.http_webteleport",
New: func() caddy.Module { return new(Middleware) },
}
}
func (m *Middleware) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
m.logger.Info("webteleport module inited: " + m.Station)
go ufo.Serve(m.Station, m.Handler())
return nil
}
func (m *Middleware) Validate() error {
return nil
}
var Next caddyhttp.Handler
var Server *caddyhttp.Server
var RouteGroup map[string]struct{}
var Ctx context.Context
var OriginalRequest http.Request
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
m.logger.Info("http_webteleport start")
defer m.logger.Info("http_webteleport end")
if Next != nil {
m.logger.Info("get")
return Next.ServeHTTP(w, r)
} else {
m.logger.Info("set")
Next = next
Server = r.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server)
RouteGroup = r.Context().Value("route_group").(map[string]struct{})
OriginalRequest = r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
for k, _ := range RouteGroup {
println(k)
}
Ctx = r.Context()
return Next.ServeHTTP(w, r)
}
}
func httpGet(u string, t string) {
n, err := strconv.Atoi(t)
if err != nil {
n = 1
}
for c := 0; c < n; c++ {
// println(c)
time.Sleep(time.Second)
http.Get(u)
}
}
func (m *Middleware) addDirective(directive string, arg string, pos int) {
// m.logger.Info("http_webteleport add directive: " + directive + arg)
if directive == "knock" {
switch pos {
case 1:
m.KnockURL = arg
m.KnockTimes = "1"
case 2:
m.KnockTimes = arg
}
}
// m.logger.Info("http_webteleport unknown directive: " + directive)
}
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() && d.NextArg() {
m.Station = d.Val()
}
var directive string = ""
_ = directive
for i := 0; d.Next(); i++ {
val := d.Val()
if val == "knock" {
i = 0
}
fmt.Sprintf("using %d %s", i, val)
switch i {
case 0:
directive = val
case 1, 2:
m.addDirective(directive, val, i)
}
}
go httpGet(m.KnockURL, m.KnockTimes)
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Middleware
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// Interface guards
var (
_ caddy.Provisioner = (*Middleware)(nil)
_ caddy.Validator = (*Middleware)(nil)
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
_ caddyfile.Unmarshaler = (*Middleware)(nil)
)