Compare commits
3 Commits
a454938bd7
...
v0.4.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 49694eb7cb | |||
| 15a98c05ec | |||
| a005823d44 |
@@ -1,7 +1,9 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -16,6 +18,25 @@ func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error)
|
|||||||
return fs.OpenFile(base, fullName, flags, 0644)
|
return fs.OpenFile(base, fullName, flags, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||||
|
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) (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
|
// RemoveFile deletes a file from the machine's config directory
|
||||||
func (m *Machine) RemoveFile(name string, args ...any) error {
|
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||||
base := m.zone.zones.dir
|
base := m.zone.zones.dir
|
||||||
@@ -38,6 +59,19 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
|||||||
return fs.ReadFile(base, fullName)
|
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 {
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -72,6 +73,38 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
// GetWireguardConfig reads a wgN.conf file
|
// GetWireguardConfig reads a wgN.conf file
|
||||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||||
data, err := m.ReadFile("wg%v.conf", ring)
|
data, err := m.ReadFile("wg%v.conf", ring)
|
||||||
|
|||||||
Reference in New Issue
Block a user