-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathscrape.go
More file actions
194 lines (178 loc) · 5.79 KB
/
scrape.go
File metadata and controls
194 lines (178 loc) · 5.79 KB
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
package tsgen
import (
"fmt"
"go/types"
"regexp"
"strings"
"github.com/projectdiscovery/utils/errkit"
)
// scrape.go scrapes all information of exported type from different package
func (p *EntityParser) scrapeAndCreate(typeName string) error {
if p.newObjects[typeName] == nil {
return nil
}
// get package name
pkgName := strings.Split(typeName, ".")[0]
baseTypeName := strings.Split(typeName, ".")[1]
// get package
pkg, ok := p.imports[pkgName]
if !ok {
return errkit.Newf("package %v for type %v not found", pkgName, typeName)
}
// get type
obj := pkg.Types.Scope().Lookup(baseTypeName)
if obj == nil {
return errkit.Newf("type %v not found in package %+v", typeName, pkg)
}
// Ensure the object is a type name
typeNameObj, ok := obj.(*types.TypeName)
if !ok {
return errkit.Newf("%v is not a type name", typeName)
}
// Ensure the type is a named struct type
// Skip interfaces (like net.Conn) - they can't be scraped for fields
namedStruct, ok := typeNameObj.Type().Underlying().(*types.Struct)
if !ok {
// Not a struct (could be interface, etc.) - skip silently
return nil
}
// fmt.Printf("got named struct %v\n", namedStruct)
// Iterate over the struct fields
d := &ExtObject{
builtIn: make(map[string]string),
nested: map[string]map[string]*ExtObject{},
}
// fmt.Printf("fields %v\n", namedStruct.NumFields())
for i := 0; i < namedStruct.NumFields(); i++ {
field := namedStruct.Field(i)
fieldName := field.Name()
if field.Exported() {
recursiveScrapeType(nil, fieldName, field.Type(), d)
}
}
entityMap := make(map[string]Entity)
// convert ExtObject to Entity
properties := ConvertExtObjectToEntities(d, entityMap)
entityMap[baseTypeName] = Entity{
Name: baseTypeName,
Type: "interface",
Description: fmt.Sprintf("%v Interface", baseTypeName),
Object: Interface{
Properties: properties,
},
}
for _, entity := range entityMap {
p.entities = append(p.entities, entity)
}
return nil
}
type ExtObject struct {
builtIn map[string]string
nested map[string]map[string]*ExtObject // Changed to map of field names to ExtObject
}
func recursiveScrapeType(parentType types.Type, fieldName string, fieldType types.Type, extObject *ExtObject) {
if named, ok := fieldType.(*types.Named); ok && !named.Obj().Exported() {
// fmt.Printf("type %v is not exported\n", named.Obj().Name())
return
}
if fieldType.String() == "time.Time" {
extObject.builtIn[fieldName] = "Date"
return
}
switch t := fieldType.Underlying().(type) {
case *types.Pointer:
// fmt.Printf("type %v is a pointer\n", fieldType)
recursiveScrapeType(nil, fieldName, t.Elem(), extObject)
case *types.Signature:
// fmt.Printf("type %v is a callback or interface\n", fieldType)
case *types.Basic:
// Check for basic types (built-in types)
if parentType != nil {
switch p := parentType.Underlying().(type) {
case *types.Slice:
extObject.builtIn[fieldName] = "[]" + fieldType.String()
case *types.Array:
extObject.builtIn[fieldName] = fmt.Sprintf("[%v]", p.Len()) + fieldType.String()
}
} else {
extObject.builtIn[fieldName] = fieldType.String()
}
case *types.Struct:
// Check for struct types
if extObject.nested[fieldName] == nil {
// @tarunKoyalwar: it currently does not supported struct arrays
extObject.nested[fieldName] = make(map[string]*ExtObject)
}
nestedExtObject := &ExtObject{
builtIn: make(map[string]string),
nested: map[string]map[string]*ExtObject{},
}
extObject.nested[fieldName][fieldType.String()] = nestedExtObject
for i := 0; i < t.NumFields(); i++ {
field := t.Field(i)
if field.Exported() {
recursiveScrapeType(nil, field.Name(), field.Type(), nestedExtObject)
}
}
case *types.Array:
// fmt.Printf("type %v is an array\n", fieldType)
// get array type
recursiveScrapeType(t, fieldName, t.Elem(), extObject)
case *types.Slice:
// fmt.Printf("type %v is a slice\n", fieldType)
// get slice type
recursiveScrapeType(t, fieldName, t.Elem(), extObject)
default:
// fmt.Printf("type %v is not a builtIn or struct\n", fieldType)
}
}
var re = regexp.MustCompile(`\[[0-9]+\].*`)
// ConvertExtObjectToEntities recursively converts an ExtObject to a list of Entity objects
func ConvertExtObjectToEntities(extObj *ExtObject, nestedTypes map[string]Entity) []Property {
var properties []Property
// Iterate over the built-in types
for fieldName, fieldType := range extObj.builtIn {
var description string
if re.MatchString(fieldType) {
// if it is a fixed size array add len in description
description = fmt.Sprintf("fixed size array of length: %v", fieldType[:strings.Index(fieldType, "]")+1])
// remove length from type
fieldType = "[]" + fieldType[strings.Index(fieldType, "]")+1:]
}
if strings.Contains(fieldType, "time.Duration") {
description = "time in nanoseconds"
}
px := Property{
Name: fieldName,
Type: toTsTypes(fieldType),
Description: description,
}
if strings.HasPrefix(px.Type, "[") {
px.Type = fieldType[strings.Index(px.Type, "]")+1:] + "[]"
}
properties = append(properties, px)
}
// Iterate over the nested types
for fieldName, nestedExtObjects := range extObj.nested {
for origType, nestedExtObject := range nestedExtObjects {
// fix:me this nestedExtObject always has only one element
got := ConvertExtObjectToEntities(nestedExtObject, nestedTypes)
baseTypename := origType[strings.LastIndex(origType, ".")+1:]
// create new nestedType
nestedTypes[baseTypename] = Entity{
Name: baseTypename,
Description: fmt.Sprintf("%v Interface", baseTypename),
Type: "interface",
Object: Interface{
Properties: got,
},
}
// assign current field type to nested type
properties = append(properties, Property{
Name: fieldName,
Type: baseTypename,
})
}
}
return properties
}