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

android: don't set vpnService to nil when state is Stopped #523

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion android/src/main/java/com/tailscale/ipn/IPNService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ open class IPNService : VpnService(), libtailscale.IPNService {
override fun close() {
app.setWantRunning(false) { updateVpnStatus(false) }
Notifier.setState(Ipn.State.Stopping)
stopForeground(STOP_FOREGROUND_REMOVE)
disconnectVPN()
Libtailscale.serviceDisconnect(this)
}

override fun disconnectVPN(){
stopSelf()
}

override fun onDestroy() {
close()
super.onDestroy()
Expand Down
62 changes: 36 additions & 26 deletions libtailscale/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -165,36 +166,24 @@ func (a *App) runBackend(ctx context.Context) error {
select {
case s := <-stateCh:
state = s
if cfg.rcfg != nil && state >= ipn.Starting && vpnService.service != nil {
if state >= ipn.Starting && vpnService.service != nil && b.isConfigNonNilAndDifferent(cfg.rcfg, cfg.dcfg) {
// On state change, check if there are router or config changes requiring an update to VPNBuilder
if err := b.updateTUN(vpnService.service, cfg.rcfg, cfg.dcfg); err != nil {
if err := b.updateTUN(cfg.rcfg, cfg.dcfg); err != nil {
if errors.Is(err, errMultipleUsers) {
// TODO: surface error to user
}
log.Printf("VPN update failed: %v", err)

mp := new(ipn.MaskedPrefs)
mp.WantRunning = false
mp.WantRunningSet = true

_, err := a.EditPrefs(*mp)
if err != nil {
log.Printf("localapi edit prefs error %v", err)
}

b.lastCfg = nil
b.CloseTUNs()
a.closeVpnService(err, b)
}
}
case n := <-netmapCh:
networkMap = n
case c := <-configs:
cfg = c
if b == nil || vpnService.service == nil || cfg.rcfg == nil {
if vpnService.service == nil || !b.isConfigNonNilAndDifferent(cfg.rcfg, cfg.dcfg) {
configErrs <- nil
break
}
configErrs <- b.updateTUN(vpnService.service, cfg.rcfg, cfg.dcfg)
configErrs <- b.updateTUN(cfg.rcfg, cfg.dcfg)
case s := <-onVPNRequested:
if vpnService.service != nil && vpnService.service.ID() == s.ID() {
// Still the same VPN instance, do nothing
Expand Down Expand Up @@ -230,12 +219,9 @@ func (a *App) runBackend(ctx context.Context) error {
if networkMap != nil {
// TODO
}
if cfg.rcfg != nil && state >= ipn.Starting {
if err := b.updateTUN(vpnService.service, cfg.rcfg, cfg.dcfg); err != nil {
log.Printf("VPN update failed: %v", err)
vpnService.service.Close()
b.lastCfg = nil
b.CloseTUNs()
if state >= ipn.Starting && b.isConfigNonNilAndDifferent(cfg.rcfg, cfg.dcfg) {
if err := b.updateTUN(cfg.rcfg, cfg.dcfg); err != nil {
a.closeVpnService(err, b)
}
}
case s := <-onDisconnect:
Expand All @@ -245,9 +231,7 @@ func (a *App) runBackend(ctx context.Context) error {
vpnService.service = nil
}
case i := <-onDNSConfigChanged:
if b != nil {
go b.NetworkChanged(i)
}
go b.NetworkChanged(i)
}
}
}
Expand Down Expand Up @@ -346,3 +330,29 @@ func (a *App) newBackend(dataDir, directFileRoot string, appCtx AppContext, stor
}()
return b, nil
}

func (b *backend) isConfigNonNilAndDifferent(rcfg *router.Config, dcfg *dns.OSConfig) bool {
if reflect.DeepEqual(rcfg, b.lastCfg) && reflect.DeepEqual(dcfg, b.lastDNSCfg) {
b.logger.Logf("isConfigNonNilAndDifferent: no change to Routes or DNS, ignore")
return false
}
return rcfg != nil
}

func (a *App) closeVpnService(err error, b *backend) {
log.Printf("VPN update failed: %v", err)

mp := new(ipn.MaskedPrefs)
mp.WantRunning = false
mp.WantRunningSet = true

if _, localApiErr := a.EditPrefs(*mp); localApiErr != nil {
log.Printf("localapi edit prefs error %v", localApiErr)
}

b.lastCfg = nil
b.CloseTUNs()

vpnService.service.DisconnectVPN()
vpnService.service = nil
nickkhyl marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions libtailscale/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type IPNService interface {

Close()

DisconnectVPN()

UpdateVpnStatus(bool)
}

Expand Down
10 changes: 1 addition & 9 deletions libtailscale/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"log"
"net"
"net/netip"
"reflect"
"runtime/debug"
"strings"
"syscall"
Expand Down Expand Up @@ -125,12 +124,7 @@ var googleDNSServers = []netip.Addr{
netip.MustParseAddr("2001:4860:4860::8844"),
}

func (b *backend) updateTUN(service IPNService, rcfg *router.Config, dcfg *dns.OSConfig) error {
if reflect.DeepEqual(rcfg, b.lastCfg) && reflect.DeepEqual(dcfg, b.lastDNSCfg) {
b.logger.Logf("updateTUN: no change to Routes or DNS, ignore")
return nil
}

func (b *backend) updateTUN(rcfg *router.Config, dcfg *dns.OSConfig) error {
b.logger.Logf("updateTUN: changed")
defer b.logger.Logf("updateTUN: finished")

Expand All @@ -147,7 +141,6 @@ func (b *backend) updateTUN(service IPNService, rcfg *router.Config, dcfg *dns.O
if len(rcfg.LocalAddrs) == 0 {
return nil
}
vpnService.service = service
builder := vpnService.service.NewBuilder()
b.logger.Logf("updateTUN: got new builder")

Expand Down Expand Up @@ -258,7 +251,6 @@ func closeFileDescriptor() error {
func (b *backend) CloseTUNs() {
b.lastCfg = nil
b.devices.Shutdown()
vpnService.service = nil
}

// ifname is the interface name retrieved from LinkProperties on network change. If a network is lost, an empty string is passed in.
Expand Down
Loading