Skip to content

Commit

Permalink
这是一个临时版本修改一些ipv6问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Jun 16, 2023
1 parent 49cca96 commit 0e6a262
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
1 change: 0 additions & 1 deletion internal/cli/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/goflags/normalized_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions internal/utils/fixscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
}
44 changes: 39 additions & 5 deletions internal/utils/fixurl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import (
"errors"
"fmt"
"net/url"
"strings"
)
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 0e6a262

Please sign in to comment.