Skip to content

Commit

Permalink
refactor: faster log
Browse files Browse the repository at this point in the history
refactor: remove dns check in init.d
  • Loading branch information
BaiMeow committed May 21, 2024
1 parent 1ab23e4 commit b13cb68
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 219 deletions.
2 changes: 2 additions & 0 deletions build_linux.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$Env:GOOS="linux"
go build
14 changes: 7 additions & 7 deletions cmd/bounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/hdu-dn11/wg-quick-op/quick"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -13,23 +13,23 @@ var bounceCmd = &cobra.Command{
Long: `down and then up the interface,if the interface is not up, it will up the interface.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logrus.Errorln("bounce command requires exactly one interface name")
log.Error().Msg("bounce command requires exactly one interface name")
return
}
cfgs := quick.MatchConfig(args[0])
for iface, cfg := range cfgs {
err := quick.Down(cfg, iface, logrus.WithField("iface", iface))
err := quick.Down(cfg, iface, log.With().Str("iface", iface).Logger())
if err != nil {
logrus.WithError(err).WithField("iface", iface).Errorln("failed to down interface")
log.Err(err).Str("iface", iface).Msg("failed to down interface")
}
}
for iface, cfg := range cfgs {
err := quick.Up(cfg, iface, logrus.WithField("iface", iface))
err := quick.Up(cfg, iface, log.With().Str("iface", iface).Logger())
if err != nil {
logrus.WithError(err).WithField("iface", iface).Errorln("failed to up interface")
log.Err(err).Str("iface", iface).Msg("failed to up interface")
}
}
logrus.Infoln("bounce done")
log.Info().Msg("bounce done")
},
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cmd

import (
"github.com/hdu-dn11/wg-quick-op/quick"
"github.com/sirupsen/logrus"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -13,14 +12,14 @@ var downCmd = &cobra.Command{
Short: "down [interface name]",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logrus.Errorln("up command requires exactly one interface name")
log.Error().Msg("up command requires exactly one interface name")
return
}
cfgs := quick.MatchConfig(args[0])
for iface, cfg := range cfgs {
err := quick.Down(cfg, iface, logrus.WithField("iface", iface))
err := quick.Down(cfg, iface, log.With().Str("iface", iface).Logger())
if err != nil {
logrus.WithError(err).Errorln("failed to up interface")
log.Err(err).Msg("failed to up interface")
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"github.com/hdu-dn11/wg-quick-op/conf"
"github.com/hdu-dn11/wg-quick-op/lib/dns"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,7 +31,7 @@ func init() {
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
verbose, _ := cmd.Flags().GetBool("verbose")
if verbose {
logrus.SetLevel(logrus.DebugLevel)
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}
conf.Init(config)
dns.Init()
Expand Down
9 changes: 4 additions & 5 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cmd

import (
"github.com/hdu-dn11/wg-quick-op/quick"
"github.com/sirupsen/logrus"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -15,14 +14,14 @@ var syncCmd = &cobra.Command{
it may result in address added by PostUp being deleted.'`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logrus.Errorln("up command requires exactly one interface name")
log.Error().Msg("up command requires exactly one interface name")
return
}
cfgs := quick.MatchConfig(args[0])
for iface, cfg := range cfgs {
err := quick.Sync(cfg, iface, logrus.WithField("iface", iface))
err := quick.Sync(cfg, iface, log.With().Str("iface", iface).Logger())
if err != nil {
logrus.WithError(err).Errorln("failed to sync interface")
log.Err(err).Msg("failed to sync interface")
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// uninstallCmd represents the uninstall command
// uninstallCmd represents the uninstallation command
var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "uninstall wg-quick-op from /usr/sbin/wg-quick-op",
Expand Down
8 changes: 4 additions & 4 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/hdu-dn11/wg-quick-op/quick"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -16,14 +16,14 @@ regexp in supported, match interface with ^<input>$ by default
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logrus.Errorln("up command requires exactly one interface name")
log.Error().Msg("up command requires exactly one interface name")
return
}
cfgs := quick.MatchConfig(args[0])
for iface, cfg := range cfgs {
err := quick.Up(cfg, iface, logrus.WithField("iface", iface))
err := quick.Up(cfg, iface, log.With().Str("iface", iface).Logger())
if err != nil {
logrus.WithError(err).Errorln("failed to up interface")
log.Err(err).Msg("failed to up interface")
}
}
},
Expand Down
19 changes: 10 additions & 9 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package conf
import (
_ "embed"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"os"
"time"
Expand Down Expand Up @@ -33,21 +34,21 @@ var EnhancedDNS struct {
}

var Log struct {
Level logrus.Level
Level zerolog.Level
}

func Init(file string) {
if _, err := os.Stat(file); err != nil {
if !os.IsNotExist(err) {
logrus.WithError(err).Fatalf("get stat of %s failed", file)
log.Fatal().Err(err).Msgf("get stat of %s failed", file)
}
logrus.Infof("config not existed, creating at %s", file)
log.Info().Msgf("config not existed, creating at %s", file)
created, err := os.Create(file)
if err != nil {
logrus.WithError(err).Fatalf("create config at %s failed", file)
log.Fatal().Err(err).Msgf("create config at %s failed", file)
}
if _, err := created.Write(configSample); err != nil {
logrus.WithError(err).Fatalf("write config at %s failed", file)
log.Fatal().Err(err).Msgf("write config at %s failed", file)
}
}

Expand All @@ -59,7 +60,7 @@ func Init(file string) {

update()
if err != nil {
logrus.WithError(err).Fatalf("read config from %s failed", file)
log.Fatal().Err(err).Msgf("read config from %s failed", file)
}

viper.OnConfigChange(func(e fsnotify.Event) {
Expand All @@ -81,8 +82,8 @@ func update() {
EnhancedDNS.DirectResolver.Enabled = viper.GetBool("enhanced_dns.direct_resolver.enabled")
EnhancedDNS.DirectResolver.ROAFinder = viper.GetString("enhanced_dns.direct_resolver.roa_finder")

if level, err := logrus.ParseLevel(viper.GetString("log.level")); err == nil {
if level, err := zerolog.ParseLevel(viper.GetString("log.level")); err == nil {
Log.Level = level
logrus.SetLevel(level)
zerolog.SetGlobalLevel(level)
}
}
24 changes: 12 additions & 12 deletions daemon/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package daemon

import (
"errors"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"io"
"os"
"os/exec"
Expand All @@ -15,60 +15,60 @@ const installed = "/usr/sbin/wg-quick-op"
func Install() {
file, err := exec.LookPath(os.Args[0])
if err != nil && !errors.Is(err, exec.ErrDot) {
logrus.WithError(err).Errorln("fetch current binary path failed")
log.Err(err).Msg("fetch current binary path failed")
return
}

absFile, err := filepath.Abs(file)
if err != nil {
logrus.WithField("path", absFile).WithError(err).Errorln("The absPath failed")
log.Err(err).Str("path", absFile).Msg("The absPath failed")
return
}
logrus.Infof("current binary: %v", absFile)
log.Info().Msgf("current binary: %v", absFile)

originFp, err := os.Open(absFile)
if err != nil {
logrus.WithError(err).Errorf("open current binary failed")
log.Err(err).Msgf("open current binary failed")
return
}
defer originFp.Close()

if _, err := os.Stat(installed); err != nil {
if !os.IsNotExist(err) {
logrus.WithError(err).Errorf("fetch binary stat failed")
log.Err(err).Msgf("fetch binary stat failed")
return
}
} else {
if err := os.RemoveAll(installed); err != nil {
logrus.WithError(err).Errorf("remove old binary failed")
log.Err(err).Msgf("remove old binary failed")
return
}
}

fp, err := os.OpenFile(installed, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
logrus.WithError(err).Errorf("cannot write to %v", installed)
log.Err(err).Msgf("write to %v", installed)
return
}
defer fp.Close()
_, err = io.Copy(fp, originFp)
if err != nil {
_ = os.RemoveAll(installed)
logrus.Errorf("copy binary to %v failed: %s", installed, err)
log.Err(err).Msgf("copy binary to %s", installed)
return
}
logrus.Infof("installed wg-quick-op")
log.Info().Msg("installed wg-quick-op")
}

func Uninstall() {
file, err := exec.LookPath("wg-quick-op")
if err != nil {
logrus.WithError(err).Errorln("find wg-quick-op failed")
log.Err(err).Msg("find wg-quick-op failed")
return
}

if err := os.RemoveAll(file); err != nil {
logrus.WithField("path", file).WithError(err).Errorln("remove binary failed")
log.Err(err).Str("path", file).Msg("remove binary failed")
return
}
}
Loading

0 comments on commit b13cb68

Please sign in to comment.