|
|
@@ -2,16 +2,16 @@ package docker
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/base64"
|
|
|
"fmt"
|
|
|
+ "github.com/docker/docker/api/types"
|
|
|
+ "github.com/docker/docker/api/types/container"
|
|
|
+ "github.com/docker/docker/client"
|
|
|
"goseg/config"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
- "encoding/base64"
|
|
|
"strings"
|
|
|
- "github.com/docker/docker/api/types/container"
|
|
|
- "github.com/docker/docker/client"
|
|
|
- "github.com/docker/docker/api/types"
|
|
|
// "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
)
|
|
|
|
|
|
@@ -54,10 +54,10 @@ func wgContainerConf() (container.Config, container.HostConfig, error) {
|
|
|
desiredImage := fmt.Sprintf("%s:%s@sha256:%s", desiredRepo, desiredTag, desiredHash)
|
|
|
// construct the container config struct
|
|
|
containerConfig = container.Config{
|
|
|
- Image: desiredImage,
|
|
|
+ Image: desiredImage,
|
|
|
Entrypoint: []string{"/bin/bash"},
|
|
|
- Tty: true,
|
|
|
- OpenStdin: true,
|
|
|
+ Tty: true,
|
|
|
+ OpenStdin: true,
|
|
|
}
|
|
|
// always on wg nw
|
|
|
hostConfig = container.HostConfig{
|
|
|
@@ -79,40 +79,34 @@ func buildWgConf() (string, error) {
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
-// write wg config if it doesn't exist or doesn't match
|
|
|
func writeWgConf() error {
|
|
|
- volumeExists := true
|
|
|
- // read existing and build current conf
|
|
|
+ newConf, err := buildWgConf()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
filePath := filepath.Join(config.DockerDir, "settings", "wireguard", "_data", "wg0.conf")
|
|
|
existingConf, err := ioutil.ReadFile(filePath)
|
|
|
if err != nil {
|
|
|
- volumeExists = false
|
|
|
+ // assume it doesn't exist, so write the current config
|
|
|
+ return writeWgConfToFile(filePath, newConf)
|
|
|
}
|
|
|
- newConf, err := buildWgConf()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ if string(existingConf) != newConf {
|
|
|
+ // If they differ, overwrite
|
|
|
+ return writeWgConfToFile(filePath, newConf)
|
|
|
}
|
|
|
- ctx := context.Background()
|
|
|
- cli, err := client.NewClientWithOpts(client.FromEnv)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func writeWgConfToFile(filePath string, content string) error {
|
|
|
+ err := ioutil.WriteFile(filePath, []byte(content), 0644)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Failed to write new WG config: %v", err)
|
|
|
}
|
|
|
- _, err = cli.VolumeInspect(ctx, "wireguard")
|
|
|
+ // Copy to volume
|
|
|
+ err = copyFileToVolume(filePath, "/etc/wireguard/", "wireguard")
|
|
|
if err != nil {
|
|
|
- volumeExists = false
|
|
|
+ return fmt.Errorf("Failed to copy WG config file to volume: %v", err)
|
|
|
}
|
|
|
- // if theyre different, or if the volume doesnt exist, copy the new config to the volume
|
|
|
- if string(existingConf) != newConf || !volumeExists {
|
|
|
- err = ioutil.WriteFile("tmp/wg0.conf", []byte(newConf), 0644)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("Failed to write new WG config: %v", err)
|
|
|
- }
|
|
|
- // copy to volume
|
|
|
- err = copyFileToVolume(filepath.Join("tmp","wg0.conf"), "/etc/wireguard/", "wireguard")
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("Failed to copy WG config file to volume: %v", err)
|
|
|
- }
|
|
|
- }
|
|
|
return nil
|
|
|
}
|
|
|
|