-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/aquasecurity/tracee/cmd/traceectl/pkg/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func PrepareServer(cmd *cobra.Command, server client.ServerInfo) (client.ServerInfo, error) { | ||
var err error | ||
var address string | ||
err = determineConnectionType(server) | ||
if err != nil { | ||
return server, err | ||
} | ||
switch server.ConnectionType { | ||
case client.Protocol_UNIX: | ||
address = fmt.Sprintf("unix://%s", server.Addr) | ||
case client.Protocol_TCP: | ||
address = fmt.Sprintf(server.Addr) | ||
default: | ||
return server, fmt.Errorf("unsupported connection type: %s", server.ConnectionType) | ||
} | ||
server.Addr = address | ||
return server, nil | ||
} | ||
|
||
func determineConnectionType(server client.ServerInfo) error { | ||
if strings.Contains(server.Addr, ":") && isValidTCPAddress(server.Addr) { | ||
server.ConnectionType = client.Protocol_TCP | ||
return nil | ||
} | ||
if strings.HasPrefix(server.Addr, "/") { | ||
server.ConnectionType = client.Protocol_UNIX | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("unsupported connection type: %s", server.Addr) | ||
|
||
} | ||
func isValidTCPAddress(addr string) bool { | ||
host, port, err := net.SplitHostPort(addr) | ||
if err != nil || host == "" || port == "" { | ||
return false | ||
} | ||
if _, err := net.LookupPort("tcp", port); err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |