From 9807e5bead5e0e13ac166aa037a1d55f64bb0db3 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 25 Apr 2018 14:38:42 -0400 Subject: [PATCH] fix webapp opening in termux Open real url not html shim since android and file:// urls is a nasty kettle of fish. This commit was sponsored by John Pellman on Patreon. --- Assistant/WebApp/Configurators/Local.hs | 5 +-- Command/WebApp.hs | 44 +++++++++---------- Utility/Android.hs | 14 ++++++ ..._a60b6e4511c1df23d76c6e6e8028eeaa._comment | 3 +- git-annex.cabal | 1 + 5 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 Utility/Android.hs diff --git a/Assistant/WebApp/Configurators/Local.hs b/Assistant/WebApp/Configurators/Local.hs index 63a032be0b..e4bfbfebee 100644 --- a/Assistant/WebApp/Configurators/Local.hs +++ b/Assistant/WebApp/Configurators/Local.hs @@ -38,6 +38,7 @@ import Config import Utility.Gpg import qualified Remote.GCrypt as GCrypt import qualified Types.Remote +import Utility.Android import qualified Data.Text as T import qualified Data.Map as M @@ -144,10 +145,6 @@ defaultRepositoryPath firstrun = do legit d = not <$> doesFileExist (d "git-annex") #endif --- Detect when the Linux build is running on Android, eg in termux. -osAndroid :: IO Bool -osAndroid = ("Android" == ) <$> readProcess "uname" ["-o"] - newRepositoryForm :: FilePath -> Hamlet.Html -> MkMForm RepositoryPath newRepositoryForm defpath msg = do (pathRes, pathView) <- mreq (repositoryPathField True) (bfs "") diff --git a/Command/WebApp.hs b/Command/WebApp.hs index d9c001b226..c380c312e7 100644 --- a/Command/WebApp.hs +++ b/Command/WebApp.hs @@ -31,6 +31,7 @@ import qualified Annex import Config.Files import Upgrade import Annex.Version +import Utility.Android import Control.Concurrent import Control.Concurrent.STM @@ -207,43 +208,42 @@ openBrowser mcmd htmlshim realurl outh errh = do openBrowser' :: Maybe FilePath -> FilePath -> String -> Maybe Handle -> Maybe Handle -> IO () #ifndef __ANDROID__ -openBrowser' mcmd htmlshim _realurl outh errh = runbrowser +openBrowser' mcmd htmlshim realurl outh errh = + ifM osAndroid + {- Android does not support file:// urls well, but neither + - is the security of the url in the process table important + - there, so just use the real url. -} + ( runbrowser realurl + , runbrowser (fileUrl htmlshim) + ) #else openBrowser' mcmd htmlshim realurl outh errh = do - recordUrl url + recordUrl realurl {- Android's `am` command does not work reliably across the - wide range of Android devices. Intead, FIFO should be set to - the filename of a fifo that we can write the URL to. -} v <- getEnv "FIFO" case v of - Nothing -> runbrowser + Nothing -> runbrowser realurl Just f -> void $ forkIO $ do fd <- openFd f WriteOnly Nothing defaultFileFlags - void $ fdWrite fd url + void $ fdWrite fd realurl closeFd fd #endif where - p = case mcmd of - Just c -> proc c [htmlshim] - Nothing -> + runbrowser url = do + let p = case mcmd of + Just c -> proc c [url] + Nothing -> #ifndef mingw32_HOST_OS - browserProc url + browserProc url #else - {- Windows hack to avoid using the full path, - - which might contain spaces that cause problems - - for browserProc. -} - (browserProc (takeFileName htmlshim)) - { cwd = Just (takeDirectory htmlshim) } + {- Windows hack to avoid using the full path, + - which might contain spaces that cause problems + - for browserProc. -} + (browserProc (takeFileName htmlshim)) + { cwd = Just (takeDirectory htmlshim) } #endif -#ifdef __ANDROID__ - {- Android does not support file:// urls, but neither is - - the security of the url in the process table important - - there, so just use the real url. -} - url = realurl -#else - url = fileUrl htmlshim -#endif - runbrowser = do hPutStrLn (fromMaybe stdout outh) $ "Launching web browser on " ++ url hFlush stdout environ <- cleanEnvironment diff --git a/Utility/Android.hs b/Utility/Android.hs new file mode 100644 index 0000000000..d602ee8df6 --- /dev/null +++ b/Utility/Android.hs @@ -0,0 +1,14 @@ +{- Android stuff + - + - Copyright 2018 Joey Hess + - + - License: BSD-2-clause + -} + +module Utility.Android where + +import Common + +-- Detect when the Linux build is running on Android, eg in termux. +osAndroid :: IO Bool +osAndroid = ("Android" == ) <$> readProcess "uname" ["-o"] diff --git a/doc/todo/termux_package/comment_3_a60b6e4511c1df23d76c6e6e8028eeaa._comment b/doc/todo/termux_package/comment_3_a60b6e4511c1df23d76c6e6e8028eeaa._comment index f491d6091b..d9cf86d262 100644 --- a/doc/todo/termux_package/comment_3_a60b6e4511c1df23d76c6e6e8028eeaa._comment +++ b/doc/todo/termux_package/comment_3_a60b6e4511c1df23d76c6e6e8028eeaa._comment @@ -21,11 +21,12 @@ The webapp is able to open an url (after I upgraded termux) via xdg-open chrome was not able to access, apparently a permissions problem. The webapp.html does not add any security on android, since it's not a multiuser unix system, and so it should open the url to the webapp -directly. +directly. (Done) Accessing the sdcard may need termux-setup-storage to be run once, depending on the version of android. That sets up $HOME/storage. The webapp ought to default to making a repository somewhere in there. +(Done) webbapp won't start on boot, but could be made to using https://wiki.termux.com/wiki/Termux:Boot diff --git a/git-annex.cabal b/git-annex.cabal index d2b2a06ffc..1fc2f6f811 100644 --- a/git-annex.cabal +++ b/git-annex.cabal @@ -1000,6 +1000,7 @@ Executable git-annex Upgrade.V4 Upgrade.V5 Utility.Aeson + Utility.Android Utility.Applicative Utility.AuthToken Utility.Base64