fix write files and deprecated calls
This commit is contained in:
		
							
								
								
									
										14
									
								
								boot-v1.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								boot-v1.go
									
									
									
									
									
								
							| @ -2,7 +2,6 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| @ -58,7 +57,7 @@ func bootV1() { | |||||||
| } | } | ||||||
|  |  | ||||||
| func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) { | func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) { | ||||||
| 	cfgBytes, err := ioutil.ReadFile(cfgPath) | 	cfgBytes, err := os.ReadFile(cfgPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fatalf("failed to read %s: %v", cfgPath, err) | 		fatalf("failed to read %s: %v", cfgPath, err) | ||||||
| 	} | 	} | ||||||
| @ -134,7 +133,7 @@ func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) { | |||||||
|  |  | ||||||
| 	// - write configuration | 	// - write configuration | ||||||
| 	log.Print("writing /config.yaml") | 	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) | 		fatal("failed: ", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @ -144,7 +143,14 @@ func applyConfig(cfgPath string, bootMounted bool) (cfg *configV1) { | |||||||
|  |  | ||||||
| 		filePath := filepath.Join("/system", fileDef.Path) | 		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 | 	return | ||||||
|  | |||||||
| @ -4,7 +4,6 @@ import ( | |||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
| @ -121,7 +120,7 @@ func bootstrap(cfg *config.Config) { | |||||||
| func setUserPass(user, passwordHash string) { | func setUserPass(user, passwordHash string) { | ||||||
| 	const fpath = "/system/etc/shadow" | 	const fpath = "/system/etc/shadow" | ||||||
|  |  | ||||||
| 	ba, err := ioutil.ReadFile(fpath) | 	ba, err := os.ReadFile(fpath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fatalf("failed to read shadow: %v", err) | 		fatalf("failed to read shadow: %v", err) | ||||||
| 	} | 	} | ||||||
| @ -144,7 +143,7 @@ func setUserPass(user, passwordHash string) { | |||||||
| 		buf.WriteByte('\n') | 		buf.WriteByte('\n') | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err = ioutil.WriteFile(fpath, buf.Bytes(), 0600) | 	err = os.WriteFile(fpath, buf.Bytes(), 0600) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fatalf("failed to write shadow: %v", err) | 		fatalf("failed to write shadow: %v", err) | ||||||
| 	} | 	} | ||||||
| @ -163,7 +162,7 @@ func setAuthorizedKeys(ak []string) { | |||||||
| 		fatalf("failed to create %s: %v", sshDir, err) | 		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 { | 	if err != nil { | ||||||
| 		fatalf("failed to write authorized keys: %v", err) | 		fatalf("failed to write authorized keys: %v", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @ -25,4 +25,4 @@ require ( | |||||||
| 	golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c // indirect | 	golang.zx2c4.com/wireguard v0.0.0-20220920152132-bb719d3a6e2c // indirect | ||||||
| ) | ) | ||||||
|  |  | ||||||
| go 1.18 | go 1.21 | ||||||
|  | |||||||
| @ -1,12 +1,12 @@ | |||||||
| package main | package main | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"io/ioutil" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func param(name, defaultValue string) (value string) { | func param(name, defaultValue string) (value string) { | ||||||
| 	ba, err := ioutil.ReadFile("/proc/cmdline") | 	ba, err := os.ReadFile("/proc/cmdline") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fatal("could not read /proc/cmdline: ", err) | 		fatal("could not read /proc/cmdline: ", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								ssh.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								ssh.go
									
									
									
									
									
								
							| @ -4,7 +4,6 @@ import ( | |||||||
| 	"encoding/binary" | 	"encoding/binary" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"net" | 	"net" | ||||||
| 	"os" | 	"os" | ||||||
| @ -24,7 +23,7 @@ func startSSH(cfg *config.Config) { | |||||||
| 		PublicKeyCallback: sshCheckPubkey, | 		PublicKeyCallback: sshCheckPubkey, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	pkBytes, err := ioutil.ReadFile("/id_rsa") // TODO configurable | 	pkBytes, err := os.ReadFile("/id_rsa") // TODO configurable | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fatalf("ssh: failed to load private key: %v", err) | 		fatalf("ssh: failed to load private key: %v", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								vpn.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								vpn.go
									
									
									
									
									
								
							| @ -1,7 +1,6 @@ | |||||||
| package main | package main | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"io/ioutil" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"net" | 	"net" | ||||||
| 	"os" | 	"os" | ||||||
| @ -21,7 +20,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) { | |||||||
|  |  | ||||||
| 	// public/private key | 	// public/private key | ||||||
| 	keyFile := filepath.Join(vpnDir, "key") | 	keyFile := filepath.Join(vpnDir, "key") | ||||||
| 	keyBytes, err := ioutil.ReadFile(keyFile) | 	keyBytes, err := os.ReadFile(keyFile) | ||||||
| 	if os.IsNotExist(err) { | 	if os.IsNotExist(err) { | ||||||
| 		key, err := wgtypes.GeneratePrivateKey() | 		key, err := wgtypes.GeneratePrivateKey() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @ -30,7 +29,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) { | |||||||
|  |  | ||||||
| 		keyBytes = []byte(key.String()) | 		keyBytes = []byte(key.String()) | ||||||
|  |  | ||||||
| 		ioutil.WriteFile(keyFile, keyBytes, 0600) | 		os.WriteFile(keyFile, keyBytes, 0600) | ||||||
| 	} else if err != nil { | 	} else if err != nil { | ||||||
| 		fatalf("failed to read VPN key: %v", err) | 		fatalf("failed to read VPN key: %v", err) | ||||||
| 	} | 	} | ||||||
| @ -44,7 +43,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) { | |||||||
|  |  | ||||||
| 	// pre-shared key | 	// pre-shared key | ||||||
| 	pskeyFile := filepath.Join(vpnDir, "pskey") | 	pskeyFile := filepath.Join(vpnDir, "pskey") | ||||||
| 	pskeyBytes, err := ioutil.ReadFile(pskeyFile) | 	pskeyBytes, err := os.ReadFile(pskeyFile) | ||||||
| 	if os.IsNotExist(err) { | 	if os.IsNotExist(err) { | ||||||
| 		key, err := wgtypes.GenerateKey() | 		key, err := wgtypes.GenerateKey() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @ -53,7 +52,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) { | |||||||
|  |  | ||||||
| 		pskeyBytes = []byte(key.String()) | 		pskeyBytes = []byte(key.String()) | ||||||
|  |  | ||||||
| 		ioutil.WriteFile(pskeyFile, pskeyBytes, 0600) | 		os.WriteFile(pskeyFile, pskeyBytes, 0600) | ||||||
| 	} else if err != nil { | 	} else if err != nil { | ||||||
| 		fatalf("failed to read VPN pre-shared key: %v", err) | 		fatalf("failed to read VPN pre-shared key: %v", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user