/
context.go
70 lines (56 loc) · 1006 Bytes
/
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
package core
/**
* context.go - proxy context
*
* @author Yaroslav Pogrebnyak <yyyaroslav@gmail.com>
*/
import "net"
type Context interface {
String() string
Ip() net.IP
Port() int
Sni() string
}
/**
* Proxy tcp context
*/
type TcpContext struct {
Hostname string
/**
* Current client connection
*/
Conn net.Conn
}
func (t TcpContext) String() string {
return t.Conn.RemoteAddr().String()
}
func (t TcpContext) Ip() net.IP {
return t.Conn.RemoteAddr().(*net.TCPAddr).IP
}
func (t TcpContext) Port() int {
return t.Conn.RemoteAddr().(*net.TCPAddr).Port
}
func (t TcpContext) Sni() string {
return t.Hostname
}
/*
* Proxy udp context
*/
type UdpContext struct {
/**
* Current client remote address
*/
ClientAddr net.UDPAddr
}
func (u UdpContext) String() string {
return u.ClientAddr.String()
}
func (u UdpContext) Ip() net.IP {
return u.ClientAddr.IP
}
func (u UdpContext) Port() int {
return u.ClientAddr.Port
}
func (u UdpContext) Sni() string {
return ""
}