diff options
author | Baylac-Jacqué Félix <felix@alternativebit.fr> | 2017-08-09 11:06:26 +0200 |
---|---|---|
committer | Baylac-Jacqué Félix <felix@alternativebit.fr> | 2017-08-09 11:06:26 +0200 |
commit | 77207e733d16ec50810a0bbd7869c8c29324a731 (patch) | |
tree | 22c5d2d9d7df1735be47fa02c8dc228f911e8a12 /src | |
parent | uapi: upstream change (diff) | |
download | wireguard-hs-77207e733d16ec50810a0bbd7869c8c29324a731.tar.xz wireguard-hs-77207e733d16ec50810a0bbd7869c8c29324a731.zip |
Refactor project structure to exec + lib.
We want to add some unit tests to the project, hence, we need
a proper library.
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.hs | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/src/Main.hs b/src/Main.hs deleted file mode 100644 index 29d4953..0000000 --- a/src/Main.hs +++ /dev/null @@ -1,81 +0,0 @@ -{-# LANGUAGE RecordWildCards #-} - -module Main where - -import Control.Concurrent (getNumCapabilities) -import Control.Monad (void) -import Data.Monoid ((<>)) -import System.Directory (createDirectoryIfMissing, - doesDirectoryExist) -import System.Exit (die) -import System.FilePath.Posix (takeDirectory, (</>)) -import System.Info (os) -import System.Posix.IO (OpenMode (..), closeFd, - defaultFileFlags, dupTo, - openFd, stdError, stdInput, - stdOutput) -import System.Posix.Process (forkProcess) -import System.Posix.Types (Fd) - -import Options.Applicative - -import Network.WireGuard.Daemon (runDaemon) -import Network.WireGuard.Foreign.Tun (openTun) -import Network.WireGuard.Internal.Util (catchIOExceptionAnd) - -data Opts = Opts - { foreground :: Bool - , intfName :: String - } - -parser :: ParserInfo Opts -parser = info (helper <*> opts) fullDesc - where - opts = Opts <$> _foreground - <*> _intfName - - _foreground = switch - ( long "foreground" - <> short 'f' - <> help "run in the foreground") - - _intfName = argument str - ( metavar "interface" - <> help ("device interface name (e.g. " ++ intfNameExample ++ ")")) - - intfNameExample | os == "darwin" = "utun1" - | otherwise = "wg0" - - -main :: IO () -main = do - Opts{..} <- execParser parser - - runPath <- maybe (die "failed to find path to bind socket") return =<< findVarRun - let sockPath = runPath </> "wireguard" </> (intfName ++ ".sock") - createDirectoryIfMissing False (takeDirectory sockPath) - - fds <- openTun intfName =<< getNumCapabilities - - let runner daemon | foreground = daemon - | otherwise = void $ forkProcess $ do - mapM_ redirectToNull [stdInput, stdOutput, stdError] - daemon - - runner $ runDaemon intfName sockPath fds - -redirectToNull :: Fd -> IO () -redirectToNull fd = catchIOExceptionAnd (return ()) $ do - nullFd <- openFd "/dev/null" ReadWrite Nothing defaultFileFlags - closeFd fd - void $ dupTo nullFd fd - -findVarRun :: IO (Maybe FilePath) -findVarRun = loop ["/var/run", "/run"] - where - loop [] = return Nothing - loop (d:ds) = do - exists <- doesDirectoryExist d - if exists - then return (Just d) - else loop ds |