-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommon.py
255 lines (207 loc) · 7.16 KB
/
common.py
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from tkinter.messagebox import NO
import flatbuffers
from A6 import Method as A6Method
from A6 import TextEntry as A6Entry
from A6.Err.Code import Code as A6ErrCode
from A6.HTTPReqCall import Rewrite as HCRewrite
from A6.HTTPReqCall import Stop as HCStop
from A6.HTTPReqCall import Action as HCAction
from A6.HTTPReqCall import Resp as HCResp
from A6.PrepareConf import Resp as PCResp
from A6.Err import Resp as ErrResp
RPC_PROTOCOL_HEADER_LEN = 4
RPC_PREPARE_CONF = 1
RPC_HTTP_REQ_CALL = 2
RPC_EXTRA_INFO = 3
RPC_UNKNOWN = 0
VECTOR_TYPE_HEADER = 1
VECTOR_TYPE_QUERY = 2
VECTOR_TYPE_CONFIG = 3
VECTOR_TYPE_SOURCE_IP = 4
dictVectorParseFuncNames = {
VECTOR_TYPE_HEADER: "Headers",
VECTOR_TYPE_QUERY: "Args",
VECTOR_TYPE_CONFIG: "Conf",
}
listVectorParseFuncNames = {
VECTOR_TYPE_SOURCE_IP: "SrcIp",
}
A6MethodGET = "GET"
A6MethodHEAD = "HEAD"
A6MethodPOST = "POST"
A6MethodPUT = "PUT"
A6MethodDELETE = "DELETE"
A6MethodMKCOL = "MKCOL"
A6MethodCOPY = "COPY"
A6MethodMOVE = "MOVE"
A6MethodOPTIONS = "OPTIONS"
A6MethodPROPFIND = "PROPFIND"
A6MethodPROPPATCH = "PROPPATCH"
A6MethodLOCK = "LOCK"
A6MethodUNLOCK = "UNLOCK"
A6MethodPATCH = "PATCH"
A6MethodTRACE = "TRACE"
methodNames = {
A6Method.Method.GET: A6MethodGET,
A6Method.Method.HEAD: A6MethodHEAD,
A6Method.Method.POST: A6MethodPOST,
A6Method.Method.PUT: A6MethodPUT,
A6Method.Method.DELETE: A6MethodDELETE,
A6Method.Method.MKCOL: A6MethodMKCOL,
A6Method.Method.COPY: A6MethodCOPY,
A6Method.Method.MOVE: A6MethodMOVE,
A6Method.Method.OPTIONS: A6MethodOPTIONS,
A6Method.Method.PROPFIND: A6MethodPROPFIND,
A6Method.Method.PROPPATCH: A6MethodPROPPATCH,
A6Method.Method.LOCK: A6MethodLOCK,
A6Method.Method.UNLOCK: A6MethodUNLOCK,
A6Method.Method.PATCH: A6MethodPATCH,
A6Method.Method.TRACE: A6MethodTRACE,
}
methodCodes = {
A6MethodGET: A6Method.Method.GET,
A6MethodHEAD: A6Method.Method.HEAD,
A6MethodPOST: A6Method.Method.POST,
A6MethodPUT: A6Method.Method.PUT,
A6MethodDELETE: A6Method.Method.DELETE,
A6MethodMKCOL: A6Method.Method.MKCOL,
A6MethodCOPY: A6Method.Method.COPY,
A6MethodMOVE: A6Method.Method.MOVE,
A6MethodOPTIONS: A6Method.Method.OPTIONS,
A6MethodPROPFIND: A6Method.Method.PROPFIND,
A6MethodPROPPATCH: A6Method.Method.PROPPATCH,
A6MethodLOCK: A6Method.Method.LOCK,
A6MethodUNLOCK: A6Method.Method.UNLOCK,
A6MethodPATCH: A6Method.Method.PATCH,
A6MethodTRACE: A6Method.Method.TRACE,
}
def create_dict_entry(builder: flatbuffers.Builder, data: dict) -> list:
entries = []
if not isinstance(data, dict) or len(data) <= 0:
return entries
for key in data:
val = data[key]
key_bytes = builder.CreateString(key)
val_bytes = builder.CreateString(val)
A6Entry.Start(builder)
A6Entry.AddName(builder, key_bytes)
A6Entry.AddValue(builder, val_bytes)
entry = A6Entry.End(builder)
entries.append(entry)
return entries
def get_vector_object(action: int = 0, ty: int = 0):
objects = {
"%s:%s" % (HCAction.Action.Rewrite, VECTOR_TYPE_HEADER): HCRewrite.RewriteStartHeadersVector,
"%s:%s" % (HCAction.Action.Rewrite, VECTOR_TYPE_QUERY): HCRewrite.RewriteStartArgsVector,
"%s:%s" % (HCAction.Action.Stop, VECTOR_TYPE_HEADER): HCStop.StopStartHeadersVector,
}
return objects.get("%s:%s" % (action, ty), None)
def create_dict_vector(builder: flatbuffers.Builder, data: dict, action: int = 0, ty: int = 0):
res = 0
entries = create_dict_entry(builder, data)
entries_len = len(entries)
if entries_len == 0:
return res
vector_object = get_vector_object(action, ty)
if not vector_object:
return res
vector_object(builder, entries_len)
for i in range(entries_len - 1, -1, -1):
builder.PrependUOffsetTRelative(entries[i])
return builder.EndVector()
def create_str_vector(builder: flatbuffers.Builder, data: str):
res = 0
if not data or len(data) <= 0:
return res
data = data.encode(encoding="UTF-8")
return builder.CreateByteVector(data)
def new_builder():
return flatbuffers.Builder(256)
def get_method_name_by_code(code: int) -> str:
return methodNames.get(code)
def get_method_code_by_name(name: str) -> int:
return methodCodes.get(name)
def response_call(action_type: int):
def decorator(func):
def wrapper(cls, builder: flatbuffers.Builder):
(action, id) = func(cls, builder)
if not action or id == 0:
return False
HCResp.Start(builder)
HCResp.AddId(builder, id)
HCResp.AddActionType(builder, action_type)
HCResp.AddAction(builder, action)
res = HCResp.End(builder)
builder.Finish(res)
return True
return wrapper
return decorator
def response_config(func):
def wrapper(cls, builder: flatbuffers.Builder):
token = func(cls, builder)
if token <= 0:
return False
PCResp.Start(builder)
PCResp.AddConfToken(builder, token)
res = PCResp.End(builder)
builder.Finish(res)
return True
return wrapper
def response_unknown(func):
def wrapper(cls, builder: flatbuffers.Builder):
err_code = func(cls, builder)
if not err_code:
err_code = A6ErrCode.BAD_REQUEST
ErrResp.Start(builder)
ErrResp.AddCode(builder, err_code)
res = ErrResp.End(builder)
builder.Finish(res)
return True
return wrapper
def parse_dict_vector(cls: object, ty: int) -> dict:
res = {}
fn = dictVectorParseFuncNames.get(ty)
if not fn:
return res
length = getattr(cls, "%sLength" % fn)()
if not length or length == 0:
return res
for i in range(length):
key = getattr(cls, fn)(i).Name()
if key is not None:
key = key.decode()
val = getattr(cls, fn)(i).Value()
if key is not None:
val = val.decode()
res[key] = val
return res
def parse_list_vector(cls: object, ty: int, out_bytes: bool = False) -> list:
res = []
if out_bytes:
res = bytearray()
fn = listVectorParseFuncNames.get(ty)
if not fn:
return res
length = getattr(cls, "%sLength" % fn)()
if not length or length == 0:
return res
for i in range(length):
val = getattr(cls, fn)(i)
res.append(val)
return res