nuclei/pkg/protocols/http/race/syncedreadcloser.go

107 lines
2.3 KiB
Go
Raw Normal View History

2020-12-22 03:54:55 +05:30
package race
2020-10-19 02:57:30 +02:00
import (
"fmt"
"io"
"time"
)
// SyncedReadCloser is compatible with io.ReadSeeker and performs
2020-12-22 03:54:55 +05:30
// gate-based synced writes to enable race condition testing.
type SyncedReadCloser struct {
2020-10-19 02:57:30 +02:00
data []byte
p int64
length int64
openGate chan struct{}
2020-10-19 02:57:30 +02:00
enableBlocking bool
}
// NewSyncedReadCloser creates a new SyncedReadCloser instance.
func NewSyncedReadCloser(r io.ReadCloser) *SyncedReadCloser {
2020-10-19 02:57:30 +02:00
var (
s SyncedReadCloser
2020-10-19 02:57:30 +02:00
err error
)
s.data, err = io.ReadAll(r)
2020-10-19 02:57:30 +02:00
if err != nil {
return nil
}
Remove singletons from Nuclei engine (continuation of #6210) (#6296) * introducing execution id * wip * . * adding separate execution context id * lint * vet * fixing pg dialers * test ignore * fixing loader FD limit * test * fd fix * wip: remove CloseProcesses() from dev merge * wip: fix merge issue * protocolstate: stop memguarding on last dialer delete * avoid data race in dialers.RawHTTPClient * use shared logger and avoid race conditions * use shared logger and avoid race conditions * go mod * patch executionId into compiled template cache * clean up comment in Parse * go mod update * bump echarts * address merge issues * fix use of gologger * switch cmd/nuclei to options.Logger * address merge issues with go.mod * go vet: address copy of lock with new Copy function * fixing tests * disable speed control * fix nil ExecuterOptions * removing deprecated code * fixing result print * default logger * cli default logger * filter warning from results * fix performance test * hardcoding path * disable upload * refactor(runner): uses `Warning` instead of `Print` for `pdcpUploadErrMsg` Signed-off-by: Dwi Siswanto <git@dw1.io> * Revert "disable upload" This reverts commit 114fbe6663361bf41cf8b2645fd2d57083d53682. * Revert "hardcoding path" This reverts commit cf12ca800e0a0e974bd9fd4826a24e51547f7c00. --------- Signed-off-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Mzack9999 <mzack9999@protonmail.com> Co-authored-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Dwi Siswanto <25837540+dwisiswant0@users.noreply.github.com>
2025-07-09 14:47:26 -05:00
defer func() {
_ = r.Close()
}()
2020-10-19 02:57:30 +02:00
s.length = int64(len(s.data))
s.openGate = make(chan struct{})
2020-10-19 02:57:30 +02:00
s.enableBlocking = true
return &s
}
// NewOpenGateWithTimeout creates a new open gate with a timeout
func NewOpenGateWithTimeout(r io.ReadCloser, d time.Duration) *SyncedReadCloser {
s := NewSyncedReadCloser(r)
2020-10-19 02:57:30 +02:00
s.OpenGateAfter(d)
return s
}
// SetOpenGate sets the status of the blocking gate
func (s *SyncedReadCloser) SetOpenGate(status bool) {
2020-10-19 02:57:30 +02:00
s.enableBlocking = status
}
// OpenGate opens the gate allowing all requests to be completed
func (s *SyncedReadCloser) OpenGate() {
s.openGate <- struct{}{}
2020-10-19 02:57:30 +02:00
}
// OpenGateAfter schedules gate to be opened after a duration
func (s *SyncedReadCloser) OpenGateAfter(d time.Duration) {
2020-10-19 02:57:30 +02:00
time.AfterFunc(d, func() {
s.openGate <- struct{}{}
2020-10-19 02:57:30 +02:00
})
}
// Seek implements seek method for io.ReadSeeker
func (s *SyncedReadCloser) Seek(offset int64, whence int) (int64, error) {
2020-10-19 02:57:30 +02:00
var err error
switch whence {
case io.SeekStart:
s.p = 0
case io.SeekCurrent:
if s.p+offset < s.length {
s.p += offset
break
}
err = fmt.Errorf("offset is too big")
case io.SeekEnd:
if s.length-offset >= 0 {
s.p = s.length - offset
break
}
err = fmt.Errorf("offset is too big")
}
return s.p, err
}
// Read implements read method for io.ReadSeeker
func (s *SyncedReadCloser) Read(p []byte) (n int, err error) {
2020-10-19 02:57:30 +02:00
// If the data fits in the buffer blocks awaiting the sync instruction
if s.p+int64(len(p)) >= s.length && s.enableBlocking {
<-s.openGate
2020-10-19 02:57:30 +02:00
}
n = copy(p, s.data[s.p:])
s.p += int64(n)
if s.p == s.length {
err = io.EOF
}
return n, err
}
// Close closes an io.ReadSeeker
func (s *SyncedReadCloser) Close() error {
2020-10-19 02:57:30 +02:00
return nil
}
// Len returns the length of data in reader
func (s *SyncedReadCloser) Len() int {
2020-10-19 02:57:30 +02:00
return int(s.length)
}