[Style] Adjust delete routes on macos

This commit is contained in:
Skyxim 2022-01-08 16:55:02 +08:00
parent 4f1b227ca2
commit 7f0368da66

View file

@ -282,7 +282,7 @@ func (t *tunDarwin) IsClose() bool {
func (t *tunDarwin) Close() error { func (t *tunDarwin) Close() error {
t.stopOnce.Do(func() { t.stopOnce.Do(func() {
if t.autoRoute { if t.autoRoute {
RemoveLinuxAutoRoute() resetAutoRoute(t.tunAddress)
} }
t.closed = true t.closed = true
t.tunFile.Close() t.tunFile.Close()
@ -510,6 +510,18 @@ func setAutoRoute(tunGateway string) {
addRoute("198.18.0/16", tunGateway) addRoute("198.18.0/16", tunGateway)
} }
func resetAutoRoute(tunGateway string) {
delRoute("1", tunGateway)
delRoute("2/7", tunGateway)
delRoute("4/6", tunGateway)
delRoute("8/5", tunGateway)
delRoute("16/4", tunGateway)
delRoute("32/3", tunGateway)
delRoute("64/2", tunGateway)
delRoute("128.0/1", tunGateway)
delRoute("198.18.0/16", tunGateway)
}
func addRoute(net, name string) { func addRoute(net, name string) {
cmd := exec.Command("route", "add", "-net", net, name) cmd := exec.Command("route", "add", "-net", net, name)
var stderr bytes.Buffer var stderr bytes.Buffer
@ -518,3 +530,12 @@ func addRoute(net, name string) {
log.Errorln("[auto route] Failed to add system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String()) log.Errorln("[auto route] Failed to add system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String())
} }
} }
func delRoute(net, name string) {
cmd := exec.Command("route", "delete", "-net", net, name)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Errorln("[auto route] Failed to delete system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String())
}
}