diff --git a/go/pkg/apis/enricher/framework/go/go_detector.go b/go/pkg/apis/enricher/framework/go/go_detector.go index 89618b7b..40b142cb 100644 --- a/go/pkg/apis/enricher/framework/go/go_detector.go +++ b/go/pkg/apis/enricher/framework/go/go_detector.go @@ -11,6 +11,8 @@ package enricher import ( + "context" + "os" "regexp" "strings" @@ -28,6 +30,44 @@ func hasFramework(modules []*modfile.Require, tag string) bool { return false } +func DoGoPortsDetection(component *model.Component, ctx *context.Context) { + files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx) + if err != nil { + return + } + + matchRegexRules := model.PortMatchRules{ + MatchIndexRegexes: []model.PortMatchRule{ + { + Regex: regexp.MustCompile(`.ListenAndServe\([^,)]*`), + ToReplace: ".ListenAndServe(", + }, + { + Regex: regexp.MustCompile(`.Start\([^,)]*`), + ToReplace: ".Start(", + }, + }, + MatchRegexes: []model.PortMatchSubRule{ + { + Regex: regexp.MustCompile(`Addr:\s+"([^",]+)`), + SubRegex: regexp.MustCompile(`:*(\d+)$`), + }, + }, + } + + for _, file := range files { + bytes, err := os.ReadFile(file) + if err != nil { + continue + } + ports := GetPortFromFileGo(matchRegexRules, string(bytes)) + if len(ports) > 0 { + component.Ports = ports + return + } + } +} + func GetPortFromFileGo(rules model.PortMatchRules, text string) []int { ports := []int{} for _, matchIndexRegex := range rules.MatchIndexRegexes { diff --git a/go/pkg/apis/enricher/go_enricher.go b/go/pkg/apis/enricher/go_enricher.go index bf0dcdea..03668c9b 100644 --- a/go/pkg/apis/enricher/go_enricher.go +++ b/go/pkg/apis/enricher/go_enricher.go @@ -85,6 +85,9 @@ func (j GoEnricher) DoEnrichComponent(component *model.Component, settings model } } } + if len(component.Ports) == 0 { + framework.DoGoPortsDetection(component, ctx) + } } } if len(ports) > 0 { diff --git a/go/test/apis/component_recognizer_test.go b/go/test/apis/component_recognizer_test.go index 0ecedd50..b1a148c5 100644 --- a/go/test/apis/component_recognizer_test.go +++ b/go/test/apis/component_recognizer_test.go @@ -276,6 +276,10 @@ func TestPortDetectionGoGin(t *testing.T) { testPortDetectionInProject(t, "projectGin", []int{8789}) } +func TestPortDetectionGo(t *testing.T) { + testPortDetectionInProject(t, "projectGo", []int{8080}) +} + func TestPortDetectionGoFiber(t *testing.T) { testPortDetectionInProject(t, "projectGoFiber", []int{3000}) } diff --git a/resources/projectPortTesting/projectGo/go.mod b/resources/projectPortTesting/projectGo/go.mod new file mode 100644 index 00000000..43c3a4b6 --- /dev/null +++ b/resources/projectPortTesting/projectGo/go.mod @@ -0,0 +1,3 @@ +module github.com/burrsutter/go-net-http-hello + +go 1.19 diff --git a/resources/projectPortTesting/projectGo/main.go b/resources/projectPortTesting/projectGo/main.go new file mode 100644 index 00000000..350ce465 --- /dev/null +++ b/resources/projectPortTesting/projectGo/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "net/http" + "os" +) + +func main() { + http.HandleFunc("/", HelloHandler) + fmt.Println("Listening on localhost:8080") + http.ListenAndServe(":8080", nil) +} + +func HelloHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + hostname, err := os.Hostname() + if err != nil { + fmt.Println("unable to get hostname") + } + fmt.Fprintf(w, "Go Hello on %s\n", hostname) +}