Skip to content

Commit

Permalink
Moved fang defang logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 24, 2024
1 parent e0741fd commit 68c3541
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
28 changes: 2 additions & 26 deletions cmd/defang.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/spf13/cobra"
"log"
"os"
"regexp"
"runtime"
"soc-cli/internal/logic"
"strings"
)

Expand All @@ -39,7 +39,7 @@ var defangCmd = &cobra.Command{
input = strings.TrimSpace(in)
}

defanged := defang(input)
defanged := logic.Defang(input)
fmt.Println(defanged)
},
}
Expand All @@ -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
Expand Down
21 changes: 2 additions & 19 deletions cmd/fang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand Down
53 changes: 53 additions & 0 deletions internal/logic/fangdefang.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 68c3541

Please sign in to comment.