Projects STRLCPY prox5 Commits aebdfe21
🤬
  • ■ ■ ■ ■ ■ ■
    internal/pools/strings.go
     1 +package pools
     2 + 
     3 +import (
     4 + "strings"
     5 + "sync"
     6 +)
     7 + 
     8 +var CopABuffer = &sync.Pool{New: func() interface{} { return &strings.Builder{} }}
     9 + 
     10 +func DiscardBuffer(buf *strings.Builder) {
     11 + buf.Reset()
     12 + CopABuffer.Put(buf)
     13 +}
     14 + 
  • ■ ■ ■ ■ ■
    list_management.go
    skipped 1 lines
    2 2   
    3 3  import (
    4 4   "bufio"
    5  - "fmt"
    6 5   "io"
    7 6   "os"
    8 7   "strconv"
    9 8   "strings"
    10 9   "sync/atomic"
    11 10   "time"
    12  - 
    13  - "github.com/miekg/dns"
    14  - ipa "inet.af/netaddr"
    15 11  )
    16 12   
    17 13  // throw shit proxies here, get map
    skipped 4 lines
    22 18   inChan = make(chan string, 100000)
    23 19  }
    24 20   
    25  -func checkV6(in string) (filtered string, ok bool) {
    26  - split := strings.Split(in, "]:")
    27  - if len(split) != 2 {
    28  - return in, false
    29  - }
    30  - 
    31  - combo, err := ipa.ParseIPPort(split[0] + "]:" + split[1])
    32  - if err != nil {
    33  - return in, false
    34  - }
    35  - 
    36  - if !strings.Contains(split[1], ":") {
    37  - return combo.String(), true
    38  - }
    39  - 
    40  - split6 := strings.Split(split[1], ":")
    41  - if len(split6) != 2 {
    42  - return in, false
    43  - }
    44  - 
    45  - return fmt.Sprintf("%s:%s@%s", split6[0], split6[1], combo.String()), true
    46  -}
    47  - 
    48  -func (s *Swamp) filter(in string) (filtered string, ok bool) {
    49  - if !strings.Contains(in, ":") {
    50  - return in, false
    51  - }
    52  - split := strings.Split(in, ":")
    53  - 
    54  - if len(split) < 2 {
    55  - return in, false
    56  - }
    57  - 
    58  - if _, err := strconv.Atoi(split[1]); err != nil {
    59  - return in, false
    60  - }
    61  - 
    62  - switch len(split) {
    63  - case 2:
    64  - if _, ok := dns.IsDomainName(split[0]); ok {
    65  - return in, true
    66  - }
    67  - combo, err := ipa.ParseIPPort(in)
    68  - if err != nil {
    69  - return in, false
    70  - }
    71  - return combo.String(), true
    72  - case 4:
    73  - if _, ok := dns.IsDomainName(split[0]); ok {
    74  - return fmt.Sprintf("%s:%s@%s:%s", split[2], split[3], split[0], split[1]), true
    75  - }
    76  - combo, err := ipa.ParseIPPort(split[0] + ":" + split[1])
    77  - if err != nil {
    78  - return in, false
    79  - }
    80  - return fmt.Sprintf("%s:%s@%s", split[2], split[3], combo.String()), true
    81  - default:
    82  - if !strings.Contains(split[0], "[") || !strings.Contains(split[0], "]:") {
    83  - return in, false
    84  - }
    85  - }
    86  - return checkV6(in)
    87  -}
    88  - 
    89 21  // LoadProxyTXT loads proxies from a given seed file and feeds them to the mapBuilder to be later queued automatically for validation.
    90 22  // Expects the following formats:
    91 23  // * 127.0.0.1:1080
    skipped 33 lines
    125 57   
    126 58  // LoadSingleProxy loads a SOCKS proxy into our map. Uses the format: 127.0.0.1:1080 (host:port).
    127 59  func (s *Swamp) LoadSingleProxy(sock string) (ok bool) {
    128  - if sock, ok = s.filter(sock); !ok {
     60 + if sock, ok = filter(sock); !ok {
    129 61   return
    130 62   }
    131 63   go s.loadSingleProxy(sock)
    skipped 32 lines
  • ■ ■ ■ ■ ■ ■
    parse.go
     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 + 
  • ■ ■ ■ ■ ■ ■
    parse_test.go
     1 +package prox5
     2 + 
     3 +import "testing"
     4 + 
     5 +func Test_filter(t *testing.T) {
     6 + type args struct {
     7 + in string
     8 + }
     9 + type test struct {
     10 + name string
     11 + args args
     12 + wantFiltered string
     13 + wantOk bool
     14 + }
     15 + var tests = []test{
     16 + {
     17 + name: "simple",
     18 + args: args{
     19 + in: "127.0.0.1:1080",
     20 + },
     21 + wantFiltered: "127.0.0.1:1080",
     22 + wantOk: true,
     23 + },
     24 + {
     25 + name: "withAuth",
     26 + args: args{
     27 + in: "127.0.0.1:1080:user:pass",
     28 + },
     29 + wantFiltered: "user:[email protected]:1080",
     30 + wantOk: true,
     31 + },
     32 + {
     33 + name: "simpleDomain",
     34 + args: args{
     35 + in: "yeet.com:1080",
     36 + },
     37 + wantFiltered: "yeet.com:1080",
     38 + wantOk: true,
     39 + },
     40 + {
     41 + name: "domainWithAuth",
     42 + args: args{
     43 + in: "yeet.com:1080:user:pass",
     44 + },
     45 + wantFiltered: "user:[email protected]:1080",
     46 + wantOk: true,
     47 + },
     48 + {
     49 + name: "ipv6",
     50 + args: args{
     51 + in: "[fe80::2ef0:5dff:fe7f:c299]:1080",
     52 + },
     53 + wantFiltered: "[fe80::2ef0:5dff:fe7f:c299]:1080",
     54 + wantOk: true,
     55 + },
     56 + {
     57 + name: "ipv6WithAuth",
     58 + args: args{
     59 + in: "[fe80::2ef0:5dff:fe7f:c299]:1080:user:pass",
     60 + },
     61 + wantFiltered: "user:pass@[fe80::2ef0:5dff:fe7f:c299]:1080",
     62 + wantOk: true,
     63 + },
     64 + {
     65 + name: "invalid",
     66 + args: args{
     67 + in: "yeet",
     68 + },
     69 + wantFiltered: "",
     70 + wantOk: false,
     71 + },
     72 + }
     73 + for _, tt := range tests {
     74 + t.Run(tt.name, func(t *testing.T) {
     75 + gotFiltered, gotOk := filter(tt.args.in)
     76 + if gotFiltered != tt.wantFiltered {
     77 + t.Errorf("filter() gotFiltered = %v, want %v", gotFiltered, tt.wantFiltered)
     78 + }
     79 + if gotOk != tt.wantOk {
     80 + t.Errorf("filter() gotOk = %v, want %v", gotOk, tt.wantOk)
     81 + }
     82 + })
     83 + }
     84 +}
     85 + 
Please wait...
Page is in error, reload to recover