-
Notifications
You must be signed in to change notification settings - Fork 285
/
model.go
173 lines (158 loc) · 3.22 KB
/
model.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
package shuttle
import (
"github.com/sipt/shuttle/conn"
"github.com/sipt/shuttle/dns"
"net"
"strconv"
)
const (
CmdTCP = 0x01
CmdUDP = 0x03
ProtocolSocks = "SOCKS"
ProtocolHttp = "HTTP"
ProtocolHttps = "HTTPS"
AddrTypeIPv4 = 0x01 // 0x01:IPv4
AddrTypeDomain = 0x03 // 0x03:域名
AddrTypeIPv6 = 0x04 // 0x04:IPv6
)
//|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
type SocksRequest struct {
ver uint8
cmd uint8
rsv uint8
atyp uint8
addr string
ip net.IP
port uint16
data []byte //for udp
protocol string
target string
connID int64
answer *dns.Answer
}
func (r *SocksRequest) Network() string {
switch r.cmd {
case CmdTCP:
return conn.TCP
case CmdUDP:
return conn.UDP
}
return ""
}
func (r *SocksRequest) Domain() string {
return r.addr
}
func (r *SocksRequest) IP() string {
if len(r.ip) > 0 {
return r.ip.String()
} else if r.answer != nil {
r.ip = net.ParseIP(r.answer.GetIP())
return r.ip.String()
}
return ""
}
func (r *SocksRequest) Port() string {
if r.port != 0 {
return strconv.FormatInt(int64(r.port), 10)
}
return ""
}
func (r *SocksRequest) Answer() *dns.Answer {
return r.answer
}
func (r *SocksRequest) SetAnswer(answer *dns.Answer) {
r.answer = answer
}
//return request id
func (r *SocksRequest) ID() int64 {
return r.connID
}
//return domain!=""?domain:ip
func (r *SocksRequest) Addr() string {
if len(r.addr) > 0 {
return r.addr
}
return r.IP()
}
//return [domain/ip]:[port]
func (r *SocksRequest) Host() string {
if len(r.IP()) > 0 {
return net.JoinHostPort(r.IP(), strconv.FormatInt(int64(r.port), 10))
}
return net.JoinHostPort(r.Addr(), strconv.FormatInt(int64(r.port), 10))
}
func NewHttpRequest(network string, domain string, ip string, port string, protocol string,
target string, connID int64, answer *dns.Answer) *HttpRequest {
return &HttpRequest{
network: network,
domain: domain,
ip: ip,
port: port,
protocol: protocol,
target: target,
connID: connID,
answer: answer,
}
}
//HTTP Request
type HttpRequest struct {
network string
domain string
ip string
port string
protocol string
target string
connID int64
answer *dns.Answer
}
func (r *HttpRequest) Network() string {
return r.network
}
func (r *HttpRequest) Domain() string {
return r.domain
}
func (r *HttpRequest) IP() string {
if len(r.ip) == 0 && r.answer != nil {
r.ip = r.answer.GetIP()
}
return r.ip
}
func (r *HttpRequest) Port() string {
if len(r.port) == 0 {
if r.answer != nil && len(r.answer.Port) > 0 {
r.port = r.answer.Port
} else {
switch r.protocol {
case HTTP:
r.port = "80"
case HTTPS:
r.port = "443"
}
}
}
return r.port
}
func (r *HttpRequest) Answer() *dns.Answer {
return r.answer
}
func (r *HttpRequest) SetAnswer(answer *dns.Answer) {
r.answer = answer
}
//return request id
func (r *HttpRequest) ID() int64 {
return r.connID
}
//return domain!=""?domain:ip
func (r *HttpRequest) Addr() string {
if len(r.domain) > 0 {
return r.domain
}
return r.ip
}
//return [domain/ip]:[port]
func (r *HttpRequest) Host() string {
if len(r.IP()) > 0 {
return net.JoinHostPort(r.IP(), r.Port())
}
return net.JoinHostPort(r.Addr(), r.Port())
}