2020-02-06 20:42:46 -05:00
|
|
|
package openvpn
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2021-07-23 16:06:19 +00:00
|
|
|
"path/filepath"
|
2020-02-06 20:42:46 -05:00
|
|
|
|
2020-12-29 01:02:47 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/unix"
|
2020-02-06 20:42:46 -05:00
|
|
|
)
|
|
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
// CheckTUN checks the tunnel device is present and accessible.
|
2020-02-06 20:42:46 -05:00
|
|
|
func (c *configurator) CheckTUN() error {
|
2021-07-23 16:06:19 +00:00
|
|
|
c.logger.Info("checking for device " + c.tunDevPath)
|
|
|
|
|
f, err := os.OpenFile(c.tunDevPath, os.O_RDWR, 0)
|
2020-02-06 20:42:46 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("TUN device is not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := f.Close(); err != nil {
|
2021-07-23 17:36:08 +00:00
|
|
|
c.logger.Warn("Could not close TUN device file: " + err.Error())
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-02-08 15:30:28 +00:00
|
|
|
|
|
|
|
|
func (c *configurator) CreateTUN() error {
|
2021-07-23 16:06:19 +00:00
|
|
|
c.logger.Info("creating " + c.tunDevPath)
|
|
|
|
|
|
|
|
|
|
parentDir := filepath.Dir(c.tunDevPath)
|
|
|
|
|
if err := os.MkdirAll(parentDir, 0751); err != nil { //nolint:gomnd
|
2020-02-08 15:30:28 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
const (
|
|
|
|
|
major = 10
|
|
|
|
|
minor = 200
|
|
|
|
|
)
|
2020-12-29 01:02:47 +00:00
|
|
|
dev := c.unix.Mkdev(major, minor)
|
2021-07-23 16:06:19 +00:00
|
|
|
if err := c.unix.Mknod(c.tunDevPath, unix.S_IFCHR, int(dev)); err != nil {
|
2020-02-08 15:30:28 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2020-12-29 00:55:31 +00:00
|
|
|
|
|
|
|
|
const readWriteAllPerms os.FileMode = 0666
|
2021-07-23 16:06:19 +00:00
|
|
|
file, err := os.OpenFile(c.tunDevPath, os.O_WRONLY, readWriteAllPerms)
|
|
|
|
|
if err != nil {
|
2020-12-29 00:55:31 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file.Close()
|
2020-02-08 15:30:28 +00:00
|
|
|
}
|