Skip to content

Commit

Permalink
handle fake-ip in tun code
Browse files Browse the repository at this point in the history
  • Loading branch information
comzyh committed Feb 19, 2020
1 parent 6b60f56 commit a96a784
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion proxy/tun/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net"

"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/dns"
"github.com/google/netstack/tcpip"
"github.com/google/netstack/tcpip/buffer"
"github.com/google/netstack/tcpip/header"
Expand All @@ -15,6 +17,7 @@ type fakeConn struct {
id stack.TransportEndpointID
r *stack.Route
payload []byte
fakeip *bool
}

func (c *fakeConn) Data() []byte {
Expand All @@ -25,7 +28,7 @@ func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
v := buffer.View(b)
data := v.ToVectorisedView()
// if addr is not provided, write back use original dst Addr as src Addr
if addr == nil {
if c.FakeIP() || addr == nil {
return writeUDP(c.r, data, uint16(c.id.LocalPort), c.id.RemotePort)
}

Expand All @@ -47,6 +50,16 @@ func (c *fakeConn) Close() error {
return nil
}

func (c *fakeConn) FakeIP() bool {
if c.fakeip != nil {
return *c.fakeip
}
resolver := resolver.DefaultResolver.(*dns.Resolver)
fakeip := resolver.IsFakeIP(net.IP(c.id.LocalAddress.To4()))
c.fakeip = &fakeip
return fakeip
}

func writeUDP(r *stack.Route, data buffer.VectorisedView, localPort, remotePort uint16) (int, error) {
const protocol = udp.ProtocolNumber
// Allocate a buffer for the UDP header.
Expand Down

0 comments on commit a96a784

Please sign in to comment.