Skip to content

Commit

Permalink
Add or update files
Browse files Browse the repository at this point in the history
  • Loading branch information
evorax committed Jun 12, 2024
1 parent 541d500 commit 6eb99e7
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 0 deletions.
30 changes: 30 additions & 0 deletions compile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"os"
"os/exec"

"github.com/google/uuid"
)

func Compile(code string) (string, error) {
uuid := uuid.New().String()
path := fmt.Sprintf("compile/%s.go", uuid)
file, err := os.Create(path)
if err != nil {
return "", err
}
file.Write([]byte(code))
out, err := exec.Command("go", "run", path).Output()
if err != nil {
if err := os.Remove(path); err != nil {
return "", err
}
return "", err
}
if err := os.Remove(path); err != nil {
return "", err
}
return string(out), nil
}
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"maxint": 100,
"module": [
"os/exec",
"net",
"syscall",
"os"
],
"token": ""
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module discord-dev

go 1.22.3

require (
github.com/bwmarrin/discordgo v0.28.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)
48 changes: 48 additions & 0 deletions handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"strings"

"github.com/bwmarrin/discordgo"
)

func (c *Config) CompileHandle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}

if !Run[m.Author.ID] {
c.CompileRun(s, m)
} else {
s.ChannelMessageSend(m.ChannelID, "```you are running...```")
}
}

func (c *Config) CompileRun(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, ";compile") {
if len(m.Content) > 11 {
parse := m.Content[8+3:]
parse = parse[:len(parse)-3]
if strings.HasPrefix(parse, "go") {
Run[m.Author.ID] = true
if is, err := c.InfiniteLoop(parse[2:]); !is && c.isSafe(parse[2:]) {
if out, err := Compile(parse[2:]); err == nil && out != "" {
s.ChannelMessageSend(m.ChannelID, "```"+string(out)+"```")
} else {
fmt.Println(err)
}
} else {
if is && err == nil {
s.ChannelMessageSend(m.ChannelID, "```no loop```")
} else if err != nil {
fmt.Println(err)
} else {
s.ChannelMessageSend(m.ChannelID, "```you are send banned code```")
}
}
delete(Run, m.Author.ID)
}
}
}
}
Binary file added images/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/demo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"encoding/json"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/bwmarrin/discordgo"
)

func main() {
config, err := Load()
if err != nil {
fmt.Println(err)
return
}

dg, err := discordgo.New("Bot " + config.Token)
if err != nil {
fmt.Println(err)
return
}

dg.AddHandler(config.CompileHandle)

dg.Identify.Intents = discordgo.IntentsGuildMessages

err = dg.Open()
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc

dg.Close()
}

func Load() (*Config, error) {
file, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}

var config Config

err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
return &config, nil
}
81 changes: 81 additions & 0 deletions safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strconv"
"strings"
)

func (c *Config) isSafe(code string) bool {

fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "", code, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return false
}

for _, imp := range node.Imports {
importPath := strings.Trim(imp.Path.Value, "\"")
for _, banned := range c.Module {
if strings.HasPrefix(importPath, banned) {
return false
}
}
}

return true
}

func (c *Config) InfiniteLoop(code string) (bool, error) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "", code, parser.AllErrors)
if err != nil {
return true, err
}

var hasInfiniteLoop bool = false
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.ForStmt:
if x.Cond == nil {
hasInfiniteLoop = true
return false
}
if binExpr, ok := x.Cond.(*ast.BinaryExpr); ok && binExpr.Op == token.LEQ {
if ident, ok := binExpr.Y.(*ast.BasicLit); ok && ident.Kind == token.INT {
if val, err := strconv.Atoi(ident.Value); err == nil && val > c.MaxInt {
hasInfiniteLoop = true
return false
}
}
}
case *ast.RangeStmt:
if x.Key != nil || x.Value != nil {
if lit, ok := x.X.(*ast.BasicLit); ok && lit.Kind == token.INT {
if val, err := strconv.Atoi(lit.Value); err == nil && val > c.MaxInt {
hasInfiniteLoop = true
return false
}
}
}
case *ast.SelectStmt:
hasInfiniteLoop = true
return false
case *ast.SwitchStmt:
hasInfiniteLoop = true
return false
case *ast.BranchStmt:
if x.Tok == token.GOTO {
hasInfiniteLoop = true
return false
}
}
return true
})

return hasInfiniteLoop, nil
}
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

var Run = make(map[string]bool)

type Config struct {
MaxInt int `json:"maxint"`
Module []string `json:"module"`
Token string `json:"token"`
}

0 comments on commit 6eb99e7

Please sign in to comment.