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

Commit

Permalink
use net.dns1 as dns server and fallback to 8.8.8.8, add screenrecord …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
codeskyblue committed Nov 29, 2017
1 parent 02858c5 commit ada1da1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
14 changes: 10 additions & 4 deletions initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
httpServer *http.Server
)

func dnsLookupHost(hostname string) (ip net.IP, err error) {
func dnsLookupHost(hostname string, dnsServer string) (ip net.IP, err error) {
if !strings.HasSuffix(hostname, ".") {
hostname += "."
}
Expand All @@ -33,7 +33,8 @@ func dnsLookupHost(hostname string) (ip net.IP, err error) {
}
c := new(dns.Client)
c.SingleInflight = true
in, _, err := c.Exchange(m1, "8.8.8.8:53")

in, _, err := c.Exchange(m1, dnsServer+":53")
if err != nil {
return nil, err
}
Expand All @@ -45,7 +46,7 @@ func dnsLookupHost(hostname string) (ip net.IP, err error) {
return t.A, nil
}
if t, ok := in.Answer[0].(*dns.CNAME); ok {
return dnsLookupHost(t.Target)
return dnsLookupHost(t.Target, dnsServer)
}
return nil, errors.New("dns resolve failed: " + hostname)
}
Expand All @@ -56,11 +57,16 @@ func init() {
KeepAlive: 30 * time.Second,
DualStack: true,
}
// get default dns server
dnsServer := getProperty("net.dns1")
if dnsServer == "" {
dnsServer = "8.8.8.8"
}
// manualy dns resolve
newDialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(addr)
if net.ParseIP(host) == nil {
ip, err := dnsLookupHost(host)
ip, err := dnsLookupHost(host, dnsServer)
if err != nil {
return nil, err
}
Expand Down
66 changes: 65 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,76 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
dp := downManager.Get(id)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dp)
})
}).Methods("GET")

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

// FIXME(ssx): screenrecord is not good enough, need to change later
var recordCmd *exec.Cmd
var recordDone = make(chan bool, 1)
var recordLock sync.Mutex
var recordFolder = "/sdcard/screenrecords/"
var recordRunning = false

m.HandleFunc("/screenrecord", func(w http.ResponseWriter, r *http.Request) {
recordLock.Lock()
defer recordLock.Unlock()

if recordCmd != nil {
http.Error(w, "screenrecord not closed", 400)
return
}
os.RemoveAll(recordFolder)
os.MkdirAll(recordFolder, 0755)
recordCmd = exec.Command("screenrecord", recordFolder+"0.mp4")
if err := recordCmd.Start(); err != nil {
http.Error(w, err.Error(), 500)
return
}
recordRunning = true
go func() {
for i := 1; recordCmd.Wait() == nil && i <= 20 && recordRunning; i++ { // set limit, to prevent too many videos. max 1 hour
recordCmd = exec.Command("screenrecord", recordFolder+strconv.Itoa(i)+".mp4")
if err := recordCmd.Start(); err != nil {
log.Println("screenrecord error:", err)
break
}
}
recordDone <- true
}()
io.WriteString(w, "screenrecord started")
}).Methods("POST")

m.HandleFunc("/screenrecord", func(w http.ResponseWriter, r *http.Request) {
recordLock.Lock()
defer recordLock.Unlock()

recordRunning = false
if recordCmd != nil {
if recordCmd.Process != nil {
recordCmd.Process.Signal(os.Interrupt)
}
select {
case <-recordDone:
case <-time.After(5 * time.Second):
// force kill
exec.Command("pkill", "screenrecord").Run()
}
recordCmd = nil
}
w.Header().Set("Content-Type", "application/json")
files, _ := ioutil.ReadDir(recordFolder)
videos := []string{}
for i := 0; i < len(files); i++ {
videos = append(videos, fmt.Sprintf(recordFolder+"%d.mp4", i))
}
json.NewEncoder(w).Encode(map[string]interface{}{
"videos": videos,
})
}).Methods("PUT")

m.HandleFunc("/upgrade", func(w http.ResponseWriter, r *http.Request) {
ver := r.FormValue("version")
var err error
Expand Down
3 changes: 2 additions & 1 deletion tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"strings"
"time"

"github.com/franela/goreq"
Expand All @@ -28,7 +29,7 @@ func getDeviceInfo() *proto.DeviceInfo {
devInfo.Battery = battery

// Udid is ${Serial}-${MacAddress}-${model}
udid := getProperty("ro.serialno") + "-" + devInfo.HWAddr + "-" + getProperty("ro.product.model")
udid := getProperty("ro.serialno") + "-" + devInfo.HWAddr + "-" + strings.Replace(getProperty("ro.product.model"), " ", "_", -1)
devInfo.Udid = udid
currentDeviceInfo = devInfo
}
Expand Down

0 comments on commit ada1da1

Please sign in to comment.