Skip to content

Commit 41f8338

Browse files
committed
Merge tag 'v0.4.5' of https://github.com/SagerNet/sing-tun into meta
2 parents d5acdc1 + 960c3ce commit 41f8338

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

tun_windows.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (t *NativeTun) configure() error {
7373
if err != nil {
7474
return E.Cause(err, "set ipv4 address")
7575
}
76-
if !t.options.EXP_DisableDNSHijack {
76+
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
7777
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is4)
7878
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet4Address[0], 1) {
7979
dnsServers = []netip.Addr{t.options.Inet4Address[0].Addr().Next()}
@@ -84,14 +84,19 @@ func (t *NativeTun) configure() error {
8484
return E.Cause(err, "set ipv4 dns")
8585
}
8686
}
87+
} else {
88+
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET), nil, nil)
89+
if err != nil {
90+
return E.Cause(err, "set ipv4 dns")
91+
}
8792
}
8893
}
8994
if len(t.options.Inet6Address) > 0 {
9095
err := luid.SetIPAddressesForFamily(winipcfg.AddressFamily(windows.AF_INET6), t.options.Inet6Address)
9196
if err != nil {
9297
return E.Cause(err, "set ipv6 address")
9398
}
94-
if !t.options.EXP_DisableDNSHijack {
99+
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
95100
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is6)
96101
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet6Address[0], 1) {
97102
dnsServers = []netip.Addr{t.options.Inet6Address[0].Addr().Next()}
@@ -102,6 +107,11 @@ func (t *NativeTun) configure() error {
102107
return E.Cause(err, "set ipv6 dns")
103108
}
104109
}
110+
} else {
111+
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET6), nil, nil)
112+
if err != nil {
113+
return E.Cause(err, "set ipv6 dns")
114+
}
105115
}
106116
}
107117
if len(t.options.Inet4Address) > 0 || len(t.options.Inet6Address) > 0 {
@@ -346,7 +356,40 @@ func (t *NativeTun) configure() error {
346356
}
347357

348358
func (t *NativeTun) Read(p []byte) (n int, err error) {
349-
return 0, os.ErrInvalid
359+
t.running.Add(1)
360+
defer t.running.Done()
361+
retry:
362+
if t.close.Load() == 1 {
363+
return 0, os.ErrClosed
364+
}
365+
start := nanotime()
366+
shouldSpin := t.rate.current.Load() >= spinloopRateThreshold && uint64(start-t.rate.nextStartTime.Load()) <= rateMeasurementGranularity*2
367+
for {
368+
if t.close.Load() == 1 {
369+
return 0, os.ErrClosed
370+
}
371+
var packet []byte
372+
packet, err = t.session.ReceivePacket()
373+
switch err {
374+
case nil:
375+
n = copy(p, packet)
376+
t.session.ReleaseReceivePacket(packet)
377+
t.rate.update(uint64(n))
378+
return
379+
case windows.ERROR_NO_MORE_ITEMS:
380+
if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration {
381+
windows.WaitForSingleObject(t.readWait, windows.INFINITE)
382+
goto retry
383+
}
384+
procyield(1)
385+
continue
386+
case windows.ERROR_HANDLE_EOF:
387+
return 0, os.ErrClosed
388+
case windows.ERROR_INVALID_DATA:
389+
return 0, errors.New("send ring corrupt")
390+
}
391+
return 0, fmt.Errorf("read failed: %w", err)
392+
}
350393
}
351394

352395
func (t *NativeTun) ReadPacket() ([]byte, func(), error) {

0 commit comments

Comments
 (0)