-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitsam.go
346 lines (323 loc) · 9.59 KB
/
gitsam.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package gitsam
import (
"crypto/sha256"
"encoding/base64"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/eyedeekay/eephttpd"
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/config/helpers"
"github.com/eyedeekay/sam-forwarder/interface"
"github.com/eyedeekay/sam-forwarder/tcp"
"github.com/gliderlabs/ssh"
"github.com/sosedoff/gitkit"
)
//GitSAMTunnel is a structure which automatically configured the forwarding of
//a local service to i2p over the SAM API.
type GitSAMTunnel struct {
*samforwarder.SAMForwarder
*gitkit.SSH
GitConf gitkit.Config
OptPage *eephttpd.EepHttpd
PubKeyPath string
SecurePath string
PagePort string
page bool
up bool
prex bool
}
var err error
func (s *GitSAMTunnel) PRBytes() []byte {
r := "#!/bin/sh"
r += "GIT_WORK_TREE=" + s.GitConf.Dir + " git checkout -f master\n"
r += "GIT_WORK_TREE=" + s.GitConf.Dir + " git branch -D pages\n"
r += "GIT_WORK_TREE=" + s.GitConf.Dir + " git checkout -f pages\n"
r += "GIT_WORK_TREE=" + s.GitConf.Dir + " git merge origin/master\n"
return []byte(r)
}
func (s *GitSAMTunnel) PUBytes() []byte {
r := "#!/bin/sh"
r += "GIT_WORK_TREE=" + s.GitConf.Dir + " git update-server-info -f\n"
return []byte(r)
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if info != nil {
return !info.IsDir()
}
return false
}
func (s *GitSAMTunnel) ListDirs() ([]os.FileInfo, error) {
unclassedfiles, err := ioutil.ReadDir(s.GitConf.Dir)
if err != nil {
return nil, err
}
var directories []os.FileInfo
for _, unclassedfile := range unclassedfiles {
if unclassedfile.IsDir() {
log.Println("FoundDir", unclassedfile.Name())
directories = append(directories, unclassedfile)
}
}
gitdir, err := os.Stat(s.GitConf.Dir)
if err != nil {
return nil, err
}
if gitdir.IsDir() {
directories = append(directories, gitdir)
}
return directories, nil
}
func (s *GitSAMTunnel) AssurePostRecieve() error {
if !s.page {
return nil
}
files, err := s.ListDirs()
if err != nil {
return err
}
for _, dir := range files {
info, err := os.Stat(filepath.Join(s.GitConf.Dir, dir.Name(), ".git"))
if err == nil {
if info.IsDir() {
log.Println(filepath.Join(s.GitConf.Dir, dir.Name(), ".git"), "is a directory")
dirpath := filepath.Join(s.GitConf.Dir, dir.Name())
if strings.HasSuffix(s.GitConf.Dir, dir.Name()) {
dirpath = s.GitConf.Dir
}
cmd := exec.Command("git", "checkout", "-b", "pages")
cmd.Dir = dirpath
cmd.Env = []string{"GIT_WORK_TREE=" + dirpath}
if err := cmd.Run(); err != nil {
log.Println(err)
}
log.Println("checked out page branch for static copy", cmd.Dir)
cmd = exec.Command("git", "update-server-info")
cmd.Dir = dirpath
cmd.Env = []string{"GIT_WORK_TREE=" + dirpath}
if err := cmd.Run(); err != nil {
return err
}
log.Println("Updated git server info for static copy", cmd.Dir)
if err := os.MkdirAll(filepath.Join(s.GitConf.Dir, dir.Name(), "/hooks"), 0755); err != nil {
return err
} else {
if !FileExists(filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve")) {
log.Println("The hooks file did not exist, creating one", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve"))
if err := ioutil.WriteFile(filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve"), s.PRBytes(), 0755); err != nil {
s.prex = true
return err
} else {
log.Println("Created the hooks file:", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve"))
}
} else {
log.Println("hooks file", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve"), "Already exists, leaving it alone")
}
if !FileExists(filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-update")) {
log.Println("The hooks file did not exist, creating one", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-update"))
if err := ioutil.WriteFile(filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-update"), s.PUBytes(), 0755); err != nil {
s.prex = true
return err
} else {
log.Println("Created the hooks file:", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-update"))
}
} else {
log.Println("hooks file", filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-update"), "Already exists, leaving it alone")
}
}
} else {
log.Println(filepath.Join(s.GitConf.Dir, dir.Name(), ".git"), "is not a directory")
}
} else if os.IsNotExist(err) {
log.Println(filepath.Join(s.GitConf.Dir, dir.Name(), ".git"), "does not exist")
} else {
return err
}
}
return nil
}
func (s *GitSAMTunnel) DeletePostRecieve() error {
if s.prex {
files, err := ioutil.ReadDir(s.GitConf.Dir)
if err != nil {
return err
}
gitdir, err := os.Stat(s.GitConf.Dir)
if err != nil {
return err
}
files = append(files, gitdir)
for _, dir := range files {
if err := os.Remove(filepath.Join(s.GitConf.Dir, dir.Name(), "hooks", "post-recieve")); err != nil {
return err
}
}
}
return nil
}
func (s *GitSAMTunnel) AssureGitIgnore() error {
PubKeyPath, err := filepath.Rel(s.GitConf.Dir, s.PubKeyPath)
if err != nil {
return err
}
if filepath.Dir(s.PubKeyPath) == s.GitConf.Dir {
if FileExists(filepath.Join(s.GitConf.Dir, "/.gitignore")) {
if bytes, err := ioutil.ReadFile(filepath.Join(s.GitConf.Dir, "/.gitignore")); err == nil {
if !strings.Contains(string(bytes), s.PubKeyPath) {
f, err := os.OpenFile(filepath.Join(s.GitConf.Dir, "/.gitignore"), os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(PubKeyPath); err != nil {
return err
}
}
} else {
return err
}
} else {
if err := ioutil.WriteFile(filepath.Join(s.GitConf.Dir, "/.gitignore"), []byte(PubKeyPath+"\n"), 0644); err != nil {
return err
}
}
}
return nil
}
func (f *GitSAMTunnel) LookupKey(content string) (*gitkit.PublicKey, error) {
textkey, err := ioutil.ReadFile(f.PubKeyPath)
log.Println("This is the content\n", content, "\n", string(textkey))
if !strings.HasPrefix(string(textkey), content) {
log.Println(
"\nContent:",
content,
"\nTextKey:",
string(textkey),
)
return nil, nil
}
if err != nil {
return nil, err
}
key, _, _, _, err := ssh.ParseAuthorizedKey(textkey)
if err != nil {
return nil, err
}
hash := sha256.Sum256(key.Marshal())
print := base64.StdEncoding.EncodeToString(hash[:])
return &gitkit.PublicKey{Fingerprint: print}, nil
}
func (f *GitSAMTunnel) GetType() string {
return "gitsam"
}
func (f *GitSAMTunnel) ServeParent() {
log.Println("Starting eepssh server", f.Base32())
if err = f.SAMForwarder.Serve(); err != nil {
log.Println(err)
f.Cleanup()
}
}
func (f *GitSAMTunnel) ServePage() {
if err = f.OptPage.Serve(); err != nil {
log.Println(err)
f.Cleanup()
}
}
//Serve starts the SAM connection and and forwards the local host:port to i2p
func (f *GitSAMTunnel) Serve() error {
if f.Up() {
go f.ServePage()
go f.ServeParent()
log.Println("Starting ssh server", f.Target())
if err := f.SSH.ListenAndServe(f.Target()); err != nil {
return err
}
}
return nil
}
func (f *GitSAMTunnel) Up() bool {
return f.up
}
//Close shuts the whole thing down.
func (f *GitSAMTunnel) Close() error {
if err := f.DeletePostRecieve(); err != nil {
return err
}
return f.SAMForwarder.Close()
}
func (s *GitSAMTunnel) Load() (samtunnel.SAMTunnel, error) {
if !s.Up() {
log.Println("Started putting tunnel up", s.Conf.SaveDirectory)
}
f, e := s.SAMForwarder.Load()
if e != nil {
return nil, e
}
s.Conf.ServeDirectory = s.GitConf.Dir
s.SAMForwarder = f.(*samforwarder.SAMForwarder)
s.GitConf.KeyDir = s.SecurePath
s.SSH = gitkit.NewSSH(s.GitConf)
s.SSH.PublicKeyLookupFunc = s.LookupKey
if err := s.AssureGitIgnore(); err != nil {
return nil, err
}
if err := s.AssurePostRecieve(); err != nil {
return nil, err
}
log.Println("Finished putting tunnel up")
s.up = true
return s, nil
}
//NewGitSAMTunnel makes a new SAM forwarder with default options, accepts host:port arguments
func NewGitSAMTunnel(host, port string) (*GitSAMTunnel, error) {
return NewGitSAMTunnelFromOptions(SetHost(host), SetSSHPort(port))
}
//NewGitSAMTunnelFromOptions makes a new SAM forwarder with default options, accepts host:port arguments
func NewGitSAMTunnelFromOptions(opts ...func(*GitSAMTunnel) error) (*GitSAMTunnel, error) {
var s GitSAMTunnel
s.SAMForwarder = &samforwarder.SAMForwarder{}
s.Conf = &i2ptunconf.Conf{}
s.OptPage = &eephttpd.EepHttpd{}
//s.OptPage.SAMForwarder = &samforwarder.SAMForwarder{}
s.GitConf = gitkit.Config{}
s.SSH = &gitkit.SSH{}
s.page = true
s.up = false
log.Println("Initializing gitsam")
for _, o := range opts {
if err := o(&s); err != nil {
return nil, err
}
}
s.SAMForwarder.Config().SaveFile = true
var err error
if s.SecurePath == s.GitConf.Dir {
s.SecurePath = filepath.Dir(s.GitConf.Dir) + "/.gitsam_secure"
s.SAMForwarder.Config().SaveDirectory = s.SecurePath
s.SAMForwarder.Config().FilePath = s.SecurePath
}
s.GitConf.Auth = true
conf := *s.Conf
conf.CloseIdleTime = 6000000
conf.TargetPort = s.PagePort
conf.TunName = s.ID() + "-eephttpd"
conf.SaveDirectory = s.Conf.SaveDirectory
conf.Type = "server"
log.Println("Setting up secure path", s.Conf.SaveDirectory, conf.SaveDirectory)
if s.OptPage, err = i2ptunhelper.NewEepHttpdFromConf(&conf); err != nil {
return nil, err
}
l, e := s.Load()
//log.Println("Options loaded", s.Print())
if e != nil {
return nil, e
}
return l.(*GitSAMTunnel), nil
}