/
floaty.go
187 lines (180 loc) · 5.57 KB
/
floaty.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package floaty
import (
"net/http"
"strconv"
"time"
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
httpCaddyfile "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
caddyHttp "github.com/caddyserver/caddy/v2/modules/caddyhttp"
nanoid "github.com/matoous/go-nanoid/v2"
"go.uber.org/zap"
)
// Initialize!
type FloatyItem struct {
id string
length int `json:"length"`
duration int64 `json:"duration"`
lastWrite int64
nextWrite int64
}
type FloatyModule struct {
logger *zap.Logger
values map[string]*FloatyItem `json:"values,omitempty"`
i int8
}
// Parse the Caddyfile directives
func (module *FloatyModule) UnmarshalCaddyfile(
dispenser *caddyfile.Dispenser,
) error {
timeNow := time.Now().UnixMilli();
module.i = 2;
fmt.Printf("\x1b[1;33m[Floaty]\x1b[0;m Parsing Caddyfile... %d\n", timeNow);
// Initialize the maps
if (module.values == nil) {
module.values = make(map[string]*FloatyItem);
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Map not yet provisioned. Creating the map.");
};
module.values["rootId"] = new(FloatyItem);
// Parse the rootId parameters
arg1 := dispenser.NextArg();
arg2 := dispenser.NextArg();
var length int;
var duration int64;
if (arg1 && arg2) {
lengthRaw, err := strconv.Atoi(dispenser.Val());
if (err != nil) {
return dispenser.Err("Cannot parse length into an integer");
};
if (lengthRaw < 4) {
lengthRaw = 4;
} else if (length > 96) {
lengthRaw = 96
};
length = lengthRaw;
} else {
length = 8;
};
if (dispenser.NextArg()) {
durationObj, err := time.ParseDuration(dispenser.Val());
if (err != nil) {
return dispenser.Err("Cannot parse duration into a duration");
};
duration = durationObj.Milliseconds();
if (duration < 10000) {
duration = 10000;
};
} else {
duration = 5400000; // 15 minutes
};
module.values["rootId"].duration = duration;
module.values["rootId"].length = length;
module.values["rootId"].id = nanoid.Must(module.values["rootId"].length);
module.values["rootId"].lastWrite = timeNow;
module.values["rootId"].nextWrite = timeNow + module.values["rootId"].duration;
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Root parameters parsed.");
/*fmt.Printf("%d\n", length);
fmt.Printf("%d\n", duration);
fmt.Println(module.values["rootId"].id);
fmt.Println(module.values["rootId"].lastWrite);
fmt.Println(module.values["rootId"].nextWrite);*/
return nil;
}
func caddyParser(
helper httpCaddyfile.Helper,
) (
caddyHttp.MiddlewareHandler,
error,
) {
module := new(FloatyModule);
err := module.UnmarshalCaddyfile(helper.Dispenser);
if (err == nil) {
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m No errors are present in the Caddyfile.");
};
return module, err;
}
// Register the module
func (FloatyModule) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.floaty",
New: func() caddy.Module {
return new(FloatyModule)
},
};
}
func init() {
caddy.RegisterModule(FloatyModule{});
httpCaddyfile.RegisterHandlerDirective("floaty", caddyParser);
}
// Provision!
func (module *FloatyModule) Provision(ctx caddy.Context) error {
timeNow := time.Now().UnixMilli();
if (module.values == nil) {
module.values = make(map[string]*FloatyItem);
module.values["rootId"] = new(FloatyItem);
module.values["rootId"].duration = 5000;
module.values["rootId"].length = 24;
module.values["rootId"].id = nanoid.Must(module.values["rootId"].length);
module.values["rootId"].lastWrite = timeNow;
module.values["rootId"].nextWrite = timeNow + module.values["rootId"].duration;
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Map not yet parsed. Creating the map.");
};
module.logger = ctx.Logger();
fmt.Println(module.i);
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Now provisioned!")
return nil;
}
// Validate!
func (module *FloatyModule) Validate() error {
return nil;
}
// Handle!
// Handle requests with placeholder replacements
func (module FloatyModule) ServeHTTP(
writer http.ResponseWriter,
request *http.Request,
handler caddyHttp.Handler,
) error {
timeNow := time.Now().UnixMilli();
repl := request.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer);
// Refresh IDs when stale
// Refresh root ID
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Received a request.");
fmt.Println(module.values["rootId"].nextWrite);
fmt.Println("\x1b[1;33m[Floaty]\x1b[0;m Begins processing.");
if (module.values["rootId"].nextWrite <= timeNow) {
module.logger.Info(
"Root ID of Floaty has expired! Current state.",
zap.String("id", module.values["rootId"].id),
zap.Int64("lastWrite", module.values["rootId"].lastWrite),
zap.Int64("nextWrite", module.values["rootId"].nextWrite),
zap.Int64("timeNow", timeNow),
);
module.values["rootId"].id = nanoid.Must(module.values["rootId"].length);
module.values["rootId"].lastWrite = timeNow;
module.values["rootId"].nextWrite = timeNow + module.values["rootId"].duration;
module.logger.Info(
"Root ID of Floaty has expired! New state.",
zap.String("id", module.values["rootId"].id),
zap.Int64("lastWrite", module.values["rootId"].lastWrite),
zap.Int64("nextWrite", module.values["rootId"].nextWrite),
);
};
// Set values for placeholders
repl.Set("http.floaty", module.values["rootId"].id);
/*module.logger.Info(
"Floaty has accessed global ID: ",
zap.String("id", module.InstanceId),
);
for i0, e0 := range module.MappedIds {
repl.Set("http.floaty." + i0, e0);
};*/
return handler.ServeHTTP(writer, request);
}
// Interface guards
var (
_ caddy.Provisioner = (*FloatyModule)(nil)
_ caddyfile.Unmarshaler = (*FloatyModule)(nil)
_ caddyHttp.MiddlewareHandler = (*FloatyModule)(nil)
);