Skip to content

Commit

Permalink
feat: Match schema://host format from input data
Browse files Browse the repository at this point in the history
  • Loading branch information
mstxq17 committed Sep 2, 2024
1 parent 278f23e commit 7738ab9
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
43 changes: 37 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/cheggaaa/pb/v3"
"github.com/mstxq17/MoreFind/core"
"github.com/mstxq17/MoreFind/core/extract"
"github.com/mstxq17/MoreFind/core/utils/uniqueutil"
"github.com/mstxq17/MoreFind/update"
"github.com/mstxq17/MoreFind/vars"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -354,7 +356,7 @@ func runCommand(cmd *cobra.Command, args []string) {
}
}
}
if myUrl == false && myDomain == false && myIp == false {
if myUrl == false && myDomain == false && myIp == false && myLink == false {
myUrl = true
}
var urlList []string
Expand All @@ -378,16 +380,40 @@ func runCommand(cmd *cobra.Command, args []string) {
}
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if myUrl == true || myDomain == true {
if myUrl == true || myDomain == true || myLink == true {
searchUrl := searchUrl(line)
for _, _url := range searchUrl {
_url = strings.TrimSpace(_url)
trimUrl := strings.TrimSpace(_url)
if myLink == true {
simpleUrl, hasSchema, err := extract.SimpleUrl(trimUrl)
if err != nil {
logger.Printf("handle: %v Err: %v \n", trimUrl, err)
continue
}
if mySchema != "" && !hasSchema {
// 写入输出
simpleUrl = fmt.Sprintf("%v://%v", mySchema, simpleUrl)
if uniqueutil.IsKeyUniq(simpleUrl, found) {
outputBuffer.WriteString(simpleUrl, &customStringHandler)
found[simpleUrl] = struct{}{}
outputchan <- outputBuffer.TempString
}
} else {
// 写入输出
if uniqueutil.IsKeyUniq(simpleUrl, found) {
outputBuffer.WriteString(simpleUrl, &customStringHandler)
found[simpleUrl] = struct{}{}
outputchan <- outputBuffer.TempString
}
}
}

if myUrl == true {
if output != "" {
urlList = append(urlList, _url)
urlList = append(urlList, trimUrl)
}
// remove repeated string
if _, ok := found[_url]; !ok {
if _, ok := found[trimUrl]; !ok {
if myUrlFilter != "" {
if !filterExt(_url, myUrlFilter) {
outputBuffer.WriteString(_url, &customStringHandler)
Expand All @@ -401,7 +427,7 @@ func runCommand(cmd *cobra.Command, args []string) {
}
}
if myDomain == true {
port, _domain := searchDomain(_url, myRootDomain)
port, _domain := searchDomain(trimUrl, myRootDomain)
if _domain == "" || isIPAddr(_domain) {
continue
}
Expand Down Expand Up @@ -476,6 +502,8 @@ var (
myProgress bool
myUpdate bool
myQuiet bool
myLink bool
mySchema string
myXlsx string
myIPFormats []string
rootCmd = &cobra.Command{
Expand Down Expand Up @@ -516,6 +544,9 @@ func init() {
NewLine = core.NewLine()
rootCmd.PersistentFlags().StringVarP(&file, "file", "f", "", vars.FileHelpEn)
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", vars.OutputHelpEn)
rootCmd.PersistentFlags().BoolVarP(&myLink, "link", "k", false, vars.TargetHelpEn)
rootCmd.PersistentFlags().StringVarP(&mySchema, "schema", "", "", vars.SchemaHelpEn)
rootCmd.PersistentFlags().Lookup("schema").NoOptDefVal = ""
rootCmd.PersistentFlags().BoolVarP(&myIp, "ip", "i", false, vars.IPHelpEn)
rootCmd.PersistentFlags().BoolVarP(&myPrivateIp, "exclude", "", false, vars.ExcludeHelpEn)
rootCmd.PersistentFlags().BoolVarP(&myDomain, "domain", "d", false, vars.DomainHelpEn)
Expand Down
27 changes: 27 additions & 0 deletions core/extract/simple_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package extract

import (
"fmt"
"net/url"
)

func SimpleUrl(input string) (string, bool, error) {
parsed, err := url.Parse(input)
if err != nil {
return "", false, err
}
if parsed.Scheme != "" {
tSimpleUrl := fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host)
return tSimpleUrl, true, nil
} else {
// 拼接协议进行解析
modifiedHost := fmt.Sprintf("http://%s", input)
parsed, err := url.Parse(modifiedHost)
if err != nil {
return "", false, err
} else {
tSimpleUrl := fmt.Sprintf("%s", parsed.Hostname())
return tSimpleUrl, false, nil
}
}
}
34 changes: 34 additions & 0 deletions core/extract/simple_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package extract

import "testing"

// Test cases for SimpleUrl
func TestSimpleUrl(t *testing.T) {
testCases := []struct {
input string
expected string
hasScheme bool
}{
{"http://example.com:8080", "http://example.com:8080", true},
{"http://example.com", "http://example.com", true},
{"https://example.com/path", "https://example.com", true},
{"ftp://example.com", "ftp://example.com", true},
{"example.com/123.php?a=1", "example.com", false},
{"example.com", "example.com", false},
{"http://", "http://", true}, // Invalid URL
{"", "", false}, // Empty string
}

for _, tc := range testCases {
result, hasScheme, err := SimpleUrl(tc.input)
if err != nil {
t.Errorf("input: %s, unexpected error: %v", tc.input, err)
}
if result != tc.expected {
t.Errorf("input: %s, expected: %s, got: %s", tc.input, tc.expected, result)
}
if hasScheme != tc.hasScheme {
t.Errorf("input: %s, expected hasScheme: %v, got: %v", tc.input, tc.hasScheme, hasScheme)
}
}
}
8 changes: 8 additions & 0 deletions core/utils/uniqueutil/uniqueutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uniqueutil

func IsKeyUniq(key string, found map[string]struct{}) bool {
if _, ok := found[key]; !ok {
return true
}
return false
}
6 changes: 6 additions & 0 deletions vars/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const (
IPHelpEn = "Matches IPs from the input pipe or file."
IPHelpZh = "从输入管道或文件中匹配 IP。"

TargetHelpEn = "Matches schema://host from the input pipe or file."
TargetHelpZh = "从输入管道或文件中匹配 schema://host。"

SchemaHelpEn = "When use with -t, the default is set to the specified protocol"
SchemaHelpZh = "与 -t 一起使用,默认设置为指定协议"

ExcludeHelpEn = "Excludes internal/private IP segments when using -i/--ip."
ExcludeHelpZh = "在使用 -i/--ip 时排除内部/私有 IP 段。"

Expand Down
2 changes: 1 addition & 1 deletion vars/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package vars

// VERSION number
// 版本号
const VERSION string = "1.5.6"
const VERSION string = "1.5.7"

0 comments on commit 7738ab9

Please sign in to comment.