-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
95 lines (82 loc) · 1.7 KB
/
context.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
package proxylite
import (
"errors"
"net"
"sync"
)
type Context struct {
tn *tunnel
user net.Conn
data []byte
kvs *sync.Map
}
func makeContext(tn *tunnel, user *net.Conn, data []byte, kvMap *sync.Map) *Context {
ctx := &Context{
tn: tn,
data: data,
kvs: kvMap,
}
if user == nil {
ctx.user = nil
} else {
ctx.user = *user
}
return ctx
}
func (ctx *Context) AbortTunnel() error {
if ctx.tn == nil || ctx.tn.innerConn == nil {
return errors.New("cannot abort service because inner connection not exists")
}
return (*ctx.tn.innerConn).Close()
}
func (ctx *Context) AbortUser() error {
if ctx.user == nil {
return errors.New("cannot abort user because user connection not exists")
}
return ctx.user.Close()
}
func (ctx *Context) ServiceInfo() ServiceInfo {
if ctx.tn == nil {
return ServiceInfo{}
}
return *ctx.tn.service
}
func (ctx *Context) UserLocalAddress() net.Addr {
if ctx.user == nil {
return nil
}
return ctx.user.LocalAddr()
}
func (ctx *Context) UserRemoteAddress() net.Addr {
if ctx.user == nil {
return nil
}
return ctx.user.RemoteAddr()
}
func (ctx *Context) InnerLocalConn() net.Addr {
if ctx.tn == nil || ctx.tn.innerConn == nil {
return nil
}
return (*ctx.tn.innerConn).LocalAddr()
}
func (ctx *Context) InnerRemoteConn() net.Addr {
if ctx.tn == nil || ctx.tn.innerConn == nil {
return nil
}
return (*ctx.tn.innerConn).RemoteAddr()
}
func (ctx *Context) DataBuffer() []byte {
return ctx.data
}
func (ctx *Context) PutValue(key, value interface{}) {
if ctx.kvs == nil {
return
}
ctx.kvs.Store(key, value)
}
func (ctx *Context) GetValue(key interface{}) (interface{}, bool) {
if ctx.kvs == nil {
return nil, false
}
return ctx.kvs.Load(key)
}