2022-07-03 23:23:18 +08:00
|
|
|
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"
|
2022-07-03 23:23:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-07-03 23:23:18 +08:00
|
|
|
return &Block{
|
|
|
|
myOutboundAdapter{
|
|
|
|
protocol: C.TypeBlock,
|
2022-07-30 00:29:22 +08:00
|
|
|
network: []string{N.NetworkTCP, N.NetworkUDP},
|
2022-07-03 23:23:18 +08:00
|
|
|
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)
|
2022-07-03 23:23:18 +08:00
|
|
|
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)
|
2022-07-03 23:23:18 +08:00
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
// Deprecated
|
2022-07-07 23:36:32 +08:00
|
|
|
func (h *Block) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
2022-07-03 23:23:18 +08:00
|
|
|
conn.Close()
|
2022-07-12 15:17:29 +08:00
|
|
|
h.logger.InfoContext(ctx, "blocked connection to ", metadata.Destination)
|
2022-07-03 23:23:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
// Deprecated
|
2022-07-07 23:36:32 +08:00
|
|
|
func (h *Block) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
2022-07-03 23:23:18 +08:00
|
|
|
conn.Close()
|
2022-07-12 15:17:29 +08:00
|
|
|
h.logger.InfoContext(ctx, "blocked packet connection to ", metadata.Destination)
|
2022-07-03 23:23:18 +08:00
|
|
|
return nil
|
|
|
|
}
|