docker fix

This commit is contained in:
HappyHakunaMatata 2024-08-11 23:21:16 +02:00
parent a4284cf854
commit 0321b73448
3 changed files with 182 additions and 108 deletions

View file

@ -9,6 +9,7 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"syscall" "syscall"
@ -39,10 +40,11 @@ type node struct {
} }
var ( var (
cfg *config.NodeConfig cfg *config.NodeConfig
logger *log.Logger logger *log.Logger
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
rootpath string
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "yggdrasil", Use: "yggdrasil",
@ -63,95 +65,92 @@ var (
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
useconffileCmd = &cobra.Command{
Use: "useconffile <path>",
Args: cobra.ExactArgs(1),
Short: "Read HJSON/JSON config from specified file path",
RunE: cmdUseconffile,
Annotations: map[string]string{"type": "setup"},
}
addressCmd = &cobra.Command{ addressCmd = &cobra.Command{
Use: "address <path>", Use: "address",
Args: cobra.ExactArgs(1),
Short: "Outputs your IPv6 address", Short: "Outputs your IPv6 address",
RunE: cmdAddress, RunE: cmdAddress,
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
snetCmd = &cobra.Command{ snetCmd = &cobra.Command{
Use: "subnet <path>", Use: "subnet",
Args: cobra.ExactArgs(1),
Short: "Outputs your IPv6 subnet", Short: "Outputs your IPv6 subnet",
RunE: cmdSnet, RunE: cmdSnet,
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
pkeyCmd = &cobra.Command{ pkeyCmd = &cobra.Command{
Use: "publickey <path>", Use: "publickey",
Args: cobra.ExactArgs(1),
Short: "Outputs your public key", Short: "Outputs your public key",
RunE: cmdPkey, RunE: cmdPkey,
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
exportKeyCmd = &cobra.Command{ exportKeyCmd = &cobra.Command{
Use: "exportkey <path>", Use: "exportkey",
Args: cobra.ExactArgs(1),
Short: "Outputs your private key in PEM format", Short: "Outputs your private key in PEM format",
RunE: cmdExportKey, RunE: cmdExportKey,
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
logtoCmd = &cobra.Command{
Use: "logto <path>",
Args: cobra.ExactArgs(1),
Short: "File path to log to, \"syslog\" or \"stdout\"",
Run: cmdLogto,
Annotations: map[string]string{"type": "setup"},
}
autoconfCmd = &cobra.Command{
Use: "autoconf",
Short: "Automatic mode (dynamic IP, peer with IPv6 neighbors)",
RunE: cmdAutoconf,
Annotations: map[string]string{"type": "setup"},
}
useconfCmd = &cobra.Command{
Use: "useconf",
Short: "Read HJSON/JSON config from stdin",
RunE: cmdUseconf,
Annotations: map[string]string{"type": "setup"},
}
normaliseconfCmd = &cobra.Command{ normaliseconfCmd = &cobra.Command{
Use: "normaliseconf <path>", Use: "normaliseconf",
Args: cobra.ExactArgs(1),
Short: "Outputs your configuration normalised", Short: "Outputs your configuration normalised",
RunE: cmdNormaliseconf, RunE: cmdNormaliseconf,
Annotations: map[string]string{"type": "setup"}, Annotations: map[string]string{"type": "setup"},
} }
runCmd = &cobra.Command{
Use: "run",
Short: "Runs yggdrasil",
RunE: cmdRun,
Annotations: map[string]string{"type": "setup"},
}
) )
// The main function is responsible for configuring and starting Yggdrasil. // The main function is responsible for configuring and starting Yggdrasil.
func init() { func init() {
var err error
rootDir, err := os.Getwd()
if err != nil {
fmt.Println("Error:", err)
return
}
rootpath = filepath.Join(rootDir, "yggdrasil.conf")
_, err = os.Stat(rootpath)
if err != nil {
if os.IsNotExist(err) {
rootpath = ""
} else {
fmt.Print(err.Error())
}
}
//init cfg
cfg = config.GenerateConfig() cfg = config.GenerateConfig()
genconfCmd.Flags().BoolP("json", "j", false, "print configuration as JSON instead of HJSON")
normaliseconfCmd.Flags().BoolP("json", "j", false, "print configuration as JSON instead of HJSON") ///tested
autoconfCmd.AddCommand(logtoCmd) addressCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
useconffileCmd.AddCommand(logtoCmd)
useconfCmd.AddCommand(logtoCmd)
rootCmd.AddCommand(genconfCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(useconffileCmd)
rootCmd.AddCommand(addressCmd) rootCmd.AddCommand(addressCmd)
genconfCmd.Flags().BoolP("json", "j", false, "print configuration as JSON instead of HJSON")
rootCmd.AddCommand(genconfCmd)
snetCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
rootCmd.AddCommand(snetCmd) rootCmd.AddCommand(snetCmd)
pkeyCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
rootCmd.AddCommand(pkeyCmd) rootCmd.AddCommand(pkeyCmd)
rootCmd.AddCommand(versionCmd)
exportKeyCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
rootCmd.AddCommand(exportKeyCmd) rootCmd.AddCommand(exportKeyCmd)
rootCmd.AddCommand(autoconfCmd) normaliseconfCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
rootCmd.AddCommand(useconfCmd) normaliseconfCmd.Flags().BoolP("json", "j", false, "print configuration as JSON instead of HJSON")
rootCmd.AddCommand(normaliseconfCmd) rootCmd.AddCommand(normaliseconfCmd)
///
runCmd.Flags().StringP("logto", "t", "", "File path to log to, \"syslog\" or \"stdout\"")
runCmd.Flags().StringP("loglevel", "l", "", "loglevel to enable")
runCmd.Flags().BoolP("useconf", "u", false, "Read HJSON/JSON config from stdin")
runCmd.Flags().StringP("useconffile", "f", "", "Read HJSON/JSON config from specified file path")
rootCmd.AddCommand(runCmd)
} }
func main() { func main() {
@ -185,41 +184,20 @@ func cmdVersion(cmd *cobra.Command, args []string) {
fmt.Println("Build version:", version.BuildVersion()) fmt.Println("Build version:", version.BuildVersion())
} }
func cmdUseconffile(cmd *cobra.Command, args []string) (err error) {
useconffile := args[0]
err = ReadConfigFile(useconffile)
if err != nil {
return err
}
err = run()
if err != nil {
return err
}
return nil
}
func cmdAutoconf(cmd *cobra.Command, args []string) (err error) {
err = run()
if err != nil {
return err
}
return nil
}
func cmdUseconf(cmd *cobra.Command, args []string) (err error) {
if _, err := cfg.ReadFrom(os.Stdin); err != nil {
return err
}
err = run()
if err != nil {
return err
}
return nil
}
func cmdAddress(cmd *cobra.Command, args []string) (err error) { func cmdAddress(cmd *cobra.Command, args []string) (err error) {
useconffile := args[0] configFile, err := cmd.Flags().GetString("useconffile")
err = ReadConfigFile(useconffile) if err != nil {
fmt.Println(err)
//return
}
if configFile != "" {
rootpath = configFile
}
if rootpath == "" {
fmt.Println("No file configured")
return
}
err = ReadConfigFile(&rootpath)
if err != nil { if err != nil {
return err return err
} }
@ -232,8 +210,20 @@ func cmdAddress(cmd *cobra.Command, args []string) (err error) {
} }
func cmdSnet(cmd *cobra.Command, args []string) (err error) { func cmdSnet(cmd *cobra.Command, args []string) (err error) {
useconffile := args[0] fmt.Println("Test")
err = ReadConfigFile(useconffile) configFile, err := cmd.Flags().GetString("useconffile")
if err != nil {
fmt.Println(err)
return
}
if configFile != "" {
rootpath = configFile
}
if rootpath == "" {
fmt.Println("No file configured")
return
}
err = ReadConfigFile(&rootpath)
if err != nil { if err != nil {
return err return err
} }
@ -249,8 +239,19 @@ func cmdSnet(cmd *cobra.Command, args []string) (err error) {
} }
func cmdPkey(cmd *cobra.Command, args []string) (err error) { func cmdPkey(cmd *cobra.Command, args []string) (err error) {
useconffile := args[0] configFile, err := cmd.Flags().GetString("useconffile")
err = ReadConfigFile(useconffile) if err != nil {
fmt.Println(err)
return
}
if configFile != "" {
rootpath = configFile
}
if rootpath == "" {
fmt.Println("No file configured")
return
}
err = ReadConfigFile(&rootpath)
if err != nil { if err != nil {
return err return err
} }
@ -261,8 +262,19 @@ func cmdPkey(cmd *cobra.Command, args []string) (err error) {
} }
func cmdExportKey(cmd *cobra.Command, args []string) (err error) { func cmdExportKey(cmd *cobra.Command, args []string) (err error) {
useconffile := args[0] configFile, err := cmd.Flags().GetString("useconffile")
err = ReadConfigFile(useconffile) if err != nil {
fmt.Println(err)
return
}
if configFile != "" {
rootpath = configFile
}
if rootpath == "" {
fmt.Println("No file configured")
return
}
err = ReadConfigFile(&rootpath)
if err != nil { if err != nil {
return err return err
} }
@ -274,8 +286,7 @@ func cmdExportKey(cmd *cobra.Command, args []string) (err error) {
return nil return nil
} }
func cmdLogto(cmd *cobra.Command, args []string) { func cmdLogto(logto string) {
logto := args[0]
switch logto { switch logto {
case "stdout": case "stdout":
logger = log.New(os.Stdout, "", log.Flags()) logger = log.New(os.Stdout, "", log.Flags())
@ -297,8 +308,19 @@ func cmdNormaliseconf(cmd *cobra.Command, args []string) (err error) {
if err != nil { if err != nil {
return err return err
} }
useconffile := args[0] configFile, err := cmd.Flags().GetString("useconffile")
err = ReadConfigFile(useconffile) if err != nil {
fmt.Println(err)
return
}
if configFile != "" {
rootpath = configFile
}
if rootpath == "" {
fmt.Println("No file configured")
return
}
err = ReadConfigFile(&rootpath)
if err != nil { if err != nil {
return err return err
} }
@ -319,7 +341,40 @@ func cmdNormaliseconf(cmd *cobra.Command, args []string) (err error) {
return nil return nil
} }
func run() (err error) { func cmdRun(cmd *cobra.Command, args []string) (err error) {
isUseConf, err := cmd.Flags().GetBool("useconf")
if err != nil {
logger.Error(err.Error())
}
if isUseConf {
if _, err := cfg.ReadFrom(os.Stdin); err != nil {
logger.Error(err.Error())
return err
}
} else {
configFile, err := cmd.Flags().GetString("useconffile")
if err != nil {
logger.Error(err.Error())
}
if configFile != "" {
rootpath = configFile
}
if rootpath != "" {
err = ReadConfigFile(&rootpath)
if err != nil {
logger.Error(err.Error())
}
}
}
logto, err := cmd.Flags().GetString("logto")
if err != nil {
logger.Error(err.Error())
return err
}
if logto != "" {
cmdLogto(logto)
}
ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
// Capture the service being stopped on Windows. // Capture the service being stopped on Windows.
minwinsvc.SetOnExit(cancel) minwinsvc.SetOnExit(cancel)
@ -327,8 +382,19 @@ func run() (err error) {
if logger == nil { if logger == nil {
logger = log.New(os.Stdout, "", log.Flags()) logger = log.New(os.Stdout, "", log.Flags())
logger.Warnln("Logging defaulting to stdout") logger.Warnln("Logging defaulting to stdout")
} }
setLogLevel("info", logger) loglvl, err := cmd.Flags().GetString("loglevel")
if err != nil {
logger.Error(err.Error())
return err
}
if loglvl != "" {
setLogLevel(loglvl, logger)
} else {
setLogLevel("info", logger)
}
n := &node{} n := &node{}
// Set up the Yggdrasil node itself. // Set up the Yggdrasil node itself.
@ -408,6 +474,7 @@ func run() (err error) {
tun.InterfaceMTU(cfg.IfMTU), tun.InterfaceMTU(cfg.IfMTU),
} }
if n.tun, err = tun.New(ipv6rwc.NewReadWriteCloser(n.core), logger, options...); err != nil { if n.tun, err = tun.New(ipv6rwc.NewReadWriteCloser(n.core), logger, options...); err != nil {
logger.Printf(err.Error())
panic(err) panic(err)
} }
if n.admin != nil && n.tun != nil { if n.admin != nil && n.tun != nil {
@ -455,8 +522,16 @@ func setLogLevel(loglevel string, logger *log.Logger) {
} }
} }
func ReadConfigFile(filepath string) error { func ReadConfigFile(filepath *string) (err error) {
f, err := os.Open(filepath) _, err = os.Stat(*filepath)
if err != nil {
if os.IsNotExist(err) {
return err
} else {
fmt.Print(err.Error())
}
}
f, err := os.Open(*filepath)
if err != nil { if err != nil {
return err return err
} }

View file

@ -5,8 +5,7 @@ WORKDIR /src
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
RUN apk add git && ./build && go build -o /src/genkeys cmd/genkeys/main.go RUN apk add --no-cache git curl && ./build && go build -o /src/genkeys cmd/genkeys/main.go
FROM docker.io/alpine FROM docker.io/alpine
COPY --from=builder /src/yggdrasil /usr/bin/yggdrasil COPY --from=builder /src/yggdrasil /usr/bin/yggdrasil

View file

@ -4,10 +4,10 @@ set -e
CONF_DIR="/etc/yggdrasil-network" CONF_DIR="/etc/yggdrasil-network"
if [ ! -f "$CONF_DIR/config.conf" ]; then if [ ! -f "$CONF_DIR/yggdrasil.conf" ]; then
echo "generate $CONF_DIR/config.conf" echo "generate $CONF_DIR/yggdrasil.conf"
yggdrasil --genconf > "$CONF_DIR/config.conf" yggdrasil genconf -j > "$CONF_DIR/yggdrasil.conf"
fi fi
yggdrasil --useconf < "$CONF_DIR/config.conf" yggdrasil run --useconf -l trace < "$CONF_DIR/yggdrasil.conf"
exit $? exit $?