Pages

2023年5月11日 星期四

【GCP 】Region HTTP TO HTTPS Load Balance 設定方式(台灣)

目的:在GCP上建立台灣版的HTTP轉HTTPS的負載平衡器

步驟:

1. 進入GCP Console

2. 點選右上角的【啟用Cloud Shell】

3. 點選右下角【開啟編輯器】

4. 點選【File】➔【New File】

5. 貼上以下內容(相關名稱與domain請自行修改)


kind: compute#urlMap
name: example-http-to-https
defaultUrlRedirect:
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  httpsRedirect: True
tests:
- description: Test with no query parameters
  host: example.com
  path: /
  expectedOutputUrl: https://example.com/
  expectedRedirectResponseCode: 301

6. 點選【File】➔【Save As...】➔ 選擇tmp目錄➔存檔名稱為:web-map-http.yaml

7. 點選右上角X ➔ 離開編輯器

8. 到【VPC Network】➔【IP Address】➔尋找HTTPS負載平衡器,建立的HTTPS IP名稱。

9. 點選右上角的【啟用Cloud Shell】

10. 執行以下指令(請依自行設定的區域、IP名稱自行修改)

gcloud compute url-maps import example-http-to-https --source /tmp/web-map-http.yaml --region=asia-east1
gcloud compute url-maps describe example-http-to-https --region=asia-east1
gcloud compute target-http-proxies create http-lb-proxy --url-map=example-http-to-https --region=asia-east1
gcloud compute forwarding-rules create http-content-rule \
           --load-balancing-scheme=EXTERNAL_MANAGED \
           --address=uat-example-https2 \
           --network-tier=STANDARD \
           --region=asia-east1 \
           --target-http-proxy=http-lb-proxy \
           --target-http-proxy-region=asia-east1 \
           --ports=80

11. 完成後,網域站台即可透過HTTP協定轉到HTTPS。

申請SSL證書遇到CAA紀錄問題

 情況描述:

1. 該網域之前在GCP上,有申請SSL證書,但想使用Region的Load Balance需要自已提供證書。

2. 在某網站購買SSL證書,但在認證的時候提示CAA紀錄異常。

3. 使用CAA Lookup Tool查看CAA紀錄,發現Google把CAA紀錄加入DNS。

4. 在DNS管理平台上,添加CAA紀錄,以cloudflare為例

一般 SSL CAA紀錄設定

Wildcard SSL CAA紀錄設定

      


5. 過60分鐘後,使用CAA Lookup Tool查看CAA紀錄,已有新添加的CAA紀錄,在進行Domain認證,就可以認證SSL成功。

2023年5月4日 星期四

mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS

 mysqldump命令:


  导出数据库:mysqldump -h ip -u root -p dbname > db.sql;

  导出数据库中的某个表:mysqldump -h ip -u root -p dbname tablename > tablename.sql;


错误提示:

  mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"') FROM information_schema.COLUMN_STATISTICS


原因:


  因为新版的mysqldump默认启用了一个新标志,通过 --column-statistics=0 来禁用他


解决方法:

  mysqldump --column-statistics=0 -h 192.168.0.1 -u root -p dbname > db.sql;

Java使用Tesseract進行OCR辨識的一些心得

最近有個專案想抓取PDF的檔案內容,進行檔案rename的動作,因此才開始瞭解Tesseract相關的用法,以下是使用的一些心得。

1. Windows上安裝Tesseract 5.0後,如果想轉移到其他台Windows PC,該PC要先安裝 Visual Studio 2015 的 Visual C++ 可轉散發套件,再將Tesseract安裝的目錄整個zip起來後,在其他台Windows PC上解壓縮後,設定Tesseract相關windows環境變數路徑後,Java程式就可以直接使用。


2. Tesseract程式中辨識資料的路徑與繁體中文語言設定如下:

tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\");
tesseract.setLanguage("chi_tra");


3. 不要透過抓取PDF文字後再辨識,因為會抓不到PDF內容文字,要先轉成圖檔(TIFF)後,在進行圖片辨識文字,會比較不會出錯,下面是範例的Code。

private boolean pdfToTiffConverter(String inputPdf, String outputTiff) {
  int dpi = 300; // the resolution of the output image in DPI
  boolean result = false;
  PDDocument document = null;
  PDFRenderer pdfRenderer = null;
  BufferedImage image = null;
  try {
  	File pdfFile = new File(inputPdf);
  	Thread.sleep(100); // wait 100ms for loading pdf to memory to avoid NullPointException 
  	document = PDDocument.load(pdfFile);
    pdfRenderer = new PDFRenderer(document);
    image = pdfRenderer.renderImageWithDPI(0, dpi); // render the first page of the PDF document
    ImageIO.write(image, "TIFF", new File(outputTiff)); // write the image to a TIFF file
    result = true;
    document.close();
  } catch (IOException e) {
  	logger.error(e.getStackTrace());
  	result = false;
  } catch (InterruptedException e) {
  	logger.error(e.getStackTrace());
  } finally {
  }
  return result;
}

private String tiffOcr(String outputTiff) {
  String tiffPath = outputTiff;
  Tesseract tesseract = new Tesseract();
  String result = "";
  try {
    tesseract.setDatapath("D:\\Tesseract-OCR\\tessdata\\"); // set the path to the Tesseract data directory
    tesseract.setLanguage("chi_tra");
    result = tesseract.doOCR(new File(tiffPath));
  } catch (TesseractException e) {
    logger.error(e.getStackTrace());
  } finally {
    File imageFile = new File(outputTiff);
    if(imageFile.exists()) imageFile.delete();
  }
  return result;
}

4. import的套件很多,可以參考下面圖片的套件版本。


5. 在Eclipse上運行正常,但匯出成可運行的JAR後,辨識時會出現以下錯誤。

javax.imageio.spi.ImageOutputStreamSpi: Provider com.sun.media.imageioimpl.stream.ChannelImageOutputStreamSpi could not be instantiated

解決方式:Eclipse匯出方式需要變更為【copy required libraries into sub-folder....】。


可運行的JAR在執行前,要先把sub-folder的目錄跟可運行的JAR放在同一層執行,這樣才能順利運行。

這個問題花了我5個小時才解決掉,希望對大家有助。




2023年4月29日 星期六

【PowerShell】查詢與修改AD人員屬性

 PowerShell是一套Windows上非常強大的工具,透過它也可以操作AD相關功能,。

 

 • 查詢AD人員
Get-ADUser -Filter * -SearchBase "CN=user1,OU=IT,OU=Test,DC=DevOps,DC=com"

• 修改人員屬性值
Set-ADUser -Identity "user1" -Replace @{company="DevOps";division="IT";department="system";title="資深工程師";manager="CN=manager1,OU=IT,DC=DevOps,DC=com";employeeID="1234567";mobile="0912345678";poRoleNames=@("MR");poRoleLevels=@("0");}


CentOS7 安裝防毒軟體 ClamAV

 出處:https://blog.xuite.net/tolarku/blog/543961253-CentOS7+%E5%AE%89%E8%A3%9D%E9%98%B2%E6%AF%92%E8%BB%9F%E9%AB%94+ClamAV

雖然我感覺不出來在 Linux 一般系統裡安裝防毒軟體的作用,但在某些應用有可能被外部使用者更改檔案,且資安要求需要安裝所以就會在 CentOS 裡安裝 ClamAV ,之前寫過一篇「Linux 系統安裝防毒 - ClamAV」但後來在套用指令發現綴詞太多,於是再寫一篇快速應用,只要簡單貼上下面指令,就能在 CentOS 裡配置防毒軟體 ClamAV。

 安裝套件庫 EPEL Repository
sudo yum install epel-release -y

安裝防毒軟體
sudo yum install clamav clamd -y
會安裝:clamav、clamd 及 clamav-db

設定 selinux
sudo setsebool -P antivirus_can_scan_system 1

啟動服務 clamd
(CentOS 6) sudo chkconfig clamd on; sudo service clamd start
(CentOS 7 )sudo systemctl start clamd 

若病毒碼未更新會跳出這個警告訊息
Starting Clam AntiVirus Daemon:
LibClamAV Warning: **************************************************
LibClamAV Warning: *** The virus database is older than 7 days!                         ***
LibClamAV Warning: *** Please update it as soon as possible.                              ***
LibClamAV Warning: **************************************************

增加台灣病毒碼更新資料庫 - 不增加也可以,但速度會差很多
sudo vi /etc/freshclam.conf
DatabaseMirror clamav.stu.edu.tw
DatabaseMirror db.tw.clamav.net

更新病毒碼
sudo freshclam

ClamAV update process started at Mon Nov 6 11:54:40 2017
WARNING: DNS record is older than 3 hours.
WARNING: Invalid DNS reply. Falling back to HTTP mode.
Reading CVD header (main.cvd): OK
Downloading main-58.cdiff [100%]
main.cld updated (version: 58, sigs: 4566249, f-level: 60, builder: sigmgr)
Reading CVD header (daily.cvd): OK
WARNING: getfile: daily-21724.cdiff not found on clamav.stu.edu.tw (IP: 2001:e10:c41:eeee::1)
WARNING: getpatch: Can't download daily-21724.cdiff from clamav.stu.edu.tw
Trying host clamav.stu.edu.tw (120.119.118.1)...
WARNING: getfile: daily-21724.cdiff not found on clamav.stu.edu.tw (IP: 120.119.118.1)
WARNING: getpatch: Can't download daily-21724.cdiff from clamav.stu.edu.tw
WARNING: getpatch: Can't download daily-21724.cdiff from clamav.stu.edu.tw
WARNING: Incremental update failed, trying to download daily.cvd
Downloading daily.cvd [100%]
daily.cvd updated (version: 24010, sigs: 1769510, f-level: 63, builder: neo)
Reading CVD header (bytecode.cvd): OK
Downloading bytecode-279.cdiff [100%]
(略)
Downloading bytecode-315.cdiff [100%]
bytecode.cld updated (version: 315, sigs: 75, f-level: 63, builder: raynman)
Database updated (6335834 signatures) from clamav.stu.edu.tw (IP: 120.119.118.1)

 

設定每日排程更新病毒碼 與 掃描特定目錄,當然也可以針對 root /
sudo vi /etc/crontab
# 1 1 * * * root /usr/bin/freshclam --quiet -l /var/log/clamav/freshclam.log
# 以 yum 套件方式安裝,不需要額外設定每日更新事件,他會自動在 /etc/cron.daily/freshclam 排程更新
1 2 * * * root /usr/bin/clamscan -r /target_directory

即時掃描某目錄,若掃描正常會在檔名後面顯示OK
sudo  clamscan -r  /www/webroot

最後會產出掃描結果
----------- SCAN SUMMARY -----------
Known viruses: 6330125
Engine version: 0.99.2
Scanned directories: 39
Scanned files: 2196
Infected files: 0
Data scanned: 2860.51 MB
Data read: 12931.74 MB (ratio 0.22:1)
Time: 629.235 sec (10 m 29 s)

MySql備份Trigger與StoredProcedure

預設的mysqldump備份出來的sql檔案,是不包含StoredProcedure,但是包含Trigger指令,因此我們如過發現資料庫中有StoredProcedure的話,需要透過下方的指令,將StoredProcedure備份出來。

mysqldump -h 127.0.0.1 -u root -p -n -d -t --routines --triggers example > example.sql

MySQL指定使用utf8編碼匯入資料

 在mysql匯入SQL檔案時,如果遇到亂碼問題,可透過指定編碼的方式,將資料正確匯入。

 

 mysql -u root -p --default-character-set=utf8 example < example.sql

MySQL匯出Binary檔案資料

當使用mysqldump匯出資料庫檔案時,若有些欄位是直接將檔案儲存進去,在匯入資料庫時會導致失敗,因此需要透過以下指令將資料庫匯出後,才能在其他台mysql主機上匯入。

 

1. 使用--hex-blob匯出資料庫檔案

mysqldump -u root -p --hex-blob  example > example.sql
 

 2. 使用一般方式匯入資料庫檔案

mysql -u root -p  example < example.sql

【GCP】靜態網頁建置

 GCP上除了使用VM架設Web Server建置靜態網頁外,另外也提供了其他方式可以不使用VM的情況下,將html檔案,放置在雲端儲存體內,透過相關設定,來建構靜態網頁網站,但相關流程較為複雜,但因為是Serverless的架構,因此穩定度會高很多。

 

在GCP Console下,透過下面六個步驟(範例域名:example.com.tw),即可建置一個靜態網站。

 

1. 新增一個example.com.tw名稱的bucket

2. Edit access > Access-control -> Fine-grained: Object-level ACLs enabled

3. 上傳檔案

4. 使用cloud console輸入:gsutil -m acl set -R -a public-read gs://example.com.tw

5. 檢查檔案權限都設定為public

6. 設定@CNAME DNSc.storage.googleapis.com

 

步驟參考:https://ikala.cloud/cloud-storage-application-website/

 

 網站建置完後,如果要套用HTTPS連線,可以依照以下步驟進行:

 

SSL設定:https://www.minwt.com/website/server/21585.html

 


結論:

1. 設定較為複雜。

2. 費用跟架設便宜的VM沒差多少。

3.  更新檔案較不方便。

4. 穩定性與安全性較高。

5. 不用擔心硬體效能。

【cmd】Windows建立像是linux的link

在windows下,很多系統人員都很想建立像是linux上使用ln指令建立的連結給程式使用,目前windows指令中有提供mklink指令,來達成目的。

mklink /d c:\OneDriveLink c:\users\username\OneDrive\

說明:
第一個資料夾路徑為【實體路徑】(c:\OneDriveLink)
第二個資料夾路徑為【虛擬路徑】(c:\users\username\OneDrive\)


mklink /d D:\OneDrive\Desktop c:\users\username\OneDrive\

可用此指令將D槽的檔案同步到雲端硬碟中