2015-10-25 00:44:25 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-10-26 10:06:45 -07:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2015-10-25 00:44:25 -07:00
|
|
|
"fmt"
|
|
|
|
"github.com/pmylund/go-cache"
|
2015-10-26 10:06:45 -07:00
|
|
|
"golang.org/x/crypto/nacl/box"
|
2015-10-25 00:44:25 -07:00
|
|
|
"io/ioutil"
|
2015-10-25 03:21:50 -07:00
|
|
|
"log"
|
2015-10-26 10:06:45 -07:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2015-10-25 12:40:07 -07:00
|
|
|
"strings"
|
2015-10-26 10:06:45 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-10-25 00:44:25 -07:00
|
|
|
)
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
var backendHttpClient http.Client
|
2015-10-25 00:44:25 -07:00
|
|
|
var backendUrl string
|
|
|
|
var responseCache *cache.Cache
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
var getBacklogUrl string
|
2015-10-28 15:19:22 -07:00
|
|
|
var postStatisticsUrl string
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-10-25 12:40:07 -07:00
|
|
|
var backendSharedKey [32]byte
|
2015-10-25 14:06:56 -07:00
|
|
|
var serverId int
|
2015-10-25 12:40:07 -07:00
|
|
|
|
|
|
|
var messageBufferPool sync.Pool
|
|
|
|
|
2015-10-26 22:16:03 -07:00
|
|
|
func SetupBackend(config *ConfigFile) {
|
2015-10-25 03:21:50 -07:00
|
|
|
backendHttpClient.Timeout = 60 * time.Second
|
|
|
|
backendUrl = config.BackendUrl
|
|
|
|
if responseCache != nil {
|
|
|
|
responseCache.Flush()
|
|
|
|
}
|
2015-10-26 10:06:45 -07:00
|
|
|
responseCache = cache.New(60*time.Second, 120*time.Second)
|
2015-10-25 03:21:50 -07:00
|
|
|
|
|
|
|
getBacklogUrl = fmt.Sprintf("%s/backlog", backendUrl)
|
2015-10-28 15:19:22 -07:00
|
|
|
postStatisticsUrl = fmt.Sprintf("%s/stats", backendUrl)
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-10-25 14:06:56 -07:00
|
|
|
messageBufferPool.New = New4KByteBuffer
|
2015-10-25 12:40:07 -07:00
|
|
|
|
2015-10-25 14:06:56 -07:00
|
|
|
var theirPublic, ourPrivate [32]byte
|
2015-10-26 22:16:03 -07:00
|
|
|
copy(theirPublic[:], config.BackendPublicKey)
|
|
|
|
copy(ourPrivate[:], config.OurPrivateKey)
|
|
|
|
serverId = config.ServerId
|
2015-10-25 14:06:56 -07:00
|
|
|
|
|
|
|
box.Precompute(&backendSharedKey, &theirPublic, &ourPrivate)
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCacheKey(remoteCommand, data string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", remoteCommand, data)
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:22:06 -07:00
|
|
|
// Publish a message to clients with no caching.
|
|
|
|
// The scope must be specified because no attempt is made to recognize the command.
|
2015-10-26 10:06:45 -07:00
|
|
|
func HBackendPublishRequest(w http.ResponseWriter, r *http.Request) {
|
2015-10-26 11:22:06 -07:00
|
|
|
r.ParseForm()
|
2015-10-26 08:58:04 -07:00
|
|
|
formData, err := UnsealRequest(r.Form)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(403)
|
|
|
|
fmt.Fprintf(w, "Error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := formData.Get("cmd")
|
|
|
|
json := formData.Get("args")
|
2015-10-26 10:07:15 -07:00
|
|
|
channel := formData.Get("channel")
|
|
|
|
scope := formData.Get("scope")
|
2015-10-26 10:23:53 -07:00
|
|
|
|
|
|
|
target := MessageTargetTypeByName(scope)
|
|
|
|
|
|
|
|
if cmd == "" {
|
|
|
|
w.WriteHeader(422)
|
|
|
|
fmt.Fprintf(w, "Error: cmd cannot be blank")
|
|
|
|
return
|
|
|
|
}
|
2015-10-26 12:13:28 -07:00
|
|
|
if channel == "" && (target == MsgTargetTypeChat || target == MsgTargetTypeMultichat) {
|
2015-10-26 10:23:53 -07:00
|
|
|
w.WriteHeader(422)
|
|
|
|
fmt.Fprintf(w, "Error: channel must be specified")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:58:04 -07:00
|
|
|
cm := ClientMessage{MessageID: -1, Command: Command(cmd), origArguments: json}
|
2015-10-26 11:22:06 -07:00
|
|
|
cm.parseOrigArguments()
|
2015-10-26 08:58:04 -07:00
|
|
|
var count int
|
2015-10-26 10:23:53 -07:00
|
|
|
|
|
|
|
switch target {
|
|
|
|
case MsgTargetTypeSingle:
|
|
|
|
// TODO
|
|
|
|
case MsgTargetTypeChat:
|
2015-10-26 10:07:15 -07:00
|
|
|
count = PublishToChat(channel, cm)
|
2015-10-26 10:23:53 -07:00
|
|
|
case MsgTargetTypeMultichat:
|
|
|
|
// TODO
|
|
|
|
case MsgTargetTypeGlobal:
|
2015-10-26 10:07:15 -07:00
|
|
|
count = PublishToAll(cm)
|
2015-10-26 10:23:53 -07:00
|
|
|
case MsgTargetTypeInvalid:
|
|
|
|
default:
|
|
|
|
w.WriteHeader(422)
|
|
|
|
fmt.Fprint(w, "Invalid 'scope'. must be single, chat, multichat, channel, or global")
|
2015-10-26 08:58:04 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, count)
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:40:07 -07:00
|
|
|
func RequestRemoteData(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
|
2015-10-25 03:21:50 -07:00
|
|
|
destUrl := fmt.Sprintf("%s/cmd/%s", backendUrl, remoteCommand)
|
2015-10-25 00:44:25 -07:00
|
|
|
var authKey string
|
|
|
|
if auth.UsernameValidated {
|
|
|
|
authKey = "usernameClaimed"
|
|
|
|
} else {
|
|
|
|
authKey = "username"
|
|
|
|
}
|
|
|
|
|
|
|
|
formData := url.Values{
|
|
|
|
"clientData": []string{data},
|
2015-10-26 10:06:45 -07:00
|
|
|
authKey: []string{auth.TwitchUsername},
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
sealedForm, err := SealRequest(formData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := backendHttpClient.PostForm(destUrl, sealedForm)
|
2015-10-25 00:44:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:40:07 -07:00
|
|
|
responseStr = string(respBytes)
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
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
|
2015-10-25 12:40:07 -07:00
|
|
|
responseCache.Set(getCacheKey(remoteCommand, data), responseStr, duration)
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
|
2015-10-25 12:40:07 -07:00
|
|
|
return
|
2015-10-25 03:21:50 -07:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
func SendAggregatedData(sealedForm url.Values) (error) {
|
|
|
|
resp, err := backendHttpClient.PostForm(postStatisticsUrl, sealedForm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2015-10-26 12:13:28 -07:00
|
|
|
func FetchBacklogData(chatSubs []string) ([]ClientMessage, error) {
|
2015-10-25 03:21:50 -07:00
|
|
|
formData := url.Values{
|
2015-10-26 14:55:20 -07:00
|
|
|
"subs": chatSubs,
|
2015-10-25 03:21:50 -07:00
|
|
|
}
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
sealedForm, err := SealRequest(formData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := backendHttpClient.PostForm(getBacklogUrl, sealedForm)
|
2015-10-25 03:21:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
var messages []ClientMessage
|
|
|
|
err = dec.Decode(messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, nil
|
2015-10-25 12:40:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateKeys(outputFile, serverId, theirPublicStr string) {
|
|
|
|
var err error
|
2015-10-27 18:22:56 -07:00
|
|
|
output := ConfigFile{
|
2015-10-27 21:21:06 -07:00
|
|
|
ListenAddr: "0.0.0.0:8001",
|
2015-10-27 18:22:56 -07:00
|
|
|
SocketOrigin: "localhost:8001",
|
2015-10-27 21:21:06 -07:00
|
|
|
BackendUrl: "http://localhost:8002/ffz",
|
2015-10-27 18:22:56 -07:00
|
|
|
BannerHTML: `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<title>CatBag</title>
|
|
|
|
<link rel="stylesheet" href="//cdn.frankerfacez.com/script/catbag.css">
|
|
|
|
<div id="container">
|
|
|
|
<div id="zf0"></div><div id="zf1"></div><div id="zf2"></div>
|
|
|
|
<div id="zf3"></div><div id="zf4"></div><div id="zf5"></div>
|
|
|
|
<div id="zf6"></div><div id="zf7"></div><div id="zf8"></div>
|
|
|
|
<div id="zf9"></div><div id="catbag"></div>
|
|
|
|
<div id="bottom">
|
|
|
|
A <a href="http://www.frankerfacez.com/">FrankerFaceZ</a> Service
|
|
|
|
— CatBag by <a href="http://www.twitch.tv/wolsk">Wolsk</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
}
|
2015-10-25 12:40:07 -07:00
|
|
|
|
|
|
|
output.ServerId, err = strconv.Atoi(serverId)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ourPublic, ourPrivate, err := box.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-25 14:06:56 -07:00
|
|
|
output.OurPublicKey, output.OurPrivateKey = ourPublic[:], ourPrivate[:]
|
2015-10-25 12:40:07 -07:00
|
|
|
|
|
|
|
if theirPublicStr != "" {
|
2015-10-25 14:06:56 -07:00
|
|
|
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(theirPublicStr))
|
2015-10-25 12:40:07 -07:00
|
|
|
theirPublic, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-26 22:16:03 -07:00
|
|
|
output.BackendPublicKey = theirPublic
|
2015-10-25 12:40:07 -07:00
|
|
|
}
|
|
|
|
|
2015-10-26 22:16:03 -07:00
|
|
|
bytes, err := json.MarshalIndent(output, "", "\t")
|
2015-10-25 12:40:07 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-26 22:16:03 -07:00
|
|
|
fmt.Println(string(bytes))
|
|
|
|
err = ioutil.WriteFile(outputFile, bytes, 0600)
|
2015-10-25 12:40:07 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|