-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssh.go
167 lines (145 loc) · 3.45 KB
/
ssh.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
package main
import (
"context"
"crypto/ed25519"
"crypto/rand"
"fmt"
"log"
"net"
"github.com/anmitsu/go-shlex"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/server"
"golang.org/x/crypto/ssh"
)
func runSSH(dir, addr string) error {
config := &ssh.ServerConfig{
NoClientAuth: true,
}
_, edSigner, _ := ed25519.GenerateKey(rand.Reader)
sshSigner, _ := ssh.NewSignerFromSigner(edSigner)
config.AddHostKey(sshSigner)
log.Println("starting ssh server on", addr)
lis, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer lis.Close()
for {
conn, err := lis.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
defer conn.Close()
sshConn, chanc, reqc, err := ssh.NewServerConn(conn, config)
if err != nil {
log.Println(err)
return
}
defer sshConn.Close()
go ssh.DiscardRequests(reqc)
for chanr := range chanc {
switch chanr.ChannelType() {
case "session":
ch, reqc, err := chanr.Accept()
if err != nil {
log.Println(err)
return
}
handleSSHSession(dir, ch, reqc)
}
}
}(conn)
}
}
func handleSSHSession(dir string, ch ssh.Channel, reqc <-chan *ssh.Request) {
defer ch.Close()
var exitCode uint32
defer func() {
b := ssh.Marshal(struct{ Value uint32 }{exitCode})
ch.SendRequest("exit-status", false, b)
}()
envs := make(map[string]string)
for req := range reqc {
switch req.Type {
case "env":
payload := struct{ Key, Value string }{}
ssh.Unmarshal(req.Payload, &payload)
envs[payload.Key] = payload.Value
req.Reply(true, nil)
case "exec":
payload := struct{ Value string }{}
ssh.Unmarshal(req.Payload, &payload)
args, err := shlex.Split(payload.Value, true)
if err != nil {
log.Println("lex args", err)
exitCode = 1
return
}
cmd := args[0]
switch cmd {
case "git-upload-pack": // read
if gp := envs["GIT_PROTOCOL"]; gp != "version=2" {
log.Println("unhandled GIT_PROTOCOL", gp)
exitCode = 1
return
}
// TODO: get directory from args[1]
err := handleUploadPack(dir, ch)
if err != nil {
log.Println(err)
exitCode = 1
return
}
req.Reply(true, nil)
return
default:
req.Reply(false, nil)
exitCode = 1
return
}
default:
req.Reply(false, nil)
exitCode = 1
return
}
}
}
func handleUploadPack(dir string, ch ssh.Channel) error {
ctx := context.Background()
ep, err := transport.NewEndpoint("/")
if err != nil {
return fmt.Errorf("create transport endpoint: %w", err)
}
bfs := osfs.New(dir)
ld := server.NewFilesystemLoader(bfs)
svr := server.NewServer(ld)
sess, err := svr.NewUploadPackSession(ep, nil)
if err != nil {
return fmt.Errorf("create upload-pack session: %w", err)
}
ar, err := sess.AdvertisedReferencesContext(ctx)
if err != nil {
return fmt.Errorf("get advertised references: %w", err)
}
err = ar.Encode(ch)
if err != nil {
return fmt.Errorf("encode advertised references: %w", err)
}
upr := packp.NewUploadPackRequest()
err = upr.Decode(ch)
if err != nil {
return fmt.Errorf("decode upload-pack request: %w", err)
}
res, err := sess.UploadPack(ctx, upr)
if err != nil {
return fmt.Errorf("create upload-pack response: %w", err)
}
err = res.Encode(ch)
if err != nil {
return fmt.Errorf("encode upload-pack response: %w", err)
}
return nil
}