2018-09-10 11:23:27 +00:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-15 19:35:32 +00:00
|
|
|
"syscall"
|
2018-09-10 11:23:27 +00:00
|
|
|
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-15 19:35:32 +00:00
|
|
|
effectiveListener = DefaultListener{}
|
2018-09-10 11:23:27 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 19:35:32 +00:00
|
|
|
type DefaultListener struct{}
|
2018-09-10 11:23:27 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
func (*DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
|
2018-09-15 19:35:32 +00:00
|
|
|
var lc net.ListenConfig
|
2018-09-10 11:23:27 +00:00
|
|
|
|
2018-09-15 19:35:32 +00:00
|
|
|
if sockopt != nil {
|
|
|
|
lc.Control = func(network, address string, c syscall.RawConn) error {
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
|
|
|
|
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
})
|
2018-09-10 11:23:27 +00:00
|
|
|
}
|
2018-09-15 19:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lc.Listen(ctx, addr.Network(), addr.String())
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
func (*DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
|
2018-09-15 19:35:32 +00:00
|
|
|
var lc net.ListenConfig
|
|
|
|
|
|
|
|
if sockopt != nil {
|
|
|
|
lc.Control = func(network, address string, c syscall.RawConn) error {
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
|
|
|
|
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
})
|
2018-09-10 11:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:35:32 +00:00
|
|
|
return lc.ListenPacket(ctx, addr.Network(), addr.String())
|
2018-09-10 11:23:27 +00:00
|
|
|
}
|