-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpcintercept.go
389 lines (307 loc) · 9.54 KB
/
grpcintercept.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"regexp"
"strings"
"text/template"
)
const _pkg_prefix = "package "
const _mult_imp_ = "import ("
var (
service = flag.String("Service", "", "The receiver passed to grpc")
grpcRegister = flag.String("GRPCRegister", "", "The function to register with grpc")
)
func check(e error) {
if e != nil {
panic(e)
}
}
type TemplateData struct {
Package string
SourceFile string
ServiceType string
RegisterFunc string
IsMainFile bool
ProtobufPackages []string
InterceptorPackages []string
ServiceCalls []ServiceDef
}
type ServiceDef struct {
CallName string
ServiceType string
InputProto string
OutputProto string
InputProtoName string
InputInterceptor string
}
func isServiceFunc(st string, tmpldat TemplateData) bool {
rxp := fmt.Sprintf("^func \\([A-z_]+\\s(\\*%s|%s)\\)", tmpldat.ServiceType, tmpldat.ServiceType)
re := regexp.MustCompile(rxp)
return re.MatchString(st)
}
func stripFuncReceiver(st string, tmpldat TemplateData) string {
rxp := fmt.Sprintf("^func \\([A-z_]+\\s(\\*%s|%s)\\)", tmpldat.ServiceType, tmpldat.ServiceType)
re := regexp.MustCompile(rxp)
return re.ReplaceAllString(st, "")
}
func stripComments(rpl []byte) []byte {
re := regexp.MustCompile("(?s)//.*?\n|/\\*.*?\\*/")
return re.ReplaceAll(rpl, nil)
}
func cleanupImport(st string) string {
first := strings.TrimPrefix(st, "import ")
return strings.TrimSpace(strings.Trim(first, "\""))
}
func cleanupFuncDef(st string) string {
r := strings.NewReplacer(")", "", "(", "", "{", "", "}", "")
f := r.Replace(st)
return strings.TrimSpace(f)
}
func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
func funcArgSplit(st string) (nme string, tpe string) {
split := strings.Split(strings.TrimSpace(st), " ")
if len(split) == 1 {
return "", split[0]
} else if len(split) == 2 {
return split[0], split[1]
} else {
panic("Function arguments not 1/2")
}
}
func typePackage(st string) string {
split := strings.Split(strings.TrimSpace(st), ".")
split[0] = strings.TrimPrefix(split[0], "*")
split[0] = strings.TrimPrefix(split[0], "&")
return split[0]
}
func formatInputPackage(name string, pkg string) string {
return fmt.Sprintf("%s \"%s\"", name, pkg)
}
func main() {
flag.Parse()
info, _ := os.Stat(flag.Args()[0])
inputFileMode := info.Mode()
dat, err := ioutil.ReadFile(flag.Args()[0])
check(err)
formatted, ferr := format.Source(dat)
check(ferr)
formatted = stripComments(formatted)
scanner := bufio.NewScanner(bytes.NewReader(formatted))
tmpldat := TemplateData{
SourceFile: flag.Args()[0],
ServiceType: *service,
RegisterFunc: *grpcRegister,
IsMainFile: false,
}
insideImport := false
importMap := map[string]string{}
var curFuncName string
insideFuncInput := false
insideFuncOutput := false
funcDefinitionMap := map[string]string{}
/*Scan through our formatted ource
Comments are already stripped out here
Goal is to generate a map of service functions
with their definitions, and a map of imports to their packages
In addition we're figuring out the package name
AND we're checking if this is the main file of the package
To determine that, we look if a type with our ServiceType name
is defined in the source. If it is, we're the main
*/
for scanner.Scan() {
//fmt.Println("----")
line := scanner.Text()
//fmt.Println(scanner.Text())
if !insideFuncInput {
if strings.HasPrefix(line, "type "+tmpldat.ServiceType) {
tmpldat.IsMainFile = true
continue
}
if isServiceFunc(line, tmpldat) {
pure := stripFuncReceiver(line, tmpldat)
curFuncName = strings.TrimSpace(pure[:strings.Index(pure, "(")])
line = pure[strings.Index(pure, "(")+1:]
funcDefinitionMap[curFuncName] = ""
insideFuncInput = true
}
}
addedDef := false
if insideFuncInput {
line = strings.TrimSpace(line)
funcDefinitionMap[curFuncName] = fmt.Sprintf("%s %s", funcDefinitionMap[curFuncName], line)
addedDef = true
foundOutput := false
for _, ir := range line {
if ir == ')' {
insideFuncInput = false
}
if !insideFuncInput {
if ir == '(' {
foundOutput = true
}
}
}
if !insideFuncInput && !foundOutput {
delete(funcDefinitionMap, curFuncName)
curFuncName = ""
} else if !insideFuncInput && foundOutput {
insideFuncOutput = true
line = line[strings.Index(line, "(")+1:]
}
}
if insideFuncOutput {
line = strings.TrimSpace(line)
if !addedDef {
funcDefinitionMap[curFuncName] = fmt.Sprintf("%s %s", funcDefinitionMap[curFuncName], line)
}
hasBodyDef := false
for _, or := range line {
if or == ')' {
insideFuncOutput = false
}
if !insideFuncOutput {
if or == '{' {
hasBodyDef = true
}
}
}
if !insideFuncOutput && !hasBodyDef {
delete(funcDefinitionMap, curFuncName)
curFuncName = ""
} else if !insideFuncOutput {
curFuncName = ""
}
continue
}
//Trim line and do checks
line = strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if !insideImport {
if strings.HasPrefix(line, _pkg_prefix) {
tmpldat.Package = strings.TrimPrefix(line, _pkg_prefix)
continue
}
if strings.HasPrefix(line, _mult_imp_) {
insideImport = true
} else if strings.HasPrefix(line, "import ") {
pkg := cleanupImport(line)
split := strings.Split(pkg, "/")
rename := split[len(split)-1]
importMap[rename] = pkg
}
} else {
if strings.HasSuffix(line, ")") {
insideImport = false
continue
}
//fmt.Println(line)
if strings.Contains(line, " \"") {
//Renaming import
split := strings.SplitAfterN(line, " \"", 2)
rename := cleanupImport(split[0])
pkg := cleanupImport(split[1])
importMap[rename] = pkg
} else {
pkg := cleanupImport(line)
split := strings.Split(pkg, "/")
rename := split[len(split)-1]
importMap[rename] = pkg
}
}
}
/* Figure out which function definitions are really relevant
gRPC/protobuf has a specific format for functions, so we'll follow along with that
In protobuf, an RPC service HAS to take in 1 arg and return 1 arg
In grpc, the stubs generated from the proto files will have 2 arguments
in and 2 arguments out. The second argument returned HAS to be an error,
and the first argument taken in HAS to be context.
The remaining arguments are the service input/response.
In addition, this func HAS to have a receiver of our ServiceType type
To check if this should be intercepted, we need to see if the function
has a total of 3 input arguments, and matches all of the above.
*/
for funcName, funcDef := range funcDefinitionMap {
split := strings.SplitAfterN(funcDef, ") (", 2)
input := cleanupFuncDef(split[0])
output := cleanupFuncDef(split[1])
splitInput := strings.Split(input, ",")
splitOutput := strings.Split(output, ",")
if len(splitOutput) != 2 {
//gRPC requires 2 returns
continue
}
if len(splitInput) < 2 {
//Require at least 2 inputs
continue
}
_, lstTpe := funcArgSplit(splitOutput[1])
if lstTpe != "error" {
//Last argument must be error
continue
}
_, frstTpe := funcArgSplit(splitInput[0])
if !strings.Contains(frstTpe, ".Context") {
//First argument is not context
continue
}
//Yes, this needs to be done here
if len(splitInput) != 3 {
panic("Found service definition, but not enough input args for middleware")
}
inputProtoNme, inputProtoTpe := funcArgSplit(splitInput[1])
inputPbPkg := typePackage(inputProtoTpe)
inputPbPkgImp := formatInputPackage(inputPbPkg, importMap[inputPbPkg])
_, outputProtoTpe := funcArgSplit(splitOutput[0])
outputPbPkg := typePackage(outputProtoTpe)
outputPbPkgImp := formatInputPackage(outputPbPkg, importMap[outputPbPkg])
if !stringInSlice(inputPbPkgImp, tmpldat.ProtobufPackages) {
tmpldat.ProtobufPackages = append(tmpldat.ProtobufPackages, inputPbPkgImp)
}
if !stringInSlice(outputPbPkgImp, tmpldat.ProtobufPackages) {
tmpldat.ProtobufPackages = append(tmpldat.ProtobufPackages, outputPbPkgImp)
}
_, inputIntTpe := funcArgSplit(splitInput[2])
inputIntPkg := typePackage(inputIntTpe)
inputIntPkgImp := formatInputPackage(inputIntPkg, importMap[inputIntPkg])
if !stringInSlice(inputIntPkgImp, tmpldat.InterceptorPackages) {
tmpldat.InterceptorPackages = append(tmpldat.InterceptorPackages, inputIntPkgImp)
}
serviceDef := new(ServiceDef)
serviceDef.ServiceType = tmpldat.ServiceType
serviceDef.CallName = funcName
serviceDef.InputProto = strings.TrimSpace(splitInput[1])
serviceDef.InputProtoName = inputProtoNme
serviceDef.OutputProto = strings.TrimSpace(splitOutput[0])
serviceDef.InputInterceptor = strings.TrimSpace(inputIntTpe)
tmpldat.ServiceCalls = append(tmpldat.ServiceCalls, *serviceDef)
}
registerPkg := typePackage(tmpldat.RegisterFunc)
registerPkgImp := formatInputPackage(registerPkg, importMap[registerPkg])
if !stringInSlice(registerPkgImp, tmpldat.ProtobufPackages) {
tmpldat.ProtobufPackages = append(tmpldat.ProtobufPackages, registerPkgImp)
}
var toWrite bytes.Buffer
tmpl := template.Must(template.New("outputProtoTpe").Parse(TEMPLATE))
tmpl.Execute(&toWrite, tmpldat)
final, finalErr := format.Source(toWrite.Bytes())
check(finalErr)
outputFile := tmpldat.SourceFile[:strings.LastIndex(tmpldat.SourceFile, ".")]
outputFile = outputFile + ".intr.go"
writeErr := ioutil.WriteFile(outputFile, final, inputFileMode)
check(writeErr)
}