-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from projectdiscovery/feat-onetimepool
Moving Jarm Calculation with one time connection pool
- Loading branch information
Showing
12 changed files
with
251 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package connpool | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
|
||
mapsutil "github.com/projectdiscovery/utils/maps" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
type InFlightConns struct { | ||
inflightConns *mapsutil.SyncLockMap[net.Conn, struct{}] | ||
} | ||
|
||
func NewInFlightConns() (*InFlightConns, error) { | ||
m := &mapsutil.SyncLockMap[net.Conn, struct{}]{ | ||
Map: mapsutil.Map[net.Conn, struct{}]{}, | ||
} | ||
return &InFlightConns{inflightConns: m}, nil | ||
} | ||
|
||
func (i *InFlightConns) Add(conn net.Conn) { | ||
_ = i.inflightConns.Set(conn, struct{}{}) | ||
} | ||
|
||
func (i *InFlightConns) Remove(conn net.Conn) { | ||
i.inflightConns.Delete(conn) | ||
} | ||
|
||
func (i *InFlightConns) Close() error { | ||
var errs []error | ||
|
||
_ = i.inflightConns.Iterate(func(conn net.Conn, _ struct{}) error { | ||
if err := conn.Close(); err != nil { | ||
errs = append(errs, err) | ||
} | ||
return nil | ||
}) | ||
|
||
if ok := i.inflightConns.Clear(); !ok { | ||
errs = append(errs, errors.New("couldn't empty in flight connections")) | ||
} | ||
|
||
return multierr.Combine(errs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package connpool | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type Dialer interface { | ||
Dial(ctx context.Context, network, address string) (net.Conn, error) | ||
} | ||
|
||
// OneTimePool is a pool designed to create continous bare connections that are for one time only usage | ||
type OneTimePool struct { | ||
address string | ||
idleConnections chan net.Conn | ||
InFlightConns *InFlightConns | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
Dialer Dialer | ||
} | ||
|
||
func NewOneTimePool(ctx context.Context, address string, poolSize int) (*OneTimePool, error) { | ||
idleConnections := make(chan net.Conn, poolSize) | ||
inFlightConns, err := NewInFlightConns() | ||
if err != nil { | ||
return nil, err | ||
} | ||
pool := &OneTimePool{ | ||
address: address, | ||
idleConnections: idleConnections, | ||
InFlightConns: inFlightConns, | ||
} | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
pool.ctx, pool.cancel = context.WithCancel(ctx) | ||
return pool, nil | ||
} | ||
|
||
// Acquire acquires an idle connection from the pool | ||
func (p *OneTimePool) Acquire(c context.Context) (net.Conn, error) { | ||
select { | ||
case <-p.ctx.Done(): | ||
return nil, p.ctx.Err() | ||
case <-c.Done(): | ||
return nil, c.Err() | ||
case conn := <-p.idleConnections: | ||
p.InFlightConns.Remove(conn) | ||
return conn, nil | ||
} | ||
} | ||
|
||
func (p *OneTimePool) Run() error { | ||
for { | ||
select { | ||
case <-p.ctx.Done(): | ||
return p.ctx.Err() | ||
default: | ||
var ( | ||
conn net.Conn | ||
err error | ||
) | ||
if p.Dialer != nil { | ||
conn, err = p.Dialer.Dial(p.ctx, "tcp", p.address) | ||
} else { | ||
conn, err = net.Dial("tcp", p.address) | ||
} | ||
if err == nil { | ||
p.InFlightConns.Add(conn) | ||
p.idleConnections <- conn | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (p *OneTimePool) Close() error { | ||
p.cancel() | ||
// remove dialer references | ||
p.Dialer = nil | ||
return p.InFlightConns.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package jarm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
gojarm "github.com/hdm/jarm-go" | ||
connpool "github.com/projectdiscovery/utils/conn/connpool" | ||
) | ||
|
||
// PoolCount defines how many connection are kept in the pool | ||
var PoolCount = 3 | ||
|
||
// fingerprint probes a single host/port | ||
func HashWithDialer(dialer connpool.Dialer, host string, port int, duration int) (string, error) { | ||
var results []string | ||
addr := net.JoinHostPort(host, fmt.Sprintf("%d", port)) | ||
|
||
timeout := time.Duration(duration) * time.Second | ||
ctx, cancel := context.WithTimeout(context.Background(), (time.Duration(duration*PoolCount) * time.Second)) | ||
defer cancel() | ||
|
||
// using connection pool as we need multiple probes | ||
pool, err := connpool.NewOneTimePool(ctx, addr, PoolCount) | ||
if err != nil { | ||
return "", err | ||
} | ||
pool.Dialer = dialer | ||
|
||
defer func() { _ = pool.Close() }() | ||
go func() { _ = pool.Run() }() | ||
|
||
for _, probe := range gojarm.GetProbes(host, port) { | ||
conn, err := pool.Acquire(ctx) | ||
if err != nil { | ||
continue | ||
} | ||
if conn == nil { | ||
continue | ||
} | ||
_ = conn.SetWriteDeadline(time.Now().Add(timeout)) | ||
_, err = conn.Write(gojarm.BuildProbe(probe)) | ||
if err != nil { | ||
results = append(results, "") | ||
_ = conn.Close() | ||
continue | ||
} | ||
_ = conn.SetReadDeadline(time.Now().Add(timeout)) | ||
buff := make([]byte, 1484) | ||
_, _ = conn.Read(buff) | ||
_ = conn.Close() | ||
ans, err := gojarm.ParseServerHello(buff, probe) | ||
if err != nil { | ||
results = append(results, "") | ||
continue | ||
} | ||
results = append(results, ans) | ||
} | ||
hash := gojarm.RawHashToFuzzyHash(strings.Join(results, ",")) | ||
return hash, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.