dongqing7789 2016-06-24 14:12
浏览 379

在浏览器和本地应用程序之间进行通信-安全连接?

Currently creating an application launcher and updater based on a web page. Knowing the limitations of browsers (security reasons), for my project I needed to have a local application to control the update and the creation of the OS process.

While testing and researching for a good amount of time; Now I have a working local-app, written in Go, that can create a process and kill it from an ajax POST request. I prepared some code to show, how my current attempt looks like (nothing fancy) :

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />

  <title>api_test</title>
  <script src="js/jquery-3.0.0.min.js"></script>
  <script src="js/script.js"></script>

</head>
<body>
  <h2 id="statusText"></h2>
</body>
</html>

js/script.js

$(document).ready(function(){
  console.log("Hey! ;)");
  tryConnection();
});

function tryConnection() {
  var statusReq = $.ajax({
    method: "GET",
    url: "http://127.0.0.1:9091/api/test",
    dataType: "json"
  });

  statusReq.fail(function(){
    $('#statusText').text("Fail");
    setTimeout(tryConnection, 5000);
  });

  statusReq.done(function(json){
    $('#statusText').text(json.status);
    console.log("Integer result: " + json.result);
    setTimeout(tryConnection, 10000);
  });
}

Local application, app.go

package main

import (
  "log"
  "net/http"
  "encoding/json"
)

type APITest struct {
  Status string `json:"status"`
  Result int `json:"result,omitempty"`
};

func testHandler(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Access-Control-Allow-Origin", "*");

  TestValue := APITest{Status: "Works!", Result: 123};
  json.NewEncoder(w).Encode(&TestValue);
}

func main() {
  log.Println("Started!");
  http.HandleFunc("/api/test", testHandler);

  err := http.ListenAndServe("127.0.0.1:9091", nil);
  if err != nil {
    log.Fatal("Error occured:", err);
  }
}

Everything seems to be fine! But..

The only problem I currently can't figure out is, How can I actually add support for https:// connections without the need to carry the compiled binary with a cert and key file? Because I want to implement this feature into a https:// only website and mixing together https and http connections, results in a blocked mixed content error from browser security restrictions, which is understandable.

Maybe there are some other ways to implement this kind of communication? (to be clear, for WebSockets a secure connection is needed too).

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)