aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBaylac-Jacqué Félix <felix@alternativebit.fr>2017-08-09 11:06:26 +0200
committerBaylac-Jacqué Félix <felix@alternativebit.fr>2017-08-09 11:06:26 +0200
commit77207e733d16ec50810a0bbd7869c8c29324a731 (patch)
tree22c5d2d9d7df1735be47fa02c8dc228f911e8a12 /src
parentuapi: upstream change (diff)
downloadwireguard-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.hs81
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