/
grpc_completionJson.go
147 lines (136 loc) · 4.38 KB
/
grpc_completionJson.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
package yakgrpc
import (
"context"
"time"
"github.com/samber/lo"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/yak"
"github.com/yaklang/yaklang/common/yak/antlr4yak/yakvm"
"github.com/yaklang/yaklang/common/yak/static_analyzer/result"
"github.com/yaklang/yaklang/common/yak/yaklang"
"github.com/yaklang/yaklang/common/yakdocument"
"github.com/yaklang/yaklang/common/yakgrpc/ypb"
"github.com/davecgh/go-spew/spew"
)
var completionJsonCd = utils.NewCoolDown(5 * time.Second)
var (
completionJsonRaw []byte
)
func (s *Server) GetYakVMBuildInMethodCompletion(
ctx context.Context,
req *ypb.GetYakVMBuildInMethodCompletionRequest,
) (*ypb.GetYakVMBuildInMethodCompletionResponse, error) {
if !yaklang.IsNew() {
return &ypb.GetYakVMBuildInMethodCompletionResponse{}, nil
}
var sug []*ypb.MethodSuggestion
stringBuildin := yakvm.GetStringBuildInMethod()
if stringBuildin != nil && len(stringBuildin) > 0 {
var suggestion = make([]*ypb.SuggestionDescription, len(stringBuildin))
var index = 0
for methodName, method := range stringBuildin {
if ret, _ := method.VSCodeSnippets(); ret == "" {
spew.Dump(method)
continue
}
snippets, verbose := method.VSCodeSnippets()
suggestion[index] = &ypb.SuggestionDescription{
Label: methodName,
Description: method.Description,
InsertText: snippets,
DefinitionVerbose: verbose,
}
index++
}
sug = append(sug, &ypb.MethodSuggestion{
FuzzKeywords: []string{
"str", "host", "name", "word", "payload",
"raw", "bytes", "packet", "packets",
},
ExactKeywords: []string{"s", "ss", "a", "b", "abc"},
Suggestions: suggestion,
Verbose: "(string)",
})
}
if ret := yakvm.GetSliceBuildInMethod(); ret != nil && len(ret) > 0 {
var descs []*ypb.SuggestionDescription
for name, method := range ret {
if s, _ := method.VSCodeSnippets(); s == "" {
spew.Dump(method)
continue
}
s, v := method.VSCodeSnippets()
descs = append(descs, &ypb.SuggestionDescription{
Label: name,
Description: method.Description,
InsertText: s,
DefinitionVerbose: v,
})
}
sug = append(sug, &ypb.MethodSuggestion{
FuzzKeywords: []string{
"list", "slice", "all", "li",
"raw", "names", "passwords", "payloads",
"usernames", "dict", "ports", "hosts",
"result", "numbers",
},
ExactKeywords: []string{"ll", "l", "a", "b", "li", "abc"},
Suggestions: descs,
Verbose: `(list)`,
})
}
if ret := yakvm.GetMapBuildInMethod(); ret != nil && len(ret) > 0 {
var descs []*ypb.SuggestionDescription
for name, method := range ret {
if s, _ := method.VSCodeSnippets(); s == "" {
spew.Dump(method)
continue
}
s, v := method.VSCodeSnippets()
descs = append(descs, &ypb.SuggestionDescription{
Label: name,
Description: method.Description,
InsertText: s,
DefinitionVerbose: v,
})
}
sug = append(sug, &ypb.MethodSuggestion{
FuzzKeywords: []string{
"pair", "result", "raw", "map",
"dict",
},
ExactKeywords: []string{"m", "dict", "d", "r"},
Suggestions: descs,
Verbose: `(map)`,
})
}
return &ypb.GetYakVMBuildInMethodCompletionResponse{
Suggestions: sug,
}, nil
}
func (s *Server) GetYakitCompletionRaw(ctx context.Context, _ *ypb.Empty) (*ypb.YakitCompletionRawResponse, error) {
if completionJsonRaw != nil {
return &ypb.YakitCompletionRawResponse{RawJson: completionJsonRaw}, nil
}
completionJsonCd.Do(func() {
libs := yak.EngineToLibDocuments(yaklang.New())
completionJsonRaw, _ = yakdocument.LibDocsToCompletionJsonShort(libs...)
})
return &ypb.YakitCompletionRawResponse{RawJson: completionJsonRaw}, nil
}
func (s *Server) StaticAnalyzeError(ctx context.Context, r *ypb.StaticAnalyzeErrorRequest) (*ypb.StaticAnalyzeErrorResponse, error) {
tmpRes := yak.StaticAnalyzeYaklang(utils.UnsafeBytesToString(r.GetCode()), r.GetPluginType())
es := lo.Map(tmpRes, func(i *result.StaticAnalyzeResult, _ int) *ypb.StaticAnalyzeErrorResult {
return &ypb.StaticAnalyzeErrorResult{
Message: []byte(i.Message),
StartLineNumber: i.StartLineNumber,
EndLineNumber: i.EndLineNumber,
StartColumn: i.StartColumn,
EndColumn: i.EndColumn,
// RawMessage: []byte{},
Severity: string(i.Severity),
Tag: string(i.Tag),
}
})
return &ypb.StaticAnalyzeErrorResponse{Result: es}, nil
}