a geicko-2 based round robin ranking system designed to test c++ battleship submissions
battleship.dunkirk.sh
1package ssh
2
3import (
4 "io"
5 "net"
6 "os"
7 "path"
8 "sync"
9
10 gossh "golang.org/x/crypto/ssh"
11)
12
13const (
14 agentRequestType = "auth-agent-req@openssh.com"
15 agentChannelType = "auth-agent@openssh.com"
16
17 agentTempDir = "auth-agent"
18 agentListenFile = "listener.sock"
19)
20
21// contextKeyAgentRequest is an internal context key for storing if the
22// client requested agent forwarding
23var contextKeyAgentRequest = &contextKey{"auth-agent-req"}
24
25// SetAgentRequested sets up the session context so that AgentRequested
26// returns true.
27func SetAgentRequested(ctx Context) {
28 ctx.SetValue(contextKeyAgentRequest, true)
29}
30
31// AgentRequested returns true if the client requested agent forwarding.
32func AgentRequested(sess Session) bool {
33 return sess.Context().Value(contextKeyAgentRequest) == true
34}
35
36// NewAgentListener sets up a temporary Unix socket that can be communicated
37// to the session environment and used for forwarding connections.
38func NewAgentListener() (net.Listener, error) {
39 dir, err := os.MkdirTemp("", agentTempDir)
40 if err != nil {
41 return nil, err
42 }
43 l, err := net.Listen("unix", path.Join(dir, agentListenFile))
44 if err != nil {
45 return nil, err
46 }
47 return l, nil
48}
49
50// ForwardAgentConnections takes connections from a listener to proxy into the
51// session on the OpenSSH channel for agent connections. It blocks and services
52// connections until the listener stop accepting.
53func ForwardAgentConnections(l net.Listener, s Session) {
54 sshConn := s.Context().Value(ContextKeyConn).(gossh.Conn)
55 for {
56 conn, err := l.Accept()
57 if err != nil {
58 return
59 }
60 go func(conn net.Conn) {
61 defer conn.Close()
62 channel, reqs, err := sshConn.OpenChannel(agentChannelType, nil)
63 if err != nil {
64 return
65 }
66 defer channel.Close()
67 go gossh.DiscardRequests(reqs)
68 var wg sync.WaitGroup
69 wg.Add(2)
70 go func() {
71 io.Copy(conn, channel)
72 conn.(*net.UnixConn).CloseWrite()
73 wg.Done()
74 }()
75 go func() {
76 io.Copy(channel, conn)
77 channel.CloseWrite()
78 wg.Done()
79 }()
80 wg.Wait()
81 }(conn)
82 }
83}