Skip to content

Instantly share code, notes, and snippets.

@khia khia/Caddyfile
Last active Aug 6, 2019

Embed
What would you like to do?
Tiddlywiki with Bob on android

Setup termux sshd

  1. Install termux from f-dropid

  2. Grant permissions to access storage Android Settings -> Apps -> Termux -> Permissions

  3. Setup home directory (this wouldn't work over ssh)

    termux-setup-storage
    pkg install termux-api
    
  4. Install ssh

    pkg install openssh
    
  5. set temporary password

    passwd
    
  6. start ssh server

    sshd
    
  7. copy ssh key to the phone

    ssh-copy-id -p 8022 -i ~/.ssh/id_rsa 192.168.1.7
    
  8. istall text editor so you can edit files

    pkg install micro
    
  9. disable remote access using password. By setting PasswordAuthentication no and commenting out Subsystem sftp

    micro $PREFIX/etc/ssh/sshd_config
    

    the file should look like the foloowing

    cat $PREFIX/etc/ssh/sshd_config
    PrintMotd no
    PasswordAuthentication no
    PubkeyAcceptedKeyTypes +ssh-dss
    #Subsystem sftp /data/data/com.termux/files/usr/libexec/sftp-server
    

Setup caddy

  1. install depdendencies
    pkg update
    pkg install golang git
    
  2. get source
    go get github.com/mholt/caddy/caddy
    
  3. get build tools
    go get github.com/caddyserver/builds
    
  4. move caddy binary to bin
    mv go/bin/caddy bin/
    

Configure runit

Simplified version of runit is available in termux. It is compiled in into a busybox app.

  1. Install daemonize package
    pkg install daemonize
    
  2. Add following into ~/.bash_profile
    
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi
    
    export SVDIR="$HOME/etc/sv"
    mkdir -p $PREFIX/var/run
    mkdir -p ~/.log
    daemonize -c $PREFIX \
      -l $PREFIX/var/run/runsvdir.lock \
      -p $PREFIX/var/run/runsvdir.pid \
      $PREFIX/bin/applets/runsvdir \
      $SVDIR 2>/dev/null||:
    

Setup tiddlywki

  1. Clone Tiddlywki repository

    mkdir -p /data/data/com.termux/files/home/storage/shared/Wiki
    cd /data/data/com.termux/files/home/storage/shared/Wiki
    git clone  https://github.com/Jermolene/TiddlyWiki5
    
  2. Install nodejs

    pkg install nodejs
    
  3. Install Bob

    git clone --depth=1 https://github.com/OokTech/TW5-Bob.git  /data/data/com.termux/files/home/storage/shared/Wiki/TiddlyWiki5/plugins/OokTech/Bob
    mkdir /data/data/com.termux/files/home/storage/shared/Wikis
    cp -r /data/data/com.termux/files/home/storage/shared/Wiki/TiddlyWiki5/plugins/OokTech/Bob/MultiUserWiki /data/data/com.termux/files/home/storage/shared/Wiki/Wikis/BobWiki    
    
  4. Configure Bob

    {
      "wikis": {
        "main": {
          "__path": "main"
        },
        "testing": {
          "__path": "testing"
        }
      },
      "scripts": {
        "NewWiki": "tiddlywiki #wikiName --init #editionName"
      },
      "ws-server": {
        "port": 8090,
        "host": "127.0.0.1",
        "autoIncrementPort": false,
        "pathprefix": "/wiki"
      },
      "heartbeat": {
        "interval": 1000
      },
      "wikiPathBase": "/data/data/com.termux/files/home/storage/shared/Wiki",
      "editionsPath": "./TiddlyWiki5/editions",
      "wikisPath": "./Wikis",
      "pluginsPath": "./plugins",
      "themesPath": "./themes",
      "API": {},
      "serverInfo": {
        "ipAddress": "10.1.10.1",
        "port": 8090,
        "host": "127.0.0.1"
      },
      "includePluginList": {},
      "excludePluginList": {}
    }
    
  5. Add host alias into /etc/hosts. We cannot use IP addresses due to this issue https://github.com/mholt/caddy/issues/2356

  • open AdAway app and add phone.local into your lists
  1. Create etc/Caddy file with following content.

    # entrypoint for accessing from PC
    phone.local:8080 { 
      tls /data/data/com.termux/files/home/etc/phone.crt /data/data/com.termux/files/home/etc/phone.key
      basicauth /wiki "username" password
      proxy /wiki 127.0.0.1:8090 {
        without /wiki
      }
    # entrypoint for accessing locally from phone
    127.0.0.1:8000 { 
      proxy /wiki 127.0.0.1:8090 {
        websocket
      }
    }
    
  2. Generate self signed certificate

    pkg install openssl-tool
    openssl req -new -x509 -newkey rsa:2048 -keyout etc/phone.key -out etc/phone.crt -days 10000 -nodes -subj /CN=phone.local
    openssl pkcs12 -export -out etc/phone.pfx -inkey etc/phone.key -in etc/phone.crt
    
  3. Add start scripts for runit

    cat etc/sv/caddy/run 
    #!/data/data/com.termux/files/usr/bin/sh
    exec /data/data/com.termux/files/home/bin/caddy -conf /data/data/com.termux/files/home/etc/Caddyfile 2>&1
    
    cat etc/sv/caddy/log/run 
    #!/data/data/com.termux/files/usr/bin/sh
    # Get the name of the service from the PWD, this assumes the name of the
    # service is one level above the log directory.
    pwd=${PWD%/*} # $SVDIR/service/foo/log
    service=${pwd##*/} # foo
    
    mkdir -p "$HOME/.log/sv/$service"
    
    exec svlogd -tt "$HOME/.log/sv/$service"
    
    cat etc/sv/wiki/run 
    #!/data/data/com.termux/files/usr/bin/sh
    WIKI_ROOT=/data/data/com.termux/files/home/storage/shared/Wiki
    export TIDDLYWIKI_PLUGIN_PATH=${WIKI_ROOT}/TiddlyWiki5/plugins 
    export TIDDLYWIKI_EDITION_PATH=${WIKI_ROOT}/TiddlyWiki5/editions
    exec node ${WIKI_ROOT}/TiddlyWiki5/tiddlywiki.js ${WIKI_ROOT}/Wikis/BobWiki/ --wsserver
    
    cat etc/sv/wiki/log/run 
    #!/data/data/com.termux/files/usr/bin/sh
    # Get the name of the service from the PWD, this assumes the name of the
    # service is one level above the log directory.
    pwd=${PWD%/*} # $SVDIR/service/foo/log
    service=${pwd##*/} # foo
    
    mkdir -p "$HOME/.log/sv/$service"
    
    exec svlogd -tt "$HOME/.log/sv/$service"
    
cat etc/sv/caddy/log/run
#!/data/data/com.termux/files/usr/bin/sh
# Get the name of the service from the PWD, this assumes the name of the
# service is one level above the log directory.
pwd=${PWD%/*} # $SVDIR/service/foo/log
service=${pwd##*/} # foo
mkdir -p "$HOME/.log/sv/$service"
exec svlogd -tt "$HOME/.log/sv/$service"
cat etc/sv/caddy/run
#!/data/data/com.termux/files/usr/bin/sh
exec /data/data/com.termux/files/home/bin/caddy -conf /data/data/com.termux/files/home/etc/Caddyfile 2>&1
cat etc/Caddyfile
# entrypoint for accessing from PC
phone.local:8080 {
tls /data/data/com.termux/files/home/etc/phone.crt /data/data/com.termux/files/home/etc/phone.key
basicauth /wiki "username" password
proxy /wiki 127.0.0.1:8090 {
websocket
}
}
# entrypoint for accessing locally from phone
127.0.0.1:8000 {
proxy /wiki 127.0.0.1:8090 {
websocket
}
}
cat storage/shared/Wiki/Wikis/BobWiki/settings/settings.json
{
"wikis": {
"main": {
"__path": "main"
},
"testing": {
"__path": "testing"
}
},
"scripts": {
"NewWiki": "tiddlywiki #wikiName --init #editionName"
},
"ws-server": {
"port": 8090,
"host": "127.0.0.1",
"autoIncrementPort": false,
"pathprefix": "/wiki"
},
"heartbeat": {
"interval": 1000
},
"wikiPathBase": "/data/data/com.termux/files/home/storage/shared/Wiki",
"editionsPath": "./TiddlyWiki5/editions",
"wikisPath": "./Wikis",
"pluginsPath": "./plugins",
"themesPath": "./themes",
"API": {},
"serverInfo": {
"ipAddress": "10.1.10.1",
"port": 8090,
"host": "127.0.0.1"
},
"includePluginList": {},
"excludePluginList": {}
}
cat etc/sv/wiki/log/run
#!/data/data/com.termux/files/usr/bin/sh
# Get the name of the service from the PWD, this assumes the name of the
# service is one level above the log directory.
pwd=${PWD%/*} # $SVDIR/service/foo/log
service=${pwd##*/} # foo
mkdir -p "$HOME/.log/sv/$service"
exec svlogd -tt "$HOME/.log/sv/$service"
cat etc/sv/wiki/run
#!/data/data/com.termux/files/usr/bin/sh
WIKI_ROOT=/data/data/com.termux/files/home/storage/shared/Wiki
export TIDDLYWIKI_PLUGIN_PATH=${WIKI_ROOT}/TiddlyWiki5/plugins
export TIDDLYWIKI_EDITION_PATH=${WIKI_ROOT}/TiddlyWiki5/editions
exec node ${WIKI_ROOT}/TiddlyWiki5/tiddlywiki.js ${WIKI_ROOT}/Wikis/BobWiki/ --wsserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.