Skip to content

Commit 20d95ae

Browse files
committed
refactor: improve naming, logging, and connection handling
1 parent 2d1642f commit 20d95ae

14 files changed

Lines changed: 1151 additions & 474 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ poc/
3737

3838
# Dev
3939
.vscode
40+
openspec/

config/config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ producers:
2222
auth: auth
2323
channel: test
2424

25-
conn_timeout: 45
26-
max_tcp_payload: 4096
25+
conn_timeout: 45 # idle I/O timeout in seconds for established connections.
26+
max_tcp_payload: 4096 # bytes
27+
dial_timeout: 5 # timeout in seconds for proxy target connection.
2728

2829
capture_traffic:
2930
enabled: false

config/rules.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ rules:
3636
type: conn_handler
3737
target: mongodb
3838
- match: tcp dst port 9889
39-
type: tcp_proxy
40-
target: 127.0.0.1:9889 # Can use hostip:port for the required destination.
39+
type: proxy_tcp
40+
target: 127.0.0.1:9889
41+
- match: tcp dst port 3306
42+
type: proxy_tcp
43+
target: 127.0.0.1:3306
4144
- match: tcp
4245
type: conn_handler
4346
target: tcp

docs/configuration.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ This file holds the core settings for Glutton. Key configuration options include
1313
- **udp:** The UDP port for intercepted packets (default: `5001`).
1414
- **ssh:** Typically excluded from redirection to avoid interfering with SSH (default: `22`).
1515
- **interface:** The network interface Glutton listens on (default: `eth0`).
16-
- **max_tcp_payload:** Maximum TCP payload size in bytes (default: `4096`).
17-
- **conn_timeout:** The connection timeout duration in seconds (default: `45`).
16+
- **conn_timeout:** Idle I/O timeout, in seconds, for established connections (default: `45`).
17+
- **max_tcp_payload:** Maximum TCP payload size in bytes (default: `4096`). Proxy TCP uses this as the per-direction captured payload cap.
18+
- **dial_timeout:** Timeout, in seconds, for opening outbound proxy TCP target connections (default: `5`).
19+
- **capture_traffic.enabled:** Enables raw payload capture in logs and produced decoded events. When disabled, proxy TCP still forwards traffic and logs metadata, but raw payload bytes are omitted from decoded events.
1820
- **confpath:** The directory path where the configuration file resides.
1921
- **producers:**
2022
- **enabled**: Boolean flag to enable or disable logging/producer functionality.
@@ -55,6 +57,10 @@ producers:
5557

5658
conn_timeout: 45
5759
max_tcp_payload: 4096
60+
dial_timeout: 5
61+
62+
capture_traffic:
63+
enabled: false
5864
```
5965
6066
### config/rules.yaml
@@ -63,8 +69,8 @@ This file defines the rules that Glutton uses to determine which protocol handle
6369
6470
Key elements include:
6571
66-
- **type**: `conn_handler` to pass off to the appropriate protocol handler or `drop` to ignore packets.
67-
- **target**: Indicates the protocol handler (e.g., "http", "ftp") to be used.
72+
- **type**: `conn_handler` to pass off to the appropriate protocol handler, `proxy_tcp` to forward the TCP connection to an upstream target, or `drop` to ignore packets.
73+
- **target**: For `conn_handler`, indicates the protocol handler (e.g., `http`, `ftp`) to use. For `proxy_tcp`, this must be the upstream target in `host:port` form.
6874
- **match**: Define criteria such as source IP ranges or destination ports to match incoming traffic, according to [BPF syntax](https://biot.com/capstats/bpf.html).
6975

7076
Example rule:
@@ -80,8 +86,14 @@ rules:
8086
- match: tcp dst port 6969
8187
type: drop # drops any matching packets
8288
target: bittorrent
89+
- name: Proxy TCP example
90+
match: tcp dst port 9889
91+
type: proxy_tcp
92+
target: 127.0.0.1:9889
8393
```
8494

95+
`proxy_tcp` dials the configured `target` and forwards bytes in both directions between the incoming connection and the upstream service. Produced decoded events use the `proxy_tcp` protocol name and can include one captured payload entry per direction. Captured payloads are capped by `max_tcp_payload`; when a direction transfers more bytes than the cap, the decoded event is marked as truncated.
96+
8597
## Configuration Loading Process
8698
Glutton uses the [Viper](https://github.com/spf13/viper) library to load configuration settings. The process works as follows:
8799

glutton.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ func (g *Glutton) tcpListen() {
222222
g.Logger.Error("Failed to set connection timeout", producer.ErrAttr(err))
223223
}
224224

225-
if rule.Type == "tcp_proxy" {
225+
if rule.Type == "proxy_tcp" {
226226
if hfunc, ok := g.tcpProtocolHandlers[rule.Type]; ok {
227227
go func() {
228228
if err := hfunc(g.ctx, conn, md); err != nil {
229-
g.Logger.Error("Failed to handle TCP passthrough", producer.ErrAttr(err), slog.String("handler", "tcp_proxy"))
229+
g.Logger.Error("Failed to handle proxy TCP", producer.ErrAttr(err), slog.String("handler", "proxy_tcp"))
230230
}
231231
}()
232232
}

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ nav:
55
- Setup: setup.md
66
- Configuration: configuration.md
77
- Extension: extension.md
8+
- Engineering Guidelines: engineering-guidelines.md
89
- FAQs: faq.md
910
theme:
1011
name: readthedocs
11-

protocols/protocols.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func MapTCPProtocolHandlers(log interfaces.Logger, h interfaces.Honeypot) map[st
7272
protocolHandlers["mongodb"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
7373
return tcp.HandleMongoDB(ctx, conn, md, log, h)
7474
}
75-
protocolHandlers["tcp_proxy"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
76-
return tcp.HandlePassThrough(ctx, conn, md, log, h)
75+
protocolHandlers["proxy_tcp"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
76+
return tcp.HandleProxyTCP(ctx, conn, md, log, h)
7777
}
7878
protocolHandlers["tcp"] = func(ctx context.Context, conn net.Conn, md connection.Metadata) error {
7979
snip, bufConn, err := Peek(conn, 4)

protocols/tcp/passthrough.go

Lines changed: 0 additions & 200 deletions
This file was deleted.

0 commit comments

Comments
 (0)