Xray-core/transport/internet/udp/hub_darwin.go
Mocking 4140bcd11a Enhancement of "redirect" function, adding support for MacOS
Added the function of "MacOS" FreeBSD firewall traffic forwarding and resolving destination address
example:
"inbounds": [
{
"listen": "127.0.0.1",
"port": 1122,
"protocol": "dokodemo-door",
"tag": "dokodemo",
"settings": {
"network": "tcp",
"followRedirect": true,
"userLevel": 0
},
"streamSettings": {
"sockopt": {
"tproxy": "Redirect"
}
}
}
]

还原#1189 提交
2022-08-22 10:33:58 -04:00

47 lines
1.1 KiB
Go

//go:build darwin
// +build darwin
package udp
import (
"bytes"
"encoding/gob"
"io"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
)
// RetrieveOriginalDest from stored laddr, caddr
func RetrieveOriginalDest(oob []byte) net.Destination {
dec := gob.NewDecoder(bytes.NewBuffer(oob))
var la, ra net.UDPAddr
dec.Decode(&la)
dec.Decode(&ra)
ip, port, err := internet.OriginalDst(&la, &ra)
if err != nil {
return net.Destination{}
}
return net.UDPDestination(net.IPAddress(ip), net.Port(port))
}
// ReadUDPMsg stores laddr, caddr for later use
func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
nBytes, addr, err := conn.ReadFromUDP(payload)
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
udpAddr, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
return 0, 0, 0, nil, errors.New("invalid local address")
}
if addr == nil {
return 0, 0, 0, nil, errors.New("invalid remote address")
}
enc.Encode(udpAddr)
enc.Encode(addr)
var reader io.Reader = &buf
noob, _ := reader.Read(oob)
return nBytes, noob, 0, addr, err
}