Maint: tun package to handle tun device operations
- Moved from openvpn package to tun package - TUN check verifies Rdev value - TUN create - Inject as interface to main function - Add integration test - Clearer log message for end users if tun device does not exist - Remove unix package (unneeded for tests) - Remove tun file opening at the end of tun file creation - Do not mock unix.Mkdev (no OS operation) - Remove Tun operations from OpenVPN configurator
This commit is contained in:
38
internal/tun/create.go
Normal file
38
internal/tun/create.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type Creator interface {
|
||||
Create(path string) error
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMknod = errors.New("cannot create TUN device file node")
|
||||
)
|
||||
|
||||
// Create creates a TUN device at the path specified.
|
||||
func (t *Tun) Create(path string) error {
|
||||
parentDir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(parentDir, 0751); err != nil { //nolint:gomnd
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
major = 10
|
||||
minor = 200
|
||||
)
|
||||
dev := unix.Mkdev(major, minor)
|
||||
err := t.mknod(path, unix.S_IFCHR, int(dev))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", ErrMknod, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user