sing-box/outbound/block.go

53 lines
1.4 KiB
Go
Raw Permalink Normal View History

package outbound
import (
"context"
"io"
"net"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-08 23:03:57 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.Outbound = (*Block)(nil)
type Block struct {
myOutboundAdapter
}
2022-07-12 15:17:29 +08:00
func NewBlock(logger log.ContextLogger, tag string) *Block {
return &Block{
myOutboundAdapter{
protocol: C.TypeBlock,
2022-07-30 00:29:22 +08:00
network: []string{N.NetworkTCP, N.NetworkUDP},
logger: logger,
tag: tag,
},
}
}
func (h *Block) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "blocked connection to ", destination)
return nil, io.EOF
}
func (h *Block) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "blocked packet connection to ", destination)
return nil, io.EOF
}
2022-07-07 23:36:32 +08:00
func (h *Block) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
conn.Close()
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "blocked connection to ", metadata.Destination)
return nil
}
2022-07-07 23:36:32 +08:00
func (h *Block) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
conn.Close()
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "blocked packet connection to ", metadata.Destination)
return nil
}