-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (159 loc) · 4.17 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"net"
"os"
"sort"
"strconv"
"sync"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"gopkg.in/irc.v3"
)
func main() {
app := NewApp()
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func NewApp() *cli.App {
return &cli.App{
Name: "irc-server-scan",
Usage: "scan an irc server for channel populations",
UsageText: "irc-server-scan [global options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "required: target server address",
Required: true,
},
&cli.StringFlag{
Name: "port",
Usage: "target server port",
Value: "6667",
},
&cli.IntFlag{
Name: "minusers",
Usage: "filter list results by channel population",
Value: 500, //nolint:gomnd
},
&cli.StringFlag{
Name: "sort",
Usage: `sorting method - can also be "pop" to sort by channel population`,
Value: "alpha",
},
&cli.IntFlag{
Name: "topiclength",
Usage: "shorten channel topics in list",
Value: 125, //nolint:gomnd
},
},
Action: func(cliContext *cli.Context) error {
addr := fmt.Sprintf("%s:%s", cliContext.String("host"), cliContext.String("port"))
conn, err := net.Dial("tcp", addr)
if err != nil {
return fmt.Errorf("irc client tcp connection failure: %w", err)
}
var (
results = new(sync.Map)
idString = fmt.Sprintf("g_%v", rand.Intn(500)) //nolint:gomnd,gosec
)
handlerOpts := &handlerOptions{
MinUsers: cliContext.Int("minusers"),
Sort: cliContext.String("sort"),
TopicLength: cliContext.Int("topiclength"),
}
config := irc.ClientConfig{
Nick: idString,
Pass: "",
User: idString,
Name: idString,
Handler: ircHandler(results, handlerOpts),
}
client := irc.NewClient(conn, config)
return fmt.Errorf("irc client run context failure: %w", client.RunContext(context.Background()))
},
}
}
// channelInfo represents the structure of the result list table.
type channelInfo struct {
Name string
Visible int
Topic string
}
type handlerOptions struct {
MinUsers int
Sort string
TopicLength int
}
func ircHandler(results *sync.Map, options *handlerOptions) irc.HandlerFunc {
return func(cl *irc.Client, message *irc.Message) {
switch message.Command {
case "001":
fmt.Println("starting scan...")
if err := cl.Write("LIST"); err != nil {
fmt.Println("error writing LIST command:", err)
os.Exit(1)
}
case "322":
var (
channel = message.Params[1]
visible = message.Params[2]
topic = message.Params[3]
)
visibleInt, err := strconv.Atoi(visible)
if err != nil {
fmt.Println("unable to convert string to int:", err)
return
}
results.Store(channel, &channelInfo{channel, visibleInt, topic})
case "323":
filteredChannels := []*channelInfo{}
// Show channels with `minUsers` present - adjustable by user.
results.Range(func(_, info interface{}) bool {
channelInfo, ok := info.(*channelInfo)
if !ok {
fmt.Println("channelInfo type assertion failed")
}
if channelInfo.Visible >= options.MinUsers {
filteredChannels = append(filteredChannels, channelInfo)
}
return true
})
fmt.Println(len(filteredChannels), "results")
switch options.Sort {
case "alpha":
sort.Slice(filteredChannels, func(i, j int) bool {
return filteredChannels[i].Name < filteredChannels[j].Name
})
case "pop":
sort.Slice(filteredChannels, func(i, j int) bool {
return filteredChannels[i].Visible > filteredChannels[j].Visible
})
}
// Create table for rendering.
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Visible", "Topic"})
table.SetAutoWrapText(false)
// Append records to table for rendering.
for _, channelInfo := range filteredChannels {
table.Append([]string{
channelInfo.Name,
strconv.Itoa(channelInfo.Visible),
truncateString(channelInfo.Topic, options.TopicLength),
})
}
table.Render()
os.Exit(0)
}
}
}
func truncateString(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return fmt.Sprintf("%s...", s[:maxLen])
}