dae/cmd/run.go

315 lines
7.8 KiB
Go
Raw Normal View History

2023-01-28 00:50:21 +07:00
package cmd
import (
"fmt"
2023-03-14 14:01:55 +07:00
"github.com/daeuniverse/dae/cmd/internal"
"github.com/daeuniverse/dae/common/subscription"
"github.com/daeuniverse/dae/config"
"github.com/daeuniverse/dae/control"
"github.com/daeuniverse/dae/pkg/config_parser"
"github.com/daeuniverse/dae/pkg/logger"
"github.com/mohae/deepcopy"
"github.com/okzk/sdnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"math/rand"
"net/http"
2023-01-28 00:50:21 +07:00
"os"
"os/signal"
2023-02-09 19:17:45 +07:00
"path/filepath"
2023-02-25 01:38:21 +07:00
"runtime"
2023-02-27 14:10:15 +07:00
"strconv"
2023-02-09 22:17:49 +07:00
"strings"
2023-01-28 00:50:21 +07:00
"syscall"
"time"
2023-01-28 00:50:21 +07:00
)
2023-02-27 14:10:15 +07:00
const (
PidFilePath = "/var/run/dae.pid"
)
var (
CheckNetworkLinks = []string{
"http://www.msftconnecttest.com/connecttest.txt",
"http://www.qualcomm.cn/generate_204",
}
)
2023-02-27 14:10:15 +07:00
func init() {
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
2023-02-28 20:25:15 +07:00
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-pidfile", "", false, "not generate /var/run/dae.pid")
rand.Shuffle(len(CheckNetworkLinks), func(i, j int) {
CheckNetworkLinks[i], CheckNetworkLinks[j] = CheckNetworkLinks[j], CheckNetworkLinks[i]
})
2023-02-27 14:10:15 +07:00
}
2023-01-28 00:50:21 +07:00
var (
cfgFile string
disableTimestamp bool
2023-02-28 20:25:15 +07:00
disablePidFile bool
2023-01-28 00:50:21 +07:00
runCmd = &cobra.Command{
Use: "run",
2023-03-13 23:46:45 +07:00
Short: "To run dae in the foreground.",
2023-01-28 00:50:21 +07:00
Run: func(cmd *cobra.Command, args []string) {
if cfgFile == "" {
logrus.Fatalln("Argument \"--config\" or \"-c\" is required but not provided.")
}
// Require "sudo" if necessary.
internal.AutoSu()
// Read config from --config cfgFile.
conf, includes, err := readConfig(cfgFile)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err,
}).Fatalln("Failed to read config")
}
log := logger.NewLogger(conf.Global.LogLevel, disableTimestamp)
logrus.SetLevel(log.Level)
2023-02-09 22:17:49 +07:00
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
if err := Run(log, conf); err != nil {
2023-01-28 00:50:21 +07:00
logrus.Fatalln(err)
}
},
}
)
func Run(log *logrus.Logger, conf *config.Config) (err error) {
2023-01-28 00:50:21 +07:00
2023-02-27 12:29:42 +07:00
// New ControlPlane.
2023-03-17 12:13:42 +07:00
c, err := newControlPlane(log, nil, nil, conf)
2023-02-27 12:29:42 +07:00
if err != nil {
return err
}
// Serve tproxy TCP/UDP server util signals.
var listener *control.Listener
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGILL, syscall.SIGUSR1)
go func() {
readyChan := make(chan bool, 1)
go func() {
<-readyChan
sdnotify.Ready()
2023-02-28 20:25:15 +07:00
if !disablePidFile {
2023-02-27 14:10:15 +07:00
_ = os.WriteFile(PidFilePath, []byte(strconv.Itoa(os.Getpid())), 0644)
}
}()
if listener, err = c.ListenAndServe(readyChan, conf.Global.TproxyPort); err != nil {
2023-02-27 12:29:42 +07:00
log.Errorln("ListenAndServe:", err)
}
sigs <- nil
}()
reloading := false
2023-03-13 23:34:38 +07:00
isSuspend := false
2023-02-27 12:29:42 +07:00
loop:
for sig := range sigs {
switch sig {
case nil:
if reloading {
// Serve.
2023-02-27 12:29:42 +07:00
reloading = false
log.Warnln("[Reload] Serve")
readyChan := make(chan bool, 1)
2023-02-27 12:29:42 +07:00
go func() {
if err := c.Serve(readyChan, listener); err != nil {
2023-02-27 12:29:42 +07:00
log.Errorln("ListenAndServe:", err)
}
sigs <- nil
}()
<-readyChan
sdnotify.Ready()
log.Warnln("[Reload] Finished")
2023-02-27 12:29:42 +07:00
} else {
// Listening error.
2023-02-27 12:29:42 +07:00
break loop
}
2023-03-13 23:34:38 +07:00
case syscall.SIGHUP:
isSuspend = true
fallthrough
2023-02-27 12:29:42 +07:00
case syscall.SIGUSR1:
// Reload signal.
log.Warnln("[Reload] Received reload signal; prepare to reload")
sdnotify.Reloading()
// Load new config.
log.Warnln("[Reload] Load new config")
2023-03-13 23:34:38 +07:00
var newConf *config.Config
if isSuspend {
isSuspend = false
newConf, err = emptyConfig()
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Errorln("[Reload] Failed to reload")
sdnotify.Ready()
continue
}
} else {
var includes []string
newConf, includes, err = readConfig(cfgFile)
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Errorln("[Reload] Failed to reload")
sdnotify.Ready()
continue
}
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
}
// New logger.
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp)
logrus.SetLevel(log.Level)
// New control plane.
2023-02-27 12:29:42 +07:00
obj := c.EjectBpf()
2023-03-17 12:13:42 +07:00
dnsCache := c.CloneDnsCache()
2023-02-27 12:29:42 +07:00
log.Warnln("[Reload] Load new control plane")
2023-03-17 12:13:42 +07:00
newC, err := newControlPlane(log, obj, dnsCache, newConf)
2023-02-27 12:29:42 +07:00
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Errorln("[Reload] Failed to reload; try to roll back configuration")
// Load last config back.
2023-03-17 12:13:42 +07:00
newC, err = newControlPlane(log, obj, dnsCache, conf)
if err != nil {
sdnotify.Stopping()
obj.Close()
c.Close()
log.WithFields(logrus.Fields{
"err": err,
}).Fatalln("[Reload] Failed to roll back configuration")
}
newConf = conf
log.Warnln("[Reload] Last reload failed; rolled back configuration")
} else {
log.Warnln("[Reload] Stopped old control plane")
2023-02-27 12:29:42 +07:00
}
2023-03-07 05:38:06 +07:00
// Inject bpf objects into the new control plane life-cycle.
newC.InjectBpf(obj)
2023-03-07 05:38:06 +07:00
// Prepare new context.
oldC := c
2023-02-27 12:29:42 +07:00
c = newC
conf = newConf
2023-02-27 12:29:42 +07:00
reloading = true
2023-03-07 05:38:06 +07:00
// Ready to close.
oldC.Close()
2023-02-27 12:29:42 +07:00
default:
2023-03-13 13:25:14 +07:00
log.Infof("Received signal: %v", sig.String())
2023-02-27 12:29:42 +07:00
break loop
}
}
if e := c.Close(); e != nil {
return fmt.Errorf("close control plane: %w", e)
}
2023-02-27 14:10:15 +07:00
_ = os.Remove(PidFilePath)
2023-02-27 12:29:42 +07:00
return nil
}
2023-03-17 12:13:42 +07:00
func newControlPlane(log *logrus.Logger, bpf interface{}, dnsCache map[string]*control.DnsCache, conf *config.Config) (c *control.ControlPlane, err error) {
// Deep copy to prevent modification.
conf = deepcopy.Copy(conf).(*config.Config)
/// Get tag -> nodeList mapping.
tagToNodeList := map[string][]string{}
if len(conf.Node) > 0 {
for _, node := range conf.Node {
tagToNodeList[""] = append(tagToNodeList[""], string(node))
}
}
2023-01-28 00:50:21 +07:00
// Resolve subscriptions to nodes.
2023-02-27 14:10:15 +07:00
resolvingfailed := false
if !conf.Global.DisableWaitingNetwork && len(conf.Subscription) > 0 {
log.Infoln("Waiting for network...")
for i := 0; ; i++ {
resp, err := http.Get(CheckNetworkLinks[i%len(CheckNetworkLinks)])
if err != nil {
time.Sleep(5 * time.Second)
continue
}
resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode < 500 {
break
}
log.Infof("Bad status: %v (%v)", resp.Status, resp.StatusCode)
2023-03-15 17:15:07 +07:00
time.Sleep(5 * time.Second)
}
log.Infoln("Network online.")
}
for _, sub := range conf.Subscription {
2023-02-28 20:25:15 +07:00
tag, nodes, err := subscription.ResolveSubscription(log, filepath.Dir(cfgFile), string(sub))
2023-01-28 00:50:21 +07:00
if err != nil {
log.Warnf(`failed to resolve subscription "%v": %v`, sub, err)
2023-02-27 14:10:15 +07:00
resolvingfailed = true
2023-01-28 00:50:21 +07:00
}
if len(nodes) > 0 {
tagToNodeList[tag] = append(tagToNodeList[tag], nodes...)
}
2023-01-28 00:50:21 +07:00
}
if len(tagToNodeList) == 0 {
2023-02-27 14:10:15 +07:00
if resolvingfailed {
log.Warnln("No node found because all subscription resolving failed.")
} else {
log.Warnln("No node found.")
}
}
2023-01-28 00:50:21 +07:00
if len(conf.Global.LanInterface) == 0 && len(conf.Global.WanInterface) == 0 {
2023-03-04 12:16:26 +07:00
log.Warnln("No interface to bind.")
2023-02-01 09:59:57 +07:00
}
2023-02-27 12:29:42 +07:00
c, err = control.NewControlPlane(
2023-01-28 00:50:21 +07:00
log,
2023-02-27 12:29:42 +07:00
bpf,
2023-03-17 12:13:42 +07:00
dnsCache,
tagToNodeList,
conf.Group,
&conf.Routing,
&conf.Global,
&conf.Dns,
2023-01-28 00:50:21 +07:00
)
if err != nil {
2023-02-27 12:29:42 +07:00
return nil, err
2023-01-28 00:50:21 +07:00
}
2023-02-25 01:38:21 +07:00
// Call GC to release memory.
runtime.GC()
2023-02-27 12:29:42 +07:00
return c, nil
2023-01-28 00:50:21 +07:00
}
func readConfig(cfgFile string) (conf *config.Config, includes []string, err error) {
2023-02-09 22:17:49 +07:00
merger := config.NewMerger(cfgFile)
2023-02-10 10:55:00 +07:00
sections, includes, err := merger.Merge()
2023-01-28 00:50:21 +07:00
if err != nil {
2023-02-09 22:17:49 +07:00
return nil, nil, err
2023-01-28 00:50:21 +07:00
}
if conf, err = config.New(sections); err != nil {
2023-02-09 22:17:49 +07:00
return nil, nil, err
2023-01-28 00:50:21 +07:00
}
return conf, includes, nil
2023-01-28 00:50:21 +07:00
}
2023-03-13 23:34:38 +07:00
func emptyConfig() (conf *config.Config, err error) {
sections, err := config_parser.Parse(`global{} routing{}`)
if err != nil {
return nil, err
}
if conf, err = config.New(sections); err != nil {
return nil, err
}
return conf, nil
}
func init() {
rootCmd.AddCommand(runCmd)
}