mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
feat: add gvisor endpoint log (#375)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||
@@ -46,7 +47,7 @@ func (h *gvisorTCPHandler) handle(ctx context.Context, tcpConn net.Conn) {
|
||||
h.readFromEndpointWriteToTCPConn(ctx, tcpConn, endpoint)
|
||||
util.SafeClose(errChan)
|
||||
}()
|
||||
stack := NewStack(ctx, endpoint)
|
||||
stack := NewStack(ctx, sniffer.NewWithPrefix(endpoint, "[gVISOR] "))
|
||||
defer stack.Destroy()
|
||||
select {
|
||||
case <-errChan:
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||
@@ -29,6 +30,7 @@ func (h *gvisorTCPHandler) readFromEndpointWriteToTCPConn(ctx context.Context, c
|
||||
|
||||
pktBuffer := endpoint.ReadContext(ctx)
|
||||
if pktBuffer != nil {
|
||||
sniffer.LogPacket("[gVISOR] ", sniffer.DirectionSend, pktBuffer.NetworkProtocolNumber, pktBuffer)
|
||||
buf := pktBuffer.ToView().AsSlice()
|
||||
_, err := tcpConn.Write(buf)
|
||||
if err != nil {
|
||||
@@ -110,6 +112,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c
|
||||
Payload: buffer.MakeWithData(buf[:read]),
|
||||
})
|
||||
config.SPool.Put(buf[:])
|
||||
sniffer.LogPacket("[gVISOR] ", sniffer.DirectionRecv, protocol, pkt)
|
||||
endpoint.InjectInbound(protocol, pkt)
|
||||
pkt.DecRef()
|
||||
log.Tracef("[TUN-%s] Write to Gvisor IP-Protocol: %s, SRC: %s, DST: %s, Length: %d", layers.IPProtocol(ipProtocol).String(), layers.IPProtocol(ipProtocol).String(), src.String(), dst, read)
|
||||
|
||||
@@ -4,8 +4,11 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
glog "gvisor.dev/gvisor/pkg/log"
|
||||
"k8s.io/utils/ptr"
|
||||
)
|
||||
|
||||
@@ -57,3 +60,35 @@ func (*serverFormat) Format(e *log.Entry) ([]byte, error) {
|
||||
e.Message,
|
||||
)), nil
|
||||
}
|
||||
|
||||
type ServerEmitter struct {
|
||||
*glog.Writer
|
||||
}
|
||||
|
||||
func (g ServerEmitter) Emit(depth int, level glog.Level, timestamp time.Time, format string, args ...any) {
|
||||
// 0 = this frame.
|
||||
_, file, line, ok := runtime.Caller(depth + 2)
|
||||
if ok {
|
||||
// Trim any directory path from the file.
|
||||
slash := strings.LastIndexByte(file, byte('/'))
|
||||
if slash >= 0 {
|
||||
file = file[slash+1:]
|
||||
}
|
||||
} else {
|
||||
// We don't have a filename.
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
|
||||
// Generate the message.
|
||||
message := fmt.Sprintf(format, args...)
|
||||
|
||||
// Emit the formatted result.
|
||||
fmt.Fprintf(g.Writer, "%s %s:%d %s: %s\n",
|
||||
timestamp.Format("2006-01-02 15:04:05"),
|
||||
file,
|
||||
line,
|
||||
level.String(),
|
||||
message,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user