From 9e68e9bd66ab993c9824cb8e53986b961354fe4d Mon Sep 17 00:00:00 2001 From: Ishan Goel Date: Thu, 4 Jan 2024 12:37:35 -0800 Subject: [PATCH] use string builder for image gen --- main.go | 18 +++++++++++++++++- util.go | 8 ++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a8b9739b..1206e670 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ var ( Bans = make([]Ban, 0, 10) IDandIPsToTimesJoinedInMin = make(map[string]int, 10) // ban type has addr and id AntispamMessages = make(map[string]int) + TORIPs = make(map[string]bool) Devbot = Green.Paint("devbot") ) @@ -146,6 +147,21 @@ func main() { } os.Exit(0) }() + + // read tor list from https://www.dan.me.uk/torlist/?exit + resp, err := http.Get("https://www.dan.me.uk/torlist/?exit") + if err != nil { + Log.Println(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + Log.Println(err) + } + for _, ip := range strings.Split(string(body), "\n") { + TORIPs[ip] = true + } + ssh.Handle(func(s ssh.Session) { go keepSessionAlive(s) u := newUser(s) @@ -394,7 +410,7 @@ func newUser(s ssh.Session) *User { Log.Println("Connected " + u.Name + " [" + u.id + "]") - if bansContains(Bans, u.addr, u.id) { + if bansContains(Bans, u.addr, u.id) || TORIPs[u.addr] { Log.Println("Rejected " + u.Name + " [" + host + "] (banned)") u.writeln(Devbot, "**You are banned**. If you feel this was a mistake, please reach out to the server admin. Include the following information: [ID "+u.id+"]") s.Close() diff --git a/util.go b/util.go index f9e74fcd..58f13f35 100644 --- a/util.go +++ b/util.go @@ -202,17 +202,17 @@ func replaceImgs(md string, width int, cache map[string]image.Image) string { } func imgRender(img image.Image, width int) string { - result := "" + var builder strings.Builder img = imaging.Fit(img, width, math.MaxInt32, imaging.Lanczos) for y := 0; y < img.Bounds().Dy(); y += 2 { for x := 0; x < img.Bounds().Dx(); x++ { r1, g1, b1, _ := img.At(x, y).RGBA() r2, g2, b2, _ := img.At(x, y+1).RGBA() - result += fmt.Sprintf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm▀", r1/256, g1/256, b1/256, r2/256, g2/256, b2/256) + builder.WriteString(fmt.Sprintf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm▀", r1/256, g1/256, b1/256, r2/256, g2/256, b2/256)) } - result += "\x1b[0m\n" + builder.WriteString("\x1b[0m\n") } - return result + return builder.String() } func addLeftPad(a string, pad int) string {