網頁

2020年3月2日 星期一

git 學習筆記 3

git hash 可以只使用 6 個字元

HEAD
Branch
Tag (版本)
Remote

git log --oneline
git log -n3
git log -n3 hash

git log HEAD^^
一個 ^ 表示往前一個, 兩個^ 表示往前兩個
git log HEAD~8
~8 表示往前8個

git log --oneline file
專門針對一個 file

git log --stat file
git log --patch file

git log -S token
git log --patch -S token
有修改 token 的地方

git diff
比較 unstage 和 HEAD 的差異
git diff --staged
比較 staged 和 HEAD 的差異
git diff hash file
比較 unstage 和 hash 版本的差異
git diff hash1 hash2
比較兩次 commit 的差異

git add -p
...
Stage this hunk [y,n,q,a,d,s,e,?]?
q: 離開
a: 加入全部
y: 加入
n: 不加入
s: 分段
K: 上一段
e: 編輯
移除 -, 將 '-' 換成 ' '
移除 +, 將整行刪除

git commit --amend
隱藏舊的 commit, 用新的 commit 取代

git 學習筆記 2

git commit message 有一個 50/72 法則
第一行部要超過 50字
其餘不要超過 72字

1. Issue
2. Problem, 如何重現問題
3. Cause
4. Fix

.gitignore
參考 GitHub gitignore

Git 檔案的四種狀態
1.未追蹤(Untracked files):在版本提交後才又加進來的檔案,這些檔案並沒有被GIT所追蹤控管
2.已更改(Changes not staged for commit):已提交版本後,卻又再次修改,這些檔案會被丟回工作目錄(WD)
3.等待提交(Changes to be committed):在工作目錄(WD)的檔案執行git add後,會放在暫存區(Stage)。這些放在暫存區的檔案狀態便是等待提交囉!不論檔案是新增,修改,或刪除整個檔案,都是用 git add。
4.已提交(Committed):在暫存區(Stage)的檔案執行git commit後,檔案便置於儲存區(Repo),這些放在儲存區的檔案即是已提交的狀態。

git reset HEAD file
將 file 由 stage 返回 unstage
git reset --hard HEAD
連檔案也回復,即修改消失

git 學習筆記 1

參考書 Git - Book

mark@mark-z390-u:~/Data/git_test$ mkdir sample; cd sample
mark@mark-z390-u:~/Data/git_test/sample$ git init
Initialized empty Git repository in /home/mark/Data/git_test/sample/.git/
mark@mark-z390-u:~/Data/git_test/sample$ ls -al
total 12
drwxrwxr-x 3 mark mark 4096 Feb 29 10:06 .
drwxrwxr-x 3 mark mark 4096 Feb 29 10:05 ..
drwxrwxr-x 7 mark mark 4096 Feb 29 10:06 .git
mark@mark-z390-u:~/Data/git_test/sample$ vim README.md
mark@mark-z390-u:~/Data/git_test/sample$ git add README.md
mark@mark-z390-u:~/Data/git_test/sample$ git commit
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'mark@mark-z390-u.(none)')
mark@mark-z390-u:~/Data/git_test/sample$ git config --global user.email "mark@mark-z390-u"
mark@mark-z390-u:~/Data/git_test/sample$ git config --global user.name "mark"
mark@mark-z390-u:~/Data/git_test/sample$ git config --global core.editor vim
mark@mark-z390-u:~/Data/git_test/sample$ git config --global merge.tool vimdiff
mark@mark-z390-u:~/Data/git_test/sample$ git config --global color.ui true
mark@mark-z390-u:~/Data/git_test/sample$ git config --global alias.st status
mark@mark-z390-u:~/Data/git_test/sample$ git st
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

new file:   README.md

mark@mark-z390-u:~/Data/git_test/sample$ 
mark@mark-z390-u:~/Data/git_test/sample$ git config --list --show-origin
file:/home/mark/.gitconfig      user.email=mark@mark-z390-u
file:/home/mark/.gitconfig      user.name=mark
file:/home/mark/.gitconfig      core.editor=vim
file:/home/mark/.gitconfig      merge.tool=vimdiff
file:/home/mark/.gitconfig      color.ui=true
file:/home/mark/.gitconfig      alias.st=status
file:.git/config        core.repositoryformatversion=0
file:.git/config        core.filemode=true
file:.git/config        core.bare=false
file:.git/config        core.logallrefupdates=true
mark@mark-z390-u:~/Data/git_test/sample$ cat ~/.gitconfig 
[user]
email = mark@mark-z390-u
name = mark
[core]
editor = vim
[merge]
tool = vimdiff
[color]
ui = true
[alias]
st = status
mark@mark-z390-u:~/Data/git_test/sample$ 

2020年3月1日 星期日

vim 區塊編輯 和 多視窗

區塊編輯
<Ctrl>+v 進入區塊編輯
上下左右 選取區塊
I 編輯
c 替換
d 刪除
<Esc> 離開區塊編輯, 離開後才顯示


多視窗移動
<Ctrl>+w
h: 左移視窗
j: 下移視窗
k: 上移視窗
l: 又移視窗

2020年2月27日 星期四

https Client and Server

建立證書
參考 OpenSSL 證書

===
自我認證 CA
$ mkdir ca; cd ca
產生私鑰
$ mkdir private
$ openssl genrsa -out private/cakey.pem 2048
產生自簽章證書
$ cp /etc/ssl/openssl.cnf .
$ vi openssl.cnf
dir = .
default_md = sha512
keyUsage = cRLSign, keyCertSign
$ openssl req -new -x509 -nodes -key private/cakey.pem -out cacert.pem \
-days 3650 -subj "/C=TW/ST=Taiwan/L=Taichung/O=SDL/OU=R&D/CN=z390-CA" \
-config openssl.cnf
顯示證書
$ openssl x509 -text -noout -in cacert.pem

===
產生網站的證書
$ mkdir web1; cd web1
產生私鑰
$ openssl genrsa -out server.key 2048
$ vi ssl.conf
[ req ]
prompt = no
default_md = sha512
default_bits = 2048
distinguished_name = dn
req_extensions = v3_req

[ dn ]
C = TW
ST = Taiwan
L = Taichung
O = SDL
OU = R&D
emailAddress = mark@localhost
CN = mark-z390-u

[ v3_req ]
basicConstraints = CA:FALSE
subjectAltName = @alt_names
subjectKeyIdentifier = hash
keyUsage = nonRepudiation, digitalSignature, keyEncipherment

[ alt_names ]
DNS.1 = localhost
DNS.2 = mark-z390-u
DNS.3 = dixx.vigorddns.com
DNS.4 = 192.168.0.101
IP.1 = 127.0.0.1
IP.2 = 192.168.0.101
$ openssl req -new -sha512 -key server.key -out server.csr -config ssl.conf
顯示需求證書
$ openssl req -text -noout -in server.csr
顯示私鑰
$ openssl rsa -out -noout -in server.key
顯示公鑰
$ openssl rsa -in server.key -pubout -out server_pub.key
$ openssl rsa -in server_pub.key -pubin -noout -text

===
使用 自我認證 CA 簽署 需求證書
回到 ca 目錄
$ mkdir newcerts
$ touch index.txt
$ echo "01">serial
$ openssl ca -in ../web1/server.csr -out ../web1/server.crt -days 3650 -extennsions v3_req -extfile ../web1/ssl.conf -config openssl.cnf

===
匯入憑證到 Windows 10
cacert.pem 改名為 cacert.crt

本機裝置 的憑證 certlm.msc
目前的使用者憑證 certmgr.msc
受信任的根憑證授權單位/憑證
按滑鼠右鍵/所有工作/匯入 選擇 cacert.crt

金鑰使用方法
Certificate Signing, Off-line CRL Signing, CRL Signing (06)

===
參考 OCSP & CRL 介紹
CRL(Certificate Revocation List) 被 CA 撤銷的憑證清單
OCSP(Online Certificate Status Protocal) 線上查詢憑證狀態

參考根憑證
AAA Certificate Services
CRL 發佈點
CRL Distribution Point
URL=http://crl.comodoca.com/AAACertificateServices.crl



參考 Visual Studio(VS2017)編譯並配置C/C++-libcurl開發環境
從 https://curl.haxx.se/download.html 下載 curl-7.68.0.zip
解壓縮後進入 curl 目錄
執行 buildconf.bat
以 x86 為例(64為原則改為 x64)
開始/Visual Studio 2017/x86 Native Tools Command Prompt for VS 2017
按滑鼠右鍵選擇 Run as administrator
進入 curl/winbuild
nmake /f Makefile.vc mode=static VC=15 MACHINE=x86 DEBUG=yes
nmake /f Makefile.vc mode=static VC=15 MACHINE=x86 DEBUG=no
編譯的結果在 builds 下


Visual Studio 使用 libcurl
Configuration Properties/C/C++/Preprocessor/Preprocessor Definitions 加入 CURL_STATICLIB
#include <curl/curl.h>
加入下列 library
libcurl_a.lib(libcurl_a_debug.lib)
Ws2_32.lib
Wldap32.lib
winmm.lib(似乎不用)
Crypt32.lib
Normaliz.lib

form 使用
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
json 使用
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strU8);
curl_easy_setopt(curl, CURLOPT_POST, 1);

libcurl 之傳送接收使用  UTF-8, 函數之參數使用 Big5
在 vc 上使用 CStringA 儲存 Big5 和 UTF-8, 轉換要經過 CStringW (Unicode)

curl 之 --tlsv1.3 等於
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_3);

curl 之 --insecure 等於
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
不使用 --insecure 等於
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2); // 0 不檢查 host
curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem");

curl 之 --ssl-no-revoke 等於
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);



使用 curl 測試
===
在 windows 下,引號不能使用'要使用"
--insecure 不檢查證書
===
D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/Json -X
POST -H "Content-Type:application/json" -d "{\"updDate\":\"aaa\", \"camera\":\"b
bb\", \"ipaddress\":\"ccc\", \"addr\":\"ddd\"}" --insecure
*   Trying 114.35.104.33:5000...
* TCP_NODELAY set
* Connected to dixx.vigorddns.com (114.35.104.33) port 5000 (#0)
* schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x800903
08) - 提供給功能的權杖不正確
* Closing connection 0
* schannel: shutting down SSL/TLS connection with dixx.vigorddns.com por
t 5000
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN
(0x80090308) - 提供給功能的權杖不正確

===
Windows 7 要加上 --tlsv1.3
Windows 10 不用
===
D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/Json -X
POST -H "Content-Type:application/json" -d "{\"updDate\":\"aaa\", \"camera\":\"b
bb\", \"ipaddress\":\"ccc\", \"addr\":\"ddd\"}" --insecure --tlsv1.3


D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/Json -X
POST -H "Content-Type:application/json" -d "{\"updDate\":\"aaa\", \"camera\":\"b
bb\", \"ipaddress\":\"ccc\", \"addr\":\"ddd\"}" --tlsv1.3
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 114.35.104.33:5000...
* TCP_NODELAY set
* Connected to dixx.vigorddns.com (114.35.104.33) port 5000 (#0)
* schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) -
撤銷功能無法檢查憑證的撤銷。
* Closing connection 0
* schannel: shutting down SSL/TLS connection with dixx.vigorddns.com por
t 5000
curl: (35) schannel: next InitializeSecurityContext failed: Unknown error (0x800
92012) - 撤銷功能無法檢查憑證的撤銷。

===
要檢查證書,用參數 --cacert 傳入證書檔
===
D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/Json -X
POST -H "Content-Type:application/json" -d "{\"updDate\":\"aaa\", \"camera\":\"b
bb\", \"ipaddress\":\"ccc\", \"addr\":\"ddd\"}" --tlsv1.3 --cacert cacert.pem
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 114.35.104.33:5000...
* TCP_NODELAY set
* Connected to dixx.vigorddns.com (114.35.104.33) port 5000 (#0)
* schannel: added 1 certificate(s) from CA file 'cacert.pem'
* schannel: CertGetCertificateChain trust error CERT_TRUST_REVOCATION_STATUS_UNK
NOWN 0x00000040
* Closing connection 0
* schannel: shutting down SSL/TLS connection with dixx.vigorddns.com por
t 5000
curl: (60) schannel: CertGetCertificateChain trust error CERT_TRUST_REVOCATION_S
TATUS_UNKNOWN 0x00000040
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

===
因檢查證書時,會去檢查證書是否已經撤銷
--ssl-no-revoke 避免檢查
===
D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/Json -X
POST -H "Content-Type:application/json" -d "{\"updDate\":\"aaa\", \"camera\":\"b
bb\", \"ipaddress\":\"ccc\", \"addr\":\"ddd\"}" --tlsv1.3 --cacert cacert.pem --
ssl-no-revoke

===
使用 form
D:\temp\aaa>curl.exe -v https://dixx.vigorddns.com:5000/api/SendForm
-X POST -d "location=aaa&dt=bbb&text=ccc&camera_name=eee&X=x&Y=y&plateId=ddd" --
tlsv1.3 --cacert cacert.pem --ssl-no-revoke

2020年2月1日 星期六

Scale for menu and title bars

參考下列設定
udev匹配規則的編寫
How to automatically mirror screens when an HDMI cable is plugged in
How to reload udev rules without reboot?

Run script on screen lock/unlock


因為4K的解析度太高,導致字形按鈕等物件太小,可以直接修改下列設定
System Settings/Displays/Scale for menu and title bars
但是開關螢幕和結束螢幕保護時,會自動恢復設定

nvidia@nvidia-desktop:~$ udevadm monitor --environment --udev
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

拔起 HDMI, 出現
UDEV  [38201.950691] change   /devices/virtual/switch/hdmi (switch)
ACTION=change
DEVPATH=/devices/virtual/switch/hdmi
SEQNUM=6034
SUBSYSTEM=switch
SWITCH_NAME=hdmi
SWITCH_STATE=0
USEC_INITIALIZED=14008647
net.ifnames=0

插入 HDMI, 出現
UDEV  [38204.241016] change   /devices/virtual/switch/hdmi (switch)
ACTION=change
DEVPATH=/devices/virtual/switch/hdmi
SEQNUM=6039
SUBSYSTEM=switch
SWITCH_NAME=hdmi
SWITCH_STATE=1
USEC_INITIALIZED=14008647
net.ifnames=0

nvidia@nvidia-desktop:~$ udevadm info -a -p /devices/virtual/switch/hdmi

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/virtual/switch/hdmi':
    KERNEL=="hdmi"
    SUBSYSTEM=="switch"
    DRIVER==""
    ATTR{name}=="hdmi"
    ATTR{state}=="1"
    ATTR{uevent_in_suspend}=="Y"


HDMI 接頭插入時執行
nvidia@nvidia-desktop:~$ cat /etc/udev/rules.d/95-hdmi-hotplug.rules
SUBSYSTEM!="switch", GOTO="hdmi_end"
KERNEL!="hdmi", GOTO="hdmi_end"

ATTRS{state}=="1", RUN+="/bin/bash /home/nvidia/hdmi_on.sh"
ATTRS{state}=="0", RUN+="/bin/bash /home/nvidia/hdmi_off.sh"

LABEL="hdmi_end"
nvidia@nvidia-desktop:~$ cat /home/nvidia/hdmi_on.sh
sleep 1
# 需要切換不同身份執行
su -c '/home/nvidia/scale_factor.sh' - nvidia
nvidia@nvidia-desktop:~$ cat /home/nvidia/hdmi_off.sh
#echo "`date` off">>/home/nvidia/hdmi.log
nvidia@nvidia-desktop:~$ cat scale_factor.sh
/usr/bin/gsettings set com.ubuntu.user-interface scale-factor "{'HDMI-0': 16}"
nvidia@nvidia-desktop:~$

編寫完成 udev rule 時, 要重新載入
nvidia@nvidia-desktop:~$ sudo udevadm control --reload-rules && udevadm trigger
[sudo] password for nvidia:
nvidia@nvidia-desktop:~$


螢幕解鎖時執行
nvidia@nvidia-desktop:~$ cat screen_unlock.sh 
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean false"*)
/bin/bash /home/nvidia/scale_factor.sh
;;
esac
done
nvidia@nvidia-desktop:~$ 

2020年1月21日 星期二

DVR 之 rtsp 協定

昇銳DVR N9000
rtsp://username:password@ip:port/chID=0&streamType=main&linkType=tcp
chID 頻道
streamType 可以為 main 或 sub