分類
Linux

使用Termux搭建假wifi熱點

注:本文旨在學習研究wifi熱點相關知識,請勿用於任何違反道德之事。

需要一台Root過的Android(系統版本不低於5.0)手機,安裝好Termux和python。實現Captive Portal需要兩部分,請求重定向和授權頁面。重定向的實現有多種,這裡既然是假熱點,所以使用最簡單的iptables重定向dns。授權頁面則直接採用jczic/MicroWebSrv實現的網頁服務器。把MicroWebSrv解壓到~/scripts/MicroWeb,然後新建如下文件

#~/scripts/MicroWeb/forward.sh
remove127(){
  /system/bin/iptables -t nat -D PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.43.1:8000&&/system/bin/iptables -t nat -D PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.43.1:8000
}
add127(){
  /system/bin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.43.1:8000&&/system/bin/iptables -t nat -I PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.43.1:8000
}
if [[ $1 = "1" ]]; then
  echo "start"       
  add127
elif [[ $1 = "0" ]]; then
  echo "stop"
  remove127
fi
#~/scripts/MicroWeb/web.py
from microWebSrv import MicroWebSrv
mws = MicroWebSrv(port=8000,bindIP='0.0.0.0',webPath="/data/data/com.termux/files/home/scripts/MicroWeb/www")
mws.SetNotFoundPageUrl("http://192.168.43.1:8000/hotspot-detect.html")
mws.Start(False)
#~/scripts/MicroWeb/www/hotspot-detect.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  <title>Captive Portal</title>
  <script>
  /*!
 * clipboard.js v2.0.0
 * https://zenorocha.github.io/clipboard.js
 * 
 * Licensed MIT © Zeno Rocha
 */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ClipboardJS=e():t.ClipboardJS=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=3)}([function(t,e,n){var o,r,i;!function(a,c){r=[t,n(7)],o=c,void 0!==(i="function"==typeof o?o.apply(e,r):o)&&(t.exports=i)}(0,function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var o=function(t){return t&&t.__esModule?t:{default:t}}(e),r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=function(){function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,n,o){return n&&t(e.prototype,n),o&&t(e,o),e}}(),a=function(){function t(e){n(this,t),this.resolveOptions(e),this.initSelection()}return i(t,[{key:"resolveOptions",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.action=t.action,this.container=t.container,this.emitter=t.emitter,this.target=t.target,this.text=t.text,this.trigger=t.trigger,this.selectedText=""}},{key:"initSelection",value:function(){this.text?this.selectFake():this.target&&this.selectTarget()}},{key:"selectFake",value:function(){var t=this,e="rtl"==document.documentElement.getAttribute("dir");this.removeFake(),this.fakeHandlerCallback=function(){return t.removeFake()},this.fakeHandler=this.container.addEventListener("click",this.fakeHandlerCallback)||!0,this.fakeElem=document.createElement("textarea"),this.fakeElem.style.fontSize="12pt",this.fakeElem.style.border="0",this.fakeElem.style.padding="0",this.fakeElem.style.margin="0",this.fakeElem.style.position="absolute",this.fakeElem.style[e?"right":"left"]="-9999px";var n=window.pageYOffset||document.documentElement.scrollTop;this.fakeElem.style.top=n+"px",this.fakeElem.setAttribute("readonly",""),this.fakeElem.value=this.text,this.container.appendChild(this.fakeElem),this.selectedText=(0,o.default)(this.fakeElem),this.copyText()}},{key:"removeFake",value:function(){this.fakeHandler&&(this.container.removeEventListener("click",this.fakeHandlerCallback),this.fakeHandler=null,this.fakeHandlerCallback=null),this.fakeElem&&(this.container.removeChild(this.fakeElem),this.fakeElem=null)}},{key:"selectTarget",value:function(){this.selectedText=(0,o.default)(this.target),this.copyText()}},{key:"copyText",value:function(){var t=void 0;try{t=document.execCommand(this.action)}catch(e){t=!1}this.handleResult(t)}},{key:"handleResult",value:function(t){this.emitter.emit(t?"success":"error",{action:this.action,text:this.selectedText,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)})}},{key:"clearSelection",value:function(){this.trigger&&this.trigger.focus(),window.getSelection().removeAllRanges()}},{key:"destroy",value:function(){this.removeFake()}},{key:"action",set:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"copy";if(this._action=t,"copy"!==this._action&&"cut"!==this._action)throw new Error('Invalid "action" value, use either "copy" or "cut"')},get:function(){return this._action}},{key:"target",set:function(t){if(void 0!==t){if(!t||"object"!==(void 0===t?"undefined":r(t))||1!==t.nodeType)throw new Error('Invalid "target" value, use a valid Element');if("copy"===this.action&&t.hasAttribute("disabled"))throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');if("cut"===this.action&&(t.hasAttribute("readonly")||t.hasAttribute("disabled")))throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');this._target=t}},get:function(){return this._target}}]),t}();t.exports=a})},function(t,e,n){function o(t,e,n){if(!t&&!e&&!n)throw new Error("Missing required arguments");if(!c.string(e))throw new TypeError("Second argument must be a String");if(!c.fn(n))throw new TypeError("Third argument must be a Function");if(c.node(t))return r(t,e,n);if(c.nodeList(t))return i(t,e,n);if(c.string(t))return a(t,e,n);throw new TypeError("First argument must be a String, HTMLElement, HTMLCollection, or NodeList")}function r(t,e,n){return t.addEventListener(e,n),{destroy:function(){t.removeEventListener(e,n)}}}function i(t,e,n){return Array.prototype.forEach.call(t,function(t){t.addEventListener(e,n)}),{destroy:function(){Array.prototype.forEach.call(t,function(t){t.removeEventListener(e,n)})}}}function a(t,e,n){return u(document.body,t,e,n)}var c=n(6),u=n(5);t.exports=o},function(t,e){function n(){}n.prototype={on:function(t,e,n){var o=this.e||(this.e={});return(o[t]||(o[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){function o(){r.off(t,o),e.apply(n,arguments)}var r=this;return o._=e,this.on(t,o,n)},emit:function(t){var e=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),o=0,r=n.length;for(o;o<r;o++)n[o].fn.apply(n[o].ctx,e);return this},off:function(t,e){var n=this.e||(this.e={}),o=n[t],r=[];if(o&&e)for(var i=0,a=o.length;i<a;i++)o[i].fn!==e&&o[i].fn._!==e&&r.push(o[i]);return r.length?n[t]=r:delete n[t],this}},t.exports=n},function(t,e,n){var o,r,i;!function(a,c){r=[t,n(0),n(2),n(1)],o=c,void 0!==(i="function"==typeof o?o.apply(e,r):o)&&(t.exports=i)}(0,function(t,e,n,o){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function c(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function u(t,e){var n="data-clipboard-"+t;if(e.hasAttribute(n))return e.getAttribute(n)}var l=r(e),s=r(n),f=r(o),d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},h=function(){function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,n,o){return n&&t(e.prototype,n),o&&t(e,o),e}}(),p=function(t){function e(t,n){i(this,e);var o=a(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return o.resolveOptions(n),o.listenClick(t),o}return c(e,t),h(e,[{key:"resolveOptions",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.action="function"==typeof t.action?t.action:this.defaultAction,this.target="function"==typeof t.target?t.target:this.defaultTarget,this.text="function"==typeof t.text?t.text:this.defaultText,this.container="object"===d(t.container)?t.container:document.body}},{key:"listenClick",value:function(t){var e=this;this.listener=(0,f.default)(t,"click",function(t){return e.onClick(t)})}},{key:"onClick",value:function(t){var e=t.delegateTarget||t.currentTarget;this.clipboardAction&&(this.clipboardAction=null),this.clipboardAction=new l.default({action:this.action(e),target:this.target(e),text:this.text(e),container:this.container,trigger:e,emitter:this})}},{key:"defaultAction",value:function(t){return u("action",t)}},{key:"defaultTarget",value:function(t){var e=u("target",t);if(e)return document.querySelector(e)}},{key:"defaultText",value:function(t){return u("text",t)}},{key:"destroy",value:function(){this.listener.destroy(),this.clipboardAction&&(this.clipboardAction.destroy(),this.clipboardAction=null)}}],[{key:"isSupported",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:["copy","cut"],e="string"==typeof t?[t]:t,n=!!document.queryCommandSupported;return e.forEach(function(t){n=n&&!!document.queryCommandSupported(t)}),n}}]),e}(s.default);t.exports=p})},function(t,e){function n(t,e){for(;t&&t.nodeType!==o;){if("function"==typeof t.matches&&t.matches(e))return t;t=t.parentNode}}var o=9;if("undefined"!=typeof Element&&!Element.prototype.matches){var r=Element.prototype;r.matches=r.matchesSelector||r.mozMatchesSelector||r.msMatchesSelector||r.oMatchesSelector||r.webkitMatchesSelector}t.exports=n},function(t,e,n){function o(t,e,n,o,r){var a=i.apply(this,arguments);return t.addEventListener(n,a,r),{destroy:function(){t.removeEventListener(n,a,r)}}}function r(t,e,n,r,i){return"function"==typeof t.addEventListener?o.apply(null,arguments):"function"==typeof n?o.bind(null,document).apply(null,arguments):("string"==typeof t&&(t=document.querySelectorAll(t)),Array.prototype.map.call(t,function(t){return o(t,e,n,r,i)}))}function i(t,e,n,o){return function(n){n.delegateTarget=a(n.target,e),n.delegateTarget&&o.call(t,n)}}var a=n(4);t.exports=r},function(t,e){e.node=function(t){return void 0!==t&&t instanceof HTMLElement&&1===t.nodeType},e.nodeList=function(t){var n=Object.prototype.toString.call(t);return void 0!==t&&("[object NodeList]"===n||"[object HTMLCollection]"===n)&&"length"in t&&(0===t.length||e.node(t[0]))},e.string=function(t){return"string"==typeof t||t instanceof String},e.fn=function(t){return"[object Function]"===Object.prototype.toString.call(t)}},function(t,e){function n(t){var e;if("SELECT"===t.nodeName)t.focus(),e=t.value;else if("INPUT"===t.nodeName||"TEXTAREA"===t.nodeName){var n=t.hasAttribute("readonly");n||t.setAttribute("readonly",""),t.select(),t.setSelectionRange(0,t.value.length),n||t.removeAttribute("readonly"),e=t.value}else{t.hasAttribute("contenteditable")&&t.focus();var o=window.getSelection(),r=document.createRange();r.selectNodeContents(t),o.removeAllRanges(),o.addRange(r),e=o.toString()}return e}t.exports=n}])});
  </script>
    <style>
        .wrapper {
            text-align: center;
        }

        .button {
            position: absolute;
            top: 50%;
            margin-top:2em;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <button class="btn" id="clickButton" data-clipboard-text="just kidding :)">
            Connect to the Internet
        </button>
    </div>

    <script>
        var clipboard = new ClipboardJS('.btn');    
        clipboard.on('success', function(e) {
            console.info('Action:', e.action);
            console.info('Text:', e.text);
            console.info('Trigger:', e.trigger);

            e.clearSelection();
        });

        clipboard.on('error', function(e) {
            console.error('Action:', e.action);
            console.error('Trigger:', e.trigger);
        });  
    </script>
</body>
</html>

上面文件唯一需要修改的地方是你手機的wifi網關,可以在打開手機熱點後通過ip addr命令查看,我的是wlan0網卡,網關是192.168.43.1。

#iptable需要root權限
su
./forward.sh 1
#然后新开一个session運行web server
python web.py
#這時其他手機連上此熱點,即會彈出自定義頁面hotspot-detect.html。

本文更新於 2018/11/02。

分類
Linux 软件

AFWall+ 安卓可用的iptables

安卓手機如果root了,便可以通過AFWall+Github項目地址)來使用iptables管控網絡。

在安卓上使用DNSCrypt

由於TCP旁路阻斷技術的應用,DNSCrypt已經不能為日常使用網絡帶來太多方便,所以僅僅是記錄一下使用方法而已。先安裝好AFWall+並根據自己的喜好定製軟件的聯網規則,然後啟用iptables。我們使用Termux來至執行iptabls命令。根據自己的手機系統下載dnscrypt-proxy,比如我的就是dnscrypt-proxy-android_arm64-2.0.17.zip。如果不知道自己的手機系統,可以運行uname -a,如果出現aarch64就跟我一樣下載android_arm64版本就行。下載後解壓到/data/data/com.termux/files/home/opt/dns目錄里。複製一份文件夾里的配置文件即cp example-dnscrypt-proxy.toml dnscrypt-proxy.toml就可以了,配置也可以參考Fedora使用DNSCrypt。下面的文件我是放在家目錄下的,文件名是dns.sh,記得加上執行權限。

remove127(){
  /system/bin/iptables -t nat -D OUTPUT -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:53&&/system/bin/iptables -t nat -D OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:53&&/system/bin/iptables -t nat -D OUTPUT -d 114.114.114.114 -p tcp --dport 53 -j DNAT --to-destination 114.114.114.114:53&&/system/bin/iptables -t nat -D OUTPUT -d 114.114.114.114 -p udp --dport 53 -j DNAT --to-destination 114.114.114.114:53
}
add127(){
  /system/bin/iptables -t nat -I OUTPUT -d 114.114.114.114 -p tcp --dport 53 -j DNAT --to-destination 114.114.114.114:53&&/system/bin/iptables -t nat -I OUTPUT -d 114.114.114.114 -p udp --dport 53 -j DNAT --to-destination 114.114.114.114:53&&/system/bin/iptables -t nat -A OUTPUT -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:53&&/system/bin/iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:53
}
startDns(){
  /data/data/com.termux/files/home/opt/dns/dnscrypt-proxy -config /data/data/com.termux/files/home/opt/dns/dnscrypt-proxy.toml > /dev/null 2>1 &
  echo "starting dns"
  sleep 15
}
stopDns(){
  pkill dnscrypt-proxy
  echo "killed dns"
}
test(){
  var1=$(su -c "/system/bin/iptables -C OUTPUT -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:53")
}
status(){
  vt1="$(ps -ef | grep dnscrypt-proxy | wc -l)"
  if [[ $vt1 = "2" ]]; then
    dnsproxyStatus="1"
  else
    dnsproxyStatus="0"
  fi
  vt2="$(/system/bin/iptables -t nat -L | grep 127.0.0.1:53 | wc -l)"
  if [[ $vt2 = "2" ]]; then
    iptablesStatus="1"
  else
    iptablesStatus="0"
  fi
}
if [[ $1 = "1" ]]; then
  echo "start"      
  remove127              
  sleep 5                
  startDns       
  add127
elif [[ $1 = "2" ]]; then
  echo "restart"        
  remove127
  stopDns         
  sleep 3           
  startDns               
  add127 
elif [[ $1 = "0" ]]; then
  echo "stop"
  remove127
  stopDns
elif [[ $1 = "s" ]]; then
  status
  echo "dns: $dnsproxyStatus | iptables: $iptablesStatus"
else
  status
  if [[ $dnsproxyStatus = "1" ]]; then
    echo "$(date) dns ok" >> /data/data/com.termux/files/home/log.log
  else
    remove127
    stopDns
    remove127
    sleep 5
    startDns
    add127
    echo "$(date) dns started" >> /data/data/com.termux/files/home/log.log
  fi
  
fi
#使用說明
#首先切換的超級用戶
su
#查看當前狀態,1代表開啟,0代表關閉
./dns.sh s
#關閉dnscrypt-proxy
./dns.sh 0
#開啟dnscrypt-proxy
./dns.sh 1
#重新啟動dnscrypt-proxy
./dns.sh 2

啟動完成後可以通過dnsleaktest.com來查看當前的本機的dns。本來腳本還做了自啟動的適配,但是無論是Termux的自動啟動還是Magisk的自動啟動,都不太好用就算了。

本文更新於 2019/08/08。

分類
Linux 软件

在centos6上裝Synapse

裝了Matrix家的Synapse就可以使用Riot.im聊天了,好處是服務器是自己的,客戶端是開源的。

首先跟着官方文檔,安裝依賴。此時需要注意centos6自帶的sqlite版本太低,而且插件FTS4也未啟用。參考Install Python and Sqlite from Source安裝sqlite和python2.7即可。

#安裝sqlite3.12並開啟FTS4
mkdir -p ~/tmp/compile&&mkdir ~/tmp/opt&&cd ~/tmp/compile
wget https://www.sqlite.org/2016/sqlite-autoconf-3120200.tar.gz
tar xf ./sqlite-autoconf-3120200.tar.gz
cd sqlite-autoconf-3120200
./configure --prefix=~/opt/sqlite/sqlite3 --disable-static --enable-fts5 --enable-json1 CFLAGS="-g -O2 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_RTREE=1"
make
make install
mkdir ~/bin
ln -s ~/opt/sqlite/sqlite3/bin/sqlite3 ~/bin/sqlite
#到這裡就安裝好了,執行sqlite即可看到版本為3.12
#執行pragma compile_options;可見FTS4已開啟
#.quit退出sqlite
#安裝使用sqlite3.12的python2.7
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
xz -d Python-2.7.14.tar.xz&&tar -xvf Python-2.7.14.tar
cd Python-2.7.14
LD_RUN_PATH=$HOME/opt/sqlite/sqlite3/lib ./configure LDFLAGS="-L$HOME/opt/sqlite/sqlite3/lib" CPPFLAGS="-I $HOME/opt/sqlite/sqlite3/include"
LD_RUN_PATH=$HOME/opt/sqlite/sqlite3/lib make
LD_RUN_PATH=$HOME/opt/sqlite/sqlite3/lib make install

使用virtualenv生成一個Synapse專用的虛擬環境。安裝Synapse時還會遇到Twisted版本過低,在虛擬環境下這樣操作一下:

wget https://twistedmatrix.com/Releases/Twisted/17.1/Twisted-17.1.0.tar.bz2
tar -jxvf Twisted-17.1.0.tar.bz2
cd Twisted-17.1.0
python setup.py install 
cd ..

配置nginx前置代理的時候,用letsencrypt的centbot各種失敗,最後還是用回ssl for free的笨方法。推薦使用letsencrypt的centbot,配好後非常省時省力。順便貼一下nginx配置:

server {
    listen 443 ssl;
    server_name YOURDOMIN;

    ssl_certificate     /etc/letsencrypt/live/YOURDOMIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/YOURDOMIN/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location /_matrix {
        proxy_pass http://localhost:8008;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

由於使用了nginx做代理,所以8008端口的監聽範圍可以在homeserver.yaml中改成127.0.0.1。

最後跟着官方教程增加用戶,然後在Riot.im里勾上自定義服務器就可以聊天了。也可以參考下Run your end-to-end encrypted chat server using Matrix and Riot

如果傳送文件時提示文件大小超過限制,多半是nginx給擋下了,可以在nginx配置文件的http段里添加下面兩行來解決:

    sendfile        on;
    client_max_body_size 40M;

Android我從F-droid下載的客戶端,沒有gcm,所以在後台時檢查消息的頻率可以自行設置,我設置的5分鐘,默認10分鐘。網頁端打開的時候有一點點慢,打開後就沒問題了。Fedora可以安裝taw/Riot的源,即

sudo dnf copr enable taw/Riot 
sudo dnf install -y riot --refresh

Fedora29的包taw還沒打,我們可以自己打:

sudo dnf install npm
git clone https://github.com/vector-im/riot-web.git
cd riot-web
npm install
cp config.sample.json config.json
#修改下面4處
    "default_hs_url": "https://YOURDOMIN",
    "default_is_url": "https://YOURDOMIN",
    "disable_guests": true,
    "features": {
        "feature_groups": "labs",
        "feature_pinning": "labs",
        "feature_rich_quoting": "labs",
        "feature_presence_management": "labs",
        "feature_sticker_messages": "labs",
        "feature_jitsi": "labs",
        "feature_tag_panel": "enable",
        "feature_lazyloading": "enable"
    }
#編譯
npm run build
#編譯獨立APP
npm install electron
#運行APP
npm run electron
#生成可執行文件
node_modules/.bin/build -l --x64
#可執行文件在electron_app/dist/linux-unpacked/

##升級##
cd riot-web
git fetch origin
git reset --hard origin/master
npm install
npm run build
npm install electron
node_modules/.bin/build -l --x64

Synapse搬家

官方文檔並未給出具體備份與還原方法,經過摸索發現非常簡單。如果是按照我上面步驟安裝的,只需再新服務器上搭建好環境,然後複製如下文件到虛擬環境中就可以啟動了。如果文件路徑有變化,則需要修改homeserver.yaml和YOURDOMIN.log.config這兩個配置文件。

homeserver.yaml
YOURDOMIN.log.config   
YOURDOMIN.signing.key  
YOURDOMIN.tls.crt
YOURDOMIN.tls.dh
YOURDOMIN.tls.key
media_store
homeserver.db

Synapse重置用戶密碼

參考:reset password for matrix/synapse accounts

Centos7上裝Synapse

大致一樣啦。sqlite3還是要自己安裝,系統帶的不行。python可以用3了。Twisted要裝17.9.0。

本文更新於 2018/12/26。

分類
Linux

Termux Python3.6安裝paramiko

之前寫過一個python腳本通過paramiko使用SSH鏈接越獄後的iPhone執行一些安裝軟件或重啟SpringBoard之類的操作。後來遇到Termux覺得安卓手機也可以試試,不試不知道,一試坑不少,好在最後還是裝好了。

#首先安裝系統的依賴
apt install libffi-dev clang libsodium libsodium-dev openssl-dev libcrypt-dev python-dev
#然後安裝pynacl,直接pip裝會報錯,指定使用系統的sodium庫即可
SODIUM_INSTALL=system
pip install pynacl
#最後再安裝paramiko和python-nmap
pip install paramiko python-nmap

本文更新於 2018/04/04。

分類
Linux

Fedora使用DNSCrypt

DNSCrypt是一種認證DNS客戶端和DNS解析器之間通信的協議。 它可以防止DNS欺騙。下面記錄下在Fedora25上使用DNSCrypt的步驟。

dnscrypt-proxy 2 是一個更棒的升級,如果你用的是v1,非常建議升級。官方的安裝文檔已經寫的很詳盡,這裡再簡單記錄一下。

#用超級用戶操作
su -s
#下載dnscrypt-proxy-linux_x86_64-2.0.25.tar.gz並解壓至/opt/dnscrypt-proxy/
#其實解壓到哪裡都無所謂啦
cd /opt/dnscrypt-proxy/
cp example-dnscrypt-proxy.toml dnscrypt-proxy.toml
#選擇性修改fallback dns如
fallback_resolver = '114.114.114.114:53'
#開啟分流文件
#曾經在不分流的情況下用了一年左右,奈何網絡持續惡化,現在不分流體驗會很差
#中國域名來自https://github.com/felixonmars/dnsmasq-china-list/blob/master/accelerated-domains.china.conf
#需要替換成dnscrypt-proxy的格式
forwarding_rules = '/home/fred/Programs/dnscrypt-proxy/forwarding-rules.txt'
./dnscrypt-proxy
#出現dnscrypt-proxy is ready即為成功,可以先Ctrl+C關閉
#执行下面命令使普通用戶也能運行dnscrypt-proxy
setcap cap_net_bind_service=+pe dnscrypt-proxy
#下面設置本機dns解析方法一:
cp /etc/resolv.conf /etc/resolv.conf.backup
nano /etc/resolv.conf
#文件內容如下
nameserver 127.0.0.1
options edns0 single-request-reopen
#下面設置本機dns解析方法二:
#右擊網絡連接圖標,選擇“編輯網絡連接”,選擇網絡,點齒輪圖標。
#在“IPv4設定”選項卡,Method選“只用自動(DHCP)位址”
#DNS servers填入127.0.0.1, 8.8.4.4,按“儲存”。
#右擊網絡連接圖標,反選啟用網絡,再勾選啟用網絡。
#設置完成,測試解析
./dnscrypt-proxy -resolve ft.shaman.eu.org
#如果返回了IP就說明OK

#自動啟動我都是用的xfce的啟動程序,位於
#應用程式選單/設定值/工作階段與初始啟動/應用程式自動啟動/加入
#指令框輸入/opt/dnscrypt-proxy/dnscrypt-proxy
#NetworkManager會在開機時生成重寫/etc/resolv.conf
#所以需要在NetworkManager的IPV4設定中指定127.0.0.1為dns服務器
#或者
#給resolv.conf上把鎖
chattr +i /etc/resolv.conf
#解鎖
chattr -i /etc/resolv.conf

如果想分享dnscrypt-proxy服務給局域網用戶,可以修改dnscrypt-proxy.toml中的監聽地址為['0.0.0.0:53']。然後開啟防火牆的53端口。

部分配置示例

#程序日誌
log_file = '/home/ft/Programs/dnscrypt-proxy/log.log'

#解析日誌
[query_log]
file = '/home/ft/Programs/dnscrypt-proxy/query.log'
ignored_qtypes = ['AAAA', 'DNSKEY', 'NS']

#域名黑名單
[blocked_names]
blocked_names_file = '/home/ft/Programs/dnscrypt-proxy/mybase_blocked-names.txt'
#此處使用的黑名單來自 https://download.dnscrypt.info/blacklists/domains/

#NextDNS 解析
[static]
  [static.'NextDNS-12abcd']
  stamp = 'sdns://AgEAAAAAAAAAAAAOZG5zLm5leHRkbnMuaW8HLzc3WiXnZO'
#使用 NextDNS 時還要設置 server_names
server_names = ['NextDNS-12abcd']

值得提醒的是,如果你使用 BT 下載,那麼很多黑名單是禁止 trackers 相關域名的,記得禁用這一條#tracker.*。自定義的 DNS 服務器可以使用 Online DNS Stamp calculator 來生成簽章。

不再實用的配置

安裝dnscrypt-proxy並新建一個執行用戶

sudo dnf install dnscrypt-proxy
sudo adduser -m -N  -r -s /bin/false dnscrypt

安裝完成後可以從/usr/share/dnscrypt-proxy/dnscrypt-resolvers.csv的第一列挑選一個服務商,我嘗試用香港的發現連接不上,於是選擇法國cs-fr。嘗試運行dnscrypt-proxy:

sudo dnscrypt-proxy -u dnscrypt -R cs-fr

我的桌面是Xfce,所以設置DNS很方便。找到程序/設定值/網路連線,選中需要編輯的網路點編輯。在IPv4設定面板中,如果你之前方法是“自動(DHCP)”,則變更方法為“只用自動(DHCP)位址”,並在DNS伺服器一欄填入127.0.0.1。如果之前就有固定IP,則只需要把DNS伺服器一欄改為127.0.0.1即可。多個DNS伺服器可以用半角逗號隔開。

如果沒有桌面環境,要設置DNS的話可以參考The adventurous can edit the appropriate script in /etc/sysconfig/network-scripts/. If you don't have NetworkManager installed, editing /etc/resolv.conf would work too.

重啟網絡服務已使剛剛新設置的DNS生效。可以使用dig驗證當前dns,出現SERVER: 127.0.0.1#53即說明成功。

sudo systemctl restart network.service
dig ft.wupo.info

設置自動重啟也很簡單,新建文件/etc/systemd/system/dnscrypt.service,內容如下:

[Unit]
Description=dnscrypt - Encrypted DNS service provided by OpenDNS
After=NetworkManager.service

[Service]
ExecStart=/usr/sbin/dnscrypt-proxy -u dnscrypt -R cs-fr
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=basic.target

重啟系統守護

sudo systemctl daemon-reload

添加dnscrypt到開啟啟動

sudo systemctl enable dnscrypt.service

當然你也可以手動通過systemctl命令來啟動和停止dnscrypt-proxy,像這樣:

sudo systemctl start dnscrypt.service

參考鏈接:Lucid Electric Dreams: Setting up dnscrypt on Fedora

本文更新於 2024/10/17。

分類
Linux

Fedora Workstation Tricks

較新版本:Fedora 39 Xfce tricks

Fedora32 Xfce

時間來到 2020 年,下面哪些操作很多都不需要了。Virtual Box 的擴展已開箱可用。我還是喜歡 Firefox ESR,所以會從官網去直接下載。然後解壓就能使用。關注隱私的朋友如果想要關閉 WebRTC 可以先在地址欄輸入 about:config 然後搜索 media.peerconnection.enabled 將其值改為 False 即可。如果要裝 vlc,還是要啟用 RPM Fusion。然後就可以愉快的使用了。

sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

給常用的命令起一個別名 alias,可以簡化日常的操作,對於 Fedora 可以把 alias 寫在 ~/.bashrc 文件裏。

alias p='proxychains4'
alias update='sudo dnf update'

#Fedora 的默認歷史記錄只有 1000 行
#下面兩行可將其改爲 5000 行
HISTSIZE=5000
HISTFILESIZE=5000

Virtual Box 5.2.20搭配Fedora29 Xfce

安裝完29版本後發現已經自動安裝好了虛擬機的擴展,但是共享文件夾功能是壞掉的,表現為vboxsf不識別,解決辦法如下:

#虛擬機菜單選“設備”/“安裝增強功能”
cd /run/media/fred/VBox_GAs_5.2.20/
sudo dnf install elfutils-libelf-devel kernel-devel kernel-devel-$(uname -r)
sudo ./VBoxLinuxAdditions.run
#重啟後再執行共享命令即可
sudo mount -t vboxsf share /mnt/share

#不安裝ffmpeg的話,火狐播放不了mp3和mp4
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
sudo dnf install ffmpeg

安裝 vnstat 來統計網絡使用情況。

sudo dnf install vnstat
#啓用統計
systemctl enable --now vnstat
#查看網絡統計
vnstat -d
vnstat -h

安裝 flameshot 一款更多功能的截圖工具。

sudo dnf install flameshot

安裝之後參考 flameshot 官方文檔綁定一下鍵盤上的 Print Screen 鍵,使用更方便。 XFCE 的話,打開設置里的鍵盤,切換到應用程式熱鍵,找到 xfce4-screenshooter -fd 1 - Print 的條目,把 xfce4-screenshooter -fd 1 改爲 flameshot gui 就可以了。

下面那堆Tricks基本都是不必要的,尤其是Flash……Xface面板自帶的網絡指示器也很好用。


以下內容有些久遠僅供參考

Fedora25 Win10雙系統

買了新電腦,裡面自帶兩個3.5英寸鍵盤位。於是一個硬盤裝了Fedora25一個裝了win10。先裝得Win10,這樣Fedora安裝的時候就會自動把Win10添加到啟動項中,很方便。

Fedora新機Tricks

shadowsocks可以參考shadowsocks client and firefox on Ubuntu 12以及shadowsocks的systemd開機啟動設置可以參考Host a Debian in Win7 VirtualBox

#增加軟件源Activate RPMFusion Repository
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
#安裝楷體和明體字體
sudo dnf install cjkuni-ukai-fonts cjkuni-uming-fonts
#常用軟件
nano uget nethogs vlc mozilla-vlc gimp VirtualBox
#Adobe Flash
rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux
dnf install flash-plugin alsa-plugins-pulseaudio libcurl
#Netspeed - 網速顯示控件
https://extensions.gnome.org/extension/104/netspeed/
#TaskBar - 任務欄(其實自帶也有,可以通過安裝GNOME Tweak Tool來配置)
https://extensions.gnome.org/extension/584/taskbar/
#強烈推薦安裝GNOME Tweak Tool

Fedora的坑

外接的USB無線網卡總是隨機斷線。其實wifi狀態還是連着的,但wifi標誌會變成問號,搜索後發現只要在無線設置里關閉IPV6就可以了。最後發現這個問題還是比較棘手的,搜了嘗試里很久也沒有搞定。

文件管理器的右鍵居然沒有“新增空白文件”,解決辦法是到自己的Templates(模板)文件夾下,新建一個空白文檔,右鍵就有新增文件了。

無法掛在windows的ntfs格式磁盤的時候,可以嘗試執行ntfsfix,我執行後即可正常掛在win7下的ntfs磁盤。注意此方法可能不安全。

Dell筆記本HDMI連電腦沒聲音,命令行執行pavucontrol,在配置選項卡中選HDMI即可,即使顯示未連接也沒關係。

後記

白天看到新聞說希捷關了蘇州的工廠,我還跟Emanon討論了一番,沒想到晚上回家Fedora便發出警告,說硬盤即將報廢。這塊1.5T的硬盤是08年生產的,到現在也將近9個年頭了,已經很不錯了。

最近在看機器學習的東西,發現各種工具對Ubuntu的支持還是最好,雖然有點捨不得Fedora,但晚上還是回歸到Ubuntu一段時間吧。

升級VirtualBox中的Fedora

可是太愛Xfce所以我還是在Fedora里玩。發現25已經不被支持了,於是決定升到26,升級很簡單:

sudo dnf upgrade --refresh
sudo dnf install dnf-plugin-system-upgrade
sudo dnf system-upgrade download --refresh --releasever=26
sudo dnf system-upgrade reboot

安裝完成後就是Fedora26了,VirtualBox出問題的話可以嘗試升級,然後更新虛擬機的擴展,如果還是不行,可以嘗試下面的命令載重裝擴展:

sudo dnf install VirtualBox-guest-additions kernel-devel-$(uname -r) 
sudo akmods 
sudo systemctl restart systemd-modules-load
sudo systemctl restart vboxservice

內核更新後一般需要更新 virtualbox 插件,為了方便可以排除內核更新,在 /etc/dnf/dnf.conf 增加一行:

exclude=kernel*

本文更新於 2024/04/02。

分類
Linux

服務器連接pptp

linux pptp client managment

需要從某網站頻繁獲取數據,但目標網站對IP限制嚴格,於是想到用vpn。市面上(淘寶)有種每次連接都會自動切換IP的PPTP VPN,很符合需求,下面就是怎麼用了,環境是CentOS6。

#安裝pptp
yum install pptp pptp-setup
#配置pptp
pptpsetup --create v --server vpn.server.address --username USERNAME --password PASSWORD
#淘寶的這種pptp vpn一般是不用加密設置的,萬一需要,可以在配置命令最後加--encrypt參數
#為了方便後續使用,我們複製兩個命令
cp /usr/share/doc/ppp-2.4.5/scripts/pon /usr/sbin/
cp /usr/share/doc/ppp-2.4.5/scripts/poff /usr/sbin/
chmod +x /usr/sbin/pon
chmod +x /usr/sbin/poff
#啟動pptp
pon v
#如果啟動成功路由表中應該會出現一個ppp0的設備
route -n
#如果沒出現可以查看那裡出了問題
tail -n 10 /var/log/messages | grep ppp
#這是如果把網關設置到ppp0所有流量就都走vpn了\
#但是啊,你將失去ssh連接,很恐怖吧\
#這時最好有服務器的網頁端供你連到內網進行操作\
#當然,重啟服務器也能恢復連接
#下面設置路由表
/sbin/route del -net default
/sbin/route add -net default dev ppp0
#此時所有流量就pptp了,通過下面命令查看
curl http://myip.dnsdynamic.org/
#執行需要pptp的ip完成的任務
#關閉vpn
poff
#理論上此時應該恢復之前的路由表
#但我偷了個懶,重啟了一下網絡
/etc/init.d/network restart
#從pon到重啟網絡,就完成了一個工作循環。

使用php腳本完成pptp的切換,其實就是php調用bash,兩點:運行用戶為root;命令最好寫相對路徑。

#!/usr/bin/php
<?php
class HttpClient{
    private $ch;

    function __construct($cookie_jar){
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0');
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 40);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_jar);
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_jar);
    }

    function __destruct(){
        curl_close($this->ch);
    }

    final public function setReferer($ref=''){
        if($ref != ''){
            curl_setopt($this->ch, CURLOPT_REFERER, $ref);
        }
    }

    final public function Get($url, $header=false, $nobody=false){
        curl_setopt($this->ch, CURLOPT_POST, false);
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HEADER, $header);
        curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);
        return curl_exec($this->ch);
    }

    final public function Post($url, $data=array(), $header=false, $nobody=false){
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HEADER, $header);
        curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($this->ch);
    }
}

function getNewIP(){
    $logIPFile="/home/logIP.txt";
    $res=array();
    exec("/usr/sbin/pon v", $res);
    sleep(3);
    exec("/sbin/route del -net default", $res);
    sleep(2);
    exec("/sbin/route", $res);
    sleep(6);
    exec("/sbin/route add -net default dev ppp0", $res);
//	var_dump($res);
    sleep(1);
//    需要一個cookie文件,創建一個空文件即可
    $http = new HttpClient('/home/dump.txt');
    $ip = $http->Get("http://myip.dnsdynamic.org/");
    sleep(1);
    file_put_contents($logIPFile, date("Y-m-d H:i:s", time())."_".$ip.PHP_EOL , FILE_APPEND | LOCK_EX);

    $ips=array();
    exec("tail -n 2 '$logIPFile'", $ips);
    $isNewIP=0;
    foreach( $res as $oldIP ){
        if(strpos($oldIP, $ip) != false){
           $isNewIP=$isNewIP+1;
       }
    }

    return $isNewIP; 
}

function restoreIP(){
    exec("/usr/sbin/poff",$res);
//	var_dump($res);
    sleep(2);
    exec("/etc/init.d/network restart");
}

//如果IP沒有重複兩次以上就執行任務
if( getNewIP($logIPFile) <2 ){
//    doYourOwnStaff();
} 
restoreIP();
exit();
?>

crontab的設置,這裡不用crontab -e,而是直接編輯/etc/crontab:

nano /etc/crontab
#指定運行命令的用戶為root
03 * * * * root /path/to/the/script

這樣就完工了,感謝CentOS 6下配置PPTP VPN客户端在 Linux 命令列進行 PPTP VPN 連線。在完全不了解linux路由表和iptables的情況下,我也只能做到如此了。其實理想狀態是只轉發http和https到pptp,有空再研究吧,新年快樂!


由於政策原因,動態pptp沒那麼好買了。於是年後換了一批靜態pptp,做法就是新建多個pptp配置,然後隨機取。

$vpns = array("v47", "v11", "v18", "v12");
$rand_keys = array_rand($vpns, 2);
$vpn = $vpns[$rand_keys[0]] ;

在設置過程中,遇到了unknown authentication type 26; Naking錯誤,解決辦去掉/etc/ppp/options.pptp文件中的require-mppe-128的注釋,並在修改配置文件為如下:

# written by pptpsetup
pty "pptp SERVER --nolaunchpppd"
lock
noauth
refuse-pap
refuse-eap
refuse-chap
refuse-mschap

nobsdcomp
nodeflate
require-mppe-128
name MYUSERNAME
remotename CONFNAME
ipparam CONFNAME

另一種可能的錯誤是連接的時候出現LCP: timeout sending Config-Requests,我是通過chkconfig iptables off關閉iptables解決的,反正我在內網。

上文中的/etc/init.d/network restart有時並不能恢復本機網絡,此時我們可以替換腳本中的這一句為

#網關改回原來的網關
subprocess.call('/sbin/route add default gw 172.16.2.1 netmask 0.0.0.0 dev eth0', shell=True)

linux l2tp client managment

時間來到2018年,pptp換成了l2tp,發現同樣的需求使用NetworkManager真是簡單。設置好l2tp連接後,一行命令即可打開關閉連接。

#passwd-file只需配置一行PSK
vpn.secrets.password:YOUR_PSK
#打開l2tp連接
subprocess.call('nmcli con up l2tp76 passwd-file /home/42/vpnpass.txt', shell=True)
#關閉l2tp連接
subprocess.call('nmcli con down l2tp76', shell=True)

本文更新於 2018/11/09。