block.go 1.4 KB

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