a geicko-2 based round robin ranking system designed to test c++ battleship submissions
battleship.dunkirk.sh
1package ssh
2
3import (
4 "crypto/subtle"
5 "net"
6
7 gossh "golang.org/x/crypto/ssh"
8)
9
10type Signal string
11
12// POSIX signals as listed in RFC 4254 Section 6.10.
13const (
14 SIGABRT Signal = "ABRT"
15 SIGALRM Signal = "ALRM"
16 SIGFPE Signal = "FPE"
17 SIGHUP Signal = "HUP"
18 SIGILL Signal = "ILL"
19 SIGINT Signal = "INT"
20 SIGKILL Signal = "KILL"
21 SIGPIPE Signal = "PIPE"
22 SIGQUIT Signal = "QUIT"
23 SIGSEGV Signal = "SEGV"
24 SIGTERM Signal = "TERM"
25 SIGUSR1 Signal = "USR1"
26 SIGUSR2 Signal = "USR2"
27)
28
29// DefaultHandler is the default Handler used by Serve.
30var DefaultHandler Handler
31
32// Option is a functional option handler for Server.
33type Option func(*Server) error
34
35// Handler is a callback for handling established SSH sessions.
36type Handler func(Session)
37
38// BannerHandler is a callback for displaying the server banner.
39type BannerHandler func(ctx Context) string
40
41// PublicKeyHandler is a callback for performing public key authentication.
42type PublicKeyHandler func(ctx Context, key PublicKey) bool
43
44// PasswordHandler is a callback for performing password authentication.
45type PasswordHandler func(ctx Context, password string) bool
46
47// KeyboardInteractiveHandler is a callback for performing keyboard-interactive authentication.
48type KeyboardInteractiveHandler func(ctx Context, challenger gossh.KeyboardInteractiveChallenge) bool
49
50// PtyHandler is a callback for handling PTY allocation requests.
51type PtyHandler func(ctx Context, s Session, pty Pty) (func() error, error)
52
53// PtyCallback is a hook for handling PTY allocation requests.
54type PtyCallback func(ctx Context, req Pty) bool
55
56// SessionRequestCallback is a callback for allowing or denying SSH sessions.
57type SessionRequestCallback func(sess Session, requestType string) bool
58
59// ConnCallback is a hook for new connections before handling.
60// It allows wrapping for timeouts and limiting by returning
61// the net.Conn that will be used as the underlying connection.
62type ConnCallback func(ctx Context, conn net.Conn) net.Conn
63
64// LocalPortForwardingCallback is a hook for allowing port forwarding
65type LocalPortForwardingCallback func(ctx Context, destinationHost string, destinationPort uint32) bool
66
67// ReversePortForwardingCallback is a hook for allowing reverse port forwarding
68type ReversePortForwardingCallback func(ctx Context, bindHost string, bindPort uint32) bool
69
70// ServerConfigCallback is a hook for creating custom default server configs
71type ServerConfigCallback func(ctx Context) *gossh.ServerConfig
72
73// ConnectionFailedCallback is a hook for reporting failed connections
74// Please note: the net.Conn is likely to be closed at this point
75type ConnectionFailedCallback func(conn net.Conn, err error)
76
77// Window represents the size of a PTY window.
78//
79// From https://datatracker.ietf.org/doc/html/rfc4254#section-6.2
80//
81// Zero dimension parameters MUST be ignored. The character/row dimensions
82// override the pixel dimensions (when nonzero). Pixel dimensions refer
83// to the drawable area of the window.
84type Window struct {
85 // Width is the number of columns.
86 // It overrides WidthPixels.
87 Width int
88 // Height is the number of rows.
89 // It overrides HeightPixels.
90 Height int
91
92 // WidthPixels is the drawable width of the window, in pixels.
93 WidthPixels int
94 // HeightPixels is the drawable height of the window, in pixels.
95 HeightPixels int
96}
97
98// Pty represents a PTY request and configuration.
99type Pty struct {
100 impl
101
102 // Term is the TERM environment variable value.
103 Term string
104
105 // Window is the Window sent as part of the pty-req.
106 Window Window
107
108 // Modes represent a mapping of Terminal Mode opcode to value as it was
109 // requested by the client as part of the pty-req. These are outlined as
110 // part of https://datatracker.ietf.org/doc/html/rfc4254#section-8.
111 //
112 // The opcodes are defined as constants in golang.org/x/crypto/ssh (VINTR,VQUIT,etc.).
113 // Boolean opcodes have values 0 or 1.
114 //
115 // Note: golang.org/x/crypto/ssh currently (2022-03-12) doesn't have a
116 // definition for opcode 42 "iutf8" which was introduced in https://datatracker.ietf.org/doc/html/rfc8160.
117 Modes gossh.TerminalModes
118}
119
120// Serve accepts incoming SSH connections on the listener l, creating a new
121// connection goroutine for each. The connection goroutines read requests and
122// then calls handler to handle sessions. Handler is typically nil, in which
123// case the DefaultHandler is used.
124func Serve(l net.Listener, handler Handler, options ...Option) error {
125 srv := &Server{Handler: handler}
126 for _, option := range options {
127 if err := srv.SetOption(option); err != nil {
128 return err
129 }
130 }
131 return srv.Serve(l)
132}
133
134// ListenAndServe listens on the TCP network address addr and then calls Serve
135// with handler to handle sessions on incoming connections. Handler is typically
136// nil, in which case the DefaultHandler is used.
137func ListenAndServe(addr string, handler Handler, options ...Option) error {
138 srv := &Server{Addr: addr, Handler: handler}
139 for _, option := range options {
140 if err := srv.SetOption(option); err != nil {
141 return err
142 }
143 }
144 return srv.ListenAndServe()
145}
146
147// Handle registers the handler as the DefaultHandler.
148func Handle(handler Handler) {
149 DefaultHandler = handler
150}
151
152// KeysEqual is constant time compare of the keys to avoid timing attacks.
153func KeysEqual(ak, bk PublicKey) bool {
154 // avoid panic if one of the keys is nil, return false instead
155 if ak == nil || bk == nil {
156 return false
157 }
158
159 a := ak.Marshal()
160 b := bk.Marshal()
161 return (len(a) == len(b) && subtle.ConstantTimeCompare(a, b) == 1)
162}