1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-08-01 23:48:31 +00:00

rename listener package to internal/server

This commit is contained in:
Kane York 2015-10-25 00:44:25 -07:00
parent 93c3f6f672
commit 2a6c36bba5
9 changed files with 346 additions and 284 deletions

View file

@ -0,0 +1,72 @@
package server
import (
"net/http"
"time"
"fmt"
"net/url"
"github.com/pmylund/go-cache"
"strconv"
"io/ioutil"
)
var httpClient http.Client
var backendUrl string
var responseCache *cache.Cache
func SetupBackend(url string) {
httpClient.Timeout = 60 * time.Second
backendUrl = url
responseCache = cache.New(60 * time.Second, 120 * time.Second)
}
func getCacheKey(remoteCommand, data string) string {
return fmt.Sprintf("%s/%s", remoteCommand, data)
}
func RequestRemoteDataCached(remoteCommand, data string, auth AuthInfo) (string, error) {
cached, ok := responseCache.Get(getCacheKey(remoteCommand, data))
if ok {
return cached.(string), nil
}
return RequestRemoteData(remoteCommand, data, auth)
}
func RequestRemoteData(remoteCommand, data string, auth AuthInfo) (string, error) {
destUrl := fmt.Sprintf("%s/%s", backendUrl, remoteCommand)
var authKey string
if auth.UsernameValidated {
authKey = "usernameClaimed"
} else {
authKey = "username"
}
formData := url.Values{
"clientData": []string{data},
authKey: []string{auth.TwitchUsername},
}
resp, err := httpClient.PostForm(destUrl, formData)
if err != nil {
return "", err
}
respBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", err
}
responseJson := string(respBytes)
if resp.Header.Get("FFZ-Cache") != "" {
durSecs, err := strconv.ParseInt(resp.Header.Get("FFZ-Cache"), 10, 64)
if err != nil {
return "", fmt.Errorf("The RPC server returned a non-integer cache duration: %v", err)
}
duration := time.Duration(durSecs) * time.Second
responseCache.Set(getCacheKey(remoteCommand, data), responseJson, duration)
}
return responseJson, nil
}

View file

@ -0,0 +1,134 @@
package server
import (
"golang.org/x/net/websocket"
"github.com/satori/go.uuid"
"log"
)
var ResponseSuccess = ClientMessage{Command: SuccessCommand}
var ResponseFailure = ClientMessage{Command: "False"}
func HandleCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) {
handler, ok := CommandHandlers[msg.Command]
if !ok {
log.Print("[!] Unknown command", msg.Command, "- sent by client", client.ClientID, "@", conn.RemoteAddr())
// uncomment after commands are implemented
// closer()
return
}
log.Println(conn.RemoteAddr(), msg.MessageID, msg.Command, msg.Arguments)
client.Mutex.Lock()
response, err := CallHandler(handler, conn, client, msg)
client.Mutex.Unlock()
if err == nil {
response.MessageID = msg.MessageID
FFZCodec.Send(conn, response)
} else if response.Command == AsyncResponseCommand {
// Don't send anything
// The response will be delivered over client.MessageChannel / serverMessageChan
} else {
FFZCodec.Send(conn, ClientMessage{
MessageID: msg.MessageID,
Command: "error",
Arguments: err.Error(),
})
}
}
func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
version, clientId, err := msg.ArgumentsAsTwoStrings()
if err != nil {
return
}
client.Version = version
client.ClientID = uuid.FromStringOrNil(clientId)
if client.ClientID == uuid.Nil {
client.ClientID = uuid.NewV4()
}
return ClientMessage{
Arguments: client.ClientID.String(),
}, nil
}
func HandleSetUser(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
username, err := msg.ArgumentsAsString()
if err != nil {
return
}
client.TwitchUsername = username
client.UsernameValidated = false
return ResponseSuccess, nil
}
func HandleSub(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
channel, err := msg.ArgumentsAsString()
AddToSliceS(&client.CurrentChannels, channel)
// TODO - get backlog
return ResponseSuccess, nil
}
func HandleUnsub(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
channel, err := msg.ArgumentsAsString()
RemoveFromSliceS(&client.CurrentChannels, channel)
return ResponseSuccess, nil
}
func HandleSubChannel(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
channel, err := msg.ArgumentsAsString()
AddToSliceS(&client.WatchingChannels, channel)
// TODO - get backlog
return ResponseSuccess, nil
}
func HandleUnsubChannel(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
channel, err := msg.ArgumentsAsString()
RemoveFromSliceS(&client.WatchingChannels, channel)
return ResponseSuccess, nil
}
func HandleSurvey(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
log.Println("Ignoring survey response from", client.ClientID)
return ResponseSuccess, nil
}
func HandleTrackFollow(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
return ResponseSuccess, nil
}
func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
return ResponseSuccess, nil
}
func HandleRemoteCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
go func(conn *websocket.Conn, msg ClientMessage, authInfo AuthInfo) {
resp, err := RequestRemoteDataCached(string(msg.Command), msg.origArguments, authInfo)
if err != nil {
FFZCodec.Send(conn, ClientMessage{MessageID: msg.MessageID, Command: ErrorCommand, Arguments: err.Error()})
} else {
FFZCodec.Send(conn, ClientMessage{MessageID: msg.MessageID, Command: SuccessCommand, origArguments: resp})
}
}(conn, msg, client.AuthInfo)
return ClientMessage{Command: AsyncResponseCommand}, nil
}

View file

@ -0,0 +1,378 @@
package server // import "bitbucket.org/stendec/frankerfacez/socketserver/server"
import (
"net/http"
"golang.org/x/net/websocket"
"crypto/tls"
"log"
"strings"
"strconv"
"errors"
"encoding/json"
"fmt"
"sync"
)
const MAX_PACKET_SIZE = 1024
type Config struct {
SSLCertificateFile string
SSLKeyFile string
UseSSL bool
SocketOrigin string
}
// A command is how the client refers to a function on the server. It's just a string.
type Command string
// A function that is called to respond to a Command.
type CommandHandler func(*websocket.Conn, *ClientInfo, ClientMessage) (ClientMessage, error)
var CommandHandlers = map[Command]CommandHandler{
HelloCommand: HandleHello,
"setuser": HandleSetUser,
"sub": HandleSub,
"unsub": HandleUnsub,
"sub_channel": HandleSubChannel,
"unsub_channel": HandleUnsubChannel,
"track_follow": HandleTrackFollow,
"emoticon_uses": HandleEmoticonUses,
"survey": HandleSurvey,
"twitch_emote": HandleRemoteCommand,
"get_link": HandleRemoteCommand,
"get_display_name": HandleRemoteCommand,
"update_follow_buttons": HandleRemoteCommand,
"chat_history": HandleRemoteCommand,
}
// Sent by the server in ClientMessage.Command to indicate success.
const SuccessCommand Command = "True"
// Sent by the server in ClientMessage.Command to indicate failure.
const ErrorCommand Command = "error"
// This must be the first command sent by the client once the connection is established.
const HelloCommand Command = "hello"
// A handler returning a ClientMessage with this Command will prevent replying to the client.
// It signals that the work has been handed off to a background goroutine.
const AsyncResponseCommand Command = "_async"
// A websocket.Codec that translates the protocol into ClientMessage objects.
var FFZCodec websocket.Codec = websocket.Codec{
Marshal: MarshalClientMessage,
Unmarshal: UnmarshalClientMessage,
}
// Errors that get returned to the client.
var ProtocolError error = errors.New("FFZ Socket protocol error.")
var ExpectedSingleString = errors.New("Error: Expected single string as arguments.")
var ExpectedSingleInt = errors.New("Error: Expected single integer as arguments.")
var ExpectedTwoStrings = errors.New("Error: Expected array of string, string as arguments.")
var ExpectedStringAndInt = errors.New("Error: Expected array of string, int as arguments.")
var ExpectedStringAndBool = errors.New("Error: Expected array of string, bool as arguments.")
var ExpectedStringAndIntGotFloat = errors.New("Error: Second argument was a float, expected an integer.")
// Create a websocket.Server with the options from the provided Config.
func SetupServer(config *Config) *websocket.Server {
sockConf, err := websocket.NewConfig("/", config.SocketOrigin)
if err != nil {
panic(err)
}
if config.UseSSL {
cert, err := tls.LoadX509KeyPair(config.SSLCertificateFile, config.SSLKeyFile)
if err != nil {
panic(err)
}
tlsConf := &tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: config.SocketOrigin,
}
tlsConf.BuildNameToCertificate()
sockConf.TlsConfig = tlsConf
}
sockServer := &websocket.Server{}
sockServer.Config = *sockConf
sockServer.Handler = HandleSocketConnection
return sockServer
}
// Set up a websocket listener and register it on /.
// (Uses http.DefaultServeMux .)
func SetupServerAndHandle(config *Config) {
sockServer := SetupServer(config)
http.HandleFunc("/", sockServer.ServeHTTP)
}
// Handle a new websocket connection from a FFZ client.
// This runs in a goroutine started by net/http.
func HandleSocketConnection(conn *websocket.Conn) {
// websocket.Conn is a ReadWriteCloser
var _closer sync.Once
closer := func() {
_closer.Do(func() {
conn.Close()
})
}
defer func() {
closer()
}()
log.Print("! Got a connection from ", conn.RemoteAddr())
_clientChan := make(chan ClientMessage)
_serverMessageChan := make(chan ClientMessage)
_errorChan := make(chan error)
// Receive goroutine
go func(errorChan chan <- error, clientChan chan <- ClientMessage) {
var msg ClientMessage
var err error
for ; err == nil; err = FFZCodec.Receive(conn, &msg) {
if msg.MessageID == 0 {
continue
}
clientChan <- msg
}
errorChan <- err
close(errorChan)
close(clientChan)
// exit
}(_errorChan, _clientChan)
var client ClientInfo
client.MessageChannel = _serverMessageChan
var errorChan <-chan error = _errorChan
var clientChan <-chan ClientMessage = _clientChan
var serverMessageChan <-chan ClientMessage = _serverMessageChan
RunLoop:
for {
select {
case err := <-errorChan:
FFZCodec.Send(conn, ClientMessage{
MessageID: -1,
Command: "error",
Arguments: err.Error(),
}) // note - socket might be closed, but don't care
break RunLoop
case msg := <-clientChan:
if client.Version == "" && msg.Command != HelloCommand {
FFZCodec.Send(conn, ClientMessage{
MessageID: msg.MessageID,
Command: "error",
Arguments: "Error - the first message sent must be a 'hello'",
})
break RunLoop
}
HandleCommand(conn, &client, msg)
case smsg := <-serverMessageChan:
FFZCodec.Send(conn, smsg)
}
}
// exit
}
func CallHandler(handler CommandHandler, conn *websocket.Conn, client *ClientInfo, cmsg ClientMessage) (rmsg ClientMessage, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
fmt.Print("[!] Error executing command", cmsg.Command, "--", r)
err, ok = r.(error)
if !ok {
err = fmt.Errorf("command handler: %v", r)
}
}
}()
return handler(conn, client, cmsg)
}
// Unpack a message sent from the client into a ClientMessage.
func UnmarshalClientMessage(data []byte, payloadType byte, v interface{}) (err error) {
var spaceIdx int
out := v.(*ClientMessage)
dataStr := string(data)
// Message ID
spaceIdx = strings.IndexRune(dataStr, ' ')
if spaceIdx == -1 {
return ProtocolError
}
messageId, err := strconv.Atoi(dataStr[:spaceIdx])
if messageId <= 0 {
return ProtocolError
}
out.MessageID = messageId
dataStr = dataStr[spaceIdx + 1:]
spaceIdx = strings.IndexRune(dataStr, ' ')
if spaceIdx == -1 {
out.Command = Command(dataStr)
out.Arguments = nil
return nil
} else {
out.Command = Command(dataStr[:spaceIdx])
}
dataStr = dataStr[spaceIdx + 1:]
argumentsJson := dataStr
out.origArguments = argumentsJson
err = json.Unmarshal([]byte(argumentsJson), &out.Arguments)
if err != nil {
return
}
return nil
}
func MarshalClientMessage(clientMessage interface{}) (data []byte, payloadType byte, err error) {
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
if msg.Command == "" && msg.MessageID == 0 {
panic("MarshalClientMessage: attempt to send an empty ClientMessage")
}
if msg.Command == "" {
msg.Command = SuccessCommand
}
if msg.MessageID == 0 {
msg.MessageID = -1
}
if msg.Arguments != nil {
argBytes, err := json.Marshal(msg.Arguments)
if err != nil {
return nil, 0, err
}
dataStr = fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, string(argBytes))
} else {
dataStr = fmt.Sprintf("%d %s", msg.MessageID, msg.Command)
}
return []byte(dataStr), websocket.TextFrame, nil
}
// Command handlers should use this to construct responses.
func NewClientMessage(arguments interface{}) ClientMessage {
return ClientMessage{
MessageID: 0, // filled by the select loop
Command: SuccessCommand,
Arguments: arguments,
}
}
// Convenience method: Parse the arguments of the ClientMessage as a single string.
func (cm *ClientMessage) ArgumentsAsString() (string1 string, err error) {
var ok bool
string1, ok = cm.Arguments.(string)
if !ok {
err = ExpectedSingleString; return
} else {
return string1, nil
}
}
// Convenience method: Parse the arguments of the ClientMessage as a single int.
func (cm *ClientMessage) ArgumentsAsInt() (int1 int, err error) {
var ok bool
var num float64
num, ok = cm.Arguments.(float64)
if !ok {
err = ExpectedSingleInt; return
} else {
int1 = int(num)
return int1, nil
}
}
// Convenience method: Parse the arguments of the ClientMessage as an array of two strings.
func (cm *ClientMessage) ArgumentsAsTwoStrings() (string1, string2 string, err error) {
var ok bool
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedTwoStrings; return
} else {
if len(ary) != 2 {
err = ExpectedTwoStrings; return
}
string1, ok = ary[0].(string)
if !ok {
err = ExpectedTwoStrings; return
}
string2, ok = ary[1].(string)
if !ok {
err = ExpectedTwoStrings; return
}
return string1, string2, nil
}
}
// Convenience method: Parse the arguments of the ClientMessage as an array of a string and an int.
func (cm *ClientMessage) ArgumentsAsStringAndInt() (string1 string, int int64, err error) {
var ok bool
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedStringAndInt; return
} else {
if len(ary) != 2 {
err = ExpectedStringAndInt; return
}
string1, ok = ary[0].(string)
if !ok {
err = ExpectedStringAndInt; return
}
var num float64
num, ok = ary[1].(float64)
if !ok {
err = ExpectedStringAndInt; return
}
int = int64(num)
if float64(int) != num {
err = ExpectedStringAndIntGotFloat; return
}
return string1, int, nil
}
}
// Convenience method: Parse the arguments of the ClientMessage as an array of a string and an int.
func (cm *ClientMessage) ArgumentsAsStringAndBool() (str string, flag bool, err error) {
var ok bool
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedStringAndBool; return
} else {
if len(ary) != 2 {
err = ExpectedStringAndBool; return
}
str, ok = ary[0].(string)
if !ok {
err = ExpectedStringAndBool; return
}
flag, ok = ary[1].(bool)
if !ok {
err = ExpectedStringAndBool; return
}
return str, flag, nil
}
}

View file

@ -0,0 +1,57 @@
package server
import (
"golang.org/x/net/websocket"
"fmt"
"testing"
)
func ExampleUnmarshalClientMessage() {
sourceData := []byte("100 hello [\"ffz_3.5.30\",\"898b5bfa-b577-47bb-afb4-252c703b67d6\"]")
var cm ClientMessage
err := UnmarshalClientMessage(sourceData, websocket.TextFrame, &cm)
fmt.Println(err)
fmt.Println(cm.MessageID)
fmt.Println(cm.Command)
fmt.Println(cm.Arguments)
// Output:
// <nil>
// 100
// hello
// [ffz_3.5.30 898b5bfa-b577-47bb-afb4-252c703b67d6]
}
func ExampleMarshalClientMessage() {
var cm ClientMessage = ClientMessage{
MessageID: -1,
Command: "do_authorize",
Arguments: "1234567890",
}
data, payloadType, err := MarshalClientMessage(&cm)
fmt.Println(err)
fmt.Println(payloadType == websocket.TextFrame)
fmt.Println(string(data))
// Output:
// <nil>
// true
// -1 do_authorize "1234567890"
}
func TestArgumentsAsStringAndBool(t *testing.T) {
sourceData := []byte("1 foo [\"string\", false]")
var cm ClientMessage
err := UnmarshalClientMessage(sourceData, websocket.TextFrame, &cm)
if err != nil {
t.Fatal(err)
}
str, boolean, err := cm.ArgumentsAsStringAndBool()
if err != nil {
t.Fatal(err)
}
if str != "string" {
t.Error("Expected first array item to be 'string', got", str)
}
if boolean != false {
t.Error("Expected second array item to be false, got", boolean)
}
}

View file

@ -0,0 +1,59 @@
package server
import (
"github.com/satori/go.uuid"
"sync"
)
type ClientMessage struct {
// Message ID. Increments by 1 for each message sent from the client.
// When replying to a command, the message ID must be echoed.
// When sending a server-initiated message, this is -1.
MessageID int
// The command that the client wants from the server.
// When sent from the server, the literal string 'True' indicates success.
// Before sending, a blank Command will be converted into SuccessCommand.
Command Command
// Result of json.Unmarshal on the third field send from the client
Arguments interface{}
origArguments string
}
type AuthInfo struct {
// The client's claimed username on Twitch.
TwitchUsername string
// Whether or not the server has validated the client's claimed username.
UsernameValidated bool
}
type ClientInfo struct {
// The client ID.
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
ClientID uuid.UUID
// The client's version.
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
Version string
// This mutex protects writable data in this struct.
// If it seems to be a performance problem, we can split this.
Mutex sync.Mutex
AuthInfo
// Username validation nonce.
ValidationNonce string
// The list of chats this client is currently in.
// Protected by Mutex
CurrentChannels []string
// This list of channels this client needs UI updates for.
// Protected by Mutex
WatchingChannels []string
// Server-initiated messages should be sent here
MessageChannel chan <- ClientMessage
}

View file

@ -0,0 +1,34 @@
package server
import (
)
func AddToSliceS(ary *[]string, val string) {
slice := *ary
for _, v := range slice {
if v == val {
return
}
}
slice = append(slice, val)
*ary = slice
}
func RemoveFromSliceS(ary *[]string, val string) {
slice := *ary
var idx int = -1
for i, v := range slice {
if v == val {
idx = i
break
}
}
if idx == -1 {
return
}
slice[idx] = slice[len(slice) - 1]
slice = slice[:len(slice) - 1]
*ary = slice
}