1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 05:15:54 +00:00
FrankerFaceZ/socketserver/internal/server/utils.go

162 lines
3.1 KiB
Go
Raw Normal View History

package server
2015-10-24 22:38:04 -07:00
import (
2015-10-25 14:06:56 -07:00
"bytes"
"crypto/rand"
2015-10-25 14:06:56 -07:00
"encoding/base64"
"errors"
"golang.org/x/crypto/nacl/box"
2015-10-25 14:06:56 -07:00
"log"
"net/url"
"strconv"
"strings"
2015-10-24 22:38:04 -07:00
)
2015-10-25 12:40:07 -07:00
func FillCryptoRandom(buf []byte) error {
remaining := len(buf)
for remaining > 0 {
count, err := rand.Read(buf)
if err != nil {
return err
}
remaining -= count
}
return nil
}
2015-10-25 14:06:56 -07:00
func New4KByteBuffer() interface{} {
return make([]byte, 0, 4096)
}
func SealRequest(form url.Values) (url.Values, error) {
var nonce [24]byte
var err error
err = FillCryptoRandom(nonce[:])
if err != nil {
return nil, err
}
cipherMsg := box.SealAfterPrecomputation(nil, []byte(form.Encode()), &nonce, &backendSharedKey)
bufMessage := new(bytes.Buffer)
enc := base64.NewEncoder(base64.URLEncoding, bufMessage)
enc.Write(cipherMsg)
enc.Close()
cipherString := bufMessage.String()
bufNonce := new(bytes.Buffer)
enc = base64.NewEncoder(base64.URLEncoding, bufNonce)
enc.Write(nonce[:])
enc.Close()
nonceString := bufNonce.String()
retval := url.Values{
"nonce": []string{nonceString},
"msg": []string{cipherString},
"id": []string{strconv.Itoa(serverId)},
2015-10-25 14:06:56 -07:00
}
return retval, nil
}
var ErrorShortNonce = errors.New("Nonce too short.")
var ErrorInvalidSignature = errors.New("Invalid signature or contents")
func UnsealRequest(form url.Values) (url.Values, error) {
var nonce [24]byte
nonceString := form.Get("nonce")
dec := base64.NewDecoder(base64.URLEncoding, strings.NewReader(nonceString))
count, err := dec.Read(nonce[:])
if err != nil {
return nil, err
}
if count != 24 {
return nil, ErrorShortNonce
}
cipherString := form.Get("msg")
dec = base64.NewDecoder(base64.URLEncoding, strings.NewReader(cipherString))
cipherBuffer := new(bytes.Buffer)
cipherBuffer.ReadFrom(dec)
message, ok := box.OpenAfterPrecomputation(nil, cipherBuffer.Bytes(), &nonce, &backendSharedKey)
if !ok {
return nil, ErrorInvalidSignature
}
retValues, err := url.ParseQuery(string(message))
if err != nil {
// Assume that the signature was accidentally correct but the contents were garbage
log.Print(err)
return nil, ErrorInvalidSignature
}
return retValues, nil
2015-10-25 12:40:07 -07:00
}
2015-10-25 03:21:50 -07:00
func AddToSliceS(ary *[]string, val string) bool {
2015-10-24 22:38:04 -07:00
slice := *ary
for _, v := range slice {
if v == val {
2015-10-25 03:21:50 -07:00
return false
2015-10-24 22:38:04 -07:00
}
}
slice = append(slice, val)
*ary = slice
2015-10-25 03:21:50 -07:00
return true
2015-10-24 22:38:04 -07:00
}
2015-10-25 03:21:50 -07:00
func RemoveFromSliceS(ary *[]string, val string) bool {
2015-10-24 22:38:04 -07:00
slice := *ary
var idx int = -1
for i, v := range slice {
if v == val {
idx = i
break
}
}
if idx == -1 {
2015-10-25 03:21:50 -07:00
return false
2015-10-24 22:38:04 -07:00
}
slice[idx] = slice[len(slice)-1]
slice = slice[:len(slice)-1]
2015-10-24 22:38:04 -07:00
*ary = slice
2015-10-25 03:21:50 -07:00
return true
2015-10-24 22:38:04 -07:00
}
2015-10-25 03:21:50 -07:00
func AddToSliceC(ary *[]chan<- ClientMessage, val chan<- ClientMessage) bool {
2015-10-25 03:21:50 -07:00
slice := *ary
for _, v := range slice {
if v == val {
return false
}
}
slice = append(slice, val)
*ary = slice
return true
}
func RemoveFromSliceC(ary *[]chan<- ClientMessage, val chan<- ClientMessage) bool {
2015-10-25 03:21:50 -07:00
slice := *ary
var idx int = -1
for i, v := range slice {
if v == val {
idx = i
break
}
}
if idx == -1 {
return false
}
slice[idx] = slice[len(slice)-1]
slice = slice[:len(slice)-1]
2015-10-25 03:21:50 -07:00
*ary = slice
return true
}