github.com/metacubex/mihomo@v1.18.5/adapter/outbound/reject.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/metacubex/mihomo/common/buf"
    10  	"github.com/metacubex/mihomo/component/dialer"
    11  	C "github.com/metacubex/mihomo/constant"
    12  )
    13  
    14  type Reject struct {
    15  	*Base
    16  	drop bool
    17  }
    18  
    19  type RejectOption struct {
    20  	Name string `proxy:"name"`
    21  }
    22  
    23  // DialContext implements C.ProxyAdapter
    24  func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
    25  	if r.drop {
    26  		return NewConn(dropConn{}, r), nil
    27  	}
    28  	return NewConn(nopConn{}, r), nil
    29  }
    30  
    31  // ListenPacketContext implements C.ProxyAdapter
    32  func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
    33  	return newPacketConn(&nopPacketConn{}, r), nil
    34  }
    35  
    36  func NewRejectWithOption(option RejectOption) *Reject {
    37  	return &Reject{
    38  		Base: &Base{
    39  			name: option.Name,
    40  			tp:   C.Direct,
    41  			udp:  true,
    42  		},
    43  	}
    44  }
    45  
    46  func NewReject() *Reject {
    47  	return &Reject{
    48  		Base: &Base{
    49  			name:   "REJECT",
    50  			tp:     C.Reject,
    51  			udp:    true,
    52  			prefer: C.DualStack,
    53  		},
    54  	}
    55  }
    56  
    57  func NewRejectDrop() *Reject {
    58  	return &Reject{
    59  		Base: &Base{
    60  			name:   "REJECT-DROP",
    61  			tp:     C.RejectDrop,
    62  			udp:    true,
    63  			prefer: C.DualStack,
    64  		},
    65  		drop: true,
    66  	}
    67  }
    68  
    69  func NewPass() *Reject {
    70  	return &Reject{
    71  		Base: &Base{
    72  			name:   "PASS",
    73  			tp:     C.Pass,
    74  			udp:    true,
    75  			prefer: C.DualStack,
    76  		},
    77  	}
    78  }
    79  
    80  type nopConn struct{}
    81  
    82  func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
    83  
    84  func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
    85  
    86  func (rw nopConn) Write(b []byte) (int, error)          { return 0, io.EOF }
    87  func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
    88  func (rw nopConn) Close() error                         { return nil }
    89  func (rw nopConn) LocalAddr() net.Addr                  { return nil }
    90  func (rw nopConn) RemoteAddr() net.Addr                 { return nil }
    91  func (rw nopConn) SetDeadline(time.Time) error          { return nil }
    92  func (rw nopConn) SetReadDeadline(time.Time) error      { return nil }
    93  func (rw nopConn) SetWriteDeadline(time.Time) error     { return nil }
    94  
    95  var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
    96  
    97  type nopPacketConn struct{}
    98  
    99  func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
   100  	return len(b), nil
   101  }
   102  func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
   103  	return 0, nil, io.EOF
   104  }
   105  func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
   106  	return nil, nil, nil, io.EOF
   107  }
   108  func (npc nopPacketConn) Close() error                     { return nil }
   109  func (npc nopPacketConn) LocalAddr() net.Addr              { return udpAddrIPv4Unspecified }
   110  func (npc nopPacketConn) SetDeadline(time.Time) error      { return nil }
   111  func (npc nopPacketConn) SetReadDeadline(time.Time) error  { return nil }
   112  func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
   113  
   114  type dropConn struct{}
   115  
   116  func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF }
   117  func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error {
   118  	time.Sleep(C.DefaultDropTime)
   119  	return io.EOF
   120  }
   121  func (rw dropConn) Write(b []byte) (int, error)          { return 0, io.EOF }
   122  func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
   123  func (rw dropConn) Close() error                         { return nil }
   124  func (rw dropConn) LocalAddr() net.Addr                  { return nil }
   125  func (rw dropConn) RemoteAddr() net.Addr                 { return nil }
   126  func (rw dropConn) SetDeadline(time.Time) error          { return nil }
   127  func (rw dropConn) SetReadDeadline(time.Time) error      { return nil }
   128  func (rw dropConn) SetWriteDeadline(time.Time) error     { return nil }