github.com/sagernet/sing-box@v1.9.0-rc.20/outbound/block.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  
     8  	"github.com/sagernet/sing-box/adapter"
     9  	C "github.com/sagernet/sing-box/constant"
    10  	"github.com/sagernet/sing-box/log"
    11  	M "github.com/sagernet/sing/common/metadata"
    12  	N "github.com/sagernet/sing/common/network"
    13  )
    14  
    15  var _ adapter.Outbound = (*Block)(nil)
    16  
    17  type Block struct {
    18  	myOutboundAdapter
    19  }
    20  
    21  func NewBlock(logger log.ContextLogger, tag string) *Block {
    22  	return &Block{
    23  		myOutboundAdapter{
    24  			protocol: C.TypeBlock,
    25  			network:  []string{N.NetworkTCP, N.NetworkUDP},
    26  			logger:   logger,
    27  			tag:      tag,
    28  		},
    29  	}
    30  }
    31  
    32  func (h *Block) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
    33  	h.logger.InfoContext(ctx, "blocked connection to ", destination)
    34  	return nil, io.EOF
    35  }
    36  
    37  func (h *Block) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
    38  	h.logger.InfoContext(ctx, "blocked packet connection to ", destination)
    39  	return nil, io.EOF
    40  }
    41  
    42  func (h *Block) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    43  	conn.Close()
    44  	h.logger.InfoContext(ctx, "blocked connection to ", metadata.Destination)
    45  	return nil
    46  }
    47  
    48  func (h *Block) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
    49  	conn.Close()
    50  	h.logger.InfoContext(ctx, "blocked packet connection to ", metadata.Destination)
    51  	return nil
    52  }