2022-09-04 22:52:52 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
type UserEndpoint struct {
|
|
|
|
conn.StdNetEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
type packet struct {
|
2022-09-04 22:52:52 +02:00
|
|
|
buff []byte
|
2022-09-05 02:03:16 +02:00
|
|
|
addr *net.UDPAddr
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
type UserBind struct {
|
|
|
|
endpointsLock sync.RWMutex
|
|
|
|
endpoints map[netip.AddrPort]*UserEndpoint
|
|
|
|
sharedConn net.PacketConn
|
|
|
|
|
|
|
|
Packets chan packet
|
2022-09-04 22:52:52 +02:00
|
|
|
closeSignal chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func NewUserBind(sharedConn net.PacketConn) *UserBind {
|
|
|
|
return &UserBind{sharedConn: sharedConn}
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) Open(port uint16) ([]conn.ReceiveFunc, uint16, error) {
|
2022-09-04 22:52:52 +02:00
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
b.Packets = make(chan packet, 1000)
|
|
|
|
b.closeSignal = make(chan struct{})
|
2022-09-04 22:52:52 +02:00
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
return []conn.ReceiveFunc{b.receive}, port, nil
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) receive(buff []byte) (int, conn.Endpoint, error) {
|
2022-09-04 22:52:52 +02:00
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
/*n, endpoint, err := b.sharedConn.ReadFrom(buff)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
2022-09-05 02:03:16 +02:00
|
|
|
e, err := netip.ParseAddrPort(endpoint.String())
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
2022-09-05 02:03:16 +02:00
|
|
|
return n, (*conn.StdNetEndpoint)(&net.UDPAddr{
|
|
|
|
IP: e.addr().AsSlice(),
|
|
|
|
Port: int(e.Port()),
|
|
|
|
Zone: e.addr().Zone(),
|
|
|
|
}), err*/
|
2022-09-04 22:52:52 +02:00
|
|
|
|
|
|
|
select {
|
2022-09-05 02:03:16 +02:00
|
|
|
case <-b.closeSignal:
|
2022-09-04 22:52:52 +02:00
|
|
|
return 0, nil, net.ErrClosed
|
2022-09-05 02:03:16 +02:00
|
|
|
case pkt := <-b.Packets:
|
|
|
|
/*log.Infof("received packet %d from %s to copy to buffer %d", binary.Size(pkt.buff), pkt.addr.String(),
|
|
|
|
len(buff))*/
|
|
|
|
return copy(buff, pkt.buff), (*conn.StdNetEndpoint)(pkt.addr), nil
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
2022-09-05 02:03:16 +02:00
|
|
|
}
|
2022-09-04 22:52:52 +02:00
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) Close() error {
|
|
|
|
if b.closeSignal != nil {
|
2022-09-04 22:52:52 +02:00
|
|
|
select {
|
2022-09-05 02:03:16 +02:00
|
|
|
case <-b.closeSignal:
|
2022-09-04 22:52:52 +02:00
|
|
|
default:
|
2022-09-05 02:03:16 +02:00
|
|
|
close(b.closeSignal)
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMark sets the mark for each packet sent through this Bind.
|
|
|
|
// This mark is passed to the kernel as the socket option SO_MARK.
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) SetMark(mark uint32) error {
|
2022-09-04 22:52:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) Send(buff []byte, endpoint conn.Endpoint) error {
|
2022-09-04 22:52:52 +02:00
|
|
|
nend, ok := endpoint.(*conn.StdNetEndpoint)
|
|
|
|
if !ok {
|
|
|
|
return conn.ErrWrongEndpointType
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
//log.Infof("sending packet %d from %s to %s", binary.Size(buff), b.sharedConn.LocalAddr().String(), (*net.UDPAddr)(nend).String())
|
|
|
|
|
|
|
|
_, err := b.sharedConn.WriteTo(buff, (*net.UDPAddr)(nend))
|
2022-09-04 22:52:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseEndpoint creates a new endpoint from a string.
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) ParseEndpoint(s string) (ep conn.Endpoint, err error) {
|
|
|
|
e, err := netip.ParseAddrPort(s)
|
2022-09-04 22:52:52 +02:00
|
|
|
return (*conn.StdNetEndpoint)(&net.UDPAddr{
|
2022-09-05 02:03:16 +02:00
|
|
|
IP: e.Addr().AsSlice(),
|
|
|
|
Port: int(e.Port()),
|
|
|
|
Zone: e.Addr().Zone(),
|
2022-09-04 22:52:52 +02:00
|
|
|
}), err
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:03:16 +02:00
|
|
|
func (b *UserBind) OnData(buff []byte, addr *net.UDPAddr) {
|
|
|
|
b.Packets <- packet{
|
|
|
|
buff: buff,
|
|
|
|
addr: addr,
|
2022-09-04 22:52:52 +02:00
|
|
|
}
|
|
|
|
}
|