2015年10月5日 星期一

Forward PPTP server packet from Pi

這個問題花了我不少時間去處理,原因居然是因為Rasbian使用的3.18 kernel將PPTP packet視為invalid packet。不過還好後來有找到解決方法,分享給各位。

ping: sendmsg: Operation not permitted
出現以上訊息時代表iptables沒設定好,可重設iptables
# Reset/Flush iptables
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Flush end
但須注意此時已經policy設為ACCEPT,為debug使用,記得結束後要將iptables -P 設回需要的policy

PPTP forward參考設定如下

#!/bin/sh
### BEGIN INIT INFO
# Provides:         firewall.sh
# Required-Start:   $all
# Required-Stop:
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
### END INIT INFO

INTIF="你的對內介面,如brlan"

INTNET="對內介面的網段"

INTIP="PPTP server的IP"

EXTIF="對外介面,如為撥接通常為ppp0"

#Loading required stateful/NAT kernel modules...

/sbin/depmod -a
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
/sbin/modprobe iptable_nat
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_nat_irc

#below has to be loaded for pptp
/sbin/modprobe nf_nat_pptp 
/sbin/modprobe nf_conntrack_pptp 
/sbin/modprobe nf_conntrack_proto_gre 
#就是上面這三個kernel需要load,否則再怎麼設都是枉然

echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

UNIVERSE="0.0.0.0/0"

iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

iptables -A FORWARD -i $EXTIF -o $INTIF -d $INTIP -j ACCEPT

# Enable SNAT (MASQUERADE) functionality on $EXTIF
iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

#DMZ setting
iptables -t nat -A PREROUTING -i $EXTIF -p tcp -m multiport --dport 1:65535 -j DNAT --to $INTIP
iptables -t nat -A PREROUTING -i $EXTIF -p udp -m multiport --dport 1:65535 -j DNAT --to $INTIP

# PPTP PREROUTING GRE packets
iptables -t nat -A PREROUTING -i $EXTIF -p 47 -j DNAT --to $INTIP
iptables -A FORWARD -i $EXTIF -p 47 -j ACCEPT

如果不是使用DMZ的話可將DMZ setting改成--dport 1723只轉port 1723到PPTP server就好
另外如果是使用pfsense之類的firewall,INTIP為pfsense的WAN端




將script註冊為service
sudo update-rc.d <script undre /etc/init.d/> defaults
將service移除
sudo update-rc.d -f foobar remove
設定啟動相依性(如在某service啟動後啟動)
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs

2015/10/5更新
遇到一個問題是source ip被snat蓋掉。因為以前使用ddwrt時沒有遇到這個問題,因此研究了一下,發現是需要設-m state --state NEW在iptables裡對應PREROUTING的port。才能夠觸發output。否則單純用MASQUERADE是可以但是就變成snat會將source ip蓋掉變成內部ip,不是我要的。
因此上面#DMZ setting要多增加兩行
iptables -t nat -A INPUT -i $EXTIF -p udp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT
iptables -t nat -A INPUT -i $EXTIF -p tcp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT

參考資料
register service
https://mkaz.github.io/2013/07/03/run-script-at-start-on-debian/
setting linux firewall
http://www.aboutdebian.com/firewall.htm
http://www.linuxjournal.com/article/3866
PPTP forward
http://ubuntuforums.org/showthread.php?t=801207
http://www.linuxquestions.org/questions/linux-networking-3/port-forward-gre-and-pptp-using-iptables-210334/
http://serverfault.com/questions/466030/pptp-iptables-routing-issue
https://wiki.archlinux.org/index.php/PPTP_server#iptables_firewall_configuration
https://lists.debian.org/debian-firewall/2004/04/msg00103.html
http://wiki.linuxmce.org/index.php/PPTP_server
tcpdump PPTP packet
http://serverfault.com/questions/342604/how-to-sniff-request-packet-on-vpn-server
DMZ, nat preserve source ip
https://www.debian-administration.org/article/73/Port_forwarding_for_iptables_DMZ


2015年8月10日 星期一

apache php 接收 json

關鍵在apache的目錄必須開啟apache user讀寫權限。另外我遇到的狀況是傳送整個json檔案,因此必須使用json_decode函式將json file轉換成array才能進一步使用。
<?php
//header("Content-Type: application/json", true);

$filename = fopen('test.json', 'w');  //打開json暫存檔,'w'參數為每次打開皆清空檔案

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){ 
    $postText = file_get_contents('php://input'); 


fwrite($filename, $postText);  //寫入暫存檔
fclose($filename);  //關閉暫存檔

$url = "http://target.php";  //改成需要轉發的url

$data_decode = json_decode($postText);  //json格式解碼
$data_string = json_encode($data_decode);  //json格式編碼,這邊的目的是再將收到的json轉發出去                                                                                 
                                                                                                                     
$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
                                                                                                                     
$result = curl_exec($ch);


}

?>

上面的範例是當對方發送json過來時,將收到的json file再轉發到另一個url的webserver。
另外jquery必須將.js嵌入網頁,可由網路或本地端,之後才可以使用。

清除舊的ubuntu repository資料
sudo sed -i -e 's/archive.ubuntu.com\|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
sudo apt-get update

參考資料

php curl post

http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php
http://askubuntu.com/questions/9293/how-do-i-install-curl-in-php5
https://www.digitalocean.com/community/questions/installing-curl-on-ubuntu-12-04
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
http://reader.roodo.com/esabear/archives/16358749.html
http://blog.roodo.com/esabear/archives/16358749.html
http://stackoverflow.com/questions/19758954/get-data-from-json-file-with-php
http://markonphp.com/php-curl-basic-post-example/
http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

php receive post

http://stackoverflow.com/questions/938811/receive-xml-file-via-post-in-php
http://stackoverflow.com/questions/19004783/reading-json-post-using-php
http://stackoverflow.com/questions/15220704/how-to-detect-if-post-is-set
http://www.blog.magepsycho.com/sending-json-data-remote-server/
http://www.php.net/manual/en/function.json-decode.php
http://codex.wiki/post/166087-764/
http://josh.gourneau.com/blog/2010/11/16/serve-json-as-content-type-applicationjson-with-apache-on-ubuntu/
https://blog-ldkrsi.rhcloud.com/php%E6%8E%A5%E6%94%B6post%E4%BE%86%E7%9A%84json%E8%B3%87%E6%96%99/
http://www.j2h.tw/bbs/bbs16/519.html
http://phpwolf.blogspot.tw/2013/08/php-post-json.html
http://stackoverflow.com/questions/18866571/receive-json-post-with-php

POST測試

https://www.hurl.it/

jquery傳送資料給php,php再回傳

https://docs.google.com/document/d/1U0n01CVFllcpd5Irzk8999x8ALHck5M4vOJizA_mYvs/pub
http://italwaysrainonme.blogspot.tw/2013/02/jqueryjsonphp-phpjsonjquery.html
http://kinomelma.pixnet.net/blog/post/30403239-%5B%E8%B6%85%E7%B0%A1%E6%98%93%5D-jquery-json-%E6%A0%BC%E5%BC%8F%E6%8E%A5%E6%94%B6%E8%88%87%E5%88%86%E6%9E%90

android溝通

http://j796160836.pixnet.net/blog/post/28994669-%5Bandroid%5D-%E4%BD%BF%E7%94%A8http%E7%9A%84post%E6%96%B9%E5%BC%8F%E5%92%8C%E7%B6%B2%E9%A0%81%E8%A1%A8%E5%96%AE%E6%BA%9D%E9%80%9A

install jquery

http://stackoverflow.com/questions/1458349/installing-jquery
http://www.mkyong.com/jquery/jquery-html-example/
https://developers.google.com/speed/libraries/#jquery
http://www.mkyong.com/jquery/jquery-hello-world/
http://expect7.pixnet.net/blog/post/37919326-%5B%E7%A8%8B%E5%BC%8F%5D%5Bjquery%5D-jquery%E4%B8%AD%E7%9A%84ajax%E7%9A%84%E5%9F%BA%E7%A4%8E%E9%81%8B%E7%94%A8%E3%80%82%E6%8F%90%E4%BE%9B%E7%AF%84
http://blog.jex.tw/blog/2013/02/19/php-json/
http://www.mkyong.com/jquery/jquery-html-example/

php server上傳

http://givemepass.blogspot.tw/2013/06/php-server.html

php fwrite example

http://php.net/manual/en/function.fwrite.php

2015年2月3日 星期二

linux架站

當http://localhost/phpmyadmin出現404 not found時
#cd /var/www
#ln -s /usr/share/phpmyadmin

mysql new account

  • select
  • insert
  • update
  • create
  • alter
  • index
  • drop
  • reference
  • lock tables
drupal theme 教學
https://www.youtube.com/watch?v=6ekJOn2ocRE
https://drupal.org/node/83501

30個好用的themes
http://drupalchina.cn/node/1597

更改資料夾擁有者
sudo chown -R www-data:www-data /var/www/drupal
https://drupal.org/node/1036494

安裝smtp module讓mail功能正常
https://drupal.org/project/smtp
https://drupal.org/node/1108514


參考資料
https://drupal.org/project/drupal
https://drupal.org/documentation/install/create-database
http://altinukshini.wordpress.com/2011/01/09/how-to-install-drupal-7-on-linux/
http://shaurong.blogspot.tw/2014/01/drupal-726-centos-65-x64.html
https://www.youtube.com/watch?v=WBFqlgt7kuI
http://linadonis.pixnet.net/blog/post/27585552-ubuntu-server-%E5%AE%89%E8%A3%9D-phpmyadmin

Robot Framework

Regular Expression
在使用 Read Until Regexp 時,卡在$及 . 很久,研究的結果是,由於使用或 ( | ) 所以要轉義兩次,第一次轉義後由於仍在 ( | ) 內所以仍被當成regular expression看,故要再轉義一次。

所以最後結果是 (\\$|\\.txt)

在使用SSHLibrary抓取terminal內字串時遇到換行符號,本來以為是\n,結果試了不是,又試了轉義以及\r都不是,最後靈機一動,居然是\r\n,記錄在這邊,希望能夠幫到人。

SSHLibrary Open Connection
與Telnet不同,ssh open connection 必須同時使用open connection及login兩個keyword,原因應該是因為ssh連接時是使用username@ip。robot因考慮Telnet使用同一組Keyword所以造成此限制。另外AttributeError: 'NoneType'錯誤則是因為沒有輸入帳號密碼,也就是上面所說沒有配合使用login造成。

unbalanced parenthesis則是括號沒有配合到,我遇到的狀況是空了兩格造成robot將右括號視作另一個argument了

SSHLibrary.Read until regexp 我有遇到狀況是會卡住,即使timeout時間到也不會FAIL。後來使用Read交叉測試的結果是因為output內有不同的編碼所以decode時卡住。Read結果的錯誤訊息為
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 318: invalid start byte
解決方法是close connection等到該段output被跳過之後再重新建立連線,換言之就是跳過該段output。另外也有一可能是整個編碼錯誤,如開始英文後轉為中文。此時可於open connection指定編碼encoding=

參考資料
自己測試結果
Read until regexp卡住
http://stackoverflow.com/questions/6180521/unicodedecodeerror-utf8-codec-cant-decode-bytes-in-position-3-6-invalid-dat
http://www.robotframework.net/question/68

phpmailer

參考資料
http://belleaya.pixnet.net/blog/post/27410978-%5B%E6%95%99%E5%AD%B8%5D-php-%E5%88%A9%E7%94%A8-phpmailer-%E9%80%8F%E9%81%8E-gmail-%E5%AF%84%E4%BF%A1
http://blog.jsdan.com/php-%E4%BD%BF%E7%94%A8phpmailer%E9%80%8F%E9%81%8Egmail%E5%AF%84%E4%BF%A1/
http://who-know.com/use-phpmailer-to-send-emails-via-gmail-smtp/
while try catch自動分行讀取
http://kris-14.blogspot.tw/2009/12/php-txt.html
http://phpwrite.blogspot.tw/2010/05/php5try-catch.html
fopen 檔案路徑寫法
http://www.php.net/manual/en/function.fopen.php
輸入及呼叫
http://www.dami.tw/2013/11/11phpcode.html
http://www.php5.idv.tw/html.php?mod=article&do=show&shid=54
http://www.php5.idv.tw/html.php?mod=article&do=show&shid=68
http://stackoverflow.com/questions/15890627/phpmailer-body-with-variables
php重複定義
http://witmax.cn/php-cannot-redeclare-class.html
html註解
http://ant4html.blogspot.tw/2008/11/blog-post.html
php運行時間限制以及後台運行
ignore_user_abort(true);
set_time_limit(0);
http://www.ptt.cc/man/PHP/DEAA/M.1359052474.A.47F.html
http://www.blueshop.com.tw/board/FUM20041006152627A9N/BRD2012020312083365R.html
http://www.allenj.net/archives/1910
http://blog.wu-boy.com/2007/06/php-%E5%96%84%E7%94%A8-ignore_user_abort-%E5%87%BD%E5%BC%8F/
http://www.isdee.com/notes/ecshop_phpmailer.html
http://hi.baidu.com/yxl7808/item/00b9ca9c85628ae62916471d
http://sourceforge.net/p/phpmailer/discussion/81620/thread/6819cd16/
清除收件者欄位
由於預設是用addaddress()所以當跑迴圈時會重複寄送
因此需使用clearaddresses()並且不能用unset(), unset只是暫時重設,下次再呼叫時會讀取unset前之數值
http://www.wibibi.com/info.php?tid=236
http://chan15.blogspot.tw/2011/11/phpmailer.html
textarea
使用input只有一行,因此一般的留言板都應該使用textarea
另外input可以設定長度跟高度,雖然還是只能輸入一行
http://stackoverflow.com/questions/4391344/how-can-i-set-the-size-of-the-box-of-a-form
http://crazy.molerat.net/learner/cpuroom/programmer/reading.php?filename=1010407112825.dov
http://www.php-example.com/2011/05/php-html-form-textarea-example.html
斷行處理
http://chensh.loxa.edu.tw/php/X_8.php
http://www.jollen.org/php/jollen_php_book_79_textarea.html
http://itgroup.blueshop.com.tw/ann71727/ann71727?n=convew&i=6032

java心得

java的變數不能使用數字開頭,否則會判斷為數字。

有關筆電透過無線網路及VPN觀看遠端區域網路影片斷線LAG及雜音問題

這個問題困擾我好久,並且每次在我播放影片時就會出現。經過測試發現斷線問題是因為無線網路切換問題,固定在802.11g就不會斷線,一開始還以為是VPN問題。另外LAG及雜音則應該是因為硬碟空間不足,所以在讀取時需要一邊清空間一邊播放造成LAG及有雜音,想說i5應該不會不夠力才對。剛剛測試把硬碟空間清出來就沒有再遇到相同問題,明天再測試一次就知道究竟是不是solution了。

更新:
換了一台筆電之後就沒問題,看來是Hardware issue

參考資料
11樓
http://www.mobile01.com/topicdetail.php?f=300&t=1347232&p=2