-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin to send pcap data over Unix socket using gRPC
- Loading branch information
Showing
12 changed files
with
273 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "deps/agent-plugins-grpc"] | ||
path = deps/agent-plugins-grpc | ||
url = [email protected]:deepfence/agent-plugins-grpc.git | ||
branch = add-pcap |
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 @@ | ||
git submodule update --init --remote --recursive ./deps/agent-plugins-grpc/ |
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,5 @@ | ||
output: | ||
plugins: | ||
agent: | ||
socketPath: /tmp/agent.sock | ||
pcapMode: all |
Submodule agent-plugins-grpc
added at
a94c8f
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/deepfence/PacketStreamer/pkg/config" | ||
pb "github.com/deepfence/PacketStreamer/proto" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
SOCKET_RETRIES = 10 | ||
) | ||
|
||
type Plugin struct { | ||
client pb.PcapForwarderClient | ||
stopChan chan struct{} | ||
} | ||
|
||
func NewPlugin(conf *config.AgentPluginConfig) (*Plugin, error) { | ||
|
||
_, errw := os.Stat(conf.SocketPath) | ||
tries := 1 | ||
for errors.Is(errw, os.ErrNotExist) { | ||
_, errw = os.Stat(conf.SocketPath) | ||
time.Sleep(1 * time.Second) | ||
if tries == SOCKET_RETRIES { | ||
return nil, fmt.Errorf("No socket found: %v", errw) | ||
} | ||
tries += 1 | ||
} | ||
|
||
conn, err := grpc.Dial("unix://"+conf.SocketPath, grpc.WithAuthority("dummy"), grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Plugin{ | ||
client: pb.NewPcapForwarderClient(conn), | ||
stopChan: make(chan struct{}, 1), | ||
}, nil | ||
} | ||
|
||
func (p *Plugin) Start(ctx context.Context) (chan<- string, error) { | ||
|
||
stream, err := p.client.SendPackets(ctx) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
recvChan := make(chan string, 10) | ||
|
||
go func() { | ||
loop: | ||
for { | ||
select { | ||
case packet := <-recvChan: | ||
stream.Send(&pb.Packet{Payload: []byte(packet)}) | ||
case <-p.stopChan: | ||
break loop | ||
} | ||
} | ||
stream.CloseAndRecv() | ||
}() | ||
|
||
return recvChan, nil | ||
} | ||
|
||
func (p *Plugin) Stop() { | ||
p.stopChan <- struct{}{} | ||
} |
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