Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决重启clash过程中tun设备配置偶尔失败的问题 #1

Open
wants to merge 1 commit into
base: meta
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func init() {
func open(name string) (int, error) {
fd, err := unix.Open(controlPath, unix.O_RDWR, 0)
if err != nil {
return -1, err
return -1, E.Cause(err, "open:", controlPath)
}

var ifr struct {
Expand All @@ -84,12 +84,12 @@ func open(name string) (int, error) {
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), unix.TUNSETIFF, uintptr(unsafe.Pointer(&ifr)))
if errno != 0 {
unix.Close(fd)
return -1, errno
return -1, E.Cause(errno, "ioctl")
}

if err = unix.SetNonblock(fd, true); err != nil {
unix.Close(fd)
return -1, err
return -1, E.Cause(err, "set nonblock")
}

return fd, nil
Expand All @@ -107,25 +107,25 @@ func (t *NativeTun) configure(tunLink netlink.Link) error {
if len(t.options.Inet4Address) > 0 {
for _, address := range t.options.Inet4Address {
addr4, _ := netlink.ParseAddr(address.String())
err = netlink.AddrAdd(tunLink, addr4)
err = netlink.AddrReplace(tunLink, addr4)
if err != nil {
return err
return E.Cause(err, "add inet4 addr:", address.String())
}
}
}
if len(t.options.Inet6Address) > 0 {
for _, address := range t.options.Inet6Address {
addr6, _ := netlink.ParseAddr(address.String())
err = netlink.AddrAdd(tunLink, addr6)
err = netlink.AddrReplace(tunLink, addr6)
if err != nil {
return err
return E.Cause(err, "add inet6 addr:", address.String())
}
}
}

err = netlink.LinkSetUp(tunLink)
if err != nil {
return err
return E.Cause(err, "link setup")
}

if t.options.TableIndex == 0 {
Expand All @@ -141,7 +141,7 @@ func (t *NativeTun) configure(tunLink netlink.Link) error {
err = t.setRoute(tunLink)
if err != nil {
_ = t.unsetRoute0(tunLink)
return err
return E.Cause(err, "set route")
}

err = t.unsetRules()
Expand All @@ -151,7 +151,7 @@ func (t *NativeTun) configure(tunLink netlink.Link) error {
err = t.setRules()
if err != nil {
_ = t.unsetRules()
return err
return E.Cause(err, "set rules")
}

if t.options.AutoRoute && runtime.GOOS == "android" {
Expand Down