This commit is contained in:
eyedeekay
2025-02-03 23:40:09 -05:00
parent 43d9954ebb
commit 1713fd7f9b
5 changed files with 347 additions and 0 deletions

136
doc.md Normal file
View File

@ -0,0 +1,136 @@
# filter
--
import "github.com/go-i2p/go-connfilter"
## Usage
```go
var ErrInvalidFilter = errors.New("target and replacement must have the same length")
```
```go
var ErrInvalidFunctionFilter = errors.New("invalid Function filter")
```
```go
var ErrInvalidRegexFilter = errors.New("invalid regex filter")
```
#### func NewConnFilter
```go
func NewConnFilter(parentConn net.Conn, targets, replacements []string) (net.Conn, error)
```
NewConnFilter creates a new ConnFilter that replaces occurrences of target
strings with replacement strings in the data read from the connection. It
returns an error if the lengths of target and replacement slices are not equal.
#### func NewFunctionConnFilter
```go
func NewFunctionConnFilter(parentConn net.Conn, readFilter, writeFilter func(b []byte) ([]byte, error)) (net.Conn, error)
```
NewFunctionConnFilter creates a new FunctionConnFilter that has the powerful
ability to rewrite any byte that comes across the net.Conn with user-defined
functions. By default, the filters are no-op functions.
#### func NewRegexConnFilter
```go
func NewRegexConnFilter(parentConn net.Conn, regex string) (net.Conn, error)
```
NewRegexConnFilter creates a new RegexConnFilter that replaces occurrences of
target regex with empty strings in the data read from the connection. It returns
an error if the lengths of target and replacement slices are not equal.
#### type ConnFilter
```go
type ConnFilter struct {
net.Conn
}
```
#### func (*ConnFilter) Read
```go
func (c *ConnFilter) Read(b []byte) (n int, err error)
```
Read reads data from the underlying connection and replaces all occurrences of
target strings with their corresponding replacement strings. The replacements
are made sequentially for each target-replacement pair. The modified data is
then copied back to the provided buffer.
#### func (*ConnFilter) Write
```go
func (c *ConnFilter) Write(b []byte) (n int, err error)
```
Write writes the data to the underlying connection after replacing all
occurrences of target strings with their corresponding replacement strings. The
replacements are made sequentially for each target-replacement pair.
#### type FunctionConnFilter
```go
type FunctionConnFilter struct {
net.Conn
ReadFilter func(b []byte) ([]byte, error)
WriteFilter func(b []byte) ([]byte, error)
}
```
#### func (*FunctionConnFilter) Read
```go
func (c *FunctionConnFilter) Read(b []byte) (n int, err error)
```
Read reads data from the underlying connection and modifies the bytes according
to c.Filter
#### func (*FunctionConnFilter) Write
```go
func (c *FunctionConnFilter) Write(b []byte) (n int, err error)
```
Write modifies the bytes according to c.Filter and writes the result to the
underlying connection
#### type RegexConnFilter
```go
type RegexConnFilter struct {
FunctionConnFilter
}
```
#### func (*RegexConnFilter) Read
```go
func (c *RegexConnFilter) Read(b []byte) (n int, err error)
```
Read reads data from the underlying connection and replaces all occurrences of
target regex with empty strings. The modified data is then copied back to the
provided buffer.
#### func (*RegexConnFilter) ReadFilter
```go
func (c *RegexConnFilter) ReadFilter(b []byte) ([]byte, error)
```
NewRegexConnFilter creates a new RegexConnFilter that replaces occurrences of
target regex with empty strings in the data read from the connection. target
regex is c.match
#### func (*RegexConnFilter) WriteFilter
```go
func (c *RegexConnFilter) WriteFilter(b []byte) ([]byte, error)
```
WriteFilter creates a new RegexConnFilter that replaces occurrences of target
regex with empty strings in the data read from the connection. target regex is
c.match

102
http/doc.md Normal file
View File

@ -0,0 +1,102 @@
# httpinspector
--
import "github.com/go-i2p/go-connfilter/http"
Package httpinspector provides HTTP traffic inspection and modification
capabilities by wrapping the standard net.Listener interface.
## Usage
```go
var (
ErrInvalidModification = errors.New("invalid HTTP message modification")
ErrMalformedHTTP = errors.New("malformed HTTP message")
ErrClosedInspector = errors.New("inspector is closed")
)
```
Common errors returned by the inspector.
#### func DefaultRequestCallback
```go
func DefaultRequestCallback(*http.Request) error
```
DefaultRequestCallback is a no-op request callback.
#### func DefaultResponseCallback
```go
func DefaultResponseCallback(*http.Response) error
```
DefaultResponseCallback is a no-op response callback.
#### type Config
```go
type Config struct {
OnRequest RequestCallback // Called for each request
OnResponse ResponseCallback // Called for each response
}
```
Config contains configuration options for the HTTP inspector.
#### func DefaultConfig
```go
func DefaultConfig() Config
```
DefaultConfig returns a Config with reasonable defaults.
#### type Inspector
```go
type Inspector struct {
}
```
Inspector wraps a net.Listener to provide HTTP traffic inspection.
#### func New
```go
func New(listener net.Listener, config Config) *Inspector
```
New creates a new Inspector wrapping the provided listener.
#### func (*Inspector) Accept
```go
func (i *Inspector) Accept() (net.Conn, error)
```
Accept implements the net.Listener Accept method.
#### func (*Inspector) Addr
```go
func (i *Inspector) Addr() net.Addr
```
Addr implements the net.Listener Addr method.
#### func (*Inspector) Close
```go
func (i *Inspector) Close() error
```
Close implements the net.Listener Close method.
#### type RequestCallback
```go
type RequestCallback func(*http.Request) error
```
RequestCallback is called for each HTTP request intercepted.
#### type ResponseCallback
```go
type ResponseCallback func(*http.Response) error
```
ResponseCallback is called for each HTTP response intercepted.

2
http/example/doc.md Normal file
View File

@ -0,0 +1,2 @@
# example
--

105
irc/doc.md Normal file
View File

@ -0,0 +1,105 @@
# ircinspector
--
import "github.com/go-i2p/go-connfilter/irc"
## Usage
#### type Config
```go
type Config struct {
OnMessage func(*Message) error
OnNumeric func(int, *Message) error
Logger Logger
}
```
Config contains inspector configuration
#### type Filter
```go
type Filter struct {
Command string
Channel string
Prefix string
Callback func(*Message) error
}
```
Filter defines criteria for message filtering
#### type Inspector
```go
type Inspector struct {
}
```
Inspector implements the net.Listener interface with IRC inspection
#### func New
```go
func New(listener net.Listener, config Config) *Inspector
```
New creates a new IRC inspector wrapping an existing listener
#### func (*Inspector) Accept
```go
func (i *Inspector) Accept() (net.Conn, error)
```
Accept implements net.Listener Accept method
#### func (*Inspector) AddFilter
```go
func (i *Inspector) AddFilter(filter Filter)
```
#### func (*Inspector) Addr
```go
func (i *Inspector) Addr() net.Addr
```
Addr implements net.Listener Addr method
#### func (*Inspector) Close
```go
func (i *Inspector) Close() error
```
Close implements net.Listener Close method
#### type Logger
```go
type Logger interface {
Debug(format string, args ...interface{})
Error(format string, args ...interface{})
}
```
Logger interface for customizable logging
#### type Message
```go
type Message struct {
Raw string
Prefix string
Command string
Params []string
Trailing string
}
```
Message represents a parsed IRC message
#### func (*Message) String
```go
func (m *Message) String() string
```

2
irc/example/doc.md Normal file
View File

@ -0,0 +1,2 @@
# example
--