From b9b7d96e856a359f8915e39b3895b622e1d7f1eb Mon Sep 17 00:00:00 2001 From: SiHuan Date: Fri, 18 Dec 2020 21:03:30 +0800 Subject: [PATCH] add remote mc --- data/mc/rmc/go.mod | 3 ++ data/mc/rmc/main.go | 64 +++++++++++++++++++++++++++++++++ sample_config.py | 4 ++- worker/mc.py | 88 +++++++++++++++++++++++++++++---------------- 4 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 data/mc/rmc/go.mod create mode 100644 data/mc/rmc/main.go diff --git a/data/mc/rmc/go.mod b/data/mc/rmc/go.mod new file mode 100644 index 0000000..e40a8ab --- /dev/null +++ b/data/mc/rmc/go.mod @@ -0,0 +1,3 @@ +module rmc + +go 1.15 diff --git a/data/mc/rmc/main.go b/data/mc/rmc/main.go new file mode 100644 index 0000000..224aabe --- /dev/null +++ b/data/mc/rmc/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "net/http" + "os/exec" +) + +func checkErr(err error) { + if err != nil { + fmt.Println(err) + } +} + +func handleStatus(writer http.ResponseWriter, request *http.Request) { + cmd := exec.Command("papermc", "status") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + fmt.Fprintf(writer, "Error") + return + } + fmt.Fprintf(writer, out.String()) +} + +func handleList(writer http.ResponseWriter, request *http.Request) { + cmd := exec.Command("papermc", "command", "list") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + fmt.Fprintf(writer, "Error") + return + } + fmt.Fprintf(writer, out.String()) +} + +func handleSay(writer http.ResponseWriter, request *http.Request) { + saywhat, _ := ioutil.ReadAll(request.Body) + cmd := exec.Command("papermc", "command", "say", string(saywhat)) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + fmt.Fprintf(writer, "Error") + return + } + fmt.Fprintf(writer, "0") +} + +func main() { + http.HandleFunc("/status", handleStatus) + http.HandleFunc("/list", handleList) + http.HandleFunc("/say", handleSay) + fmt.Println("Running at port 58941 ...") + err := http.ListenAndServe("172.26.66.2:58941", nil) + if err != nil { + log.Fatal("ListenAndServe: ", err.Error()) + } +} diff --git a/sample_config.py b/sample_config.py index 56baf85..5bd18c1 100644 --- a/sample_config.py +++ b/sample_config.py @@ -6,4 +6,6 @@ AUTHORIZATION = '' HOST = '0.0.0.0' -PORT = 5580 \ No newline at end of file +PORT = 5580 + +REMOTE_MC_URL = '' \ No newline at end of file diff --git a/worker/mc.py b/worker/mc.py index 7691d81..66f2f4a 100644 --- a/worker/mc.py +++ b/worker/mc.py @@ -1,7 +1,9 @@ from zzcore import StdAns -import re +import re, requests from subprocess import getoutput,call +from config import REMOTE_MC_URL + class Ans(StdAns): AllowGroup = [959613860, 125733077, 204097403, 1140391080] AllowUser = [1318000868] @@ -13,39 +15,65 @@ class Ans(StdAns): if cmd in AllowCmd: if cmd == 'status': - output = getoutput('spigot status') - p = re.compile(r'processes = ([0-9]*) \(') - prsnum = re.findall(p,output)[0] - p = re.compile(r' \((.*?)\)',re.S) - prsnames = re.findall(p,output)[0].split(', ') - p = re.compile(r'Total memory usage = (.*)$') - memory = re.findall(p,output)[0] - msg = '咱的MC服务器现在有 ' - for prsname in prsnames: - msg = msg + prsname + ' ' - msg = msg + '这' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。' + msg = getStatus() elif cmd == 'list': - output = getoutput('spigot command list') - p = re.compile(r'There are (.*?)[ of a max]', re.S) - online = re.findall(p,output)[0] - if online == '0': - msg = '咱看着没有人在线哎\n_(-ω-`_)⌒)_' - else: - msg = '有' + online + '个小伙伴在线!' - p = re.compile(r'online: (.*?)[\n>]', re.S) - players = re.findall(p,output)[0].split(', ') - for player in players: - msg = msg + '\n' + player + msg = getList() elif cmd == 'say': saywhat = self.raw_msg['message'][8:] - if not saywhat: - return '汝让咱say what?o(≧口≦)o' - shellcmd = ['spigot','command','say',saywhat] - if call(shellcmd) == 0: - msg = '咱已经把消息传过去了。' - else: - msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!' + msg = say(saywhat) else: msg = '汝是不是在mc后面添加了奇怪的参数,咱可只知道 status list 和 say。' return msg + +def getStatus(): + if REMOTE_MC_URL: + output = requests.post(f'{REMOTE_MC_URL}/status') + else: + output = getoutput('papermc status') + + p = re.compile(r'processes = ([0-9]*) \(') + prsnum = re.findall(p,output)[0] + p = re.compile(r' \((.*?)\)',re.S) + prsnames = re.findall(p,output)[0].split(', ') + p = re.compile(r'Total memory usage = (.*)$') + memory = re.findall(p,output)[0] + msg = '咱的MC服务器现在有 ' + for prsname in prsnames: + msg = msg + prsname + ' ' + msg = msg + '这' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。' + return msg + + +def getList(): + if REMOTE_MC_URL: + output = requests.post(f'{REMOTE_MC_URL}/list') + else: + output = getoutput('papermc command list') + + p = re.compile(r'There are (.*?)[ of a max]', re.S) + online = re.findall(p,output)[0] + if online == '0': + msg = '咱看着没有人在线哎\n_(-ω-`_)⌒)_' + else: + msg = '有' + online + '个小伙伴在线!' + p = re.compile(r'online: (.*?)[\n>]', re.S) + players = re.findall(p,output)[0].split(', ') + for player in players: + msg = msg + '\n' + player + return msg + +def say(saywhat): + if not saywhat: + return '汝让咱say what?o(≧口≦)o' + + if REMOTE_MC_URL: + code = requests.post(f'{REMOTE_MC_URL}/say',data=saywhat) + else: + shellcmd = ['papermc','command','say',saywhat] + code = call(shellcmd) + if code == 0: + msg = '咱已经把消息传过去了。' + else: + msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!' + return msg \ No newline at end of file