Skip to content

Commit

Permalink
use string builder for image gen
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed Jan 4, 2024
1 parent dfa348e commit 9e68e9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9e68e9b

Please sign in to comment.