dae/component/control/tproxy_utils.go

61 lines
1.7 KiB
Go

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) since 2023, mzz2017 <mzz@tuta.io>
*/
package control
import (
"encoding/binary"
"fmt"
"github.com/v2rayA/dae/common"
"github.com/v2rayA/dae/common/consts"
internal "github.com/v2rayA/dae/pkg/ebpf_internal"
"golang.org/x/sys/unix"
"net/netip"
"syscall"
)
func (c *ControlPlaneCore) RetrieveOutboundIndex(src, dst netip.AddrPort, l4proto uint8) (consts.OutboundIndex, error) {
srcIp6 := src.Addr().As16()
dstIp6 := dst.Addr().As16()
var outboundIndex uint32
if err := c.bpf.RoutingTuplesMap.Lookup(bpfTuples{
Src: bpfIpPort{
Ip: common.Ipv6ByteSliceToUint32Array(srcIp6[:]),
Port: internal.Htons(src.Port()),
},
Dst: bpfIpPort{
Ip: common.Ipv6ByteSliceToUint32Array(dstIp6[:]),
Port: internal.Htons(dst.Port()),
},
L4proto: l4proto,
}, &outboundIndex); err != nil {
return 0, fmt.Errorf("reading map: key %v: %w", src.String(), err)
}
if outboundIndex > uint32(consts.OutboundLogicalMax) {
return 0, fmt.Errorf("bad outbound index")
}
return consts.OutboundIndex(outboundIndex), nil
}
func RetrieveOriginalDest(oob []byte) netip.AddrPort {
msgs, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return netip.AddrPort{}
}
for _, msg := range msgs {
if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
ip := msg.Data[4:8]
port := binary.BigEndian.Uint16(msg.Data[2:4])
return netip.AddrPortFrom(netip.AddrFrom4(*(*[4]byte)(ip)), port)
} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == unix.IPV6_RECVORIGDSTADDR {
ip := msg.Data[8:24]
port := binary.BigEndian.Uint16(msg.Data[2:4])
return netip.AddrPortFrom(netip.AddrFrom4(*(*[4]byte)(ip)), port)
}
}
return netip.AddrPort{}
}