mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-11-01 19:22:45 +08:00
hotfix: cleanup resource if daemon rocess be killed
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package cmds
|
package cmds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -17,6 +18,7 @@ import (
|
|||||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon"
|
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon"
|
||||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action"
|
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action"
|
||||||
|
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
|
||||||
"github.com/wencaiwulue/kubevpn/v2/pkg/dns"
|
"github.com/wencaiwulue/kubevpn/v2/pkg/dns"
|
||||||
"github.com/wencaiwulue/kubevpn/v2/pkg/util"
|
"github.com/wencaiwulue/kubevpn/v2/pkg/util"
|
||||||
)
|
)
|
||||||
@@ -45,6 +47,22 @@ func CmdDaemon(_ cmdutil.Factory) *cobra.Command {
|
|||||||
return initLogfile(action.GetDaemonLogPath())
|
return initLogfile(action.GetDaemonLogPath())
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||||
|
defer func() {
|
||||||
|
var cli rpc.DaemonClient
|
||||||
|
if !opt.IsSudo {
|
||||||
|
cli = daemon.GetClient(true)
|
||||||
|
} else {
|
||||||
|
cli = daemon.GetClient(false)
|
||||||
|
}
|
||||||
|
if cli == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := cli.Quit(context.Background(), &rpc.QuitRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = util.PrintGRPCStream[rpc.QuitResponse](resp, nil)
|
||||||
|
}()
|
||||||
defer opt.Stop()
|
defer opt.Stop()
|
||||||
defer func() {
|
defer func() {
|
||||||
if errors.Is(err, http.ErrServerClosed) {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
|
|||||||
@@ -50,12 +50,12 @@ func (o *SvrOption) Start(ctx context.Context) error {
|
|||||||
LocalTime: true,
|
LocalTime: true,
|
||||||
Compress: false,
|
Compress: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// for gssapi to lookup KDCs in DNS
|
// for gssapi to lookup KDCs in DNS
|
||||||
// c.LibDefaults.DNSLookupKDC = true
|
// c.LibDefaults.DNSLookupKDC = true
|
||||||
// c.LibDefaults.DNSLookupRealm = true
|
// c.LibDefaults.DNSLookupRealm = true
|
||||||
net.DefaultResolver.PreferGo = true
|
net.DefaultResolver.PreferGo = true
|
||||||
|
|
||||||
util.InitLoggerForServer(true)
|
util.InitLoggerForServer(true)
|
||||||
log.SetOutput(l)
|
log.SetOutput(l)
|
||||||
klog.SetOutput(l)
|
klog.SetOutput(l)
|
||||||
@@ -102,7 +102,11 @@ func (o *SvrOption) Start(ctx context.Context) error {
|
|||||||
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
|
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
|
||||||
// startup a http server
|
// startup a http server
|
||||||
// With downgrading-capable gRPC server, which can also handle HTTP.
|
// With downgrading-capable gRPC server, which can also handle HTTP.
|
||||||
downgradingServer := &http.Server{}
|
downgradingServer := &http.Server{
|
||||||
|
BaseContext: func(listener net.Listener) context.Context {
|
||||||
|
return o.ctx
|
||||||
|
},
|
||||||
|
}
|
||||||
defer downgradingServer.Close()
|
defer downgradingServer.Close()
|
||||||
var h2Server http2.Server
|
var h2Server http2.Server
|
||||||
err = http2.ConfigureServer(downgradingServer, &h2Server)
|
err = http2.ConfigureServer(downgradingServer, &h2Server)
|
||||||
@@ -122,7 +126,16 @@ func (o *SvrOption) Start(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
o.svr = &action.Server{Cancel: cancel, IsSudo: o.IsSudo, GetClient: GetClient, LogFile: l, ID: o.ID}
|
o.svr = &action.Server{Cancel: cancel, IsSudo: o.IsSudo, GetClient: GetClient, LogFile: l, ID: o.ID}
|
||||||
rpc.RegisterDaemonServer(svr, o.svr)
|
rpc.RegisterDaemonServer(svr, o.svr)
|
||||||
return downgradingServer.Serve(lis)
|
var errChan = make(chan error)
|
||||||
|
go func() {
|
||||||
|
errChan <- downgradingServer.Serve(lis)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case err = <-errChan:
|
||||||
|
return err
|
||||||
|
case <-o.ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
//return o.svr.Serve(lis)
|
//return o.svr.Serve(lis)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user