mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 16:30:07 +08:00
Some checks failed
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Failing after 1s
Test / test (1.21, ubuntu-latest) (push) Failing after 1s
Test / test (1.22, ubuntu-latest) (push) Failing after 1s
Test / test (1.23, ubuntu-latest) (push) Failing after 1s
Test / test (1.24, ubuntu-latest) (push) Failing after 1s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 1s
The DNS resolution of the overall UDP part has been delayed to the connection initiation stage. During the rule matching process, it will only be triggered when the IP rule without no-resolve is matched. For direct and wireguard outbound, the same logic as the TCP part will be followed, that is, when direct-nameserver (or DNS configured by wireguard) exists, the result of the matching process will be discarded and the domain name will be re-resolved. This re-resolution logic is only effective for fakeip. For reject and DNS outbound, no resolution is required. For other outbound, resolution will still be performed when the connection is initiated, and the domain name will not be sent directly to the remote server at present.
139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"github.com/metacubex/mihomo/common/buf"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
)
|
|
|
|
type Reject struct {
|
|
*Base
|
|
drop bool
|
|
}
|
|
|
|
type RejectOption struct {
|
|
Name string `proxy:"name"`
|
|
}
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
if r.drop {
|
|
return NewConn(dropConn{}, r), nil
|
|
}
|
|
return NewConn(nopConn{}, r), nil
|
|
}
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
|
if err := r.ResolveUDP(ctx, metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
return newPacketConn(&nopPacketConn{}, r), nil
|
|
}
|
|
|
|
func (r *Reject) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
|
if !metadata.Resolved() {
|
|
metadata.DstIP = netip.IPv4Unspecified()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewRejectWithOption(option RejectOption) *Reject {
|
|
return &Reject{
|
|
Base: &Base{
|
|
name: option.Name,
|
|
tp: C.Reject,
|
|
udp: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewReject() *Reject {
|
|
return &Reject{
|
|
Base: &Base{
|
|
name: "REJECT",
|
|
tp: C.Reject,
|
|
udp: true,
|
|
prefer: C.DualStack,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewRejectDrop() *Reject {
|
|
return &Reject{
|
|
Base: &Base{
|
|
name: "REJECT-DROP",
|
|
tp: C.RejectDrop,
|
|
udp: true,
|
|
prefer: C.DualStack,
|
|
},
|
|
drop: true,
|
|
}
|
|
}
|
|
|
|
func NewPass() *Reject {
|
|
return &Reject{
|
|
Base: &Base{
|
|
name: "PASS",
|
|
tp: C.Pass,
|
|
udp: true,
|
|
prefer: C.DualStack,
|
|
},
|
|
}
|
|
}
|
|
|
|
type nopConn struct{}
|
|
|
|
func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
|
|
|
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
|
|
func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
func (rw nopConn) Close() error { return nil }
|
|
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
|
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
|
func (rw nopConn) SetDeadline(time.Time) error { return nil }
|
|
func (rw nopConn) SetReadDeadline(time.Time) error { return nil }
|
|
func (rw nopConn) SetWriteDeadline(time.Time) error { return nil }
|
|
|
|
var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
|
|
|
type nopPacketConn struct{}
|
|
|
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
|
return len(b), nil
|
|
}
|
|
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
return 0, nil, io.EOF
|
|
}
|
|
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
|
return nil, nil, nil, io.EOF
|
|
}
|
|
func (npc nopPacketConn) Close() error { return nil }
|
|
func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
|
func (npc nopPacketConn) SetDeadline(time.Time) error { return nil }
|
|
func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil }
|
|
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
|
|
|
type dropConn struct{}
|
|
|
|
func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
|
func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
time.Sleep(C.DefaultDropTime)
|
|
return io.EOF
|
|
}
|
|
func (rw dropConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
func (rw dropConn) Close() error { return nil }
|
|
func (rw dropConn) LocalAddr() net.Addr { return nil }
|
|
func (rw dropConn) RemoteAddr() net.Addr { return nil }
|
|
func (rw dropConn) SetDeadline(time.Time) error { return nil }
|
|
func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
|
|
func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }
|