fix write files and deprecated calls

This commit is contained in:
Mikaël Cluseau 2023-11-27 14:08:44 +01:00
parent 69cc01db9b
commit 86d85f014c
6 changed files with 21 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -58,7 +57,7 @@ func bootV1() {
}
func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) {
cfgBytes, err := ioutil.ReadFile(cfgPath)
cfgBytes, err := os.ReadFile(cfgPath)
if err != nil {
fatalf("failed to read %s: %v", cfgPath, err)
}
@ -134,7 +133,7 @@ func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) {
// - write configuration
log.Print("writing /config.yaml")
if err := ioutil.WriteFile("/system/config.yaml", cfgBytes, 0600); err != nil {
if err := os.WriteFile("/system/config.yaml", cfgBytes, 0600); err != nil {
fatal("failed: ", err)
}
@ -144,7 +143,14 @@ func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) {
filePath := filepath.Join("/system", fileDef.Path)
ioutil.WriteFile(filePath, []byte(fileDef.Content), fileDef.Mode)
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
log.Printf("failed to create dir %s: %v", filepath.Dir(fileDef.Path), err)
}
err = os.WriteFile(filePath, []byte(fileDef.Content), fileDef.Mode)
if err != nil {
fatalf("failed to write %s: %v", fileDef.Path, err)
}
}
return

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
@ -121,7 +120,7 @@ func bootstrap(cfg *config.Config) {
func setUserPass(user, passwordHash string) {
const fpath = "/system/etc/shadow"
ba, err := ioutil.ReadFile(fpath)
ba, err := os.ReadFile(fpath)
if err != nil {
fatalf("failed to read shadow: %v", err)
}
@ -144,7 +143,7 @@ func setUserPass(user, passwordHash string) {
buf.WriteByte('\n')
}
err = ioutil.WriteFile(fpath, buf.Bytes(), 0600)
err = os.WriteFile(fpath, buf.Bytes(), 0600)
if err != nil {
fatalf("failed to write shadow: %v", err)
}
@ -163,7 +162,7 @@ func setAuthorizedKeys(ak []string) {
fatalf("failed to create %s: %v", sshDir, err)
}
err = ioutil.WriteFile(filepath.Join(sshDir, "authorized_keys"), buf.Bytes(), 0600)
err = os.WriteFile(filepath.Join(sshDir, "authorized_keys"), buf.Bytes(), 0600)
if err != nil {
fatalf("failed to write authorized keys: %v", err)
}

2
go.mod
View File

@ -25,4 +25,4 @@ require (
golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c // indirect
)
go 1.18
go 1.21

View File

@ -1,12 +1,12 @@
package main
import (
"io/ioutil"
"os"
"strings"
)
func param(name, defaultValue string) (value string) {
ba, err := ioutil.ReadFile("/proc/cmdline")
ba, err := os.ReadFile("/proc/cmdline")
if err != nil {
fatal("could not read /proc/cmdline: ", err)
}

3
ssh.go
View File

@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
@ -24,7 +23,7 @@ func startSSH(cfg *config.Config) {
PublicKeyCallback: sshCheckPubkey,
}
pkBytes, err := ioutil.ReadFile("/id_rsa") // TODO configurable
pkBytes, err := os.ReadFile("/id_rsa") // TODO configurable
if err != nil {
fatalf("ssh: failed to load private key: %v", err)
}

9
vpn.go
View File

@ -1,7 +1,6 @@
package main
import (
"io/ioutil"
"log"
"net"
"os"
@ -21,7 +20,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
// public/private key
keyFile := filepath.Join(vpnDir, "key")
keyBytes, err := ioutil.ReadFile(keyFile)
keyBytes, err := os.ReadFile(keyFile)
if os.IsNotExist(err) {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
@ -30,7 +29,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
keyBytes = []byte(key.String())
ioutil.WriteFile(keyFile, keyBytes, 0600)
os.WriteFile(keyFile, keyBytes, 0600)
} else if err != nil {
fatalf("failed to read VPN key: %v", err)
}
@ -44,7 +43,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
// pre-shared key
pskeyFile := filepath.Join(vpnDir, "pskey")
pskeyBytes, err := ioutil.ReadFile(pskeyFile)
pskeyBytes, err := os.ReadFile(pskeyFile)
if os.IsNotExist(err) {
key, err := wgtypes.GenerateKey()
if err != nil {
@ -53,7 +52,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
pskeyBytes = []byte(key.String())
ioutil.WriteFile(pskeyFile, pskeyBytes, 0600)
os.WriteFile(pskeyFile, pskeyBytes, 0600)
} else if err != nil {
fatalf("failed to read VPN pre-shared key: %v", err)
}