2015-10-27 21:21:06 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2015-11-19 17:49:48 -08:00
|
|
|
"strconv"
|
2015-12-16 11:15:17 -08:00
|
|
|
"strings"
|
2016-04-28 14:36:59 -07:00
|
|
|
|
2017-07-22 15:31:12 -07:00
|
|
|
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server"
|
|
|
|
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/rate"
|
2016-04-28 14:36:59 -07:00
|
|
|
"github.com/gorilla/websocket"
|
2017-09-15 12:21:41 -07:00
|
|
|
"gopkg.in/abiosoft/ishell.v1"
|
2015-10-27 21:21:06 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func commandLineConsole() {
|
|
|
|
|
2017-09-01 16:16:40 -04:00
|
|
|
shell := ishell.New()
|
2015-10-27 21:21:06 -07:00
|
|
|
|
2015-11-08 16:44:16 -08:00
|
|
|
shell.Register("help", func(args ...string) (string, error) {
|
|
|
|
shell.PrintCommands()
|
|
|
|
return "", nil
|
|
|
|
})
|
|
|
|
|
2015-10-27 21:21:06 -07:00
|
|
|
shell.Register("clientcount", func(args ...string) (string, error) {
|
2015-11-19 16:39:47 -08:00
|
|
|
server.GlobalSubscriptionLock.RLock()
|
|
|
|
count := len(server.GlobalSubscriptionInfo)
|
|
|
|
server.GlobalSubscriptionLock.RUnlock()
|
2015-10-27 21:21:06 -07:00
|
|
|
return fmt.Sprintln(count, "clients connected"), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
shell.Register("globalnotice", func(args ...string) (string, error) {
|
|
|
|
msg := server.ClientMessage{
|
|
|
|
MessageID: -1,
|
|
|
|
Command: "message",
|
|
|
|
Arguments: args[0],
|
|
|
|
}
|
2017-07-22 15:31:12 -07:00
|
|
|
server.PublishToAll(msg, rate.Unlimited())
|
2015-10-27 21:21:06 -07:00
|
|
|
return "Message sent.", nil
|
|
|
|
})
|
|
|
|
|
2015-10-28 17:24:21 -07:00
|
|
|
shell.Register("publish", func(args ...string) (string, error) {
|
|
|
|
if len(args) < 4 {
|
|
|
|
return "Usage: publish [room.sirstendec | _ALL] -1 reload_ff 23", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target := args[0]
|
|
|
|
line := strings.Join(args[1:], " ")
|
|
|
|
msg := server.ClientMessage{}
|
|
|
|
err := server.UnmarshalClientMessage([]byte(line), websocket.TextMessage, &msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int
|
|
|
|
if target == "_ALL" {
|
2017-07-22 15:31:12 -07:00
|
|
|
count = server.PublishToAll(msg, rate.Unlimited())
|
2015-10-28 17:24:21 -07:00
|
|
|
} else {
|
2017-07-22 15:31:12 -07:00
|
|
|
count = server.PublishToChannel(target, msg, rate.Unlimited())
|
2015-10-28 17:24:21 -07:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("Published to %d clients", count), nil
|
|
|
|
})
|
|
|
|
|
2015-10-27 21:21:06 -07:00
|
|
|
shell.Register("memstatsbysize", func(args ...string) (string, error) {
|
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
m := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
for _, val := range m.BySize {
|
|
|
|
if val.Mallocs == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-11-01 13:17:35 -08:00
|
|
|
shell.Print(fmt.Sprintf("%5d: %6d outstanding (%d total)\n", val.Size, val.Mallocs-val.Frees, val.Mallocs))
|
2015-10-27 21:21:06 -07:00
|
|
|
}
|
|
|
|
shell.Println(m.NumGC, "collections occurred")
|
|
|
|
return "", nil
|
|
|
|
})
|
|
|
|
|
2015-11-08 16:44:16 -08:00
|
|
|
shell.Register("authorizeeveryone", func(args ...string) (string, error) {
|
|
|
|
if len(args) == 0 {
|
2015-11-15 18:43:34 -08:00
|
|
|
if server.Configuration.SendAuthToNewClients {
|
2015-11-08 16:44:16 -08:00
|
|
|
return "All clients are recieving auth challenges upon claiming a name.", nil
|
|
|
|
}
|
2015-11-15 18:43:34 -08:00
|
|
|
return "All clients are not recieving auth challenges upon claiming a name.", nil
|
2015-11-08 16:44:16 -08:00
|
|
|
} else if args[0] == "on" {
|
2015-11-15 18:43:34 -08:00
|
|
|
server.Configuration.SendAuthToNewClients = true
|
2015-11-08 16:44:16 -08:00
|
|
|
return "All new clients will recieve auth challenges upon claiming a name.", nil
|
|
|
|
} else if args[0] == "off" {
|
2015-11-15 18:43:34 -08:00
|
|
|
server.Configuration.SendAuthToNewClients = false
|
2015-11-08 16:44:16 -08:00
|
|
|
return "All new clients will not recieve auth challenges upon claiming a name.", nil
|
|
|
|
}
|
2015-11-15 18:43:34 -08:00
|
|
|
return "Usage: authorizeeveryone [ on | off ]", nil
|
2015-11-08 16:44:16 -08:00
|
|
|
})
|
|
|
|
|
2015-11-19 17:49:48 -08:00
|
|
|
shell.Register("kickclients", func(args ...string) (string, error) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return "Please enter either a count or a fraction of clients to kick.", nil
|
|
|
|
}
|
|
|
|
input, err := strconv.ParseFloat(args[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
return "Argument must be a number", err
|
|
|
|
}
|
|
|
|
var count int
|
|
|
|
if input >= 1 {
|
|
|
|
count = int(input)
|
|
|
|
} else {
|
|
|
|
server.GlobalSubscriptionLock.RLock()
|
|
|
|
count = int(float64(len(server.GlobalSubscriptionInfo)) * input)
|
|
|
|
server.GlobalSubscriptionLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:15:17 -08:00
|
|
|
msg := server.ClientMessage{Arguments: &server.CloseRebalance}
|
2015-11-19 17:49:48 -08:00
|
|
|
server.GlobalSubscriptionLock.RLock()
|
|
|
|
defer server.GlobalSubscriptionLock.RUnlock()
|
|
|
|
|
|
|
|
kickCount := 0
|
|
|
|
for i, cl := range server.GlobalSubscriptionInfo {
|
|
|
|
if i >= count {
|
|
|
|
break
|
|
|
|
}
|
2017-02-02 22:59:17 -08:00
|
|
|
if cl.Send(msg) {
|
|
|
|
kickCount++
|
2015-11-19 17:49:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Kicked %d clients", kickCount), nil
|
|
|
|
})
|
|
|
|
|
2015-11-01 13:17:35 -08:00
|
|
|
shell.Register("panic", func(args ...string) (string, error) {
|
|
|
|
go func() {
|
|
|
|
panic("requested panic")
|
|
|
|
}()
|
|
|
|
return "", nil
|
|
|
|
})
|
|
|
|
|
2015-10-27 21:21:06 -07:00
|
|
|
shell.Start()
|
|
|
|
}
|