This file is indexed.

/usr/share/gocode/src/github.com/alicebob/miniredis/server/server.go is in golang-github-alicebob-miniredis-dev 2.2.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
package server

import (
	"bufio"
	"fmt"
	"net"
	"strings"
	"sync"
)

func errUnknownCommand(cmd string) string {
	return fmt.Sprintf("ERR unknown command '%s'", strings.ToLower(cmd))
}

// Cmd is what Register expects
type Cmd func(c *Peer, cmd string, args []string)

// Server is a simple redis server
type Server struct {
	l         net.Listener
	cmds      map[string]Cmd
	peers     map[net.Conn]struct{}
	mu        sync.Mutex
	wg        sync.WaitGroup
	infoConns int
	infoCmds  int
}

// NewServer makes a server listening on addr. Close with .Close().
func NewServer(addr string) (*Server, error) {
	s := Server{
		cmds:  map[string]Cmd{},
		peers: map[net.Conn]struct{}{},
	}

	l, err := net.Listen("tcp", addr)
	if err != nil {
		return nil, err
	}
	s.l = l

	s.wg.Add(1)
	go func() {
		defer s.wg.Done()
		s.serve(l)
	}()
	return &s, nil
}

func (s *Server) serve(l net.Listener) {
	for {
		conn, err := l.Accept()
		if err != nil {
			return
		}
		s.wg.Add(1)
		go func() {
			defer s.wg.Done()
			defer conn.Close()
			s.mu.Lock()
			s.peers[conn] = struct{}{}
			s.infoConns++
			s.mu.Unlock()

			s.servePeer(conn)

			s.mu.Lock()
			delete(s.peers, conn)
			s.mu.Unlock()
		}()
	}
}

// Addr has the net.Addr struct
func (s *Server) Addr() *net.TCPAddr {
	s.mu.Lock()
	defer s.mu.Unlock()
	if s.l == nil {
		return nil
	}
	return s.l.Addr().(*net.TCPAddr)
}

// Close a server started with NewServer. It will wait until all clients are
// closed.
func (s *Server) Close() {
	s.mu.Lock()
	if s.l != nil {
		s.l.Close()
	}
	s.l = nil
	for c := range s.peers {
		c.Close()
	}
	s.mu.Unlock()
	s.wg.Wait()
}

// Register a command. It can't have been registered before. Safe to call on a
// running server.
func (s *Server) Register(cmd string, f Cmd) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	cmd = strings.ToUpper(cmd)
	if _, ok := s.cmds[cmd]; ok {
		return fmt.Errorf("command already registered: %s", cmd)
	}
	s.cmds[cmd] = f
	return nil
}

func (s *Server) servePeer(c net.Conn) {
	r := bufio.NewReader(c)
	cl := &Peer{
		w: bufio.NewWriter(c),
	}
	for {
		args, err := readArray(r)
		if err != nil {
			return
		}
		s.dispatch(cl, args)
		cl.w.Flush()
		if cl.closed {
			c.Close()
			return
		}
	}
}

func (s *Server) dispatch(c *Peer, args []string) {
	cmd, args := strings.ToUpper(args[0]), args[1:]
	s.mu.Lock()
	cb, ok := s.cmds[cmd]
	s.mu.Unlock()
	if !ok {
		c.WriteError(errUnknownCommand(cmd))
		return
	}

	s.mu.Lock()
	s.infoCmds++
	s.mu.Unlock()
	cb(c, cmd, args)
}

// TotalCommands is total (known) commands since this the server started
func (s *Server) TotalCommands() int {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.infoCmds
}

// ClientsLen gives the number of connected clients right now
func (s *Server) ClientsLen() int {
	s.mu.Lock()
	defer s.mu.Unlock()
	return len(s.peers)
}

// TotalConnections give the number of clients connected since the server
// started, including the currently connected ones
func (s *Server) TotalConnections() int {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.infoConns
}

// Peer is a client connected to the server
type Peer struct {
	w      *bufio.Writer
	closed bool
	Ctx    interface{} // anything goes, server won't touch this
}

// Flush the write buffer. Called automatically after every redis command
func (c *Peer) Flush() {
	c.w.Flush()
}

// Close the client connection after the current command is done.
func (c *Peer) Close() {
	c.closed = true
}

// WriteError writes a redis 'Error'
func (c *Peer) WriteError(e string) {
	fmt.Fprintf(c.w, "-%s\r\n", e)
}

// WriteInline writes a redis inline string
func (c *Peer) WriteInline(s string) {
	fmt.Fprintf(c.w, "+%s\r\n", s)
}

// WriteOK write the inline string `OK`
func (c *Peer) WriteOK() {
	c.WriteInline("OK")
}

// WriteBulk writes a bulk string
func (c *Peer) WriteBulk(s string) {
	fmt.Fprintf(c.w, "$%d\r\n%s\r\n", len(s), s)
}

// WriteNull writes a redis Null element
func (c *Peer) WriteNull() {
	fmt.Fprintf(c.w, "$-1\r\n")
}

// WriteLen starts an array with the given length
func (c *Peer) WriteLen(n int) {
	fmt.Fprintf(c.w, "*%d\r\n", n)
}

// WriteInt writes an integer
func (c *Peer) WriteInt(i int) {
	fmt.Fprintf(c.w, ":%d\r\n", i)
}