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
|
package validator
import (
"net/http"
"strings"
json "github.com/json-iterator/go"
"github.com/spiral/errors"
handler "github.com/spiral/roadrunner/v2/pkg/worker_handler"
"github.com/spiral/roadrunner/v2/plugins/http/attributes"
)
type AccessValidatorFn = func(r *http.Request, channels ...string) (*AccessValidator, error)
const (
joinServer string = "ws:joinServer"
joinTopics string = "ws:joinTopics"
)
type AccessValidator struct {
Header http.Header `json:"headers"`
Status int `json:"status"`
Body []byte
}
func ServerAccessValidator(r *http.Request) ([]byte, error) {
const op = errors.Op("server_access_validator")
err := attributes.Set(r, "ws:joinServer", true)
if err != nil {
return nil, errors.E(op, err)
}
defer delete(attributes.All(r), joinServer)
req := &handler.Request{
RemoteAddr: handler.FetchIP(r.RemoteAddr),
Protocol: r.Proto,
Method: r.Method,
URI: handler.URI(r),
Header: r.Header,
Cookies: make(map[string]string),
RawQuery: r.URL.RawQuery,
Attributes: attributes.All(r),
}
data, err := json.Marshal(req)
if err != nil {
return nil, errors.E(op, err)
}
return data, nil
}
func TopicsAccessValidator(r *http.Request, topics ...string) ([]byte, error) {
const op = errors.Op("topic_access_validator")
err := attributes.Set(r, "ws:joinTopics", strings.Join(topics, ","))
if err != nil {
return nil, errors.E(op, err)
}
defer delete(attributes.All(r), joinTopics)
req := &handler.Request{
RemoteAddr: handler.FetchIP(r.RemoteAddr),
Protocol: r.Proto,
Method: r.Method,
URI: handler.URI(r),
Header: r.Header,
Cookies: make(map[string]string),
RawQuery: r.URL.RawQuery,
Attributes: attributes.All(r),
}
data, err := json.Marshal(req)
if err != nil {
return nil, errors.E(op, err)
}
return data, nil
}
|