Android: patch

This commit is contained in:
wwqgtxx
2023-10-23 16:50:43 +08:00
parent 79a29826a4
commit a551d7ebd9
30 changed files with 512 additions and 59 deletions

View File

@ -83,5 +83,11 @@ func closeAllLocalCoon(lAddr string) {
}
func handleSocket(ctx C.ConnContext, outbound net.Conn) {
left := unwrap(ctx.Conn())
right := unwrap(outbound)
if relayHijack(left, right) {
return
}
N.Relay(ctx.Conn(), outbound)
}

73
tunnel/patch.go Normal file
View File

@ -0,0 +1,73 @@
package tunnel
import (
"net"
C "github.com/Dreamacro/clash/constant"
)
func relayHijack(left net.Conn, right net.Conn) bool {
var l *net.TCPConn
var r *net.TCPConn
var ok bool
if l, ok = left.(*net.TCPConn); !ok {
return false
}
if r, ok = right.(*net.TCPConn); !ok {
return false
}
closed := make(chan struct{})
go func() {
defer close(closed)
r.ReadFrom(l)
r.Close()
}()
l.ReadFrom(r)
l.Close()
<-closed
return true
}
func unwrap(conn net.Conn) net.Conn {
r := conn
for {
w, ok := r.(C.WrappedConn)
if !ok {
break
}
rc, ok := w.RawConn()
if !ok {
break
}
r = rc
}
return r
}
func unwrapPacket(conn net.PacketConn) net.PacketConn {
r := conn
for {
w, ok := r.(C.WrappedPacketConn)
if !ok {
break
}
rc, ok := w.RawPacketConn()
if !ok {
break
}
r = rc
}
return r
}

25
tunnel/statistic/patch.go Normal file
View File

@ -0,0 +1,25 @@
package statistic
import (
"net"
)
func (m *Manager) Total() (up, down int64) {
return m.uploadTotal.Load(), m.downloadTotal.Load()
}
func (tt *tcpTracker) RawConn() (net.Conn, bool) {
if tt.Chain.Last() == "DIRECT" {
return tt.Conn, true
}
return nil, false
}
func (ut *udpTracker) RawPacketConn() (net.PacketConn, bool) {
if ut.Chain.Last() == "DIRECT" {
return ut.PacketConn, true
}
return nil, false
}

View File

@ -47,6 +47,8 @@ var (
findProcessMode P.FindProcessMode
fakeIPRange netip.Prefix
procesCache string
)
type tunnel struct{}
@ -627,6 +629,10 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
metadata.Process = filepath.Base(path)
metadata.ProcessPath = path
metadata.Uid = uid
if procesCache != metadata.Process {
log.Debugln("[Process] %s from process %s", metadata.String(), path)
}
procesCache = metadata.Process
}
}