2018-09-10 07:23:27 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-15 15:35:32 -04:00
|
|
|
"syscall"
|
2018-09-10 07:23:27 -04:00
|
|
|
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-15 15:35:32 -04:00
|
|
|
effectiveListener = DefaultListener{}
|
2018-09-10 07:23:27 -04:00
|
|
|
)
|
|
|
|
|
2018-12-03 11:41:24 -05:00
|
|
|
type controller func(network, address string, fd uintptr) error
|
2018-09-10 07:23:27 -04:00
|
|
|
|
2018-12-03 11:41:24 -05:00
|
|
|
type DefaultListener struct {
|
|
|
|
contollers []controller
|
|
|
|
}
|
2018-09-10 07:23:27 -04:00
|
|
|
|
2018-12-03 11:41:24 -05:00
|
|
|
func getControlFunc(ctx context.Context, sockopt *SocketConfig, contollers []controller) func(network, address string, c syscall.RawConn) error {
|
|
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
if sockopt != nil {
|
2018-09-15 15:35:32 -04:00
|
|
|
if err := applyInboundSocketOptions(network, fd, sockopt); err != nil {
|
|
|
|
newError("failed to apply socket options to incoming connection").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
2018-12-03 11:41:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, controller := range contollers {
|
|
|
|
if err := controller(network, address, fd); err != nil {
|
|
|
|
newError("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
|
|
|
|
var lc net.ListenConfig
|
|
|
|
|
|
|
|
if sockopt != nil || len(dl.contollers) > 0 {
|
|
|
|
lc.Control = getControlFunc(ctx, sockopt, dl.contollers)
|
2018-09-15 15:35:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return lc.Listen(ctx, addr.Network(), addr.String())
|
|
|
|
}
|
|
|
|
|
2018-12-03 11:41:24 -05:00
|
|
|
func (dl *DefaultListener) ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
|
2018-09-15 15:35:32 -04:00
|
|
|
var lc net.ListenConfig
|
|
|
|
|
2018-12-03 11:41:24 -05:00
|
|
|
if sockopt != nil || len(dl.contollers) > 0 {
|
|
|
|
lc.Control = getControlFunc(ctx, sockopt, dl.contollers)
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:35:32 -04:00
|
|
|
return lc.ListenPacket(ctx, addr.Network(), addr.String())
|
2018-09-10 07:23:27 -04:00
|
|
|
}
|
2018-12-03 11:41:24 -05:00
|
|
|
|
|
|
|
// RegisterListenerController adds a controller to the effective system listener.
|
|
|
|
// The controller can be used to operate on file descriptors before they are put into use.
|
2018-12-03 17:39:21 -05:00
|
|
|
//
|
|
|
|
// v2ray:api:beta
|
2018-12-03 11:41:24 -05:00
|
|
|
func RegisterListenerController(controller func(network, address string, fd uintptr) error) error {
|
|
|
|
if controller == nil {
|
|
|
|
return newError("nil listener controller")
|
|
|
|
}
|
|
|
|
|
|
|
|
effectiveListener.contollers = append(effectiveListener.contollers, controller)
|
|
|
|
return nil
|
|
|
|
}
|