github.com/chwjbn/xclash@v0.2.0/adapter/outbound/reject.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/chwjbn/xclash/component/dialer"
    10  	C "github.com/chwjbn/xclash/constant"
    11  )
    12  
    13  type Reject struct {
    14  	*Base
    15  }
    16  
    17  // DialContext implements C.ProxyAdapter
    18  func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
    19  	return NewConn(&nopConn{}, r), nil
    20  }
    21  
    22  // ListenPacketContext implements C.ProxyAdapter
    23  func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
    24  	return newPacketConn(&nopPacketConn{}, r), nil
    25  }
    26  
    27  func NewReject() *Reject {
    28  	return &Reject{
    29  		Base: &Base{
    30  			name: "REJECT",
    31  			tp:   C.Reject,
    32  			udp:  true,
    33  		},
    34  	}
    35  }
    36  
    37  type nopConn struct{}
    38  
    39  func (rw *nopConn) Read(b []byte) (int, error) {
    40  	return 0, io.EOF
    41  }
    42  
    43  func (rw *nopConn) Write(b []byte) (int, error) {
    44  	return 0, io.EOF
    45  }
    46  
    47  func (rw *nopConn) Close() error                     { return nil }
    48  func (rw *nopConn) LocalAddr() net.Addr              { return nil }
    49  func (rw *nopConn) RemoteAddr() net.Addr             { return nil }
    50  func (rw *nopConn) SetDeadline(time.Time) error      { return nil }
    51  func (rw *nopConn) SetReadDeadline(time.Time) error  { return nil }
    52  func (rw *nopConn) SetWriteDeadline(time.Time) error { return nil }
    53  
    54  type nopPacketConn struct{}
    55  
    56  func (npc *nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { return len(b), nil }
    57  func (npc *nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error)           { return 0, nil, io.EOF }
    58  func (npc *nopPacketConn) Close() error                                       { return nil }
    59  func (npc *nopPacketConn) LocalAddr() net.Addr                                { return &net.UDPAddr{IP: net.IPv4zero, Port: 0} }
    60  func (npc *nopPacketConn) SetDeadline(time.Time) error                        { return nil }
    61  func (npc *nopPacketConn) SetReadDeadline(time.Time) error                    { return nil }
    62  func (npc *nopPacketConn) SetWriteDeadline(time.Time) error                   { return nil }