mirror of
https://github.com/MetaCubeX/ClashMetaForAndroid.git
synced 2025-02-21 09:13:16 +03:00
Fix: should close all endpoint on exit
This commit is contained in:
parent
42f5331a0c
commit
634c25894c
@ -9,13 +9,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
stack tun2socket.Stack
|
|
||||||
device *os.File
|
device *os.File
|
||||||
|
stack tun2socket.Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
var tun *context
|
var tun *context
|
||||||
|
|
||||||
|
func (ctx *context) close() {
|
||||||
|
_ = ctx.stack.Close()
|
||||||
|
_ = ctx.device.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func Start(fd, mtu int, dns string) error {
|
func Start(fd, mtu int, dns string) error {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
@ -33,11 +38,15 @@ func Start(fd, mtu int, dns string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := &context{
|
||||||
|
device: device,
|
||||||
|
stack: stack,
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// device -> lwip
|
// device -> lwip
|
||||||
|
|
||||||
defer device.Close()
|
defer ctx.close()
|
||||||
defer stack.Close()
|
|
||||||
|
|
||||||
buf := make([]byte, mtu)
|
buf := make([]byte, mtu)
|
||||||
|
|
||||||
@ -54,8 +63,7 @@ func Start(fd, mtu int, dns string) error {
|
|||||||
go func() {
|
go func() {
|
||||||
// lwip -> device
|
// lwip -> device
|
||||||
|
|
||||||
defer device.Close()
|
defer ctx.close()
|
||||||
defer stack.Close()
|
|
||||||
|
|
||||||
buf := make([]byte, mtu)
|
buf := make([]byte, mtu)
|
||||||
|
|
||||||
@ -72,7 +80,7 @@ func Start(fd, mtu int, dns string) error {
|
|||||||
go func() {
|
go func() {
|
||||||
// lwip tcp
|
// lwip tcp
|
||||||
|
|
||||||
defer stack.TCP().Close()
|
defer ctx.close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := stack.TCP().Accept()
|
conn, err := stack.TCP().Accept()
|
||||||
@ -96,7 +104,7 @@ func Start(fd, mtu int, dns string) error {
|
|||||||
go func() {
|
go func() {
|
||||||
// lwip udp
|
// lwip udp
|
||||||
|
|
||||||
defer stack.UDP().Close()
|
defer ctx.close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
buf := allocUDP(mtu)
|
buf := allocUDP(mtu)
|
||||||
@ -123,10 +131,7 @@ func Start(fd, mtu int, dns string) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tun = &context{
|
tun = ctx
|
||||||
stack: stack,
|
|
||||||
device: device,
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -140,8 +145,7 @@ func Stop() {
|
|||||||
|
|
||||||
func stopLocked() {
|
func stopLocked() {
|
||||||
if tun != nil {
|
if tun != nil {
|
||||||
tun.device.Close()
|
tun.close()
|
||||||
tun.stack.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tun = nil
|
tun = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user