[Fixed] Remove the Linux automatic routing configuration Change the name of the Linux network card to utun
This commit is contained in:
parent
28d8bf17cb
commit
1c1eb6bdfb
2 changed files with 4 additions and 65 deletions
|
@ -1,13 +1,5 @@
|
||||||
package dev
|
package dev
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TunDevice is cross-platform tun interface
|
// TunDevice is cross-platform tun interface
|
||||||
type TunDevice interface {
|
type TunDevice interface {
|
||||||
Name() string
|
Name() string
|
||||||
|
@ -18,54 +10,3 @@ type TunDevice interface {
|
||||||
Read(buff []byte) (int, error)
|
Read(buff []byte) (int, error)
|
||||||
Write(buff []byte) (int, error)
|
Write(buff []byte) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLinuxAutoRoute() {
|
|
||||||
log.Infoln("Tun adapter auto setting global route")
|
|
||||||
addLinuxSystemRoute("0")
|
|
||||||
//addLinuxSystemRoute("1")
|
|
||||||
//addLinuxSystemRoute("2/7")
|
|
||||||
//addLinuxSystemRoute("4/6")
|
|
||||||
//addLinuxSystemRoute("8/5")
|
|
||||||
//addLinuxSystemRoute("16/4")
|
|
||||||
//addLinuxSystemRoute("32/3")
|
|
||||||
//addLinuxSystemRoute("64/2")
|
|
||||||
//addLinuxSystemRoute("128.0/1")
|
|
||||||
//addLinuxSystemRoute("198.18.0/16")
|
|
||||||
}
|
|
||||||
|
|
||||||
func RemoveLinuxAutoRoute() {
|
|
||||||
log.Infoln("Tun adapter removing global route")
|
|
||||||
delLinuxSystemRoute("0")
|
|
||||||
//delLinuxSystemRoute("1")
|
|
||||||
//delLinuxSystemRoute("2/7")
|
|
||||||
//delLinuxSystemRoute("4/6")
|
|
||||||
//delLinuxSystemRoute("8/5")
|
|
||||||
//delLinuxSystemRoute("16/4")
|
|
||||||
//delLinuxSystemRoute("32/3")
|
|
||||||
//delLinuxSystemRoute("64/2")
|
|
||||||
//delLinuxSystemRoute("128.0/1")
|
|
||||||
//delLinuxSystemRoute("198.18.0/16")
|
|
||||||
}
|
|
||||||
|
|
||||||
func addLinuxSystemRoute(net string) {
|
|
||||||
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cmd := exec.Command("route", "add", "-net", net, "meta")
|
|
||||||
var stderr bytes.Buffer
|
|
||||||
cmd.Stderr = &stderr
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
log.Errorln("[auto route] Failed to add system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func delLinuxSystemRoute(net string) {
|
|
||||||
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cmd := exec.Command("route", "delete", "-net", net, "meta")
|
|
||||||
_ = cmd.Run()
|
|
||||||
//if err := cmd.Run(); err != nil {
|
|
||||||
// log.Errorln("[auto route]Failed to delete system route: %s, cmd: %s", err.Error(), cmd.String())
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -38,7 +39,7 @@ type tunLinux struct {
|
||||||
|
|
||||||
// OpenTunDevice return a TunDevice according a URL
|
// OpenTunDevice return a TunDevice according a URL
|
||||||
func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
|
func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
|
||||||
deviceURL, _ := url.Parse("dev://meta")
|
deviceURL, _ := url.Parse("dev://utun")
|
||||||
mtu, _ := strconv.ParseInt(deviceURL.Query().Get("mtu"), 0, 32)
|
mtu, _ := strconv.ParseInt(deviceURL.Query().Get("mtu"), 0, 32)
|
||||||
|
|
||||||
t := &tunLinux{
|
t := &tunLinux{
|
||||||
|
@ -62,7 +63,7 @@ func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
SetLinuxAutoRoute()
|
log.Warnln("linux unsupported automatic route")
|
||||||
}
|
}
|
||||||
return dev, nil
|
return dev, nil
|
||||||
case "fd":
|
case "fd":
|
||||||
|
@ -76,7 +77,7 @@ func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
SetLinuxAutoRoute()
|
log.Warnln("linux unsupported automatic route")
|
||||||
}
|
}
|
||||||
return dev, nil
|
return dev, nil
|
||||||
}
|
}
|
||||||
|
@ -105,9 +106,6 @@ func (t *tunLinux) IsClose() bool {
|
||||||
|
|
||||||
func (t *tunLinux) Close() error {
|
func (t *tunLinux) Close() error {
|
||||||
t.stopOnce.Do(func() {
|
t.stopOnce.Do(func() {
|
||||||
if t.autoRoute {
|
|
||||||
RemoveLinuxAutoRoute()
|
|
||||||
}
|
|
||||||
t.closed = true
|
t.closed = true
|
||||||
t.tunFile.Close()
|
t.tunFile.Close()
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue