2015-11-16 13:28:16 -08:00
|
|
|
package server // import "bitbucket.org/stendec/frankerfacez/socketserver/server"
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-10-26 10:06:45 -07:00
|
|
|
"errors"
|
2015-10-24 19:59:34 -07:00
|
|
|
"fmt"
|
2015-10-27 21:21:06 -07:00
|
|
|
"io"
|
2015-10-28 18:12:20 -07:00
|
|
|
"io/ioutil"
|
2015-10-25 12:40:07 -07:00
|
|
|
"log"
|
2015-10-26 10:06:45 -07:00
|
|
|
"net/http"
|
2015-11-08 21:10:24 -08:00
|
|
|
"net/url"
|
2015-12-02 19:08:19 -08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2016-03-30 17:33:45 -07:00
|
|
|
"regexp"
|
2015-12-23 21:55:15 -08:00
|
|
|
"runtime"
|
2015-10-26 10:06:45 -07:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-11-17 19:53:58 -08:00
|
|
|
"sync/atomic"
|
2015-12-02 19:08:19 -08:00
|
|
|
"syscall"
|
2015-11-18 18:33:20 -08:00
|
|
|
"time"
|
2015-11-19 16:55:03 -08:00
|
|
|
"unicode/utf8"
|
2016-03-30 17:33:45 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2015-10-24 19:59:34 -07:00
|
|
|
)
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// SuccessCommand is a Reply Command to indicate success in reply to a C2S Command.
|
2015-11-01 00:30:49 -07:00
|
|
|
const SuccessCommand Command = "ok"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// ErrorCommand is a Reply Command to indicate that a C2S Command failed.
|
2015-10-25 00:44:25 -07:00
|
|
|
const ErrorCommand Command = "error"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// HelloCommand is a C2S Command.
|
|
|
|
// HelloCommand must be the Command of the first ClientMessage sent during a connection.
|
|
|
|
// Sending any other command will result in a CloseFirstMessageNotHello.
|
2015-10-24 21:33:55 -07:00
|
|
|
const HelloCommand Command = "hello"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-12-16 13:59:51 -08:00
|
|
|
// ReadyCommand is a C2S Command.
|
|
|
|
// It indicates that the client is finished sending the initial 'sub' commands and the server should send the backlog.
|
|
|
|
const ReadyCommand Command = "ready"
|
|
|
|
|
2016-01-16 17:49:39 -08:00
|
|
|
const SetUserCommand Command = "setuser"
|
2015-12-16 13:59:51 -08:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// AuthorizeCommand is a S2C Command sent as part of Twitch username validation.
|
2015-11-08 16:44:16 -08:00
|
|
|
const AuthorizeCommand Command = "do_authorize"
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// AsyncResponseCommand is a pseudo-Reply Command.
|
|
|
|
// It indicates that the Reply Command to the client's C2S Command will be delivered
|
|
|
|
// on a goroutine over the ClientInfo.MessageChannel and should not be delivered immediately.
|
2015-10-24 22:38:04 -07:00
|
|
|
const AsyncResponseCommand Command = "_async"
|
2015-10-24 19:59:34 -07:00
|
|
|
|
2015-11-17 11:11:14 -08:00
|
|
|
const defaultMinMemoryKB = 1024 * 24
|
2015-11-17 11:04:10 -08:00
|
|
|
|
2016-02-03 22:06:46 -08:00
|
|
|
// DotTwitchDotTv is the .twitch.tv suffix.
|
|
|
|
const DotTwitchDotTv = ".twitch.tv"
|
2016-03-30 17:33:45 -07:00
|
|
|
|
2016-04-01 19:22:55 -07:00
|
|
|
const dotCbenniDotCom = ".cbenni.com"
|
|
|
|
|
|
|
|
var OriginRegexp = regexp.MustCompile("(" + DotTwitchDotTv + "|" + dotCbenniDotCom + ")" + "$")
|
2015-11-17 11:04:10 -08:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// ResponseSuccess is a Reply ClientMessage with the MessageID not yet filled out.
|
2015-11-08 22:34:06 -08:00
|
|
|
var ResponseSuccess = ClientMessage{Command: SuccessCommand}
|
2015-10-24 19:59:34 -07:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// Configuration is the active ConfigFile.
|
|
|
|
var Configuration *ConfigFile
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-12-16 14:23:28 -08:00
|
|
|
var janitorsOnce sync.Once
|
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
var CommandPool *StringPool
|
2016-01-17 19:46:01 -08:00
|
|
|
var PubSubChannelPool *StringPool
|
|
|
|
var TwitchChannelPool *StringPool
|
2016-01-17 17:45:37 -08:00
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// SetupServerAndHandle starts all background goroutines and registers HTTP listeners on the given ServeMux.
|
|
|
|
// Essentially, this function completely preps the server for a http.ListenAndServe call.
|
|
|
|
// (Uses http.DefaultServeMux if `serveMux` is nil.)
|
2015-10-28 15:19:22 -07:00
|
|
|
func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
2015-11-15 18:43:34 -08:00
|
|
|
Configuration = config
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-11-17 11:11:14 -08:00
|
|
|
if config.MinMemoryKBytes == 0 {
|
|
|
|
config.MinMemoryKBytes = defaultMinMemoryKB
|
2015-11-17 11:04:10 -08:00
|
|
|
}
|
|
|
|
|
2016-06-02 08:36:02 -07:00
|
|
|
Backend = setupBackend(config)
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
if serveMux == nil {
|
|
|
|
serveMux = http.DefaultServeMux
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-10-28 18:12:20 -07:00
|
|
|
bannerBytes, err := ioutil.ReadFile("index.html")
|
|
|
|
if err != nil {
|
2015-11-08 21:10:24 -08:00
|
|
|
log.Fatalln("Could not open index.html:", err)
|
2015-10-28 18:12:20 -07:00
|
|
|
}
|
|
|
|
BannerHTML = bannerBytes
|
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
serveMux.HandleFunc("/", HTTPHandleRootURL)
|
2015-12-23 21:57:33 -08:00
|
|
|
serveMux.Handle("/.well-known/", http.FileServer(http.Dir("/tmp/letsencrypt/")))
|
2016-06-02 08:16:16 -07:00
|
|
|
serveMux.HandleFunc("/healthcheck", HTTPSayOK)
|
2015-11-16 13:25:25 -08:00
|
|
|
serveMux.HandleFunc("/stats", HTTPShowStatistics)
|
2015-12-23 21:57:33 -08:00
|
|
|
serveMux.HandleFunc("/hll/", HTTPShowHLL)
|
2015-12-23 22:18:11 -08:00
|
|
|
serveMux.HandleFunc("/hll_force_write", HTTPWriteHLL)
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
serveMux.HandleFunc("/drop_backlog", HTTPBackendDropBacklog)
|
2015-11-16 13:25:25 -08:00
|
|
|
serveMux.HandleFunc("/uncached_pub", HTTPBackendUncachedPublish)
|
|
|
|
serveMux.HandleFunc("/cached_pub", HTTPBackendCachedPublish)
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2016-06-02 08:47:07 -07:00
|
|
|
announceForm, err := Backend.SealRequest(url.Values{
|
2015-11-08 21:10:24 -08:00
|
|
|
"startup": []string{"1"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Unable to seal requests:", err)
|
|
|
|
}
|
2016-06-02 09:03:43 -07:00
|
|
|
resp, err := Backend.HTTPClient.PostForm(Backend.announceStartupURL, announceForm)
|
2015-11-08 21:10:24 -08:00
|
|
|
if err != nil {
|
2015-11-16 13:07:02 -08:00
|
|
|
log.Println("could not announce startup to backend:", err)
|
2015-11-08 21:10:24 -08:00
|
|
|
} else {
|
|
|
|
resp.Body.Close()
|
2016-06-02 08:59:40 -07:00
|
|
|
Backend.lastSuccessLock.Lock()
|
|
|
|
Backend.lastSuccess[bPathAnnounceStartup] = time.Now().UTC()
|
|
|
|
Backend.lastSuccessLock.Unlock()
|
2015-12-16 11:48:37 -08:00
|
|
|
}
|
2015-12-16 11:47:51 -08:00
|
|
|
|
2015-12-23 21:55:15 -08:00
|
|
|
janitorsOnce.Do(startJanitors)
|
|
|
|
}
|
2015-11-08 16:44:16 -08:00
|
|
|
|
2016-01-17 18:40:08 -08:00
|
|
|
func init() {
|
2016-01-17 19:46:01 -08:00
|
|
|
setupInterning()
|
2016-01-17 18:40:08 -08:00
|
|
|
}
|
|
|
|
|
2015-12-23 21:55:15 -08:00
|
|
|
// startJanitors starts the 'is_init_func' goroutines
|
|
|
|
func startJanitors() {
|
|
|
|
loadUniqueUsers()
|
|
|
|
|
|
|
|
go authorizationJanitor()
|
|
|
|
go bunchCacheJanitor()
|
|
|
|
go pubsubJanitor()
|
|
|
|
go aggregateDataSender()
|
|
|
|
go commandCounter()
|
|
|
|
|
|
|
|
go ircConnection()
|
|
|
|
go shutdownHandler()
|
2015-11-19 17:49:48 -08:00
|
|
|
}
|
|
|
|
|
2015-12-23 21:56:56 -08:00
|
|
|
// is_init_func
|
2015-11-19 17:49:48 -08:00
|
|
|
func shutdownHandler() {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, syscall.SIGUSR1)
|
2015-12-23 21:57:33 -08:00
|
|
|
signal.Notify(ch, syscall.SIGTERM)
|
2015-11-19 17:49:48 -08:00
|
|
|
<-ch
|
|
|
|
log.Println("Shutting down...")
|
|
|
|
|
2015-12-23 21:57:33 -08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2016-01-15 20:54:30 -08:00
|
|
|
writeHLL()
|
2015-12-23 21:57:33 -08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2015-11-19 17:49:48 -08:00
|
|
|
StopAcceptingConnections = true
|
|
|
|
close(StopAcceptingConnectionsCh)
|
|
|
|
|
2015-12-02 19:08:19 -08:00
|
|
|
time.Sleep(1 * time.Second)
|
2015-12-23 21:57:33 -08:00
|
|
|
wg.Wait()
|
2015-11-19 17:49:48 -08:00
|
|
|
os.Exit(0)
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2015-12-23 21:56:56 -08:00
|
|
|
// is_init_func +test
|
|
|
|
func dumpStackOnCtrlZ() {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, syscall.SIGTSTP)
|
2016-06-02 08:16:16 -07:00
|
|
|
for range ch {
|
2015-12-23 21:56:56 -08:00
|
|
|
fmt.Println("Got ^Z")
|
|
|
|
|
|
|
|
buf := make([]byte, 10000)
|
|
|
|
byteCnt := runtime.Stack(buf, true)
|
|
|
|
fmt.Println(string(buf[:byteCnt]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 08:16:16 -07:00
|
|
|
// HTTPSayOK replies with 200 and a body of "ok\n".
|
|
|
|
func HTTPSayOK(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.(interface {
|
|
|
|
WriteString(string) error
|
|
|
|
}).WriteString("ok\n")
|
|
|
|
}
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// SocketUpgrader is the websocket.Upgrader currently in use.
|
2015-11-08 22:34:06 -08:00
|
|
|
var SocketUpgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
2016-04-20 14:28:35 -07:00
|
|
|
return r.Header.Get("Origin") == "" || OriginRegexp.MatchString(r.Header.Get("Origin"))
|
2015-11-08 22:34:06 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// BannerHTML is the content served to web browsers viewing the socket server website.
|
|
|
|
// Memes go here.
|
2015-11-08 22:34:06 -08:00
|
|
|
var BannerHTML []byte
|
|
|
|
|
2016-01-17 16:50:17 -08:00
|
|
|
// StopAcceptingConnectionsCh is closed while the server is shutting down.
|
2015-11-19 17:49:48 -08:00
|
|
|
var StopAcceptingConnectionsCh = make(chan struct{})
|
|
|
|
var StopAcceptingConnections = false
|
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// HTTPHandleRootURL is the http.HandleFunc for requests on `/`.
|
|
|
|
// It either uses the SocketUpgrader or writes out the BannerHTML.
|
|
|
|
func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
|
2015-11-17 13:44:52 -08:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
fmt.Println(404)
|
|
|
|
return
|
|
|
|
}
|
2015-11-19 17:49:48 -08:00
|
|
|
|
|
|
|
// racy, but should be ok?
|
|
|
|
if StopAcceptingConnections {
|
|
|
|
w.WriteHeader(503)
|
|
|
|
fmt.Fprint(w, "server is shutting down")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 17:33:45 -07:00
|
|
|
if strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") {
|
2015-11-17 11:01:42 -08:00
|
|
|
updateSysMem()
|
|
|
|
|
2015-12-23 21:55:15 -08:00
|
|
|
if Statistics.SysMemFreeKB > 0 && Statistics.SysMemFreeKB < Configuration.MinMemoryKBytes {
|
2015-12-16 12:00:37 -08:00
|
|
|
atomic.AddUint64(&Statistics.LowMemDroppedConnections, 1)
|
2015-11-17 11:01:42 -08:00
|
|
|
w.WriteHeader(503)
|
2016-01-15 20:59:33 -08:00
|
|
|
fmt.Fprint(w, "error: low memory")
|
2015-11-17 11:01:42 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-15 20:59:33 -08:00
|
|
|
if Configuration.MaxClientCount != 0 {
|
|
|
|
curClients := atomic.LoadUint64(&Statistics.CurrentClientCount)
|
|
|
|
if curClients >= Configuration.MaxClientCount {
|
|
|
|
w.WriteHeader(503)
|
|
|
|
fmt.Fprint(w, "error: client limit reached")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
conn, err := SocketUpgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "error: %v", err)
|
2015-10-26 18:00:29 -07:00
|
|
|
return
|
|
|
|
}
|
2015-11-16 12:50:00 -08:00
|
|
|
RunSocketConnection(conn)
|
2015-10-28 15:19:22 -07:00
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
2015-10-28 18:12:20 -07:00
|
|
|
w.Write(BannerHTML)
|
2015-10-26 18:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrProtocolGeneric is sent in a ErrorCommand Reply.
|
|
|
|
var ErrProtocolGeneric error = errors.New("FFZ Socket protocol error.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrProtocolNegativeMsgID is sent in a ErrorCommand Reply when a negative MessageID is received.
|
|
|
|
var ErrProtocolNegativeMsgID error = errors.New("FFZ Socket protocol error: negative or zero message ID.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedSingleString is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedSingleString = errors.New("Error: Expected single string as arguments.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedSingleInt is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedSingleInt = errors.New("Error: Expected single integer as arguments.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedTwoStrings is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedTwoStrings = errors.New("Error: Expected array of string, string as arguments.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedStringAndBool is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedStringAndBool = errors.New("Error: Expected array of string, bool as arguments.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedStringAndInt is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedStringAndInt = errors.New("Error: Expected array of string, int as arguments.")
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// ErrExpectedStringAndIntGotFloat is sent in a ErrorCommand Reply when the Arguments are of the wrong type.
|
|
|
|
var ErrExpectedStringAndIntGotFloat = errors.New("Error: Second argument was a float, expected an integer.")
|
|
|
|
|
2015-11-19 17:49:48 -08:00
|
|
|
// CloseGoingAway is sent when the server is restarting.
|
|
|
|
var CloseGoingAway = websocket.CloseError{Code: websocket.CloseGoingAway, Text: "server restarting"}
|
|
|
|
|
|
|
|
// CloseRebalance is sent when the server has too many clients and needs to shunt some to another server.
|
|
|
|
var CloseRebalance = websocket.CloseError{Code: websocket.CloseGoingAway, Text: "kicked for rebalancing, please select a new server"}
|
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// CloseGotBinaryMessage is the termination reason when the client sends a binary websocket frame.
|
2015-10-28 15:19:22 -07:00
|
|
|
var CloseGotBinaryMessage = websocket.CloseError{Code: websocket.CloseUnsupportedData, Text: "got binary packet"}
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// CloseTimedOut is the termination reason when the client fails to send or respond to ping frames.
|
2015-11-23 23:34:57 -08:00
|
|
|
var CloseTimedOut = websocket.CloseError{Code: 3003, Text: "no ping replies for 5 minutes"}
|
2015-11-16 13:25:25 -08:00
|
|
|
|
2015-11-17 00:28:42 -08:00
|
|
|
// CloseTooManyBufferedMessages is the termination reason when the sending thread buffers too many messages.
|
|
|
|
var CloseTooManyBufferedMessages = websocket.CloseError{Code: websocket.CloseMessageTooBig, Text: "too many pending messages"}
|
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// CloseFirstMessageNotHello is the termination reason
|
2015-10-28 17:31:44 -07:00
|
|
|
var CloseFirstMessageNotHello = websocket.CloseError{
|
|
|
|
Text: "Error - the first message sent must be a 'hello'",
|
|
|
|
Code: websocket.ClosePolicyViolation,
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-11-19 16:55:03 -08:00
|
|
|
var CloseNonUTF8Data = websocket.CloseError{
|
2015-11-23 23:34:57 -08:00
|
|
|
Code: websocket.CloseUnsupportedData,
|
2015-11-19 16:55:03 -08:00
|
|
|
Text: "Non UTF8 data recieved. Network corruption likely.",
|
|
|
|
}
|
|
|
|
|
2016-01-17 17:28:33 -08:00
|
|
|
const sendMessageBufferLength = 30
|
|
|
|
const sendMessageAbortLength = 20
|
2015-11-17 00:28:42 -08:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
// RunSocketConnection contains the main run loop of a websocket connection.
|
2016-01-17 16:50:17 -08:00
|
|
|
//
|
2015-11-16 12:50:00 -08:00
|
|
|
// First, it sets up the channels, the ClientInfo object, and the pong frame handler.
|
|
|
|
// It starts the reader goroutine pointing at the newly created channels.
|
|
|
|
// The function then enters the run loop (a `for{select{}}`).
|
|
|
|
// The run loop is broken when an object is received on errorChan, or if `hello` is not the first C2S Command.
|
2016-01-17 16:50:17 -08:00
|
|
|
//
|
2015-11-16 12:50:00 -08:00
|
|
|
// After the run loop stops, the function launches a goroutine to drain
|
|
|
|
// client.MessageChannel, signals the reader goroutine to stop, unsubscribes
|
|
|
|
// from all pub/sub channels, waits on MsgChannelKeepalive (remember, the
|
|
|
|
// messages are being drained), and finally closes client.MessageChannel
|
|
|
|
// (which ends the drainer goroutine).
|
|
|
|
func RunSocketConnection(conn *websocket.Conn) {
|
2015-10-24 19:59:34 -07:00
|
|
|
// websocket.Conn is a ReadWriteCloser
|
|
|
|
|
2015-11-17 19:53:58 -08:00
|
|
|
atomic.AddUint64(&Statistics.ClientConnectsTotal, 1)
|
|
|
|
atomic.AddUint64(&Statistics.CurrentClientCount, 1)
|
2015-10-27 21:21:06 -07:00
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
_clientChan := make(chan ClientMessage)
|
2015-11-17 00:28:42 -08:00
|
|
|
_serverMessageChan := make(chan ClientMessage, sendMessageBufferLength)
|
2015-10-24 20:40:49 -07:00
|
|
|
_errorChan := make(chan error)
|
2015-10-29 01:30:25 -07:00
|
|
|
stoppedChan := make(chan struct{})
|
2015-10-24 20:40:49 -07:00
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
var client ClientInfo
|
|
|
|
client.MessageChannel = _serverMessageChan
|
2015-11-01 13:17:35 -08:00
|
|
|
client.RemoteAddr = conn.RemoteAddr()
|
2015-11-03 16:44:42 -08:00
|
|
|
client.MsgChannelIsDone = stoppedChan
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2016-01-17 17:05:52 -08:00
|
|
|
// var report logstasher.ConnectionReport
|
|
|
|
// report.ConnectTime = time.Now()
|
|
|
|
// report.RemoteAddr = client.RemoteAddr
|
2015-12-16 14:19:23 -08:00
|
|
|
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.SetPongHandler(func(pongBody string) error {
|
2015-11-16 12:50:00 -08:00
|
|
|
client.Mutex.Lock()
|
2015-10-28 22:59:27 -07:00
|
|
|
client.pingCount = 0
|
2015-11-16 12:50:00 -08:00
|
|
|
client.Mutex.Unlock()
|
2015-10-28 22:59:27 -07:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// All set up, now enter the work loop
|
2015-12-16 11:15:17 -08:00
|
|
|
go runSocketReader(conn, _errorChan, _clientChan, stoppedChan)
|
|
|
|
closeReason := runSocketWriter(conn, &client, _errorChan, _clientChan, _serverMessageChan)
|
2015-12-02 17:28:15 -08:00
|
|
|
|
|
|
|
// Exit
|
2016-01-17 17:05:52 -08:00
|
|
|
closeConnection(conn, closeReason)
|
|
|
|
// closeConnection(conn, closeReason, &report)
|
2015-12-02 17:28:15 -08:00
|
|
|
|
|
|
|
// Launch message draining goroutine - we aren't out of the pub/sub records
|
|
|
|
go func() {
|
|
|
|
for _ = range _serverMessageChan {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Closes client.MsgChannelIsDone and also stops the reader thread
|
|
|
|
close(stoppedChan)
|
|
|
|
|
|
|
|
// Stop getting messages...
|
|
|
|
UnsubscribeAll(&client)
|
|
|
|
|
|
|
|
// Wait for pending jobs to finish...
|
|
|
|
client.MsgChannelKeepalive.Wait()
|
|
|
|
client.MessageChannel = nil
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-12-02 17:28:15 -08:00
|
|
|
// And done.
|
|
|
|
// Close the channel so the draining goroutine can finish, too.
|
|
|
|
close(_serverMessageChan)
|
|
|
|
|
|
|
|
if !StopAcceptingConnections {
|
|
|
|
// Don't perform high contention operations when server is closing
|
2015-12-16 11:15:17 -08:00
|
|
|
atomic.AddUint64(&Statistics.CurrentClientCount, NegativeOne)
|
2016-01-15 21:22:36 -08:00
|
|
|
atomic.AddUint64(&Statistics.ClientDisconnectsTotal, 1)
|
2015-12-16 11:15:17 -08:00
|
|
|
|
2016-01-17 17:05:52 -08:00
|
|
|
// report.UsernameWasValidated = client.UsernameValidated
|
|
|
|
// report.TwitchUsername = client.TwitchUsername
|
|
|
|
// logstasher.Submit(&report)
|
2016-01-15 21:22:36 -08:00
|
|
|
}
|
2015-12-16 11:15:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runSocketReader(conn *websocket.Conn, errorChan chan<- error, clientChan chan<- ClientMessage, stoppedChan <-chan struct{}) {
|
|
|
|
var msg ClientMessage
|
|
|
|
var messageType int
|
|
|
|
var packet []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer close(errorChan)
|
|
|
|
defer close(clientChan)
|
|
|
|
|
|
|
|
for ; err == nil; messageType, packet, err = conn.ReadMessage() {
|
|
|
|
if messageType == websocket.BinaryMessage {
|
|
|
|
err = &CloseGotBinaryMessage
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if messageType == websocket.CloseMessage {
|
|
|
|
err = io.EOF
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
UnmarshalClientMessage(packet, messageType, &msg)
|
|
|
|
if msg.MessageID == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case clientChan <- msg:
|
|
|
|
case <-stoppedChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case errorChan <- err:
|
|
|
|
case <-stoppedChan:
|
2015-12-02 17:28:15 -08:00
|
|
|
}
|
2015-12-16 11:15:17 -08:00
|
|
|
// exit goroutine
|
2015-12-02 17:28:15 -08:00
|
|
|
}
|
2015-11-17 19:53:58 -08:00
|
|
|
|
2015-12-16 11:15:17 -08:00
|
|
|
func runSocketWriter(conn *websocket.Conn, client *ClientInfo, errorChan <-chan error, clientChan <-chan ClientMessage, serverMessageChan <-chan ClientMessage) websocket.CloseError {
|
2015-10-24 20:40:49 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errorChan:
|
2015-10-28 15:19:22 -07:00
|
|
|
if err == io.EOF {
|
2015-12-02 17:28:15 -08:00
|
|
|
return websocket.CloseError{
|
2015-11-17 19:53:58 -08:00
|
|
|
Code: websocket.CloseGoingAway,
|
|
|
|
Text: err.Error(),
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
} else if closeMsg, isClose := err.(*websocket.CloseError); isClose {
|
2015-12-02 17:28:15 -08:00
|
|
|
return *closeMsg
|
2015-10-28 15:19:22 -07:00
|
|
|
} else {
|
2015-12-02 17:28:15 -08:00
|
|
|
return websocket.CloseError{
|
2015-10-28 15:19:22 -07:00
|
|
|
Code: websocket.CloseInternalServerErr,
|
|
|
|
Text: err.Error(),
|
2015-11-17 19:53:58 -08:00
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 21:33:55 -07:00
|
|
|
case msg := <-clientChan:
|
2015-11-16 14:30:09 -08:00
|
|
|
if client.VersionString == "" && msg.Command != HelloCommand {
|
2015-12-02 17:28:15 -08:00
|
|
|
return CloseFirstMessageNotHello
|
2015-10-24 21:33:55 -07:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:55:03 -08:00
|
|
|
for _, char := range msg.Command {
|
|
|
|
if char == utf8.RuneError {
|
2015-12-02 17:28:15 -08:00
|
|
|
return CloseNonUTF8Data
|
2015-11-19 16:55:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:28:15 -08:00
|
|
|
DispatchC2SCommand(conn, client, msg)
|
2015-10-28 22:59:27 -07:00
|
|
|
|
2015-11-16 12:50:00 -08:00
|
|
|
case msg := <-serverMessageChan:
|
2015-11-17 00:28:42 -08:00
|
|
|
if len(serverMessageChan) > sendMessageAbortLength {
|
2015-12-02 17:28:15 -08:00
|
|
|
return CloseTooManyBufferedMessages
|
2015-11-17 00:28:42 -08:00
|
|
|
}
|
2015-11-19 17:49:48 -08:00
|
|
|
if cls, ok := msg.Arguments.(*websocket.CloseError); ok {
|
2015-12-02 17:28:15 -08:00
|
|
|
return *cls
|
2015-11-19 17:49:48 -08:00
|
|
|
}
|
2015-11-16 12:50:00 -08:00
|
|
|
SendMessage(conn, msg)
|
2015-10-28 22:59:27 -07:00
|
|
|
|
2015-11-01 13:17:35 -08:00
|
|
|
case <-time.After(1 * time.Minute):
|
2015-11-16 12:50:00 -08:00
|
|
|
client.Mutex.Lock()
|
2015-10-28 22:59:27 -07:00
|
|
|
client.pingCount++
|
2015-11-16 12:50:00 -08:00
|
|
|
tooManyPings := client.pingCount == 5
|
|
|
|
client.Mutex.Unlock()
|
|
|
|
if tooManyPings {
|
2015-12-02 17:28:15 -08:00
|
|
|
return CloseTimedOut
|
2015-10-28 22:59:27 -07:00
|
|
|
} else {
|
|
|
|
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
|
|
|
}
|
2015-11-19 17:49:48 -08:00
|
|
|
|
|
|
|
case <-StopAcceptingConnectionsCh:
|
2015-12-02 17:28:15 -08:00
|
|
|
return CloseGoingAway
|
2015-10-24 20:40:49 -07:00
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:59:27 -07:00
|
|
|
func getDeadline() time.Time {
|
|
|
|
return time.Now().Add(1 * time.Minute)
|
|
|
|
}
|
|
|
|
|
2016-01-17 17:05:52 -08:00
|
|
|
func closeConnection(conn *websocket.Conn, closeMsg websocket.CloseError) {
|
2015-11-16 20:26:59 -08:00
|
|
|
closeTxt := closeMsg.Text
|
|
|
|
if strings.Contains(closeTxt, "read: connection reset by peer") {
|
|
|
|
closeTxt = "read: connection reset by peer"
|
2015-11-18 09:07:34 -08:00
|
|
|
} else if strings.Contains(closeTxt, "use of closed network connection") {
|
|
|
|
closeTxt = "read: use of closed network connection"
|
2015-11-16 20:26:59 -08:00
|
|
|
} else if closeMsg.Code == 1001 {
|
|
|
|
closeTxt = "clean shutdown"
|
|
|
|
}
|
2015-12-16 11:15:17 -08:00
|
|
|
|
2016-01-17 17:05:52 -08:00
|
|
|
// report.DisconnectCode = closeMsg.Code
|
|
|
|
// report.DisconnectReason = closeTxt
|
|
|
|
// report.DisconnectTime = time.Now()
|
2015-11-16 13:07:02 -08:00
|
|
|
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(closeMsg.Code, closeMsg.Text), getDeadline())
|
2015-10-28 15:19:22 -07:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// SendMessage sends a ClientMessage over the websocket connection with a timeout.
|
|
|
|
// If marshalling the ClientMessage fails, this function will panic.
|
2015-10-28 15:19:22 -07:00
|
|
|
func SendMessage(conn *websocket.Conn, msg ClientMessage) {
|
|
|
|
messageType, packet, err := MarshalClientMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to marshal: %v %v", err, msg))
|
|
|
|
}
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.SetWriteDeadline(getDeadline())
|
2015-10-28 15:19:22 -07:00
|
|
|
conn.WriteMessage(messageType, packet)
|
2015-12-16 12:00:37 -08:00
|
|
|
atomic.AddUint64(&Statistics.MessagesSent, 1)
|
2015-10-28 15:19:22 -07:00
|
|
|
}
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
// UnmarshalClientMessage unpacks websocket TextMessage into a ClientMessage provided in the `v` parameter.
|
2015-10-28 15:19:22 -07:00
|
|
|
func UnmarshalClientMessage(data []byte, payloadType int, v interface{}) (err error) {
|
2015-10-24 19:59:34 -07:00
|
|
|
var spaceIdx int
|
|
|
|
|
|
|
|
out := v.(*ClientMessage)
|
|
|
|
dataStr := string(data)
|
|
|
|
|
|
|
|
// Message ID
|
|
|
|
spaceIdx = strings.IndexRune(dataStr, ' ')
|
|
|
|
if spaceIdx == -1 {
|
2015-11-16 12:50:00 -08:00
|
|
|
return ErrProtocolGeneric
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
2015-11-15 18:43:34 -08:00
|
|
|
messageID, err := strconv.Atoi(dataStr[:spaceIdx])
|
|
|
|
if messageID < -1 || messageID == 0 {
|
2015-11-16 12:50:00 -08:00
|
|
|
return ErrProtocolNegativeMsgID
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2015-11-15 18:43:34 -08:00
|
|
|
out.MessageID = messageID
|
2015-10-26 10:06:45 -07:00
|
|
|
dataStr = dataStr[spaceIdx+1:]
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
spaceIdx = strings.IndexRune(dataStr, ' ')
|
|
|
|
if spaceIdx == -1 {
|
2016-01-17 19:46:01 -08:00
|
|
|
out.Command = CommandPool.InternCommand(dataStr)
|
2015-10-24 19:59:34 -07:00
|
|
|
out.Arguments = nil
|
|
|
|
return nil
|
|
|
|
} else {
|
2016-01-17 19:46:01 -08:00
|
|
|
out.Command = CommandPool.InternCommand(dataStr[:spaceIdx])
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
2015-10-26 10:06:45 -07:00
|
|
|
dataStr = dataStr[spaceIdx+1:]
|
2016-01-17 17:46:34 -08:00
|
|
|
argumentsJSON := string([]byte(dataStr))
|
2015-11-15 18:43:34 -08:00
|
|
|
out.origArguments = argumentsJSON
|
2015-10-26 11:22:06 -07:00
|
|
|
err = out.parseOrigArguments()
|
2015-10-24 19:59:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:22:06 -07:00
|
|
|
func (cm *ClientMessage) parseOrigArguments() error {
|
|
|
|
err := json.Unmarshal([]byte(cm.origArguments), &cm.Arguments)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
func MarshalClientMessage(clientMessage interface{}) (payloadType int, data []byte, err error) {
|
2015-10-24 19:59:34 -07:00
|
|
|
var msg ClientMessage
|
|
|
|
var ok bool
|
|
|
|
msg, ok = clientMessage.(ClientMessage)
|
|
|
|
if !ok {
|
|
|
|
pMsg, ok := clientMessage.(*ClientMessage)
|
|
|
|
if !ok {
|
|
|
|
panic("MarshalClientMessage: argument needs to be a ClientMessage")
|
|
|
|
}
|
|
|
|
msg = *pMsg
|
|
|
|
}
|
|
|
|
var dataStr string
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
if msg.Command == "" && msg.MessageID == 0 {
|
|
|
|
panic("MarshalClientMessage: attempt to send an empty ClientMessage")
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
if msg.Command == "" {
|
|
|
|
msg.Command = SuccessCommand
|
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
if msg.MessageID == 0 {
|
|
|
|
msg.MessageID = -1
|
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
if msg.Arguments != nil {
|
|
|
|
argBytes, err := json.Marshal(msg.Arguments)
|
|
|
|
if err != nil {
|
2015-10-28 15:19:22 -07:00
|
|
|
return 0, nil, err
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dataStr = fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, string(argBytes))
|
|
|
|
} else {
|
|
|
|
dataStr = fmt.Sprintf("%d %s", msg.MessageID, msg.Command)
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
return websocket.TextMessage, []byte(dataStr), nil
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
// ArgumentsAsString parses the arguments of the ClientMessage as a single string.
|
2015-10-24 19:59:34 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsString() (string1 string, err error) {
|
|
|
|
var ok bool
|
|
|
|
string1, ok = cm.Arguments.(string)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedSingleString
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
return string1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
// ArgumentsAsInt parses the arguments of the ClientMessage as a single int.
|
2015-10-26 14:55:20 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsInt() (int1 int64, err error) {
|
2015-10-25 00:44:25 -07:00
|
|
|
var ok bool
|
|
|
|
var num float64
|
|
|
|
num, ok = cm.Arguments.(float64)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedSingleInt
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
} else {
|
2015-10-26 14:55:20 -07:00
|
|
|
int1 = int64(num)
|
2015-10-25 00:44:25 -07:00
|
|
|
return int1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
// ArgumentsAsTwoStrings parses the arguments of the ClientMessage as an array of two strings.
|
2015-10-24 19:59:34 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsTwoStrings() (string1, string2 string, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedTwoStrings
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedTwoStrings
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
string1, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedTwoStrings
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
2015-10-28 16:07:51 -07:00
|
|
|
// clientID can be null
|
|
|
|
if ary[1] == nil {
|
|
|
|
return string1, "", nil
|
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
string2, ok = ary[1].(string)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedTwoStrings
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
return string1, string2, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
// ArgumentsAsStringAndInt parses the arguments of the ClientMessage as an array of a string and an int.
|
2015-10-24 19:59:34 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsStringAndInt() (string1 string, int int64, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndInt
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndInt
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
string1, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndInt
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
var num float64
|
|
|
|
num, ok = ary[1].(float64)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndInt
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
int = int64(num)
|
|
|
|
if float64(int) != num {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndIntGotFloat
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
return string1, int, nil
|
|
|
|
}
|
|
|
|
}
|
2015-10-25 00:44:25 -07:00
|
|
|
|
2016-01-17 18:01:21 -08:00
|
|
|
// ArgumentsAsStringAndBool parses the arguments of the ClientMessage as an array of a string and an int.
|
2015-10-25 00:44:25 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsStringAndBool() (str string, flag bool, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndBool
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndBool
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
str, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndBool
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
flag, ok = ary[1].(bool)
|
|
|
|
if !ok {
|
2015-11-16 12:50:00 -08:00
|
|
|
err = ErrExpectedStringAndBool
|
2015-10-26 10:06:45 -07:00
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
return str, flag, nil
|
|
|
|
}
|
|
|
|
}
|