Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bd307db0d3 | |||
| 7ca01aa1e4 | |||
| 8b72667f4d | |||
| 49694eb7cb | |||
| 15a98c05ec | |||
| a005823d44 |
@@ -1,7 +1,9 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -17,13 +19,22 @@ func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error)
|
||||
}
|
||||
|
||||
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||
func (m *Machine) CreateTruncFile(name string, args ...any) (fs.File, error) {
|
||||
return m.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, args...)
|
||||
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||||
}
|
||||
|
||||
// CreateFile creates a file on the machine's config directory
|
||||
func (m *Machine) CreateFile(name string, args ...any) (fs.File, error) {
|
||||
return m.OpenFile(name, os.O_RDWR|os.O_CREATE, args...)
|
||||
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE, args...)
|
||||
}
|
||||
|
||||
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f.(io.WriteCloser), nil
|
||||
}
|
||||
|
||||
// RemoveFile deletes a file from the machine's config directory
|
||||
@@ -48,6 +59,19 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
return fs.ReadFile(base, fullName)
|
||||
}
|
||||
|
||||
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||
f, err := m.CreateTruncFile(name, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buf := bytes.NewBufferString(value)
|
||||
_, err = buf.WriteTo(f)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Machine) getFilename(name string, args ...any) string {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
|
||||
+67
-36
@@ -73,6 +73,60 @@ func (m *Machine) tryReadWireguardKeys(ring int) error {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
||||
func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||
var err error
|
||||
var key, pub string
|
||||
var ri *RingInfo
|
||||
|
||||
ri, _ = m.getRingInfo(ring)
|
||||
if ri != nil {
|
||||
key = ri.Keys.PrivateKey.String()
|
||||
pub = ri.Keys.PublicKey.String()
|
||||
}
|
||||
|
||||
switch {
|
||||
case key == "":
|
||||
return fs.ErrNotExist
|
||||
case pub == "":
|
||||
pub = ri.Keys.PrivateKey.Public().String()
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(key, "wg%v.key", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(pub, "wg%v.pub", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||
// the machine's config directory
|
||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||
var err error
|
||||
|
||||
err = m.RemoveFile("wg%v.pub", ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// ignore
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.RemoveFile("wg%v.key", ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWireguardConfig reads a wgN.conf file
|
||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||
data, err := m.ReadFile("wg%v.conf", ring)
|
||||
@@ -197,6 +251,17 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||
// config directory.
|
||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||
err := m.RemoveFile("wg%v.conf", ring)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (*Machine) syncRingConfig(_ int) error {
|
||||
// _, err := m.getRingNodes(ring)
|
||||
return nil
|
||||
@@ -229,41 +294,18 @@ func (m *Machine) writeRingInfo(ri *RingInfo) error {
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
|
||||
err = m.writeRingInfoPrivate(ri.Ring, ri.Keys.PrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.writeRingInfoPublic(ri.Ring, ri.Keys.PublicKey)
|
||||
err = m.WriteWireguardKeys(ri.Ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !ri.Enabled {
|
||||
return m.deleteRingInfoConf(ri.Ring)
|
||||
return m.RemoveWireguardConfig(ri.Ring)
|
||||
}
|
||||
|
||||
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
|
||||
}
|
||||
|
||||
func (m *Machine) writeRingInfoPrivate(ring int, _ wireguard.PrivateKey) error {
|
||||
f, err := m.CreateTruncFile("wg%v.key", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
func (m *Machine) writeRingInfoPublic(ring int, _ wireguard.PublicKey) error {
|
||||
f, err := m.CreateTruncFile("wg%v.pub", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
|
||||
f, err := m.CreateTruncFile("wg%v.conf", ring)
|
||||
if err != nil {
|
||||
@@ -273,14 +315,3 @@ func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) deleteRingInfoConf(ring int) error {
|
||||
err := m.RemoveFile("wg%v.conf", ring)
|
||||
|
||||
switch err {
|
||||
case os.ErrNotExist:
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user