ハードウェアエンジニアの備忘録

電子工学(半導体物性)→応用光学・半導体プロセス→アナログ回路→C/C++→C#/.NETと低レイヤーから順調に(?)キャリアを登ってきているハードウェアエンジニアの備忘録。ブログ開始時点でiOSやサーバーサイドはほぼ素人です。IoTがマイブーム。

motion-mmalをストリーミングで見るときにパスワード認証をかける

今はまだローカルでRaspberry Piが閉じている環境なので、セキュリティに関してシビアではないが、いずれ外部ネットワークからも観れるようにする予定である。

それに備え、motion-mmalcam-both.confを書き換えてID認証を導入する。

①motion-mmalcam-both.confの書き換え

$ cd /opt/motion-mmal/
$
sudo vi motion-mmalcam-both.conf

ファイルを開いたら下記箇所を変更する。viで/stream_authと打つとすぐ見つかる。

stream_auth_method 1
stream_authentication newid:newpassword

これで、次回からストリーミング配信を見ようとすると、ID認証を求められるようになる。

f:id:tosh419:20160417202644p:plain

iPhone側の実装も変更

ほぼここのコピペだが、、、FirstViewControler.swiftを変更する。

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // loadCamView()
        loadCamViewWithAuth()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBOutlet weak var MonitorWebView: UIWebView!
    
    func loadCamView(){
        var url: String = "http://192.168.10.102:8081/?action=snapshot"
        let requestURL = NSURL(string: url)
        let req = NSURLRequest(URL: requestURL!)
        
        MonitorWebView.loadRequest(req)
    }
    
    func loadCamViewWithAuth(){
        var url_with_basic_auth = "http://192.168.10.102:8081/?action=snapshot"
        var url = NSURL(string: url_with_basic_auth)
        var req = NSMutableURLRequest(URL: url!)
        
        var username = "newid"
        var password = "newpassword"
        var authStr = "\(username):\(password)"
        var data = authStr.dataUsingEncoding(NSUTF8StringEncoding)
        
        var authData = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
        var authValue = "Basic \(authData)"
        
        req.setValue(authValue, forHTTPHeaderField: "Authorization")
        
        MonitorWebView.loadRequest(req)
    }
    
    @IBOutlet weak var ReloadWebView: UIButton!
    
    @IBAction func ReloadWebView_pressed(sender: UIButton) {
        // loadCamView()
        loadCamViewWithAuth()
    }    
}

 loadCamViewWithAuth関数を追加し、そちらに実装を書いた。NSDataBase64EncodingOptions.allZerosは現在のSwiftのバージョンではなくなったようだ。かわりにNSDataBase64EncodingOptions.allZeros()としておいて問題なかった。

 

しかし、C#C++などの古い言語しか触ってない人からみると、Swiftなどの新しい言語の仕様はすごく変化が早い。開発者は大変だな。