/
proxy.go
189 lines (163 loc) · 3.68 KB
/
proxy.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
// This file is part of Gate.
// Copyright (C) 2012-2015 Cyril Adrian <cyril.adrian@gmail.com>
//
// Gate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Gate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gate. If not, see <http://www.gnu.org/licenses/>.
package client
// Access to the server
import (
"gate/client/ui"
"gate/core"
"gate/core/errors"
"gate/core/exec"
"gate/server"
serverimpl "gate/server/impl"
)
import (
"fmt"
"io"
"os"
osexec "os/exec"
"strings"
"time"
)
var _proxy server.Server
func dirname() (result string) {
path := strings.Split(os.Args[0], "/")
if len(path) > 1 {
result = strings.Join(path[:len(path)-1], "/")
}
return
}
func startServer() (err error) {
pipe := make(chan io.WriteCloser, 1)
prepare := func(cmd *exec.Cmd) (err error) {
p, err := cmd.StdinPipe()
if err != nil {
return errors.Decorated(err)
}
pipe <- p
return
}
run := func(cmd *exec.Cmd) (err error) {
p := <-pipe
dir := dirname()
var exe string
if dir == "" {
exe, err = osexec.LookPath("server")
if err != nil {
return errors.Decorated(err)
}
} else {
exe = fmt.Sprintf("%s/server", dir)
}
var rc string
if len(os.Args) > 1 {
rc = os.Args[1]
}
p.Write([]byte(fmt.Sprintf("#!/bin/bash\n%s \"%s\" > /tmp/server-%s.log 2>&1 & disown\n", exe, rc, time.Now().Format("20060102150405"))))
err = p.Close()
if err != nil {
return errors.Decorated(err)
}
return
}
err = exec.Command(prepare, run, "at", "now")
if err != nil {
return
}
return
}
func readNewMaster(mmi ui.UserInteraction, reason string) (result string, err error) {
var pass1, pass2, text string
text = fmt.Sprintf("%s,\nplease enter an encryption phrase.", reason)
for result == "" {
pass1, err = mmi.ReadPassword(text)
if err != nil {
return
}
pass2, err = mmi.ReadPassword("Please enter the same encryption phrase again.")
if err != nil {
return
}
if pass1 == pass2 {
result = pass1
} else {
text = fmt.Sprintf("Your phrases did not match.\n%s,\nplease enter an encryption phrase.", reason)
}
}
return
}
func openVault(srv server.Server, config core.Config) (err error) {
xdg, err := config.Xdg()
if err != nil {
return
}
data_home, err := xdg.DataHome()
if err != nil {
return
}
vault_path := fmt.Sprintf("%s/vault", data_home)
vault_info, err := os.Stat(vault_path)
if err != nil {
return errors.Decorated(err)
}
mmi, err := ui.Ui(srv, config)
if err != nil {
return
}
var master string
if vault_info == nil {
master, err = readNewMaster(mmi, "This is a new vault")
if err != nil {
return
}
} else {
master, err = mmi.ReadPassword("Please enter your encryption phrase\nto open the password vault.")
if err != nil {
return
}
}
var isopen bool
err = srv.Open(master, &isopen)
if err != nil {
return
}
if !isopen {
return errors.New("Could not open vault")
}
return
}
func proxy(config core.Config) (result server.Server, err error) {
result = _proxy
if result == nil {
s, err := serverimpl.Proxy(config, startServer)
if err != nil {
return
}
var isopen bool
err = s.IsOpen(false, &isopen)
if err != nil {
err = errors.Decorated(err)
return
}
if !isopen {
err = openVault(s, config)
if err != nil {
return
}
}
result = s
_proxy = result
}
return
}