-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
evorax
committed
Jun 12, 2024
1 parent
541d500
commit 6eb99e7
Showing
9 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"maxint": 100, | ||
"module": [ | ||
"os/exec", | ||
"net", | ||
"syscall", | ||
"os" | ||
], | ||
"token": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |