2015年9月10日 星期四

使用Go 做反向代理伺服器

使用Go作反向代理,可以使用go 自帶的reverse proxy
但是如果背後的http server是走https的話,你又是用自簽憑證那你就會遇到inseurce verify的問題

解法就是取代原本reverse proxy自帶的client transport設定

package main import (
"net/http"
"net/http/httputil"
"net/url"
"time"
"net"
"log"
"fmt"
"crypto/tls"
)
func main() {
go ReverseHttpsProxy(445,"https://127.0.0.1:443/","my.crt","my.key")
ReverseHttpProxy(8081,"http://127.0.0.1:8080/")
}
func ReverseHttpsProxy(port int,dst string,crt string,key string) {
u, e := url.Parse(dst)
if e != nil {
log.Fatal("Bad destination.")
}
h := httputil.NewSingleHostReverseProxy(u)
//if your certificate signed by yourself,you need use this bypass secure verify
var InsecureTransport http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 10 * time.Second,
}
h.Transport = InsecureTransport
err := http.ListenAndServeTLS(fmt.Sprintf(":%d",port),crt, key ,h)
if err != nil {
log.Println("Error:",err)
}
}
func ReverseHttpProxy(port int,dst string) {
u, e := url.Parse(dst)
if e != nil {
log.Fatal("Bad http destination.")
}
h := httputil.NewSingleHostReverseProxy(u)
err := http.ListenAndServe(fmt.Sprintf(":%d",port),h)
if err != nil {
log.Println("Error:",err)
}
}


gist:https://gist.github.com/matishsiao/8270e18923d8f78f56c2

2015年9月2日 星期三

Virtual Box Windows10 使用NAT來做Windows SSH service

最近升級到了win10,才發現原本ubuntu的橋接網路卡沒辦法抓到
找了一些文章,看起來是Virtual box對win102的支援度還不夠
所以就用了一個繞路的方法實現windows to ubuntu VM ssh port

方法很簡單
在你的ubuntu VM設定中的網路設定2張nat網卡(1張是做NAT,1張是上網用的)
再來選第一張nat網卡的連接埠轉送功能
設定一下你想要轉送的port就大功告成了
主機IP就是你裝Virtual box的電腦IP
客體IP就是你的Ubuntu VM eth0 IP


2015年9月1日 星期二

Go的多字串比對

使用Go的精簡算法而完成的多字串比對

================================================

package main

import "fmt"

func main() {
fmt.Println("Multi string comparison")
a := "hello"
        b := "gopher"
c := "world"
d := "hello"

        // Go 可以支援精簡算法

        ok := a == b || a == c

fmt.Printf("check a == b || a == c:%v var a:%s b:%s c:%s d:%s\n",ok,a,b,c,d)

ok = a == d

fmt.Printf("check a == d:%v var a:%s b:%s c:%s d:%s\n",ok,a,b,c,d)
}

範例程式:
https://play.golang.org/p/e7Jz5CJ5Xm

2015年8月24日 星期一

如何用Go組出Y-m-d H:i:s 格式時間字串

最近的再處理有關Go 時間格式的問題,覺得沒有一隻小function 可以幫忙處理時間字串很麻煩
所以寫了一隻小function來用
參數說明:
Y:2009 //Year
y:09   //Year
M:01   //Month
m:1    //Month
D:02   //Date
D:2    //Date
H:15   //Hour
I:04   //Minute
i:4    //Minute
s:03   //Second

code 位置:
https://gist.github.com/matishsiao/bf3bfa9f13c9eaff2750

2015年8月21日 星期五

產生Go API Document

因為公司的關係接觸了Go lang,現在目標每天分享一篇關於Go的資訊
第一篇就是
https://engineroom.teamwork.com/generate-api-from-annotations-in-go/?utm_source=golangweekly&utm_medium=email

這主要是可以產生API document,方便文件管理,不過我自己還沒時間測試

我也會分享在Facebook Group :Golang Gopher Taiwan

2013年8月20日 星期二

mongo DB 用途

最近因為公司專案的需求,所以認真地看完mongoDB官方出的書(中文版的~XD)
下面是我覺得適合使用mongo DB的用途跟優缺點
mongo DB 用途
1.資料是弱關聯的
2.需要大量寫入資料

優點:
1.高效能(在弱關聯下)
2.寫入及查詢速度都不錯
3.類似SQL語法操作,但是還是function base,不過概念上是相同的
4.有(master + slave), (master + master), (replica set)備份方式

缺點:
1.需要配置大量記憶體空間
2.需要配置大量硬碟空間,資料庫會預支空間,所以有些時候會有空間浪費的狀況


結論:
   如果要做data mining相關的處理,就不適合用mongo DB,原因是因為mongo DB的高效能只對弱關聯有用,如果用了很多關聯的功能,效能會嚴重低落~Orz

mongo DB網址:
http://docs.mongodb.org/

2013年7月24日 星期三

PHP 內建的樣板引擎

最近剛好在研究PHP的樣板引擎,發現到原本PHP其實就可以做樣板引擎
他主要的原理是透過輸出緩衝資料的Buffer做資料組合,轉換成HTML格式

下面是自己測試的PHP檔:
------------------------------------------------------------------
coreTemplate.php 組合樣板用的類別
------------------------------------------------------------------
<?php
class CoreTemplate{
/**組合樣板資料
* @param $template 樣板路徑
* @param $data 導入到樣板的資料變數(樣板的PHP檔須從這個變數取得所需資料)
* @return 組合完成的資料
*/
function render($template, $data) {
ob_start();
include $template;
$result = ob_get_contents();
ob_end_clean();

return $result;
}
}
?>

------------------------------------------------------------------
test.tpl.php 樣板的php程式碼
------------------------------------------------------------------
<html>
   <head>
        <title>Test</title>
    </head>
    <body>
        <table>
            <tr>
                <td>ID</td><td>Name</td>
            </tr>
            <!-- PHP 樣板文字 $data為coreTemplate所設定的資料變數名稱-->
            <?php foreach($data as $list): ?>
            <tr>
                <td><?=$list['id']?></td><td><?=$list['name']?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </body>
</html>

------------------------------------------------------------------
test.php 程式邏輯跟輸出
------------------------------------------------------------------
<?php
include_once("coreTemplate.php");

$ct = new CoreTemplate();
$info = array();
//產生10筆資料
for($i = 0; $i < 10;$i++){
array_push($info, array('id'=>$i, 'name'=>'name'. $i));
}
//輸出HTML
echo $ct->render('test.tpl.php' , $info);
?>
----------------------------------------------------------------------
輸出結果
----------------------------------------------------------------------


結論:
這次的實驗主要只是看php做樣板引擎的方法而已,真正要自己實作應該也會需要時間
所以還是用Smarty做樣板引擎~XD!