chore: better parseAddr
This commit is contained in:
parent
37eca8af24
commit
ba6163574e
8 changed files with 40 additions and 33 deletions
|
@ -16,11 +16,11 @@ func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Ad
|
||||||
for _, addition := range additions {
|
for _, addition := range additions {
|
||||||
addition.Apply(metadata)
|
addition.Apply(metadata)
|
||||||
}
|
}
|
||||||
if ip, port, err := parseAddr(source.String()); err == nil {
|
if ip, port, err := parseAddr(source); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil {
|
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
||||||
metadata.InIP = ip
|
metadata.InIP = ip
|
||||||
metadata.InPort = port
|
metadata.InPort = port
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,11 @@ func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) *cont
|
||||||
for _, addition := range additions {
|
for _, addition := range additions {
|
||||||
addition.Apply(metadata)
|
addition.Apply(metadata)
|
||||||
}
|
}
|
||||||
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil {
|
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
||||||
metadata.InIP = ip
|
metadata.InIP = ip
|
||||||
metadata.InPort = port
|
metadata.InPort = port
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,12 +24,12 @@ func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions
|
||||||
for _, addition := range additions {
|
for _, addition := range additions {
|
||||||
addition.Apply(metadata)
|
addition.Apply(metadata)
|
||||||
}
|
}
|
||||||
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
|
if ip, port, err := parseAddr(packet.LocalAddr()); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
||||||
if ip, port, err := parseAddr(p.InAddr().String()); err == nil {
|
if ip, port, err := parseAddr(p.InAddr()); err == nil {
|
||||||
metadata.InIP = ip
|
metadata.InIP = ip
|
||||||
metadata.InPort = port
|
metadata.InPort = port
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,22 +18,13 @@ func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Ad
|
||||||
addition.Apply(metadata)
|
addition.Apply(metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteAddr := conn.RemoteAddr()
|
if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil {
|
||||||
|
metadata.SrcIP = ip
|
||||||
// Filter when net.Addr interface is nil
|
metadata.SrcPort = port
|
||||||
if remoteAddr != nil {
|
|
||||||
if ip, port, err := parseAddr(remoteAddr.String()); err == nil {
|
|
||||||
metadata.SrcIP = ip
|
|
||||||
metadata.SrcPort = port
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
localAddr := conn.LocalAddr()
|
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
||||||
// Filter when net.Addr interface is nil
|
metadata.InIP = ip
|
||||||
if localAddr != nil {
|
metadata.InPort = port
|
||||||
if ip, port, err := parseAddr(localAddr.String()); err == nil {
|
|
||||||
metadata.InIP = ip
|
|
||||||
metadata.InPort = port
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return context.NewConnContext(conn, metadata)
|
return context.NewConnContext(conn, metadata)
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package inbound
|
package inbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/nnip"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
@ -57,8 +58,19 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAddr(addr string) (netip.Addr, string, error) {
|
func parseAddr(addr net.Addr) (netip.Addr, string, error) {
|
||||||
host, port, err := net.SplitHostPort(addr)
|
// Filter when net.Addr interface is nil
|
||||||
|
if addr == nil {
|
||||||
|
return netip.Addr{}, "", errors.New("nil addr")
|
||||||
|
}
|
||||||
|
if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok {
|
||||||
|
ip, port, err := parseAddr(rawAddr.RawAddr())
|
||||||
|
if err == nil {
|
||||||
|
return ip, port, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addrStr := addr.String()
|
||||||
|
host, port, err := net.SplitHostPort(addrStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return netip.Addr{}, "", err
|
return netip.Addr{}, "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -361,7 +361,6 @@ func (t *clientImpl) ListenPacketWithDialer(ctx context.Context, metadata *C.Met
|
||||||
pc := &quicStreamPacketConn{
|
pc := &quicStreamPacketConn{
|
||||||
connId: connId,
|
connId: connId,
|
||||||
quicConn: quicConn,
|
quicConn: quicConn,
|
||||||
lAddr: quicConn.LocalAddr(),
|
|
||||||
inputConn: N.NewBufferedConn(pipe2),
|
inputConn: N.NewBufferedConn(pipe2),
|
||||||
udpRelayMode: t.UdpRelayMode,
|
udpRelayMode: t.UdpRelayMode,
|
||||||
maxUdpRelayPacketSize: t.MaxUdpRelayPacketSize,
|
maxUdpRelayPacketSize: t.MaxUdpRelayPacketSize,
|
||||||
|
|
|
@ -109,7 +109,6 @@ var _ net.Conn = &quicStreamConn{}
|
||||||
type quicStreamPacketConn struct {
|
type quicStreamPacketConn struct {
|
||||||
connId uint32
|
connId uint32
|
||||||
quicConn quic.Connection
|
quicConn quic.Connection
|
||||||
lAddr net.Addr
|
|
||||||
inputConn *N.BufferedConn
|
inputConn *N.BufferedConn
|
||||||
|
|
||||||
udpRelayMode string
|
udpRelayMode string
|
||||||
|
@ -251,7 +250,7 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *quicStreamPacketConn) LocalAddr() net.Addr {
|
func (q *quicStreamPacketConn) LocalAddr() net.Addr {
|
||||||
return q.lAddr
|
return q.quicConn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ net.PacketConn = &quicStreamPacketConn{}
|
var _ net.PacketConn = &quicStreamPacketConn{}
|
||||||
|
|
|
@ -140,7 +140,6 @@ func (s *serverHandler) parsePacket(packet Packet, udpRelayMode string) (err err
|
||||||
pc := &quicStreamPacketConn{
|
pc := &quicStreamPacketConn{
|
||||||
connId: assocId,
|
connId: assocId,
|
||||||
quicConn: s.quicConn,
|
quicConn: s.quicConn,
|
||||||
lAddr: s.quicConn.LocalAddr(),
|
|
||||||
inputConn: nil,
|
inputConn: nil,
|
||||||
udpRelayMode: udpRelayMode,
|
udpRelayMode: udpRelayMode,
|
||||||
maxUdpRelayPacketSize: s.MaxUdpRelayPacketSize,
|
maxUdpRelayPacketSize: s.MaxUdpRelayPacketSize,
|
||||||
|
@ -152,12 +151,12 @@ func (s *serverHandler) parsePacket(packet Packet, udpRelayMode string) (err err
|
||||||
return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{
|
return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{
|
||||||
pc: pc,
|
pc: pc,
|
||||||
packet: &packet,
|
packet: &packet,
|
||||||
rAddr: s.genServerAssocIdAddr(assocId),
|
rAddr: s.genServerAssocIdAddr(assocId, s.quicConn.RemoteAddr()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serverHandler) genServerAssocIdAddr(assocId uint32) net.Addr {
|
func (s *serverHandler) genServerAssocIdAddr(assocId uint32, addr net.Addr) net.Addr {
|
||||||
return ServerAssocIdAddr(fmt.Sprintf("tuic-%s-%d", s.uuid.String(), assocId))
|
return &ServerAssocIdAddr{assocId: fmt.Sprintf("tuic-%s-%d", s.uuid.String(), assocId), addr: addr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serverHandler) handleStream() (err error) {
|
func (s *serverHandler) handleStream() (err error) {
|
||||||
|
@ -274,14 +273,21 @@ func (s *serverHandler) handleUniStream() (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAssocIdAddr string
|
type ServerAssocIdAddr struct {
|
||||||
|
assocId string
|
||||||
|
addr net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
func (a ServerAssocIdAddr) Network() string {
|
func (a ServerAssocIdAddr) Network() string {
|
||||||
return "ServerAssocIdAddr"
|
return "ServerAssocIdAddr"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ServerAssocIdAddr) String() string {
|
func (a ServerAssocIdAddr) String() string {
|
||||||
return string(a)
|
return a.assocId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a ServerAssocIdAddr) RawAddr() net.Addr {
|
||||||
|
return a.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverUDPPacket struct {
|
type serverUDPPacket struct {
|
||||||
|
|
Loading…
Reference in a new issue