Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor code #371

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 15 additions & 90 deletions pkg/core/tunhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package core

import (
"context"
"fmt"
"math"
"math/rand"
"net"
"sync"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
log "github.com/sirupsen/logrus"

"github.com/wencaiwulue/kubevpn/v2/pkg/config"
Expand Down Expand Up @@ -62,14 +57,6 @@ func (n *RouteMap) RouteTo(ip net.IP) net.Addr {
return n.routes[ip.String()]
}

func (n *RouteMap) Range(f func(key string, value net.Addr)) {
n.lock.RLock()
defer n.lock.RUnlock()
for k, v := range n.routes {
f(k, v)
}
}

// TunHandler creates a handler for tun tunnel.
func TunHandler(chain *Chain, node *Node) Handler {
return &tunHandler{
Expand All @@ -89,19 +76,6 @@ func (h *tunHandler) Handle(ctx context.Context, tun net.Conn) {
}
}

func (h *tunHandler) printRoute(ctx context.Context) {
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
for ctx.Err() == nil {
select {
case <-ticker.C:
h.routeMapUDP.Range(func(key string, value net.Addr) {
log.Debugf("To: %s, route: %s", key, value.String())
})
}
}
}

type Device struct {
tun net.Conn

Expand Down Expand Up @@ -163,12 +137,12 @@ func (d *Device) Close() {
}

func heartbeats(ctx context.Context, tun net.Conn) {
conn, err := util.GetTunDeviceByConn(tun)
tunIfi, err := util.GetTunDeviceByConn(tun)
if err != nil {
log.Errorf("Failed to get tun device: %s", err.Error())
return
}
srcIPv4, srcIPv6, err := util.GetLocalTunIP(conn.Name)
srcIPv4, srcIPv6, err := util.GetTunDeviceIP(tunIfi.Name)
if err != nil {
return
}
Expand All @@ -178,10 +152,17 @@ func heartbeats(ctx context.Context, tun net.Conn) {
if config.RouterIP6.To4().Equal(srcIPv6) {
return
}
if config.DockerRouterIP.To4().Equal(srcIPv4) {
return
}
var dstIPv4, dstIPv6 = net.IPv4zero, net.IPv6zero
if config.CIDR.Contains(srcIPv4) {
dstIPv4, dstIPv6 = config.RouterIP, config.RouterIP6
} else if config.DockerCIDR.Contains(srcIPv4) {
dstIPv4 = config.RouterIP
}
if config.CIDR6.Contains(srcIPv6) {
dstIPv6 = config.RouterIP6
}
if config.DockerCIDR.Contains(srcIPv4) {
dstIPv4 = config.DockerRouterIP
}

Expand All @@ -198,69 +179,15 @@ func heartbeats(ctx context.Context, tun net.Conn) {
var src, dst net.IP
src, dst = srcIPv4, dstIPv4
if !dst.IsUnspecified() {
_, _ = util.Ping(ctx, src.String(), dst.String())
go util.Ping(ctx, src.String(), dst.String())
}
src, dst = srcIPv6, dstIPv6
if !dst.IsUnspecified() {
_, _ = util.Ping(ctx, src.String(), dst.String())
go util.Ping(ctx, src.String(), dst.String())
}
}
}

func genICMPPacket(src net.IP, dst net.IP) ([]byte, error) {
buf := gopacket.NewSerializeBuffer()
var id uint16
for _, b := range src {
id += uint16(b)
}
icmpLayer := layers.ICMPv4{
TypeCode: layers.CreateICMPv4TypeCode(layers.ICMPv4TypeEchoRequest, 0),
Id: id,
Seq: uint16(rand.Intn(math.MaxUint16 + 1)),
}
ipLayer := layers.IPv4{
Version: 4,
SrcIP: src,
DstIP: dst,
Protocol: layers.IPProtocolICMPv4,
Flags: layers.IPv4DontFragment,
TTL: 64,
IHL: 5,
Id: uint16(rand.Intn(math.MaxUint16 + 1)),
}
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
err := gopacket.SerializeLayers(buf, opts, &ipLayer, &icmpLayer)
if err != nil {
return nil, fmt.Errorf("failed to serialize icmp packet, err: %v", err)
}
return buf.Bytes(), nil
}

func genICMPPacketIPv6(src net.IP, dst net.IP) ([]byte, error) {
buf := gopacket.NewSerializeBuffer()
icmpLayer := layers.ICMPv6{
TypeCode: layers.CreateICMPv6TypeCode(layers.ICMPv6TypeEchoRequest, 0),
}
ipLayer := layers.IPv6{
Version: 6,
SrcIP: src,
DstIP: dst,
NextHeader: layers.IPProtocolICMPv6,
HopLimit: 255,
}
opts := gopacket.SerializeOptions{
FixLengths: true,
}
err := gopacket.SerializeLayers(buf, opts, &ipLayer, &icmpLayer)
if err != nil {
return nil, fmt.Errorf("failed to serialize icmp6 packet, err: %v", err)
}
return buf.Bytes(), nil
}

func (d *Device) Start(ctx context.Context) {
go d.readFromTun()
go d.tunInboundHandler(d.tunInbound, d.tunOutbound)
Expand All @@ -281,8 +208,6 @@ func (d *Device) SetTunInboundHandler(handler func(tunInbound <-chan *DataElem,
}

func (h *tunHandler) HandleServer(ctx context.Context, tun net.Conn) {
go h.printRoute(ctx)

device := &Device{
tun: tun,
tunInbound: make(chan *DataElem, MaxSize),
Expand All @@ -296,7 +221,7 @@ func (h *tunHandler) HandleServer(ctx context.Context, tun net.Conn) {
log.Errorf("[UDP] Failed to listen %s: %v", h.node.Addr, err)
return
}
err = transportTun(ctx, tunInbound, tunOutbound, packetConn, h.routeMapUDP, h.routeMapTCP)
err = transportTunServer(ctx, tunInbound, tunOutbound, packetConn, h.routeMapUDP, h.routeMapTCP)
if err != nil {
log.Errorf("[TUN] %s: %v", tun.LocalAddr(), err)
}
Expand Down Expand Up @@ -480,7 +405,7 @@ func (p *Peer) Close() {
p.conn.Close()
}

func transportTun(ctx context.Context, tunInbound <-chan *DataElem, tunOutbound chan<- *DataElem, packetConn net.PacketConn, routeMapUDP *RouteMap, routeMapTCP *sync.Map) error {
func transportTunServer(ctx context.Context, tunInbound <-chan *DataElem, tunOutbound chan<- *DataElem, packetConn net.PacketConn, routeMapUDP *RouteMap, routeMapTCP *sync.Map) error {
p := &Peer{
conn: packetConn,
connInbound: make(chan *udpElem, MaxSize),
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/tunhandlerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut
_, err := packetConn.WriteTo(e.data[:e.length], remoteAddr)
config.LPool.Put(e.data[:])
if err != nil {
errChan <- errors.Wrap(err, fmt.Sprintf("failed to write packet to remote %s", remoteAddr))
util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to write packet to remote %s", remoteAddr)))
return
}
}
Expand All @@ -100,7 +100,7 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut
b := config.LPool.Get().([]byte)[:]
n, _, err := packetConn.ReadFrom(b[:])
if err != nil {
errChan <- errors.Wrap(err, fmt.Sprintf("failed to read packet from remote %s", remoteAddr))
util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to read packet from remote %s", remoteAddr)))
return
}
util.SafeWrite(tunOutbound, &DataElem{data: b[:], length: n})
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/handler/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (w *wsHandler) handle(c context.Context) {
log.Info("Connected tunnel")
go func() {
for ctx.Err() == nil {
_, _ = util.Ping(ctx, clientIP.IP.String(), ip.String())
util.Ping(ctx, clientIP.IP.String(), ip.String())
time.Sleep(time.Second * 5)
}
}()
Expand Down
Loading
Loading