-
Notifications
You must be signed in to change notification settings - Fork 7
/
ircdog.go
622 lines (556 loc) · 18 KB
/
ircdog.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Copyright (c) 2017 Daniel Oaks <[email protected]>
// released under the ISC license
package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
docopt "github.com/docopt/docopt-go"
supportscolor "github.com/jwalton/go-supportscolor"
"github.com/ergochat/irc-go/ircfmt"
"github.com/ergochat/irc-go/ircmsg"
libconsole "github.com/ergochat/ircdog/console"
"github.com/ergochat/ircdog/lib"
)
// set via linker flags, either by make or by goreleaser:
var commit = "" // git hash
var version = "" // tagged version
const (
usage = `ircdog connects to an IRC server, then sends and receives raw IRC protocol
lines. Its interface is similar to telnet or netcat, but by default, it
automatically responds to PING messages from the server, keeping your
connection alive without the need for active user input. It also renders IRC
formatting codes for terminal display.
Usage:
ircdog <host> [<port>] [options]
ircdog -h | --help
ircdog --version
The <host> argument can be a URL, in which case the <port> argument is optional.
wss:// (WebSocket over TLS), ws:// (WebSocket over plaintext), ircs:// (IRC over
TLS), and irc:// (IRC over plaintext) URLs are accepted.
Sending Escapes:
ircdog supports escape sequences in its input (use --raw to disable this).
The following are case-sensitive:
---------------------------------
Name | Escape | Raw
---------------------------------
CTCP Escape | [[CTCP]] | 0x01
Bold | [[B]] | 0x02
Colour | [[C]] | 0x03
Monospace | [[M]] | 0x11
Italic | [[I]] | 0x1d
Strikethrough | [[S]] | 0x1e
Underscore | [[U]] | 0x1f
Reset | [[R]] | 0x0f
C hex escape | [[\x??]] | 0x??
---------------------------------
Options:
--tls Connect using TLS.
--tls-noverify Don't verify the provided TLS certificates.
--client-cert=<file> A file containing a TLS client cert & key, to use for TLS connections.
--listen=<address> Listen on an address like ":7778", pass through traffic.
--hide=<messages> Comma-separated list of commands/numerics to not print.
--origin=<url> URL to send as the Origin header for a WebSocket connection.
-r --raw Don't interpret incoming formatting codes or outgoing escapes.
--transcript=<file> Append a transcript of raw traffic to a file.
--escape Display incoming lines with irc-go escapes:
https://pkg.go.dev/github.com/goshuirc/irc-go/ircfmt
--italics Enable ANSI italics codes (not widely supported).
--color=<mode> Override detected color support ('none', '16', '256').
--no-readline Disable readline support.
--script=<file> Read an initial list of commands to send from a file.
--reconnect=<time> If disconnected unexpectedly, reconnect after a pause
('30' for 30 seconds, '5m' for 5 minutes, etc.)
-p --nopings Don't automatically respond to incoming pings.
-v --verbose Output additional loglines.
-h --help Show this screen.
--version Show version.`
)
func parsePort(portStr string) (port int, err error) {
if port, pErr := strconv.Atoi(portStr); pErr == nil && 1 <= port && port <= 65535 {
return port, nil
} else {
return 0, fmt.Errorf("Invalid port number `%s`", portStr)
}
}
func parseConnectionConfig(arguments map[string]any) (config lib.ConnectionConfig, err error) {
tlsNoverify := arguments["--tls-noverify"].(bool)
config.TLS = arguments["--tls"].(bool) || tlsNoverify
host := arguments["<host>"].(string)
u, uErr := url.Parse(host)
if uErr != nil {
err = fmt.Errorf("Invalid host: %w", uErr)
return
}
if u.Scheme == "https" {
u.Scheme = "wss"
} else if u.Scheme == "http" {
u.Scheme = "ws"
}
if u.Scheme == "" {
// bare hostname, not a URL
config.Host = strings.TrimPrefix(host, "unix:")
portstring := arguments["<port>"]
if portstring == nil {
if config.TLS {
config.Port = 6697
} else if !strings.HasPrefix(host, "/") {
err = fmt.Errorf("An explicit port number is required for plaintext (try 6667)")
return
}
} else {
config.Port, err = parsePort(portstring.(string))
if err != nil {
return
}
}
} else if u.Scheme == "ws" || u.Scheme == "wss" {
// WebsocketURL supersedes Host and Port options
config.WebsocketURL = host
if config.TLS && u.Scheme == "ws" {
err = fmt.Errorf("To enable TLS on a WebSocket URL, use the scheme wss://")
return
}
} else if u.Scheme == "irc" || u.Scheme == "ircs" {
// ircs:// switches TLS on, but so should --tls with an irc:// URL
if u.Scheme == "ircs" {
config.TLS = true
}
if hostStr, portStr, splitErr := net.SplitHostPort(u.Host); splitErr == nil {
config.Host = hostStr
config.Port, err = parsePort(portStr)
if err != nil {
return
}
} else {
config.Host = u.Host
// no port in URL, use the protocol default
if config.TLS {
config.Port = 6697
} else {
config.Port = 6667
}
}
}
if originString := arguments["--origin"]; originString != nil {
config.Origin = originString.(string)
}
if tlsNoverify {
config.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
if clientCert := arguments["--client-cert"]; clientCert != nil {
if config.TLSConfig == nil {
config.TLSConfig = new(tls.Config)
}
clientCert, tErr := tls.LoadX509KeyPair(clientCert.(string), clientCert.(string))
if tErr != nil {
err = fmt.Errorf("Cannot load TLS client cert/key: %w", tErr)
return
}
config.TLSConfig.Certificates = []tls.Certificate{clientCert}
}
return
}
func parseReconnectDuration(reconnectArg any) (result time.Duration, err error) {
if reconnectArg == nil {
return 0, nil
}
reconnectStr := reconnectArg.(string)
if intSeconds, err := strconv.Atoi(reconnectStr); err == nil {
return time.Duration(intSeconds) * time.Second, nil
}
return time.ParseDuration(reconnectStr)
}
func determineColorLevel(colorArg any) (colorLevel lib.ColorLevel) {
// call this unconditionally for its side effects
// (it does something to Windows terminals to make them ANSI-compliant)
colorSupportResult := supportscolor.SupportsColor(os.Stdout.Fd(), supportscolor.SniffFlagsOption(false))
colorLevel = lib.ColorLevel(colorSupportResult.Level)
// now handle the override arg:
if colorArg != nil {
switch strings.ToLower(colorArg.(string)) {
case "no", "none", "off", "false":
colorLevel = lib.ColorLevelNone
case "basic", "16", "ansi":
colorLevel = lib.ColorLevelBasic
case "256", "ansi256", "256color":
colorLevel = lib.ColorLevelAnsi256
case "16m", "ansi16m", "truecolor":
// in practice this is treated the same as ColorLevelAnsi256
colorLevel = lib.ColorLevelAnsi16m
case "on", "yes":
if colorLevel < lib.ColorLevelBasic {
colorLevel = lib.ColorLevelBasic
}
case "default":
// ok
default:
log.Fatalf("Invalid --color argument: `%s`", colorArg.(string))
}
}
return
}
func versionString() string {
var semVer string
if version != "" {
semVer = version
} else if commit != "" {
semVer = fmt.Sprintf("%s-%s", lib.SemVer, commit)
} else {
semVer = lib.SemVer
}
return fmt.Sprintf("ircdog %s [%s]", semVer, runtime.Version())
}
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
arguments, _ := docopt.Parse(usage, nil, true, versionString(), false)
connectionConfig, err := parseConnectionConfig(arguments)
if err != nil {
log.Fatalf("Invalid arguments: %v", err)
}
// list of commands/numerics to not print
var hiddenString string
if arguments["--hide"] != nil {
hiddenString = arguments["--hide"].(string)
}
hiddenList := strings.Split(hiddenString, ",")
hiddenCommands := make(map[string]bool)
for _, cmd := range hiddenList {
if 0 < len(cmd) {
hiddenCommands[strings.ToUpper(cmd)] = true
}
}
raw := arguments["--raw"].(bool)
escape := arguments["--escape"].(bool)
useItalics := arguments["--italics"].(bool)
if raw && (escape || useItalics) {
log.Fatal("Cannot combine --raw with --escape or --italics")
}
answerPings := !arguments["--nopings"].(bool)
colorLevel := determineColorLevel(arguments["--color"])
verbose := arguments["--verbose"].(bool)
disableReadline := arguments["--no-readline"].(bool) || os.Getenv("IRCDOG_READLINE") == "0"
var transcript *lib.Transcript
if transcriptFile := arguments["--transcript"]; transcriptFile != nil {
transcript, err = lib.NewTranscript(transcriptFile.(string))
if err != nil {
log.Fatalf("Could not open transcript file: %v", err)
}
}
// no more log.Fatal from here on out, it would break this defer:
defer transcript.Close()
var script string
if scriptArg := arguments["--script"]; scriptArg != nil {
script = scriptArg.(string)
}
reconnectDuration, err := parseReconnectDuration(arguments["--reconnect"])
if err != nil {
log.Fatalf("Invalid --reconnect argument: %v", err)
}
var exitStatus int
if listenAddr := arguments["--listen"]; listenAddr == nil {
exitStatus = runClient(
connectionConfig, hiddenCommands, transcript,
raw, escape, answerPings, useItalics, colorLevel,
verbose, disableReadline, script, reconnectDuration,
)
} else {
exitStatus = runListenProxy(
listenAddr.(string), connectionConfig, hiddenCommands, transcript,
raw, escape, useItalics, colorLevel,
)
}
os.Exit(exitStatus)
}
func runClient(
connectionConfig lib.ConnectionConfig,
hiddenCommands map[string]bool, transcript *lib.Transcript,
raw, escape, answerPings, useItalics bool, colorLevel lib.ColorLevel,
verbose, disableReadline bool, script string, reconnectDuration time.Duration) int {
console, err := libconsole.NewConsole(!(raw || disableReadline), os.Getenv("IRCDOG_HISTFILE"))
if err != nil {
log.Printf("** ircdog could not initialize console: %s\n", err.Error())
return 1
}
defer console.Close()
lineChan := make(chan string)
openChan := make(chan struct{})
go func() {
<-openChan // wait to show the prompt until connection established
for {
line, err := console.Readline()
if err == nil {
lineChan <- line
} else {
if err != io.EOF {
log.Println("** ircdog error: failed to read new input line:", err.Error())
}
close(lineChan)
return
}
}
}()
for {
status := connectExternal(
console, lineChan, openChan, connectionConfig, hiddenCommands, transcript,
raw, escape, answerPings, useItalics, colorLevel,
verbose, script,
)
if status == 0 {
return 0
} else if reconnectDuration == 0 {
return status
} else {
log.Printf("** ircdog disconnected unexpectedly, waiting %v to reconnect", reconnectDuration)
time.Sleep(reconnectDuration)
openChan = nil // we are already prompting
}
}
}
func connectExternal(
console libconsole.Console, lineChan chan string, openChan chan struct{},
connectionConfig lib.ConnectionConfig, hiddenCommands map[string]bool, transcript *lib.Transcript,
raw, escape, answerPings, useItalics bool, colorLevel lib.ColorLevel,
verbose bool, script string) (status int) {
status = 1
if verbose {
log.Printf("** ircdog connecting to remote host")
}
connection, err := lib.NewConnection(connectionConfig)
if err != nil {
log.Printf("** ircdog could not create new connection: %s\n", err.Error())
return
}
if verbose {
log.Printf("** ircdog connected to remote host at %s", connection.RemoteAddr().String())
}
defer connection.Disconnect()
if openChan != nil {
close(openChan) // connection established, show the prompt
}
doneChan := make(chan struct{})
// process incoming lines from server
go func() {
defer func() {
close(doneChan)
}()
for {
line, err := connection.GetLine()
if line != "" || err == nil {
transcript.WriteLine(line, false)
}
if err != nil {
log.Println("** ircdog disconnected:", err.Error())
return
}
msg, parseErr := ircmsg.ParseLine(line)
if !(parseErr == nil && hiddenCommands[msg.Command]) {
// print line
if raw || parseErr != nil {
fmt.Fprintln(console, line)
} else if escape {
fmt.Fprintln(console, ircfmt.Escape(line))
} else {
fmt.Fprintln(console, lib.IRCLineToAnsi(line, colorLevel, useItalics))
}
}
// respond to incoming PINGs
if parseErr == nil && answerPings && msg.Command == "PING" && len(msg.Params) != 0 {
pong := makePong(msg)
if !hiddenCommands["PONG"] {
fmt.Fprintln(console, pong)
}
connection.SendLine(pong)
transcript.WriteLine(pong, true)
}
}
}()
if script != "" {
if scriptCommands, err := lib.ReadScript(script); err == nil {
for _, command := range scriptCommands {
if err := connection.SendLine(command); err != nil {
log.Println("** ircdog error: failed to send line:", err.Error())
return 1
}
transcript.WriteLine(command, true)
// don't bother handling --ignore for scripted commands
fmt.Fprintln(console, command)
}
} else {
log.Printf("** ircdog was unable to read script, ignoring: %v", err)
}
}
// process incoming lines from user
for {
select {
case line, ok := <-lineChan:
if !ok {
// no more stdin, assume the user sent EOF and wants ircdog to stop
// (this conflates EOF with real errors but it shouldn't matter)
status = 0
return
}
line = strings.TrimRight(line, "\r\n")
if parsedLine, err := ircmsg.ParseLine(line); err == nil && parsedLine.Command == "QUIT" {
// user-initiated QUIT, ircdog should stop
status = 0
}
if !raw {
line = lib.ReplaceControlCodes(line)
}
err = connection.SendLine(line)
if err != nil {
log.Println("** ircdog error: failed to send line:", err.Error())
return
}
transcript.WriteLine(line, true)
case <-doneChan:
return
}
}
}
func makePong(msg ircmsg.Message) string {
// make a stylish irc-go PONG message that omits the : if possible
// PONG parameter is the final parameter from PING:
pongMsg := ircmsg.MakeMessage(nil, "", "PONG", msg.Params[len(msg.Params)-1])
pong, _ := pongMsg.Line()
pong = pong[:len(pong)-2] // trim \r\n
return pong
}
type listenConnectionManager struct {
ln net.Listener
connectionConfig lib.ConnectionConfig
hiddenCommands map[string]bool
transcript *lib.Transcript
raw bool
escape bool
useItalics bool
colorLevel lib.ColorLevel
// prevent client and server from writing to stdout concurrently
outputMutex sync.Mutex
// allow at most one proxied connection at once:
// 0 means no active connection, otherwise the unique ID of a connection
activeConnection atomic.Uint64
}
func runListenProxy(
listenAddress string, connectionConfig lib.ConnectionConfig,
hiddenCommands map[string]bool, transcript *lib.Transcript,
raw, escape, useItalics bool, colorLevel lib.ColorLevel) int {
ln, err := net.Listen("tcp", listenAddress)
if err != nil {
log.Println("** ircdog could not open listener:", err.Error())
log.Println("Listener should have the form [host]:<port> like localhost:6667 or :8889")
return 1
}
log.Printf("** ircdog listening on %s, waiting for client connection", listenAddress)
manager := listenConnectionManager{
ln: ln,
connectionConfig: connectionConfig,
hiddenCommands: hiddenCommands,
transcript: transcript,
raw: raw,
escape: escape,
useItalics: useItalics,
colorLevel: colorLevel,
}
return manager.acceptLoop()
}
func (m *listenConnectionManager) acceptLoop() int {
var connectionCounter uint64
for {
clientConn, err := m.ln.Accept()
if err != nil {
log.Printf("** ircdog could not accept incoming connection from listener: %v", err)
return 1
}
connectionCounter++
connectionID := connectionCounter
if m.activeConnection.CompareAndSwap(0, connectionID) {
log.Printf("** ircdog accepted connection from %s, connecting to remote", clientConn.RemoteAddr().String())
// create new server connection
server, err := lib.NewConnection(m.connectionConfig)
if err != nil {
log.Printf("** ircdog could not create new connection: %s\n", err.Error())
clientConn.Write([]byte("ERROR :ircdog could not connect to remote server\r\n"))
clientConn.Close()
m.activeConnection.CompareAndSwap(connectionID, 0)
}
log.Printf("** ircdog connected to remote host at %s", server.RemoteAddr().String())
client := lib.MakeSocket(clientConn)
go m.relay(connectionID, client, server, true)
go m.relay(connectionID, server, client, false)
} else {
clientConn.Write([]byte("ERROR :ircdog already has an active connection\r\n"))
clientConn.Close()
}
}
}
const (
// printable indicators for whether the captured line is going from client to server,
// or vice versa.
c2sMarkerPlain = " -> "
s2cMarkerPlain = " <- "
c2sMarkerColor = "\x1b[31;100m -> \x1b[0m"
s2cMarkerColor = "\x1b[32;100m <- \x1b[0m"
)
func (m *listenConnectionManager) relay(connectionID uint64, input, output lib.IRCConnection, inputIsClient bool) {
defer func() {
input.Disconnect()
output.Disconnect()
m.activeConnection.CompareAndSwap(connectionID, 0)
}()
var inputName, outputName, marker string
usePlainMarkers := m.raw || m.escape || m.colorLevel == lib.ColorLevelNone
if inputIsClient {
inputName, outputName, marker = "client", "server", c2sMarkerColor
if usePlainMarkers {
marker = c2sMarkerPlain
}
} else {
inputName, outputName, marker = "server", "client", s2cMarkerColor
if usePlainMarkers {
marker = s2cMarkerPlain
}
}
for {
line, err := input.GetLine()
if line != "" || err == nil {
m.transcript.WriteLine(line, inputIsClient)
}
if err != nil {
log.Printf("** ircdog %s disconnected: %v", inputName, err.Error())
return
}
msg, parseErr := ircmsg.ParseLine(line)
if parseErr == nil && m.hiddenCommands[msg.Command] {
continue
}
// print line
m.outputMutex.Lock()
if m.raw {
fmt.Printf("%s%s\n", marker, line)
} else if m.escape {
fmt.Printf("%s%s\n", marker, ircfmt.Escape(line))
} else {
fmt.Printf("%s%s\n", marker, lib.IRCLineToAnsi(line, m.colorLevel, m.useItalics))
}
m.outputMutex.Unlock()
err = output.SendLine(line)
if err != nil {
log.Printf("** ircdog couldn't send line to %s: %v", outputName, err)
return
}
}
}