2014年12月31日 星期三

[PaaS]試用 OpenShift 平台-3(安裝 MongoDB 及其前端網頁管理工具 RockMongo)

、安裝 MongoDB 及其前端網頁管理工具 RockMongo
3.1. 安裝 MongoDB
cd myapp1
rhc cartridge add mongodb-2.4

3.2. 安裝 MongoDB 的前端網頁管理工具 RockMongo
rhc cartridge add rockmongo-1.1

若不想背太多指令,可以透過Web介面來做設定

3.3. 測試一下
3.3.1. rhc ssh

3.3.2. mongo
3.3.3. use myapp1(切換資料庫)

3.3.4. var icd10cm = {"action":"I", "code":"A", "pre":"", "content":"感染和寄生蟲疾病 Certain infectious and parasitic diseases", "quest":"Q:哪種類型?"};
3.3.5. db.icd10cm.insert(icd10cm );
3.3.6. db.icd10cm.find();

3.3.7. 安裝 mongojs
npm install mongojs

 3.3.8. 修改 server.js,增加"路由"程式碼
在 self.createRoutes 的 function 中增加以下的程式碼
self.routes['/db'] = function(req, res) {
    var mongojs = require('mongojs');
    var mydb = "myapp1";
    var mycollection = "icd10cm";
    var connectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME  
      ":"  
      process.env.OPENSHIFT_MONGODB_DB_PASSWORD  
      "@"  
      process.env.OPENSHIFT_MONGODB_DB_HOST  
      "/"  
      mydb;
    var db = mongojs(connectionString, ['icd10cm']);
    var mycollection = db.collection('icd10cm');

    db.icd10cm.find(function(err, docs) {
        res.send(docs);
    });
};

3.3.9. 提交程式碼
git add .
git status
git commit -a -m "Add mongojs to node.js"
git push

3.3.10. 輸入網址測試資料回傳
http://myapp1-bobohan.rhcloud.com/db

參考資料
[1] Installing the OpenShift Client Tools
[2] Getting Started with MongoDB on Node.js on OpenShift
[3] Running Nginx on OpenShift
[4] 《OPENSHIFT》可綁網址、無限流免費雲端主機
[5] 在 OpenShift 上部署 MongoDB 和 Node.js 应用
[6] OpenShift安裝Nginx+MYSQL+PHP5.4

2014年12月21日 星期日

健保e化抽審-4(PDF加入浮水印)

怕有日後資料外流不易追查,進而引發不必要的資安議題(申請單都消化不完了,還要搞這個),所以還是小心點好,在PDF檔裡加入浮水印好了,避免有心人傳來傳去。 必要時還可以加入當初使用者是誰,方便源頭的追蹤管理。
private void button19_Click(object sender, EventArgs e)
{
  string fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System)   @"\..\Fonts\kaiu.ttf";
  BaseFont bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    
  string fileBefore = @"C:\temp\fileBefore.pdf";
  string fileAfter = @"C:\temp\fileAfter.pdf";

  // Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
  using (PdfReader reader = new PdfReader(fileBefore))
  // Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
  using (FileStream fs = new FileStream(fileAfter, FileMode.Create, FileAccess.Write, FileShare.None))
  using (PdfStamper stamper = new PdfStamper(reader, fs))
  {
    // Getting total number of pages of the Existing Document
    int pageCount = reader.NumberOfPages;

    // Create New Layer for Watermark
    PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);

    // Loop through each Page
    for (int pageNum = 1; pageNum <= pageCount; pageNum  )
    {
      // Getting the Page Size
      iTextSharp.text.Rectangle rect = reader.GetPageSize(pageNum);

      // Get the ContentByte object
      PdfContentByte cb = stamper.GetUnderContent(pageNum);
   
      // Tell the cb that the next commands should be "bound" to this new layer
      cb.BeginLayer(layer);
      cb.SetFontAndSize(bfChinese, 50);
      cb.SetColorFill(BaseColor.BLACK);

      PdfGState gState = new PdfGState();
      gState.FillOpacity = 0.25f;
      cb.SetGState(gState);

      cb.BeginText();
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "僅供健保e化抽審使用", rect.Width / 2, rect.Height / 2, 45f);
      cb.EndText();

      // Close the layer
      cb.EndLayer();
    }
  }
}
參考資料
[1] Adding Watermark to PDF Document using Layer
[2] Nested using statements in C#

2014年12月11日 星期四

[PaaS]試用 OpenShift 平台-2(建立可以運作 Node.js 的平台及設定 FTP)

二、建立可以運作 Node.js 的平台(PaaS)及設定 FTP
2.1. 建立可以運作 Node.js 的平台(PaaS)
2.1.1. rhc create-app myapp1 nodejs-0.10

2.1.2. 輸入"rhc ssh"連線到遠端,並測試一下「hello, world」

2.2. 測試Git
2.2.1. 建立測試檔案
echo console.log('hello, world'); > helloworld.js

2.2.2. 檢查目前 Repository 狀態
git status

2.2.3. 將測試檔案加入 Repository
git add helloworld.js

2.2.4. 提交檔案
git commit -m "Add helloworld.js to test git and node.js"
or
git commit -a -m "Add helloworld.js to test git and node.js"

2.2.5. 上傳檔案
git push
PS: 若遇到 "git push error failed to push some refs to" 的問題
請先輸入 "git pull --rebase"

2.3. 設定FTP
2.3.1. 使用 Putty Key Generator 產生公鑰及私鑰

2.3.2. 於 OpenShift 設定 Public Keys 及 Remote Access


2.3.3. 於 FileZilla 加入 SFTP 私鑰檔案設定

2.3.4. 於 FileZilla 加入主機設定(Host、Logon Type、User)

2.3.5. 連線測試一下,OK

參考資料
[1] Installing the OpenShift Client Tools
[2] Getting Started with MongoDB on Node.js on OpenShift
[3] Running Nginx on OpenShift
[4] 《OPENSHIFT》可綁網址、無限流免費雲端主機
[5] 在 OpenShift 上部署 MongoDB 和 Node.js 应用
[6] OpenShift安裝Nginx+MYSQL+PHP5.4

[PaaS]試用 OpenShift 平台-1(安裝 OpenShift Client 端工具)

一、安裝 OpenShift Client 端工具
1.1. 設定 OpenShift 的 Windows 環境
1.1.1. 安裝 rubyinstaller-1.9.3-p551.exe,記得要將設定Path勾選進來

1.1.2. 測試一下,輸入ruby -v

1.1.3. 安裝 Git,順便驗證版本

1.1.4. 安裝及設定 OpenShift gem
gem install rhc

rhc setup,並輸入帳號/密碼



參考資料
[1] Installing the OpenShift Client Tools
[2] Getting Started with MongoDB on Node.js on OpenShift
[3] Running Nginx on OpenShift
[4] 《OPENSHIFT》可綁網址、無限流免費雲端主機
[5] 在 OpenShift 上部署 MongoDB 和 Node.js 应用
[6] OpenShift安裝Nginx+MYSQL+PHP5.4

2014年12月7日 星期日

[硬體/系統]Mail Server(Zimbra)送信異常問題處理(Mail Relay Attack)

最近 email server 常被攻擊,然後就會成為 mail relay 的跳板。Google了一些資料,並對我們的 email 主機作了一些設定上的變更,希望它只能 relay 我們 domain 的信件。當然,我們希望能有足夠的時間依照服務廠商的建議,來調整 email server 架構,將送信跟收信兩個功能拆成兩部伺服器來處理。
 
第一步:修改Postfix設定檔(/opt/zimbra/postfix/conf/main.cf)
修改前~
mydestination = localhost
無relay_domains

修改後~
mydestination = $myhostname, $mydomain
relay_domains = $mydomain


第二步:使用 telnet 的方式來模擬 email 的寄送
(1)建立連線,若失敗再敲一次,我們家的主機會這樣,學藝不精不知道為什麼
ehlo DomainName
(2)輸入寄件者
mail from:<user1@DomainName>
(3)輸入收件者
rcpt to:<user2@DomainName>
(4)輸入信件內容,並最後一行輸入"."表示結束
data
測試信件
.
(5)結束連線
quit
(6)檢查log,取出最後15行
tail -n 15 /var/log/maillog


第三步:安裝流量監控工具(iftop)
鑑於以上的攻擊事件,想要透過流量的分析,了解到到底壞人(IP)在那裡。若仍不行檔下來這些攻擊,那只好將這些IP設入防火前牆的黑名單內。服務廠商推薦iftop,所以就裝上它來試看看。
以下就相關的安裝過程~
(1)檢查CentOS版本
cat /etc/redhat-release
(2)因為yum指令找不到套件
yum search iftop
(3)所以只好用rpm的版本來安裝iftop
wget http://pkgs.repoforge.org/iftop/iftop-0.17-1.el5.rf.i386.rpm
rpm -ivh iftop-0.17-1.el5.rf.i386.rpm

這是我會使用的功能~
【B】顯示40秒的平均值
【3】以40秒的平均值來排序
【T】顯示總流量
【t】切換上傳下載顯示模式
【p】是否顯示來源端與目的端的port

相關操作我是參考這篇blog~
iftop流量監控使用教學,立刻抓出吃流量兇手

參考資料
[1] 鳥哥的 Linux 私房菜 - 第二十二章、郵件伺服器: Postfix
[2] iftop: display bandwidth usage on an interface
[3] How to setup an E-Mail Relay Host with Sendmail ?
[4] Send a test mail using Telnet
[5] Authenticated SMTP