-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
transport.go
70 lines (58 loc) · 1.46 KB
/
transport.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
package irt
import (
"net"
"net/http"
"strings"
"time"
)
const (
DefaultTimeout = time.Second * 60
)
type TransportHeaders = map[string]string
// Client Specific
type ClientTransport interface {
GetAuthorization() *Authorization
SetAuthorization(auth *Authorization) error
GetHeaders() TransportHeaders
SetHeaders(headers TransportHeaders) error
Send(service string, method string, dataIn interface{}, dataOut interface{}) error
}
type ClientSocketTransport interface {
ClientTransport
// This one supports Buzzer and Streams
}
// Server specific
type OnConnect func(connection *ConnectionContext, request *http.Request) error
type OnAuth func(connection *ConnectionContext) error
type OnDisconnect func(connection *ConnectionContext)
type ConnectionHandlers struct {
OnConnect OnConnect
OnAuth OnAuth
OnDisconnect OnDisconnect
}
type SystemContext struct {
RemoteAddr string
Auth *Authorization
}
func (c *SystemContext) Update(r *http.Request) error {
header := r.Header
c.RemoteAddr = header.Get("X-Forwarded-For")
if c.RemoteAddr != "" {
// There might be a few IP address, just pick the first one
ips := strings.Split(c.RemoteAddr, ", ")
c.RemoteAddr = ips[0]
} else {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
c.RemoteAddr = ip
}
}
if c.Auth == nil {
c.Auth = &Authorization{}
}
return c.Auth.UpdateFromHeaders(header)
}
type ConnectionContext struct {
System *SystemContext
User interface{}
}