顯示具有 android 標籤的文章。 顯示所有文章
顯示具有 android 標籤的文章。 顯示所有文章

2018年7月26日 星期四

以netcat取代ping監測設備是否online

這兩天在寫一個程式需要判斷手機是否在線上,一開始使用ping結果後來經過不斷測試(測試過休眠時WIFI保持連線,wakelock,powersaver,power optimization)發現,android手機(我是samsung note 8)只要按電源鍵休眠五分鐘後就會進入doze mode會block ping造成判斷失誤。經過survey後找到可以使用netcat/nmap(後來發現nmap需要另外安裝,我只有使用nmap掃描)獲取手機狀態可得知是否在線。

nc -w 4 -vz <port> <ip>

以下是我試出來可以使用的幾個Port
8080
6101 tcp backupexec
6789 tcp ibm-db2-admin
8701 tcp unknown
9593 tcp cba8
9595 tcp pds

另外由於port通常是close所以nc會是error,這個時候要把error save to file or variable需使用2>&1 std參數

20190131更新: 這幾天更新android後發現WIFI會連上又斷,經過檢查後確定問題應該出在android把我這個nc檢查的行為當成攻擊所以斷開wifi,經過加上sleep延長檢查時間後問題解決,我的機型是samsung note8,供參考。問題發生時好像會顯示不安全的網路及網路連線不可用,不太確定。


參考資料
自己
error output to file and variable

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日 星期二

foldersync reupload issue

android上使用foldersync同步到FTP時發現會有重複同步的問題,看了一下是由於兩邊檔案時間不同,但是明明是同一個檔案。後來研究了一下似乎是foldersync的問題,沒有支援MFMT時間會錯亂。解決方法可以將容許時間調大,或者是乾脆就不要覆蓋舊的,只能等更新了。

參考資料
https://groups.google.com/forum/#!topic/tacitdynamics/GhfLILUGo2g

2013年10月24日 星期四

LG電視播放與字幕問題(同場加映Plex media server)

LG電視播放內嵌或外掛的PGS字幕時都無法顯示,這應該是LG電視先天的限制,我研究了許久並沒有找到解決方法,不過其他srt字幕等就可以辨識。

由於也沒有撥放APP可以下載所以無法,後來我試了用android手機連區域網路共享然後用mx player播放,這樣的話的確可以撥放不過會有一個問題就是mx player只能辨識檔名完全一樣的srt外掛字幕(如果是手機內撥放的話則可以允許如 *.cht.srt 這樣的格式)

後來又用了Plex media server,可以自己下載matadata資訊,相當漂亮的介面,自己架設server的話是個好選擇,搭配android的Plex app很好用,也可以顯示PGS字幕,還可以更改bitrate。在設定myplex的時候要注意如果手動設定連接埠則需要將NAT forward到預設的32400 port才可以正常偵測,也有想是不是在設置-網路-高級設置裡面更改web manage預設路徑就不會有此限制,不過後來沒試,有興趣的人可以自己嘗試一下。

另外有搜尋到PleXBMC的addon支援PGS字幕,不過後來發現那是給XBMC使用的,所以目前LG電視直接撥放PGS字幕還是無解的,也許有一天會更新吧。

還有LG電視的媒體連結(medialink)是可以連結Plex media server的,資訊也都會秀出來,不過我測試常常顯示無法撥放媒體,並且也還是無法支援PGS字幕,因為還是用內建軟體撥放。

後記:如果有使用android app時發現.srt字幕時間會慢的時候(此時video是directplay),可以去setting裡把"Prefer local subtitles"選項關閉則video會轉為transcode即可解決。

參考資料
http://forums.plexapp.com/index.php/topic/48130-subtitles-in-medialink/
http://forums.plexapp.com/index.php/topic/26123-supported-file-formatscodecs/
http://forums.plexapp.com/index.php/topic/67290-medialink-2-suggestions-screen-size-pgs-subtitles/
http://elan.plexapp.com/2012/12/23/mele-kalikimaka/
http://en.wikipedia.org/wiki/XBMC
https://www.youtube.com/watch?v=9qvaCQ-S-O0
http://www.plexapp.com/medialink/files/E_install5.html
http://wiki.xbmc.org/?title=Add-on%3APleXBMC

android內建vpn撥接傳輸速度上不去問題

使用android內建VPN撥接到自己架設的PPTP VPN server播放影片或傳輸檔案時,常會lag或者是傳輸很慢,使用電腦則不會,因此讓我想到也許是android效能不足無法同時處理。或者之後等手機效能趕上時就不會如此,不過目前我得到的暫時解決方案是使用FTP協定,利用Fillazilla ftp server架設伺服器並用es file explorer連入,便可以直接點選檔案並撥放串流,我是用MX Player載入DTS自訂解碼器之後便可以很輕鬆的看影片,也不會再像之前一樣LAG,真的很方便呢。

2013年7月2日 星期二

android系統間藉由藍芽共用網路

轉錄http://www.scaine.net/site/2013/01/android-to-android-tethering-over-bluetooth/

Android to Android Tethering over Bluetooth

  1. It’s not battery friendly – both devices need to run their WIFI full time.
  2. It’s a broadcast – the hotspot’s SSID can be hidden of course, but anyone sniffing the airwaves will still see my hotspot and may attempt to hack it. Unlikely that they’d succeed, given that it’s WPA2, but still.
  3. I have to manually turn on the hotspot on my GNex and wait 20 seconds for my N7 to see the connection and get an IP address.
  4. I have to manually turn off the hotspot on my GNex.
  5. I also have to remember to turn back on WIFI on the GNex, or it’ll just use 3G all day thereafter, because turning on a hotspot automatically turns off your WIFI.
Lately, I wondered if there was a better way to do things. Could I use Bluetooth and just leave the service running full time?
Well, since both my devices run Android’s latest “Jelly Bean” release, I thought it would be a doddle to set up. I was wrong, sadly, because there’s a fairly obscure setting you have to set up before hand. However, once you know about that, it’s very simple. In the hope it helps you, here’s the full step by step :
First, and most obviously, turn on the both device’s “Bluetooth” setting.
NetworkSettings
You’ll need to do that on both devices.
Then, pair them up by clicking “Search for devices” on one of the devices and clicking on the name of other device. You’ll be asked to verify a PIN number. It doesn’t matter what the PIN number is, as long as it’s the same PIN number on both devices.
Now, on the GNex, here’s the bit that caused me a headache. Go to Settings, choose More… then choose Tethering & Portable Hotspot. In there, you have tick Bluetooth tethering. If you don’t, the tether will (silently!) fail and you’ll be left, like me, scratching your head.
NetworkSettings-MoreTethering-PortableHotspot
Okay, now you’re ready to use the GNex’s internet via Bluetooth. All you have to do is tell the N7 to actually use it.
On the N7, go to Settings, then click on the word Bluetooth (not the toggle – the word). Finally, click on the name of your GNex you paired earlier. To get it all working, choose Internet Access.
BluetoothSettings
That’s it. Assuming that your GNex has some kind of internet access, then so now will your N7 tablet, via Bluetooth.
I’ve only just completed this myself and initial impressions are that the browsing on my N7 is significantly slower than over WIFI (despite my GNex using WIFI). Obviously, there’s a bandwidth limitation when using Bluetooth – but at around 2Mbits, that shouldn’t hinder web page rendering. Both devices are Bluetooth 3 capable, although the GNex apparently has hardware capable of Bluetooth 4. However, from what I could find, all Bluetooth devices communicate at around 2Mbits unless they use other technology to create high-speed channels, such as Bluetooth 3′s HS capability, which simply creates a WIFI 802.11 channel – which would kind of defeat the purpose of this anyway!
Here’s the advantages of this set up, assuming the speed issue is all in my head :
  1. Low power Bluetooth will preserve battery on the N7.
  2. The devices will pair up when they see each other, so no manual steps to get internet access on the bus. (I need to confirm this – I have a sneaky suspicion that I still need to tell the N7 to use the GNex internet each time they reconnect – which would devalue this for me quite a bit)
  3. I can keep WIFI enabled on the GNex and this tether will simply use that faster connection if it’s available.
  4. I’m not advertising an available WIFI hotspot to everyone on the bus!
Time will tell. I’ll use it for a few days and see how it goes.

2011年11月26日 星期六

vpn

最近幾天自己架了一個openvpn server。然後在筆電上裝了openvpn client,最後在sensation上也裝了openvpn。因為過程實在有些複雜,po上來備忘兼給其他人參考。

首先openvpn server的部份,由於我的目標是讓外部網路可以透過vpn peer到我家內部網路,所以我是走tap而非tun。這部份主要是先安裝openvpn for windows然後bridge虛擬網卡與區域網路。openvpn server config部份要注意的是

proto udp

dev tap
dev-node tap-bridge

server-bridge 192.168.1.52 255.255.255.0 192.168.1.60 192.168.1.254

push "route 10.66.77.0 255.255.255.0"

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp" 這邊好像是設定讓client端連外是server端ip的樣子

client-to-client

duplicate-cn

接著client部份


dev tap

proto udp

ca ca.crt
cert client1.crt
key client1.key

其他部份幾乎都是預設值,但是同樣的設定到阿璇那邊就變成不通,不知道為何。另外因為有開BT的關係中間有出現連不上,後來才查明是因為BT流量開太大所以導致vpn client連不進來。

所以後來使用xp vpn server之方法PPTP讓阿璇可以連進來。但xp vpn server只能允許同時一個使用者連入的樣子。設定方法

新增連線,序列埠,不選擇,允許私人連線及工作連線。最後設定分配的ip區段。但是阿璇說會無法上網,這點還要再測試。

最後是最麻煩的android openvpn設定。

首先sensation自己就有vpn設定,也是走PPTP,但是一開始設定好卻始終連不上,以為是server端問題但又不像。最後查到原來是3g會擋vpn PPTP,也難怪我使用筆電連手機上網要用xp vpn PPTP撥回來始終連不上。有看到有人說明可以走L2TP(xp要另外裝東西才能支援),不過最後決定用openvpn。

先在market上下載openvpn installer跟openvpn setting。要安裝openvpn之前要先安裝busybox。這邊要注意的是如果是用原廠的ROM則market上下載的busybox installer並不會去更改你的/system/bin/route & ifconfig。所以必須要手動去link busybox與route & ifconfig。

可用terminal終端機鍵入

ln -s /system/xbin/busybox /system/xbin/bb/route
ln -s /system/xbin/busybox /system/xbin/bb/ifconfig

前半要換成你busybox安裝的位置。

接下來就是tun.ko的部份,也是我花最久時間處理的部份。由於tun.ko必須配合你的kernel。所以我找了許久,最後甚至自己compile一個但是還是無法使用。最後是刷了PYR_UNITY_V13_OC1512_CIFS_TUN_NOOP_2WCR_VDD.zip這個kernel才解決。

compile的方法是先下載android-ndk-r4-linux-x86.zip,Pyramid_LE_CRC-2.6.35-877c198.tar.gz,另android-sdk因為只是用來取出手機內的/proc/config.gz所以我就沒下載
我是用vitualbox安裝utunbu在linux下操作,先將上面兩個及config.gz解壓縮並令裡面的CONFIG_TUN=m。接著改Pyramid_LE_CRC-2.6.35-877c198.tar.gz裡的MakefileEXTRAVERSION = 為你的kernel最後小數點後面的字串)。最後設定變數

export ARCH=arm
export CROSS_COMPILE=arm-eabi-
export PATH=$PATH:~/android/android-ndk-r4/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/

cdPyramid_LE_CRC-2.6.35-877c198目錄下並下指令make modules,則可在Pyramid_LE_CRC-2.6.35-877c198/driver/net下找到tun.ko

2012/3/23補充
後來有查詢到一些資料即iptable導致wifi與3G衝突可能引起連不上openvpn,此時可下指令ip ru del table gprs可解決。

2019/7/12 update
其實就是android不支援tap(因為需要root權限)


參考資料