mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-07-25 20:18:31 +00:00
golint part 1
This commit is contained in:
parent
aa6f090fcc
commit
66cc124e37
9 changed files with 136 additions and 112 deletions
|
@ -74,20 +74,18 @@ func commandLineConsole() {
|
|||
|
||||
shell.Register("authorizeeveryone", func(args ...string) (string, error) {
|
||||
if len(args) == 0 {
|
||||
if server.Configuation.SendAuthToNewClients {
|
||||
if server.Configuration.SendAuthToNewClients {
|
||||
return "All clients are recieving auth challenges upon claiming a name.", nil
|
||||
} else {
|
||||
return "All clients are not recieving auth challenges upon claiming a name.", nil
|
||||
}
|
||||
return "All clients are not recieving auth challenges upon claiming a name.", nil
|
||||
} else if args[0] == "on" {
|
||||
server.Configuation.SendAuthToNewClients = true
|
||||
server.Configuration.SendAuthToNewClients = true
|
||||
return "All new clients will recieve auth challenges upon claiming a name.", nil
|
||||
} else if args[0] == "off" {
|
||||
server.Configuation.SendAuthToNewClients = false
|
||||
server.Configuration.SendAuthToNewClients = false
|
||||
return "All new clients will not recieve auth challenges upon claiming a name.", nil
|
||||
} else {
|
||||
return "Usage: authorizeeveryone [ on | off ]", nil
|
||||
}
|
||||
return "Usage: authorizeeveryone [ on | off ]", nil
|
||||
})
|
||||
|
||||
shell.Register("panic", func(args ...string) (string, error) {
|
||||
|
|
|
@ -11,14 +11,14 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
var configFilename *string = flag.String("config", "config.json", "Configuration file, including the keypairs for the NaCl crypto library, for communicating with the backend.")
|
||||
var generateKeys *bool = flag.Bool("genkeys", false, "Generate NaCl keys instead of serving requests.\nArguments: [int serverId] [base64 backendPublic]\nThe backend public key can either be specified in base64 on the command line, or put in the json file later.")
|
||||
var configFilename = flag.String("config", "config.json", "Configuration file, including the keypairs for the NaCl crypto library, for communicating with the backend.")
|
||||
var flagGenerateKeys = flag.Bool("genkeys", false, "Generate NaCl keys instead of serving requests.\nArguments: [int serverId] [base64 backendPublic]\nThe backend public key can either be specified in base64 on the command line, or put in the json file later.")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *generateKeys {
|
||||
GenerateKeys(*configFilename)
|
||||
if *flagGenerateKeys {
|
||||
generateKeys(*configFilename)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func GenerateKeys(outputFile string) {
|
||||
func generateKeys(outputFile string) {
|
||||
if flag.NArg() < 1 {
|
||||
fmt.Println("Specify a numeric server ID after -genkeys")
|
||||
os.Exit(2)
|
||||
|
|
|
@ -19,39 +19,39 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var backendHttpClient http.Client
|
||||
var backendUrl string
|
||||
var backendHTTPClient http.Client
|
||||
var backendURL string
|
||||
var responseCache *cache.Cache
|
||||
|
||||
var getBacklogUrl string
|
||||
var postStatisticsUrl string
|
||||
var addTopicUrl string
|
||||
var announceStartupUrl string
|
||||
var getBacklogURL string
|
||||
var postStatisticsURL string
|
||||
var addTopicURL string
|
||||
var announceStartupURL string
|
||||
|
||||
var backendSharedKey [32]byte
|
||||
var serverId int
|
||||
var serverID int
|
||||
|
||||
var messageBufferPool sync.Pool
|
||||
|
||||
func SetupBackend(config *ConfigFile) {
|
||||
backendHttpClient.Timeout = 60 * time.Second
|
||||
backendUrl = config.BackendUrl
|
||||
func setupBackend(config *ConfigFile) {
|
||||
backendHTTPClient.Timeout = 60 * time.Second
|
||||
backendURL = config.BackendURL
|
||||
if responseCache != nil {
|
||||
responseCache.Flush()
|
||||
}
|
||||
responseCache = cache.New(60*time.Second, 120*time.Second)
|
||||
|
||||
getBacklogUrl = fmt.Sprintf("%s/backlog", backendUrl)
|
||||
postStatisticsUrl = fmt.Sprintf("%s/stats", backendUrl)
|
||||
addTopicUrl = fmt.Sprintf("%s/topics", backendUrl)
|
||||
announceStartupUrl = fmt.Sprintf("%s/startup", backendUrl)
|
||||
getBacklogURL = fmt.Sprintf("%s/backlog", backendURL)
|
||||
postStatisticsURL = fmt.Sprintf("%s/stats", backendURL)
|
||||
addTopicURL = fmt.Sprintf("%s/topics", backendURL)
|
||||
announceStartupURL = fmt.Sprintf("%s/startup", backendURL)
|
||||
|
||||
messageBufferPool.New = New4KByteBuffer
|
||||
|
||||
var theirPublic, ourPrivate [32]byte
|
||||
copy(theirPublic[:], config.BackendPublicKey)
|
||||
copy(ourPrivate[:], config.OurPrivateKey)
|
||||
serverId = config.ServerId
|
||||
serverID = config.ServerID
|
||||
|
||||
box.Precompute(&backendSharedKey, &theirPublic, &ourPrivate)
|
||||
}
|
||||
|
@ -60,8 +60,10 @@ func getCacheKey(remoteCommand, data string) string {
|
|||
return fmt.Sprintf("%s/%s", remoteCommand, data)
|
||||
}
|
||||
|
||||
// Publish a message to clients with no caching.
|
||||
// The scope must be specified because no attempt is made to recognize the command.
|
||||
// HBackendPublishRequest handles the /uncached_pub route.
|
||||
// The backend can POST here to publish a message to clients with no caching.
|
||||
// The POST arguments are `cmd`, `args`, `channel`, and `scope`.
|
||||
// The `scope` argument is required because no attempt is made to infer the scope from the command, unlike /cached_pub.
|
||||
func HBackendPublishRequest(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
formData, err := UnsealRequest(r.Form)
|
||||
|
@ -111,14 +113,18 @@ func HBackendPublishRequest(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprint(w, count)
|
||||
}
|
||||
|
||||
type BackendForwardedError string
|
||||
// ErrForwardedFromBackend is an error returned by the backend server.
|
||||
type ErrForwardedFromBackend string
|
||||
|
||||
func (bfe BackendForwardedError) Error() string {
|
||||
func (bfe ErrForwardedFromBackend) Error() string {
|
||||
return string(bfe)
|
||||
}
|
||||
|
||||
var AuthorizationNeededError = errors.New("Must authenticate Twitch username to use this command")
|
||||
// ErrAuthorizationNeeded is emitted when the backend replies with HTTP 401.
|
||||
// Indicates that an attempt to validate `ClientInfo.TwitchUsername` should be attempted.
|
||||
var ErrAuthorizationNeeded = errors.New("Must authenticate Twitch username to use this command")
|
||||
|
||||
// SendRemoteCommandCached performs a RPC call on the backend, but caches responses.
|
||||
func SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string, error) {
|
||||
cached, ok := responseCache.Get(getCacheKey(remoteCommand, data))
|
||||
if ok {
|
||||
|
@ -127,8 +133,12 @@ func SendRemoteCommandCached(remoteCommand, data string, auth AuthInfo) (string,
|
|||
return SendRemoteCommand(remoteCommand, data, auth)
|
||||
}
|
||||
|
||||
// SendRemoteCommand performs a RPC call on the backend by POSTing to `/cmd/$remoteCommand`.
|
||||
// The form data is as follows: `clientData` is the JSON in the `data` parameter
|
||||
// (should be retrieved from ClientMessage.Arguments), and either `username` or
|
||||
// `usernameClaimed` depending on whether AuthInfo.UsernameValidates is true is AuthInfo.TwitchUsername.
|
||||
func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr string, err error) {
|
||||
destUrl := fmt.Sprintf("%s/cmd/%s", backendUrl, remoteCommand)
|
||||
destURL := fmt.Sprintf("%s/cmd/%s", backendURL, remoteCommand)
|
||||
var authKey string
|
||||
if auth.UsernameValidated {
|
||||
authKey = "usernameClaimed"
|
||||
|
@ -146,7 +156,7 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
|
|||
return "", err
|
||||
}
|
||||
|
||||
resp, err := backendHttpClient.PostForm(destUrl, sealedForm)
|
||||
resp, err := backendHTTPClient.PostForm(destURL, sealedForm)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -160,13 +170,12 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
|
|||
responseStr = string(respBytes)
|
||||
|
||||
if resp.StatusCode == 401 {
|
||||
return "", AuthorizationNeededError
|
||||
return "", ErrAuthorizationNeeded
|
||||
} else if resp.StatusCode != 200 {
|
||||
if resp.Header.Get("Content-Type") == "application/json" {
|
||||
return "", BackendForwardedError(responseStr)
|
||||
} else {
|
||||
return "", httpError(resp.StatusCode)
|
||||
return "", ErrForwardedFromBackend(responseStr)
|
||||
}
|
||||
return "", httpError(resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp.Header.Get("FFZ-Cache") != "" {
|
||||
|
@ -182,7 +191,7 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
|
|||
}
|
||||
|
||||
func SendAggregatedData(sealedForm url.Values) error {
|
||||
resp, err := backendHttpClient.PostForm(postStatisticsUrl, sealedForm)
|
||||
resp, err := backendHTTPClient.PostForm(postStatisticsURL, sealedForm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -204,7 +213,7 @@ func FetchBacklogData(chatSubs []string) ([]ClientMessage, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := backendHttpClient.PostForm(getBacklogUrl, sealedForm)
|
||||
resp, err := backendHTTPClient.PostForm(getBacklogURL, sealedForm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -227,12 +236,14 @@ func FetchBacklogData(chatSubs []string) ([]ClientMessage, error) {
|
|||
return messages, nil
|
||||
}
|
||||
|
||||
type NotOkError struct {
|
||||
// ErrBackendNotOK indicates that the backend replied with something other than the string "ok".
|
||||
type ErrBackendNotOK struct {
|
||||
Response string
|
||||
Code int
|
||||
}
|
||||
|
||||
func (noe NotOkError) Error() string {
|
||||
// Implements the error interface.
|
||||
func (noe ErrBackendNotOK) Error() string {
|
||||
return fmt.Sprintf("backend returned %d: %s", noe.Code, noe.Response)
|
||||
}
|
||||
|
||||
|
@ -258,7 +269,7 @@ func sendTopicNotice(topic string, added bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
resp, err := backendHttpClient.PostForm(addTopicUrl, sealedForm)
|
||||
resp, err := backendHTTPClient.PostForm(addTopicURL, sealedForm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -271,7 +282,7 @@ func sendTopicNotice(topic string, added bool) error {
|
|||
|
||||
respStr := string(respBytes)
|
||||
if respStr != "ok" {
|
||||
return NotOkError{Code: resp.StatusCode, Response: respStr}
|
||||
return ErrBackendNotOK{Code: resp.StatusCode, Response: respStr}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -281,15 +292,15 @@ func httpError(statusCode int) error {
|
|||
return fmt.Errorf("backend http error: %d", statusCode)
|
||||
}
|
||||
|
||||
func GenerateKeys(outputFile, serverId, theirPublicStr string) {
|
||||
func GenerateKeys(outputFile, serverID, theirPublicStr string) {
|
||||
var err error
|
||||
output := ConfigFile{
|
||||
ListenAddr: "0.0.0.0:8001",
|
||||
SocketOrigin: "localhost:8001",
|
||||
BackendUrl: "http://localhost:8002/ffz",
|
||||
BackendURL: "http://localhost:8002/ffz",
|
||||
}
|
||||
|
||||
output.ServerId, err = strconv.Atoi(serverId)
|
||||
output.ServerID, err = strconv.Atoi(serverID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -12,13 +12,14 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// A command is how the client refers to a function on the server. It's just a string.
|
||||
// Command is a string indicating which RPC is requested.
|
||||
// The Commands sent from Client -> Server and Server -> Client are disjoint sets.
|
||||
type Command string
|
||||
|
||||
// A function that is called to respond to a Command.
|
||||
// CommandHandler is a RPC handler assosciated with a Command.
|
||||
type CommandHandler func(*websocket.Conn, *ClientInfo, ClientMessage) (ClientMessage, error)
|
||||
|
||||
var CommandHandlers = map[Command]CommandHandler{
|
||||
var commandHandlers = map[Command]CommandHandler{
|
||||
HelloCommand: HandleHello,
|
||||
"setuser": HandleSetUser,
|
||||
"ready": HandleReady,
|
||||
|
@ -40,7 +41,7 @@ var CommandHandlers = map[Command]CommandHandler{
|
|||
const ChannelInfoDelay = 2 * time.Second
|
||||
|
||||
func HandleCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) {
|
||||
handler, ok := CommandHandlers[msg.Command]
|
||||
handler, ok := commandHandlers[msg.Command]
|
||||
if !ok {
|
||||
handler = HandleRemoteCommand
|
||||
}
|
||||
|
@ -65,13 +66,13 @@ func HandleCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage)
|
|||
}
|
||||
|
||||
func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||
version, clientId, err := msg.ArgumentsAsTwoStrings()
|
||||
version, clientID, err := msg.ArgumentsAsTwoStrings()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client.Version = version
|
||||
client.ClientID = uuid.FromStringOrNil(clientId)
|
||||
client.ClientID = uuid.FromStringOrNil(clientID)
|
||||
if client.ClientID == uuid.Nil {
|
||||
client.ClientID = uuid.NewV4()
|
||||
}
|
||||
|
@ -125,7 +126,7 @@ func HandleSetUser(conn *websocket.Conn, client *ClientInfo, msg ClientMessage)
|
|||
client.UsernameValidated = false
|
||||
client.Mutex.Unlock()
|
||||
|
||||
if Configuation.SendAuthToNewClients {
|
||||
if Configuration.SendAuthToNewClients {
|
||||
client.MsgChannelKeepalive.Add(1)
|
||||
go client.StartAuthorization(func(_ *ClientInfo, _ bool) {
|
||||
client.MsgChannelKeepalive.Done()
|
||||
|
@ -200,7 +201,7 @@ func GetSubscriptionBacklog(conn *websocket.Conn, client *ClientInfo) {
|
|||
return
|
||||
}
|
||||
|
||||
if backendUrl == "" {
|
||||
if backendURL == "" {
|
||||
return // for testing runs
|
||||
}
|
||||
messages, err := FetchBacklogData(subs)
|
||||
|
@ -250,12 +251,17 @@ func HandleTrackFollow(conn *websocket.Conn, client *ClientInfo, msg ClientMessa
|
|||
return ResponseSuccess, nil
|
||||
}
|
||||
|
||||
// AggregateEmoteUsage is a map from emoteID to a map from chatroom name to usage count.
|
||||
var AggregateEmoteUsage map[int]map[string]int = make(map[int]map[string]int)
|
||||
|
||||
// AggregateEmoteUsageLock is the lock for AggregateEmoteUsage.
|
||||
var AggregateEmoteUsageLock sync.Mutex
|
||||
var ErrorNegativeEmoteUsage = errors.New("Emote usage count cannot be negative")
|
||||
|
||||
// ErrNegativeEmoteUsage is emitted when the submitted emote usage is negative.
|
||||
var ErrNegativeEmoteUsage = errors.New("Emote usage count cannot be negative")
|
||||
|
||||
func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||
// arguments is [1]map[EmoteId]map[RoomName]float64
|
||||
// arguments is [1]map[emoteID]map[ChatroomName]float64
|
||||
|
||||
mapRoot := msg.Arguments.([]interface{})[0].(map[string]interface{})
|
||||
|
||||
|
@ -268,7 +274,7 @@ func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMess
|
|||
for _, val2 := range mapInner {
|
||||
var count int = int(val2.(float64))
|
||||
if count <= 0 {
|
||||
err = ErrorNegativeEmoteUsage
|
||||
err = ErrNegativeEmoteUsage
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -278,16 +284,16 @@ func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMess
|
|||
defer AggregateEmoteUsageLock.Unlock()
|
||||
|
||||
for strEmote, val1 := range mapRoot {
|
||||
var emoteId int
|
||||
emoteId, err = strconv.Atoi(strEmote)
|
||||
var emoteID int
|
||||
emoteID, err = strconv.Atoi(strEmote)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
destMapInner, ok := AggregateEmoteUsage[emoteId]
|
||||
destMapInner, ok := AggregateEmoteUsage[emoteID]
|
||||
if !ok {
|
||||
destMapInner = make(map[string]int)
|
||||
AggregateEmoteUsage[emoteId] = destMapInner
|
||||
AggregateEmoteUsage[emoteID] = destMapInner
|
||||
}
|
||||
|
||||
mapInner := val1.(map[string]interface{})
|
||||
|
@ -322,23 +328,23 @@ func DoSendAggregateData() {
|
|||
|
||||
reportForm := url.Values{}
|
||||
|
||||
followJson, err := json.Marshal(follows)
|
||||
followJSON, err := json.Marshal(follows)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
reportForm.Set("follows", string(followJson))
|
||||
reportForm.Set("follows", string(followJSON))
|
||||
}
|
||||
|
||||
strEmoteUsage := make(map[string]map[string]int)
|
||||
for emoteId, usageByChannel := range emoteUsage {
|
||||
strEmoteId := strconv.Itoa(emoteId)
|
||||
strEmoteUsage[strEmoteId] = usageByChannel
|
||||
for emoteID, usageByChannel := range emoteUsage {
|
||||
strEmoteID := strconv.Itoa(emoteID)
|
||||
strEmoteUsage[strEmoteID] = usageByChannel
|
||||
}
|
||||
emoteJson, err := json.Marshal(strEmoteUsage)
|
||||
emoteJSON, err := json.Marshal(strEmoteUsage)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
reportForm.Set("emotes", string(emoteJson))
|
||||
reportForm.Set("emotes", string(emoteJSON))
|
||||
}
|
||||
|
||||
form, err := SealRequest(reportForm)
|
||||
|
@ -380,6 +386,7 @@ type BunchSubscriberList struct {
|
|||
}
|
||||
|
||||
type CacheStatus byte
|
||||
|
||||
const (
|
||||
CacheStatusNotFound = iota
|
||||
CacheStatusFound
|
||||
|
@ -396,7 +403,7 @@ var BunchCacheLastCleanup time.Time
|
|||
func bunchCacheJanitor() {
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(30*time.Minute)
|
||||
time.Sleep(30 * time.Minute)
|
||||
BunchCacheCleanupSignal.Signal()
|
||||
}
|
||||
}()
|
||||
|
@ -406,13 +413,13 @@ func bunchCacheJanitor() {
|
|||
// Unlocks CachedBunchLock, waits for signal, re-locks
|
||||
BunchCacheCleanupSignal.Wait()
|
||||
|
||||
if BunchCacheLastCleanup.After(time.Now().Add(-1*time.Second)) {
|
||||
if BunchCacheLastCleanup.After(time.Now().Add(-1 * time.Second)) {
|
||||
// skip if it's been less than 1 second
|
||||
continue
|
||||
}
|
||||
|
||||
// CachedBunchLock is held here
|
||||
keepIfAfter := time.Now().Add(-5*time.Minute)
|
||||
keepIfAfter := time.Now().Add(-5 * time.Minute)
|
||||
for req, resp := range BunchCache {
|
||||
if !resp.Timestamp.After(keepIfAfter) {
|
||||
delete(BunchCache, req)
|
||||
|
@ -518,7 +525,7 @@ const AuthorizationFailedErrorString = "Failed to verify your Twitch username."
|
|||
func doRemoteCommand(conn *websocket.Conn, msg ClientMessage, client *ClientInfo) {
|
||||
resp, err := SendRemoteCommandCached(string(msg.Command), msg.origArguments, client.AuthInfo)
|
||||
|
||||
if err == AuthorizationNeededError {
|
||||
if err == ErrAuthorizationNeeded {
|
||||
client.StartAuthorization(func(_ *ClientInfo, success bool) {
|
||||
if success {
|
||||
doRemoteCommand(conn, msg, client)
|
||||
|
|
|
@ -16,34 +16,38 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
const MAX_PACKET_SIZE = 1024
|
||||
|
||||
// Sent by the server in ClientMessage.Command to indicate success.
|
||||
// SuccessCommand is a Reply Command to indicate success in reply to a C2S Command.
|
||||
const SuccessCommand Command = "ok"
|
||||
|
||||
// Sent by the server in ClientMessage.Command to indicate failure.
|
||||
// ErrorCommand is a Reply Command to indicate that a C2S Command failed.
|
||||
const ErrorCommand Command = "error"
|
||||
|
||||
// This must be the first command sent by the client once the connection is established.
|
||||
// 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.
|
||||
const HelloCommand Command = "hello"
|
||||
|
||||
// AuthorizeCommand is a S2C Command sent as part of Twitch username validation.
|
||||
const AuthorizeCommand Command = "do_authorize"
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
const AsyncResponseCommand Command = "_async"
|
||||
|
||||
// ResponseSuccess is a Reply ClientMessage with the MessageID not yet filled out.
|
||||
var ResponseSuccess = ClientMessage{Command: SuccessCommand}
|
||||
var ResponseFailure = ClientMessage{Command: "False"}
|
||||
|
||||
var Configuation *ConfigFile
|
||||
// Configuration is the active ConfigFile.
|
||||
var Configuration *ConfigFile
|
||||
|
||||
// Set up a websocket listener and register it on /.
|
||||
// (Uses http.DefaultServeMux .)
|
||||
// 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.)
|
||||
func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||
Configuation = config
|
||||
Configuration = config
|
||||
|
||||
SetupBackend(config)
|
||||
setupBackend(config)
|
||||
|
||||
if serveMux == nil {
|
||||
serveMux = http.DefaultServeMux
|
||||
|
@ -66,7 +70,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
|||
if err != nil {
|
||||
log.Fatalln("Unable to seal requests:", err)
|
||||
}
|
||||
resp, err := backendHttpClient.PostForm(announceStartupUrl, announceForm)
|
||||
resp, err := backendHTTPClient.PostForm(announceStartupURL, announceForm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
|
@ -82,6 +86,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
|||
go ircConnection()
|
||||
}
|
||||
|
||||
// SocketUpgrader is the websocket.Upgrader currently in use.
|
||||
var SocketUpgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
|
@ -90,6 +95,8 @@ var SocketUpgrader = websocket.Upgrader{
|
|||
},
|
||||
}
|
||||
|
||||
// BannerHTML is the content served to web browsers viewing the socket server website.
|
||||
// Memes go here.
|
||||
var BannerHTML []byte
|
||||
|
||||
func ServeWebsocketOrCatbag(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -118,7 +125,6 @@ var ExpectedStringAndBool = errors.New("Error: Expected array of string, bool as
|
|||
var ExpectedStringAndIntGotFloat = errors.New("Error: Second argument was a float, expected an integer.")
|
||||
|
||||
var CloseGotBinaryMessage = websocket.CloseError{Code: websocket.CloseUnsupportedData, Text: "got binary packet"}
|
||||
var CloseGotMessageId0 = websocket.CloseError{Code: websocket.ClosePolicyViolation, Text: "got messageid 0"}
|
||||
var CloseTimedOut = websocket.CloseError{Code: websocket.CloseNoStatusReceived, Text: "no ping replies for 5 minutes"}
|
||||
var CloseFirstMessageNotHello = websocket.CloseError{
|
||||
Text: "Error - the first message sent must be a 'hello'",
|
||||
|
@ -296,6 +302,8 @@ func CloseConnection(conn *websocket.Conn, closeMsg *websocket.CloseError) {
|
|||
conn.Close()
|
||||
}
|
||||
|
||||
// SendMessage sends a ClientMessage over the websocket connection with a timeout.
|
||||
// If marshalling the ClientMessage fails, this function will panic.
|
||||
func SendMessage(conn *websocket.Conn, msg ClientMessage) {
|
||||
messageType, packet, err := MarshalClientMessage(msg)
|
||||
if err != nil {
|
||||
|
@ -305,7 +313,7 @@ func SendMessage(conn *websocket.Conn, msg ClientMessage) {
|
|||
conn.WriteMessage(messageType, packet)
|
||||
}
|
||||
|
||||
// Unpack a message sent from the client into a ClientMessage.
|
||||
// UnmarshalClientMessage unpacks websocket TextMessage into a ClientMessage provided in the `v` parameter.
|
||||
func UnmarshalClientMessage(data []byte, payloadType int, v interface{}) (err error) {
|
||||
var spaceIdx int
|
||||
|
||||
|
@ -317,12 +325,12 @@ func UnmarshalClientMessage(data []byte, payloadType int, v interface{}) (err er
|
|||
if spaceIdx == -1 {
|
||||
return ProtocolError
|
||||
}
|
||||
messageId, err := strconv.Atoi(dataStr[:spaceIdx])
|
||||
if messageId < -1 || messageId == 0 {
|
||||
messageID, err := strconv.Atoi(dataStr[:spaceIdx])
|
||||
if messageID < -1 || messageID == 0 {
|
||||
return ProtocolErrorNegativeID
|
||||
}
|
||||
|
||||
out.MessageID = messageId
|
||||
out.MessageID = messageID
|
||||
dataStr = dataStr[spaceIdx+1:]
|
||||
|
||||
spaceIdx = strings.IndexRune(dataStr, ' ')
|
||||
|
@ -334,8 +342,8 @@ func UnmarshalClientMessage(data []byte, payloadType int, v interface{}) (err er
|
|||
out.Command = Command(dataStr[:spaceIdx])
|
||||
}
|
||||
dataStr = dataStr[spaceIdx+1:]
|
||||
argumentsJson := dataStr
|
||||
out.origArguments = argumentsJson
|
||||
argumentsJSON := dataStr
|
||||
out.origArguments = argumentsJSON
|
||||
err = out.parseOrigArguments()
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
@ -24,7 +24,7 @@ func TCountOpenFDs() uint64 {
|
|||
|
||||
const IgnoreReceivedArguments = 1 + 2i
|
||||
|
||||
func TReceiveExpectedMessage(tb testing.TB, conn *websocket.Conn, messageId int, command Command, arguments interface{}) (ClientMessage, bool) {
|
||||
func TReceiveExpectedMessage(tb testing.TB, conn *websocket.Conn, messageID int, command Command, arguments interface{}) (ClientMessage, bool) {
|
||||
var msg ClientMessage
|
||||
var fail bool
|
||||
messageType, packet, err := conn.ReadMessage()
|
||||
|
@ -42,8 +42,8 @@ func TReceiveExpectedMessage(tb testing.TB, conn *websocket.Conn, messageId int,
|
|||
tb.Error(err)
|
||||
return msg, false
|
||||
}
|
||||
if msg.MessageID != messageId {
|
||||
tb.Error("Message ID was wrong. Expected", messageId, ", got", msg.MessageID, ":", msg)
|
||||
if msg.MessageID != messageID {
|
||||
tb.Error("Message ID was wrong. Expected", messageID, ", got", msg.MessageID, ":", msg)
|
||||
fail = true
|
||||
}
|
||||
if msg.Command != command {
|
||||
|
@ -65,8 +65,8 @@ func TReceiveExpectedMessage(tb testing.TB, conn *websocket.Conn, messageId int,
|
|||
return msg, !fail
|
||||
}
|
||||
|
||||
func TSendMessage(tb testing.TB, conn *websocket.Conn, messageId int, command Command, arguments interface{}) bool {
|
||||
SendMessage(conn, ClientMessage{MessageID: messageId, Command: command, Arguments: arguments})
|
||||
func TSendMessage(tb testing.TB, conn *websocket.Conn, messageID int, command Command, arguments interface{}) bool {
|
||||
SendMessage(conn, ClientMessage{MessageID: messageID, Command: command, Arguments: arguments})
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,7 @@ func TSetup(testserver **httptest.Server, urls *TURLs) {
|
|||
DumpCache()
|
||||
|
||||
conf := &ConfigFile{
|
||||
ServerId: 20,
|
||||
ServerID: 20,
|
||||
UseSSL: false,
|
||||
SocketOrigin: "localhost:2002",
|
||||
BannerHTML: `
|
||||
|
@ -160,7 +160,7 @@ func TSetup(testserver **httptest.Server, urls *TURLs) {
|
|||
BackendPublicKey: []byte{19, 163, 37, 157, 50, 139, 193, 85, 229, 47, 166, 21, 153, 231, 31, 133, 41, 158, 8, 53, 73, 0, 113, 91, 13, 181, 131, 248, 176, 18, 1, 107},
|
||||
}
|
||||
gconfig = conf
|
||||
SetupBackend(conf)
|
||||
setupBackend(conf)
|
||||
|
||||
if testserver != nil {
|
||||
serveMux := http.NewServeMux()
|
||||
|
|
|
@ -12,12 +12,12 @@ const CryptoBoxKeyLength = 32
|
|||
|
||||
type ConfigFile struct {
|
||||
// Numeric server id known to the backend
|
||||
ServerId int
|
||||
ServerID int
|
||||
ListenAddr string
|
||||
// Hostname of the socket server
|
||||
SocketOrigin string
|
||||
// URL to the backend server
|
||||
BackendUrl string
|
||||
BackendURL string
|
||||
|
||||
// SSL/TLS
|
||||
UseSSL bool
|
||||
|
@ -168,19 +168,19 @@ func (bct BacklogCacheType) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
// Implements json.Unmarshaler
|
||||
func (pbct *BacklogCacheType) UnmarshalJSON(data []byte) error {
|
||||
func (bct *BacklogCacheType) UnmarshalJSON(data []byte) error {
|
||||
var str string
|
||||
err := json.Unmarshal(data, &str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if str == "" {
|
||||
*pbct = CacheTypeInvalid
|
||||
*bct = CacheTypeInvalid
|
||||
return nil
|
||||
}
|
||||
val := BacklogCacheTypeByName(str)
|
||||
if val != CacheTypeInvalid {
|
||||
*pbct = val
|
||||
*bct = val
|
||||
return nil
|
||||
}
|
||||
return ErrorUnrecognizedCacheType
|
||||
|
@ -224,19 +224,19 @@ func (mtt MessageTargetType) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
// Implements json.Unmarshaler
|
||||
func (pmtt *MessageTargetType) UnmarshalJSON(data []byte) error {
|
||||
func (mtt *MessageTargetType) UnmarshalJSON(data []byte) error {
|
||||
var str string
|
||||
err := json.Unmarshal(data, &str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if str == "" {
|
||||
*pmtt = MsgTargetTypeInvalid
|
||||
*mtt = MsgTargetTypeInvalid
|
||||
return nil
|
||||
}
|
||||
mtt := MessageTargetTypeByName(str)
|
||||
if mtt != MsgTargetTypeInvalid {
|
||||
*pmtt = mtt
|
||||
*mtt = mtt
|
||||
return nil
|
||||
}
|
||||
return ErrorUnrecognizedTargetType
|
||||
|
|
|
@ -54,7 +54,7 @@ func SealRequest(form url.Values) (url.Values, error) {
|
|||
retval := url.Values{
|
||||
"nonce": []string{nonceString},
|
||||
"msg": []string{cipherString},
|
||||
"id": []string{strconv.Itoa(serverId)},
|
||||
"id": []string{strconv.Itoa(serverID)},
|
||||
}
|
||||
|
||||
return retval, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue