-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyar.go
332 lines (292 loc) · 6.37 KB
/
yar.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package yar
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"net"
"strings"
)
const headerSize int = 82
const magicNum uint32=0x80DFEC60
type onFunc func(c interface{})interface{}
type errorHandle func(error)
type config struct {
network string
address string
funcList map[string] onFunc
errorList []errorHandle
}
type Yaf interface {
OnError(errorHandle)
On(string,onFunc) error
Run() error
}
type header struct {
id uint32 // transaction id
version uint16 // protocl version
magicNum uint32 // default is: 0x80DFEC60
reserved uint32
provider string // reqeust from who
token string // request token, used for authentication
bodyLen uint32 // request body len
}
type body struct {
Id uint32 `json:"i"`
Method string `json:"m"`
Param []interface{} `json:"p"`
}
func (c config) OnError(h errorHandle) {
c.errorList=append(c.errorList,h)
}
func (c config) On(methodName string , h onFunc) error {
c.funcList[methodName]=h
return nil
}
func yarErr(c config,err error) {
fmt.Println(`yarErr:`,err.Error())
for _,v:= range c.errorList {
v(err)
}
}
func (c config) Run() error {
ln,err:=net.Listen(c.network,c.address)
if err!=nil{
return err
}
fmt.Println("正在监听9002")
for true {
conn, err := ln.Accept()
fmt.Println(`for`)
if err != nil {
yarErr(c,err)
}
go handle(conn,c)
}
return nil
}
func Addr(network string,address string) Yaf {
var y Yaf
var fl=make(map[string]onFunc)
var eh = make([]errorHandle,0)
y=config{network,address,fl,eh}
return y
}
func handle(conn net.Conn,c config){
//自动关闭
fmt.Println(`handle()`)
yarErr(c,errors.New(`yarErr`))
defer func() {
err:=conn.Close()
if err != nil {
yarErr(c,errors.New("net.Conn error: "+err.Error()))
return
}
fmt.Println(`conn.Close()`)
}()
b,err:=parseRequest(conn)
if err != nil {
yarErr(c,err)
return
}
fmt.Println(b)
f,ok:=c.funcList[b.Method]
if ok {
p:=f(b.Param)
var res=make(map[string]interface{})
var o= make([]interface{},0)
o=append(o,p)
res[`i`]=0
res[`s`]=0
res[`r`]=true
r,err:=json.Marshal(res)
if err != nil {
yarErr(c,err)
return
}
j:=make([]byte,8)
j2:=[]byte(`JSON`)
copy(j,j2)
r=append(j,r...)
h,err:=packHeader(len(r))
fmt.Println(len(h))
fmt.Println(len(r))
if err != nil {
yarErr(c,err)
return
}
h=append(h,r...)
fmt.Println(string(h))
lll, err := conn.Write(h)
if err!=nil{
fmt.Println(err.Error())
}
fmt.Println(lll)
return
}else{
yarErr(c,err)
return
}
}
func parseRequest(conn net.Conn) (body,error) {
var b body
bodyLen,err:=validRequest(conn)
if err != nil {
return b,err
}
bodyByte := make([]byte,bodyLen)
_,err = conn.Read(bodyByte)
if err != nil {
return b,errors.New("request read error: "+err.Error())
}
fmt.Println(string(bodyByte))
switch strings.TrimRight(string(bodyByte[0:8]),"\000") {
case `JSON`:
err=json.Unmarshal(bodyByte[8:], &b)
default:
err=errors.New(`unsupported packager`)
}
if err != nil {
return b,err
}
return b,nil
}
func validRequest(conn net.Conn) (uint32,error) {
var hand header
handByte := make([]byte, headerSize)
_, err := conn.Read(handByte)
if err != nil {
return 0,errors.New("request read error: "+err.Error())
}
fmt.Println(string(handByte))
hand,err=unpackHeader(handByte)
if err != nil {
return 0,err
}
if hand.magicNum!=magicNum{
return 0,errors.New("illegal Yar RPC request")
}
return hand.bodyLen,nil
}
func unpackHeader(b []byte) (header,error) {
var h=header{}
if len(b)!=headerSize{
return h,errors.New("unpackHeader error: header bytes len not "+string(headerSize))
}
var err error
h.id,err=bytesToUint32(b[0:4])
if err!=nil {
return h,errors.New("unpackHeader error: "+err.Error())
}
h.version,err=bytesToUint16(b[4:6])
if err!=nil {
return h,errors.New("unpackHeader error: "+err.Error())
}
h.magicNum,err=bytesToUint32(b[6:10])
if err!=nil {
return h,errors.New("unpackHeader error: "+err.Error())
}
h.reserved,err=bytesToUint32(b[10:14])
if err!=nil {
return h,errors.New("unpackHeader error: "+err.Error())
}
h.provider=strings.TrimRight(string(b[14:46])," ")
h.token=strings.TrimRight(string(b[46:78])," ")
h.bodyLen,err=bytesToUint32(b[78:82])
if err!=nil {
return h,errors.New("unpackHeader error: "+err.Error())
}
fmt.Println(h)
return h,nil
}
func packHeader(len int) ([]byte,error) {
//$bin = pack("NnNNA32A32N",
//$id, 0, 0x80DFEC60,
// 0, "Yar PHP TCP Server",
// "", $len
//);
//id 32
var b = make([]byte,0)
id,err:=uint32ToBytes(0)
if err!=nil {
return b,errors.New("packHeader error: "+err.Error())
}
b=append(b,id...)
//version 16
res,err:=uint16ToBytes(0)
if err!=nil {
fmt.Println(err.Error())
}
b=append(b,res...)
//magic_num 32
res,err=uint32ToBytes(magicNum)
if err!=nil {
fmt.Println(err.Error())
}
b=append(b,res...)
//reserved 32
res,err=uint32ToBytes(0)
if err!=nil {
fmt.Println(err.Error())
}
b=append(b,res...)
//provider 32
var provider=[]byte(`Yar PHP TCP Server`)
res =make([]byte,32)
copy(res,provider)
b=append(b,res...)
//token 32
var token=[]byte(``)
res =make([]byte,32)
copy(res,token)
b=append(b,res...)
//body_len 32
res,err=uint32ToBytes(uint32(len))
if err!=nil {
fmt.Println(err.Error())
}
b=append(b,res...)
fmt.Println(`b:`,b)
fmt.Println(string(b))
_,err=unpackHeader(b)
if err!=nil {
fmt.Println(err.Error())
}
return b,nil
}
func bytesToUint32(b []byte) (uint32,error) {
bytesBuffer := bytes.NewBuffer(b)
var x uint32
err:=binary.Read(bytesBuffer, binary.BigEndian, &x)
if err!=nil {
return 0, errors.New("bytesToUint32 error: " + err.Error())
}
return x,nil
}
//字节转换成整形
func bytesToUint16(b []byte) (uint16,error) {
bytesBuffer := bytes.NewBuffer(b)
var x uint16
err:=binary.Read(bytesBuffer, binary.BigEndian, &x)
if err!=nil {
return 0, errors.New("bytesToUint16 error: " + err.Error())
}
return x,nil
}
func uint32ToBytes(u uint32) ([]byte,error) {
bytesBuffer := bytes.NewBuffer([]byte{})
err:=binary.Write(bytesBuffer, binary.BigEndian, u)
if err!=nil {
return []byte{}, errors.New("uint32ToBytes error: " + err.Error())
}
return bytesBuffer.Bytes(),nil
}
func uint16ToBytes(u uint16) ([]byte,error) {
bytesBuffer := bytes.NewBuffer([]byte{})
err:=binary.Write(bytesBuffer, binary.BigEndian, u)
if err!=nil {
return []byte{}, errors.New("uint32ToBytes error: " + err.Error())
}
return bytesBuffer.Bytes(),nil
}