-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevmirror.go
More file actions
184 lines (147 loc) · 3.73 KB
/
devmirror.go
File metadata and controls
184 lines (147 loc) · 3.73 KB
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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
)
import (
"github.com/ciju/gotunnel/gtclient"
l "github.com/ciju/gotunnel/log"
"github.com/ciju/vercheck"
)
var version string
// -- a small server to serve the allocated subdomain.
type ServerInfo string
func (s ServerInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(w, s)
}
func startServerInfo(s string) {
err := http.ListenAndServe("localhost:17171", ServerInfo(s+":8080"))
if err != nil {
l.Fatal("Couldn't start server to serve subdomain info")
}
l.Log("Subdomain info server started")
}
// -- end: a small server to serve the allocated subdomain.
var (
subdomain = flag.String("sub", "", "request subdomain to serve on")
skipVerCheck = flag.Bool("sc", false, "Skip new update check")
chrome_path = flag.String("c", "", "Chrome application path")
)
func Usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
flag.PrintDefaults()
}
// option to set chrome binary path
// different for each OS.
var (
chrome_darwin = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
)
func chromePath() string {
if runtime.GOOS == "darwin" {
return chrome_darwin
}
if *chrome_path != "" {
l.Log("chrome path %v %v", *chrome_path, runtime.GOOS)
return *chrome_path
}
fmt.Fprintf(os.Stderr, "Please provide chrome installation path. Use option -c ")
Usage()
os.Exit(1)
return ""
}
func dwndext(url, path string) bool {
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
out, err := os.Create(path)
if err != nil {
return false
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
l.Log("%s", err)
return false
}
return true
}
const (
extensionJSON = `{
"external_crx": "{{extensiondir}}/devmirror.crx",
"external_version": "2"
}`
extensionId = "bgpenplcdojibkncmhmjcobgclchgimd"
)
// chrome_path as option.
// cross-compile
func main() {
flag.Usage = Usage
flag.Parse()
if vercheck.HasMinorUpdate(
"https://raw.github.com/ciju/devmirror/master/VERSION",
version,
) {
l.Info(`
New version of Devmirror is available.
Please update the binary, or start with
option -sc to continue with this version.
`)
os.Exit(1)
}
// TODO: do a version check
servInfo, start := make(chan string), make(chan bool)
go func() {
if !gtclient.SetupClient("9222", "localtunnel.net:34000", *subdomain, servInfo) {
flag.Usage()
os.Exit(1)
}
}()
cpath := os.TempDir() + "chrome"
lpath := cpath + "/External Extensions"
os.MkdirAll(lpath, 0755)
// l.Log("the temp path", lpath)
go func() {
// download the extension from github.
giturl := "https://github.com/ciju/devmirror/raw/master/packed-extension"
dwndext(giturl+"/devmirror.crx", lpath+"/devmirror.crx")
config := strings.Replace(extensionJSON, "{{extensiondir}}", lpath, -1)
conffile := lpath + "/" + extensionId + ".json"
err := ioutil.WriteFile(conffile, []byte(config), 0755)
if err != nil {
fmt.Println("something went wrong while creating extension config %v", err)
os.Exit(1)
}
start <- true
}()
info := <-servInfo
go startServerInfo(info)
<-start
// open the browser
cmd := exec.Command(chromePath(),
"--user-data-dir="+cpath,
"--no-first-run",
"--remote-debugging-port=9222",
)
fmt.Printf("Your session is being devmirrored at: \033[1;34m%s\033[0m\n", info)
// l.Info("Starting Chrome browser : %+v", strings.Join(cmd.Args, " "))
_, err := cmd.CombinedOutput()
if err != nil {
l.Log("error %v", err)
}
err = cmd.Run()
if err != nil {
l.Log("Something went wrong", err)
}
}