Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
support query install status
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Sep 19, 2017
1 parent 4f09155 commit 2d8e98a
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/codeskyblue/kexec"
"github.com/franela/goreq"
"github.com/gorilla/mux"
"github.com/miekg/dns"
)

Expand Down Expand Up @@ -171,7 +172,7 @@ type DownloadManager struct {
n int
}

func NewDownloadManager() *DownloadManager {
func newDownloadManager() *DownloadManager {
return &DownloadManager{
db: make(map[string]*DownloadProxy, 10),
}
Expand Down Expand Up @@ -215,7 +216,7 @@ type DownloadProxy struct {
wg sync.WaitGroup
}

func NewDownloadProxy(wr io.Writer) *DownloadProxy {
func newDownloadProxy(wr io.Writer) *DownloadProxy {
di := &DownloadProxy{
writer: wr,
}
Expand All @@ -238,9 +239,9 @@ func (d *DownloadProxy) Wait() {
d.wg.Wait()
}

var downManager = NewDownloadManager()
var downManager = newDownloadManager()

func AsyncDownloadInto(url string, filepath string, autoRelease bool) (di *DownloadProxy, err error) {
func AsyncDownloadTo(url string, filepath string, autoRelease bool) (di *DownloadProxy, err error) {
res, err := goreq.Request{
Uri: url,
MaxRedirects: 10,
Expand All @@ -254,7 +255,7 @@ func AsyncDownloadInto(url string, filepath string, autoRelease bool) (di *Downl
res.Body.Close()
return
}
di = NewDownloadProxy(file)
di = newDownloadProxy(file)
fmt.Sscanf(res.Header.Get("Content-Length"), "%d", &di.TotalSize)
downloadKey := downManager.Put(di)
go func() {
Expand All @@ -270,11 +271,13 @@ func AsyncDownloadInto(url string, filepath string, autoRelease bool) (di *Downl
}

func ServeHTTP(port int) error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
m := mux.NewRouter()

m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello World!")
})

http.HandleFunc("/shell", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/shell", func(w http.ResponseWriter, r *http.Request) {
command := r.FormValue("command")
if command == "" {
command = r.FormValue("c")
Expand All @@ -287,24 +290,24 @@ func ServeHTTP(port int) error {
})
})

http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Finished!")
go func() {
time.Sleep(100 * time.Millisecond)
os.Exit(0)
}()
})

http.HandleFunc("/screenshot", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/screenshot", func(w http.ResponseWriter, r *http.Request) {
imagePath := "/data/local/tmp/minicap-screenshot.jpg"
if err := Screenshot(imagePath); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeFile(w, r, imagePath)
})
}).Methods("GET")

http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue("url")
filepath := r.FormValue("filepath")
res, err := goreq.Request{
Expand All @@ -322,7 +325,7 @@ func ServeHTTP(port int) error {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
di := NewDownloadProxy(file)
di := newDownloadProxy(file)
fmt.Sscanf(res.Header.Get("Content-Length"), "%d", &di.TotalSize)
downloadKey := downManager.Put(di)
go func() {
Expand All @@ -334,44 +337,49 @@ func ServeHTTP(port int) error {
io.WriteString(w, downloadKey)
})

http.HandleFunc("/uploadStats", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/uploadStats", func(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
di := downManager.Get(key)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(di)
})
}).Methods("GET")

http.HandleFunc("/install", func(w http.ResponseWriter, r *http.Request) {
m.HandleFunc("/install", func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue("url")
filepath := r.FormValue("filepath")
if filepath == "" {
filepath = "/sdcard/tmp.apk"
}
di, err := AsyncDownloadInto(url, filepath, false) // use false to disable DownloadProxy auto recycle
di, err := AsyncDownloadTo(url, filepath, false) // use false to disable DownloadProxy auto recycle
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
di.Wait() // wait download finished
if runtime.GOOS == "windows" {
log.Println("fake am install")
log.Println("fake pm install")
downManager.Del(di.Id)
return
}
// -g: grant all runtime permissions
output, err := runShell("pm", "install", "-r", "-g", filepath)
if err != nil {
di.Error = err.Error() + " >> " + string(output)
downManager.DelayDel(di.Id, time.Minute*10)
} else {
downManager.Del(di.Id)
}
downManager.DelayDel(di.Id, time.Minute*5)
}()
io.WriteString(w, di.Id)
}).Methods("POST")

m.HandleFunc("/install/{id}", func(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
dp := downManager.Get(id)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dp)
})

return http.ListenAndServe(":"+strconv.Itoa(port), nil)
return http.ListenAndServe(":"+strconv.Itoa(port), m)
}

func dnsLookupHost(hostname string) (ip net.IP, err error) {
Expand Down

0 comments on commit 2d8e98a

Please sign in to comment.