From 0e6a2624ad21652004713dd93aeaaf4e2db73c1a Mon Sep 17 00:00:00 2001 From: becivells <732903873@qq.com> Date: Fri, 16 Jun 2023 12:35:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E6=98=AF=E4=B8=80=E4=B8=AA=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E7=89=88=E6=9C=AC=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?ipv6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cli/parser.go | 1 - internal/goflags/normalized_slice.go | 2 +- internal/utils/fixscheme.go | 15 ++++++++-- internal/utils/fixurl.go | 44 ++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/internal/cli/parser.go b/internal/cli/parser.go index 8ad9ceb..ca4af4d 100644 --- a/internal/cli/parser.go +++ b/internal/cli/parser.go @@ -13,7 +13,6 @@ import ( "time" "github.com/projectdiscovery/fastdialer/fastdialer" - "github.com/xiecat/fofax/internal/fx" "github.com/xiecat/fofax/internal/fxparser" "github.com/xiecat/fofax/internal/goflags" diff --git a/internal/goflags/normalized_slice.go b/internal/goflags/normalized_slice.go index 0032fdb..4ba9df3 100644 --- a/internal/goflags/normalized_slice.go +++ b/internal/goflags/normalized_slice.go @@ -15,7 +15,7 @@ func (normalizedStringSlice NormalizedStringSlice) String() string { return normalizedStringSlice.createStringArrayDefaultValue() } -//Set appends a value to the string slice. +// Set appends a value to the string slice. func (normalizedStringSlice *NormalizedStringSlice) Set(value string) error { if slice, err := ToNormalizedStringSlice(value); err != nil { return err diff --git a/internal/utils/fixscheme.go b/internal/utils/fixscheme.go index 61d18b2..dd191f4 100644 --- a/internal/utils/fixscheme.go +++ b/internal/utils/fixscheme.go @@ -7,6 +7,15 @@ import ( "github.com/xiecat/fofax/internal/printer" ) +func FixFullHostIpv6(fullUrl string) string { + if strings.Count(fullUrl, ":") > 2 && !strings.Contains(fullUrl, "[") && !strings.Contains(fullUrl, "]") { + start := strings.Index(fullUrl, "://") + 3 + ipv6 := fullUrl[start:] + return fmt.Sprintf("%s[%s]", fullUrl[0:start], ipv6) + } + return fullUrl +} + func FixFullHostInfoScheme(fields []string) string { if len(fields) < 4 { printer.Errorf("fileds len err: %v", fields) @@ -20,10 +29,10 @@ func FixFullHostInfoScheme(fields []string) string { schemaType := strings.TrimSpace(fields[3]) if strings.HasPrefix(schemaType, "https://") { - return host + return FixFullHostIpv6(host) } if strings.HasPrefix(schemaType, "http://") { - return host + return FixFullHostIpv6(host) } - return fmt.Sprintf("%s://%s", protocol, host) + return FixFullHostIpv6(fmt.Sprintf("%s://%s", protocol, host)) } diff --git a/internal/utils/fixurl.go b/internal/utils/fixurl.go index efc124c..ab3a3f4 100644 --- a/internal/utils/fixurl.go +++ b/internal/utils/fixurl.go @@ -1,6 +1,8 @@ package utils import ( + "errors" + "fmt" "net/url" "strings" ) @@ -21,6 +23,32 @@ type FixUrl struct { ParseWithScheme bool //是否解析的时候自带scheme } +// is_ipv6=true && port="8080" +// "https://2407:c080:17ef:ffff::7703:d86e" +func FixFofaHostsHttpIpv6(ipv6URL string) (*url.URL, error) { + // 提取 IPv6 地址部分 + start := strings.Index(ipv6URL, "://") + 3 + if start == -1 { + return nil, errors.New("invalid host: " + ipv6URL) + } + ipv6 := ipv6URL[start:] + + // 构建规范化的 IPv6 URL + formattedURL := fmt.Sprintf("%s[%s]", ipv6URL[0:start], ipv6) + return url.Parse(formattedURL) +} + +// "[240e:468:810:ab69:2042:7ff:fe58:881a]:8080" +func FetchIpv6AndIpv4HostAndPort(uHost string) string { + + if strings.HasPrefix(uHost, "[") { + strat := strings.Index(uHost, "[") + 1 + end := strings.Index(uHost, "]") + return "[" + uHost[strat:end] + "]" + } + return strings.Split(uHost, ":")[0] +} + // NewFixUrl 归一化url格式 func NewFixUrl(hostinfo string) (*FixUrl, error) { fullurl := hostinfo @@ -29,15 +57,21 @@ func NewFixUrl(hostinfo string) (*FixUrl, error) { fullurl = "http://" + fullurl } fullurl = strings.Trim(fullurl, " \t\r\n") - u, err := url.Parse(fullurl) + if err != nil { - return nil, err + // "https://2407:c080:17ef:ffff::7703:d86e" + + u, err = FixFofaHostsHttpIpv6(fullurl) + if err != nil { + return nil, err + } } - ipArray := strings.Split(u.Host, ":") - ip := ipArray[0] - port := u.Port() + //"[240e:468:810:ab69:2042:7ff:fe58:881a]:8080" + ip := FetchIpv6AndIpv4HostAndPort(u.Host) + port := u.Port() + //ipv6 [240e:468:810:ab69:2042:7ff:fe58:881a]:8080 if len(port) == 0 { if u.Scheme == "https" { port = "443"