| 1 | + | package prox5 |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "strconv" |
| 5 | + | "strings" |
| 6 | + | |
| 7 | + | "github.com/miekg/dns" |
| 8 | + | ipa "inet.af/netaddr" |
| 9 | + | |
| 10 | + | "git.tcp.direct/kayos/prox5/internal/pools" |
| 11 | + | ) |
| 12 | + | |
| 13 | + | func filterv6(in string) (filtered string, ok bool) { |
| 14 | + | split := strings.Split(in, "]:") |
| 15 | + | if len(split) < 2 { |
| 16 | + | |
| 17 | + | return "", false |
| 18 | + | } |
| 19 | + | split2 := strings.Split(split[1], ":") |
| 20 | + | switch len(split2) { |
| 21 | + | case 0: |
| 22 | + | combo, err := ipa.ParseIPPort(buildProxyString("", "", split[0], split2[0], true)) |
| 23 | + | if err == nil { |
| 24 | + | return combo.String(), true |
| 25 | + | } |
| 26 | + | case 1: |
| 27 | + | concat := buildProxyString("", "", split[0], split2[0], true) |
| 28 | + | combo, err := ipa.ParseIPPort(concat) |
| 29 | + | if err == nil { |
| 30 | + | return combo.String(), true |
| 31 | + | } |
| 32 | + | default: |
| 33 | + | _, err := ipa.ParseIPPort(buildProxyString("", "", split[0], split2[0], true)) |
| 34 | + | if err == nil { |
| 35 | + | return buildProxyString(split2[1], split2[2], split[0], split2[0], true), true |
| 36 | + | } |
| 37 | + | } |
| 38 | + | return "", true |
| 39 | + | } |
| 40 | + | |
| 41 | + | func isNumber(s string) bool { |
| 42 | + | _, err := strconv.Atoi(s) |
| 43 | + | return err == nil |
| 44 | + | } |
| 45 | + | |
| 46 | + | func buildProxyString(username, password, address, port string, v6 bool) (result string) { |
| 47 | + | builder := pools.CopABuffer.Get().(*strings.Builder) |
| 48 | + | if username != "" && password != "" { |
| 49 | + | builder.WriteString(username) |
| 50 | + | builder.WriteString(":") |
| 51 | + | builder.WriteString(password) |
| 52 | + | builder.WriteString("@") |
| 53 | + | } |
| 54 | + | builder.WriteString(address) |
| 55 | + | if v6 { |
| 56 | + | builder.WriteString("]") |
| 57 | + | } |
| 58 | + | builder.WriteString(":") |
| 59 | + | builder.WriteString(port) |
| 60 | + | result = builder.String() |
| 61 | + | pools.DiscardBuffer(builder) |
| 62 | + | return |
| 63 | + | } |
| 64 | + | |
| 65 | + | func filter(in string) (filtered string, ok bool) { //nolint:cyclop |
| 66 | + | if !strings.Contains(in, ":") { |
| 67 | + | return "", false |
| 68 | + | } |
| 69 | + | split := strings.Split(in, ":") |
| 70 | + | |
| 71 | + | if len(split) < 2 { |
| 72 | + | return "", false |
| 73 | + | } |
| 74 | + | switch len(split) { |
| 75 | + | case 2: |
| 76 | + | _, isDomain := dns.IsDomainName(split[0]) |
| 77 | + | if isDomain && isNumber(split[1]) { |
| 78 | + | return in, true |
| 79 | + | } |
| 80 | + | combo, err := ipa.ParseIPPort(in) |
| 81 | + | if err != nil { |
| 82 | + | return "", false |
| 83 | + | } |
| 84 | + | return combo.String(), true |
| 85 | + | case 4: |
| 86 | + | _, isDomain := dns.IsDomainName(split[0]) |
| 87 | + | if isDomain && isNumber(split[1]) { |
| 88 | + | return buildProxyString(split[2], split[3], split[0], split[1], false), true |
| 89 | + | } |
| 90 | + | _, isDomain = dns.IsDomainName(split[2]) |
| 91 | + | if isDomain && isNumber(split[3]) { |
| 92 | + | return buildProxyString(split[0], split[1], split[2], split[3], false), true |
| 93 | + | } |
| 94 | + | if _, err := ipa.ParseIPPort(split[2] + ":" + split[3]); err == nil { |
| 95 | + | return buildProxyString(split[0], split[1], split[2], split[3], false), true |
| 96 | + | } |
| 97 | + | if _, err := ipa.ParseIPPort(split[0] + ":" + split[1]); err == nil { |
| 98 | + | return buildProxyString(split[2], split[3], split[0], split[1], false), true |
| 99 | + | } |
| 100 | + | default: |
| 101 | + | if !strings.Contains(in, "[") || !strings.Contains(in, "]:") { |
| 102 | + | return "", false |
| 103 | + | } |
| 104 | + | } |
| 105 | + | return filterv6(in) |
| 106 | + | } |
| 107 | + | |