This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmain.go
216 lines (193 loc) · 5.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/facebookgo/freeport"
"github.com/gobuild/log"
"github.com/gorilla/mux"
accesslog "github.com/mash/go-accesslog"
flag "github.com/ogier/pflag"
"github.com/openatx/wdaproxy/web"
_ "github.com/shurcooL/vfsgen"
)
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
var (
version = "develop"
lisPort = 8100
pWda string
udid string
yosemiteServer string
yosemiteGroup string
debug bool
rt = mux.NewRouter()
udidNames = map[string]string{}
)
type statusResp struct {
Value map[string]interface{} `json:"value,omitempty"`
SessionId string `json:"sessionId,omitempty"`
Status int `json:"status"`
}
func getUdid() string {
if udid != "" {
return udid
}
output, err := exec.Command("idevice_id", "-l").Output()
if err != nil {
panic(err)
}
return strings.TrimSpace(string(output))
}
func assetsContent(name string) string {
fd, err := web.Assets.Open(name)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(fd)
if err != nil {
panic(err)
}
return string(data)
}
type Device struct {
Udid string `json:"serial"`
Manufacturer string `json:"manufacturer"`
}
// LocalIP returns the non loopback local IP of the host
func LocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func main() {
showVer := flag.BoolP("version", "v", false, "Print version")
flag.IntVarP(&lisPort, "port", "p", 8100, "Proxy listen port")
flag.StringVarP(&udid, "udid", "u", "", "device udid")
flag.StringVarP(&pWda, "wda", "W", "", "WebDriverAgent project directory [optional]")
flag.BoolVarP(&debug, "debug", "d", false, "Open debug mode")
// flag.StringVarP(&yosemiteServer, "yosemite-server", "S",
// os.Getenv("YOSEMITE_SERVER"),
// "server center(not open source yet")
// flag.StringVarP(&yosemiteGroup, "yosemite-group", "G",
// "everyone",
// "server center group")
flag.Parse()
if udid == "" {
udid = getUdid()
}
if *showVer {
println(version)
return
}
lis, err := net.Listen("tcp", ":"+strconv.Itoa(lisPort))
if err != nil {
log.Fatal(err)
}
// if yosemiteServer != "" {
// mockIOSProvider()
// }
errC := make(chan error)
freePort, err := freeport.Get()
if err != nil {
log.Fatal(err)
}
log.Printf("freeport %d", freePort)
go func() {
log.Printf("launch tcp-proxy, listen on %d", lisPort)
targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(freePort))
rt.HandleFunc("/wd/hub/{path:.*}", NewAppiumProxyHandlerFunc(targetURL))
rt.HandleFunc("/{path:.*}", NewReverseProxyHandlerFunc(targetURL))
errC <- http.Serve(lis, accesslog.NewLoggingHandler(rt, HTTPLogger{}))
}()
go func() {
log.Printf("launch iproxy (udid: %s)", strconv.Quote(udid))
c := exec.Command("iproxy", strconv.Itoa(freePort), "8100")
if udid != "" {
c.Args = append(c.Args, udid)
}
errC <- c.Run()
}()
go func(udid string) {
if pWda == "" {
return
}
// device name
nameBytes, _ := exec.Command("idevicename", "-u", udid).Output()
deviceName := strings.TrimSpace(string(nameBytes))
udidNames[udid] = deviceName
log.Printf("device name: %s", deviceName)
log.Printf("launch WebDriverAgent(dir=%s)", pWda)
c := exec.Command("xcodebuild",
"-verbose",
"-project", "WebDriverAgent.xcodeproj",
"-scheme", "WebDriverAgentRunner",
"-destination", "id="+udid, "test-without-building") // test-without-building
c.Dir, _ = filepath.Abs(pWda)
// Test Suite 'All tests' started at 2017-02-27 15:55:35.263
// Test Suite 'WebDriverAgentRunner.xctest' started at 2017-02-27 15:55:35.266
// Test Suite 'UITestingUITests' started at 2017-02-27 15:55:35.267
// Test Case '-[UITestingUITests testRunner]' started.
// t = 0.00s Start Test at 2017-02-27 15:55:35.270
// t = 0.01s Set Up
pipeReader, writer := io.Pipe()
c.Stdout = writer
c.Stderr = writer
c.Stdin = os.Stdin
bufrd := bufio.NewReader(pipeReader)
if err = c.Start(); err != nil {
log.Fatal(err)
}
// close writers when xcodebuild exit
go func() {
c.Wait()
writer.Close()
}()
lineStr := ""
for {
line, isPrefix, err := bufrd.ReadLine()
if isPrefix {
lineStr = lineStr + string(line)
continue
} else {
lineStr = string(line)
}
lineStr = strings.TrimSpace(string(line))
if debug {
fmt.Printf("[WDA] %s\n", lineStr)
}
if err != nil {
log.Fatal("[WDA] exit", err)
}
if strings.Contains(lineStr, "Successfully wrote Manifest cache to") {
log.Println("[WDA] test ipa successfully generated")
}
if strings.HasPrefix(lineStr, "Test Case '-[UITestingUITests testRunner]' started") {
log.Println("[WDA] successfully started")
}
lineStr = "" // reset str
}
}(udid)
log.Printf("Open webbrower with http://%s:%d", LocalIP(), lisPort)
log.Fatal(<-errC)
}