go語言實現IOS OTA安裝應用
阿新 • • 發佈:2019-02-08
現在的IOS OTA安裝應用需要使用https才能安裝。
要使得伺服器支援https,伺服器必須要有https證書。證書可以同過購買獲得或者自己生成。
這裡我們使用自己生成的證書,使用自己生成的證書必須在iphone上手動安裝之後才能訪問https伺服器,否則iphone會拒絕訪問未受信任的https伺服器。
使用openssl
建立https
證書
- 生成伺服器的私鑰
mkdir /private/etc/apache2/ssl
cd /private/etc/apache2/ssl
sudo openssl genrsa -out server.key 1024 - 生成簽署申請(Common Name必須為伺服器的ip或域名)
sudo openssl req -new -key server.key -out server.csr - 生成CA私鑰
sudo openssl genrsa -out ca.key 1024 - 用CA的私鑰產生CA的自簽署證書
sudo openssl req -new -x509 -days 365 -key ca.key -out ca.crt - 建立demoCA
demoCA裡面建立檔案index.txt和serial,serial內容為01,index.txt為空,以及資料夾newcerts
sudo openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
這樣我們就生成了server.crt,server.key,ca.crt檔案。將 server.crt,server.key配置到伺服器上,我們存放的位置是”/private/etc/apache2/ssl/server.crt”,”/private/etc/apache2/ssl/server.key”, ca.crt放到檔案根目錄中。
在使用以上過程生成證書的時候,要注意輸入common name
的時候要輸入網站的訪問的域名或者是IP地址。
go語言實現證書安裝和app安裝
var installTemplate = `
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta content="telephone=no" name="format-detection"/>
<title>application name</title>
</head>
<style>
html {
width: 100%;
height: 100%
}
body {
width: 100%;
height: 100%;
background-color: #fafafa;
font-family: "Microsoft YaHei";
color: #0a0909;
-webkit-touch-callout: none;
-webkit-user-select: none;
}
div, p, header, footer, h1, h2, h3, h4, h5, h6, span, i, b, em, ul, li, dl, dt, dd, body, input, select, form, button {
margin: 0;
padding: 0
}
ul, li {
list-style: none
}
img {
border: 0 none
}
input, img {
vertical-align: middle
}
* {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
outline: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal;
}
body {
padding: 20px;
}
.title {
font-size: 18px;
margin-bottom: 20px;
}
.install {
width: 150px;
height: 40px;
border: 1px solid #ccc;
background: transparent;
border-radius: 6px;
font-size: 14px;
margin-bottom: 10px;
display: block;
}
</style>
<body>
<p class="title">iOS應用OTA安裝</p>
<a href="itms-services://?action=download-manifest&url=https://192.168.23.7:1718/static/manifest.plist">
<button class="install">安裝應用</button>
</a>
<a title="iPhone" href="http://192.168.23.7:1717/static/ca.crt">
<button class="install">證書信任</button>
</a>
</body>
</html>
`
func main() {
http.Handle("/install/", http.HandlerFunc(install))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("test"))))
go func() {
http.ListenAndServe(":1717", nil)
}()
err := http.ListenAndServeTLS(":1718", "server.crt", "server.key", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func install(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, installTemplate)
}
首先通過http://ip:1717/install訪問安裝頁面,在這個頁面首先點選證書信任,以使iphone信任我們的臨時證書,然後在點選安裝應用則可以進行應用安裝了。
這裡使用了go自帶的FileServer來進行靜態檔案下載。
OTA安裝描述檔案manifest.plist
這個檔案可以自己寫一個,也可以在xcode匯出應用的時候通過xcode自動生成,其內容大概如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>https://192.168.23.7:1718/static/majiang-mobile.ipa</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>url</key>
<string>https://</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>url</key>
<string>https://</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.dreamgame.jd.QWXY</string>
<key>bundle-version</key>
<string>1.0</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>majiang-mobile</string>
</dict>
</dict>
</array>
</dict>
</plist>
其中bundle-identifier
必須和xcode中的設定一樣和software-package
即是通過https訪問apk檔案的路徑。