outbound.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package block
  2. import (
  3. "context"
  4. "net"
  5. "syscall"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/adapter/outbound"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/log"
  10. "github.com/sagernet/sing-box/option"
  11. "github.com/sagernet/sing/common/logger"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. )
  15. func RegisterOutbound(registry *outbound.Registry) {
  16. outbound.Register[option.StubOptions](registry, C.TypeBlock, New)
  17. }
  18. type Outbound struct {
  19. outbound.Adapter
  20. logger logger.ContextLogger
  21. }
  22. func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, _ option.StubOptions) (adapter.Outbound, error) {
  23. return &Outbound{
  24. Adapter: outbound.NewAdapter(C.TypeBlock, tag, []string{N.NetworkTCP, N.NetworkUDP}, nil),
  25. logger: logger,
  26. }, nil
  27. }
  28. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  29. h.logger.InfoContext(ctx, "blocked connection to ", destination)
  30. return nil, syscall.EPERM
  31. }
  32. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  33. h.logger.InfoContext(ctx, "blocked packet connection to ", destination)
  34. return nil, syscall.EPERM
  35. }