diff --git a/cmd/defang.go b/cmd/defang.go index 6ae730e..f4cb458 100644 --- a/cmd/defang.go +++ b/cmd/defang.go @@ -12,8 +12,8 @@ import ( "github.com/spf13/cobra" "log" "os" - "regexp" "runtime" + "soc-cli/internal/logic" "strings" ) @@ -39,7 +39,7 @@ var defangCmd = &cobra.Command{ input = strings.TrimSpace(in) } - defanged := defang(input) + defanged := logic.Defang(input) fmt.Println(defanged) }, } @@ -48,30 +48,6 @@ func init() { rootCmd.AddCommand(defangCmd) } -func defang(input string) string { - emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) - if emailRegex.MatchString(input) { - return defangEmail(input) - } - - return defangURL(input) -} - -func defangEmail(email string) string { - defanged := strings.Replace(email, "@", "[at]", 1) - defanged = strings.Replace(defanged, ".", "[.]", -1) - - return defanged -} - -func defangURL(url string) string { - defanged := strings.Replace(url, "http://", "hxxp://", 1) - defanged = strings.Replace(defanged, "https://", "hxxps://", 1) - defanged = strings.Replace(defanged, ".", "[.]", -1) - - return defanged -} - // isInputFromPipe checks if the standard input is coming from a pipe func isInputFromPipe() bool { // Check if stdin is a terminal diff --git a/cmd/fang.go b/cmd/fang.go index 4275089..4c448b1 100644 --- a/cmd/fang.go +++ b/cmd/fang.go @@ -9,33 +9,16 @@ package cmd import ( "fmt" "github.com/spf13/cobra" - "strings" + "soc-cli/internal/logic" ) -// fang reverses the defanged URLs or email addresses -func fang(input string) string { - // Replace 'hxxp' or 'hxxps' with 'http' or 'https' - fanged := strings.Replace(input, "hxxp", "http", -1) - fanged = strings.Replace(fanged, "hxxps", "https", -1) - - // Replace '[.]' back to '.' - fanged = strings.Replace(fanged, "[.]", ".", -1) - - // Replace '[at]' or similar with '@' for email addresses - fanged = strings.Replace(fanged, "[at]", "@", -1) - fanged = strings.Replace(fanged, "(at)", "@", -1) - fanged = strings.Replace(fanged, "[@]", "@", -1) - - return fanged -} - var fangCmd = &cobra.Command{ Use: "fang [input]", Short: "Convert defanged URLs or email addresses back to their original form", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { input := args[0] - result := fang(input) + result := logic.Fang(input) fmt.Println(result) }, } diff --git a/internal/logic/fangdefang.go b/internal/logic/fangdefang.go new file mode 100644 index 0000000..251b57b --- /dev/null +++ b/internal/logic/fangdefang.go @@ -0,0 +1,53 @@ +/* +Copyright © 2024 Alessandro Riva + +Licensed under the MIT License. +See the LICENSE file for details. +*/ +package logic + +import ( + "regexp" + "strings" +) + +func Defang(input string) string { + emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) + if emailRegex.MatchString(input) { + return DefangEmail(input) + } + + return DefangURL(input) +} + +func DefangEmail(email string) string { + defanged := strings.Replace(email, "@", "[at]", 1) + defanged = strings.Replace(defanged, ".", "[.]", -1) + + return defanged +} + +func DefangURL(url string) string { + defanged := strings.Replace(url, "http://", "hxxp://", 1) + defanged = strings.Replace(defanged, "https://", "hxxps://", 1) + defanged = strings.Replace(defanged, ".", "[.]", -1) + + return defanged +} + +// fang reverses the defanged URLs or email addresses +func Fang(input string) string { + // Replace 'hxxp' or 'hxxps' with 'http' or 'https' + fanged := strings.Replace(input, "hxxp", "http", -1) + fanged = strings.Replace(fanged, "hxxps", "https", -1) + + // Replace '[.]' back to '.' + fanged = strings.Replace(fanged, "[.]", ".", -1) + + // Replace '[at]' or similar with '@' for email addresses + fanged = strings.Replace(fanged, "[at]", "@", -1) + fanged = strings.Replace(fanged, "(at)", "@", -1) + fanged = strings.Replace(fanged, "[@]", "@", -1) + + return fanged +}