mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-08-24 09:34:33 -05:00
Implement banning and custom error messages
This commit is contained in:
126
api/ban.go
Normal file
126
api/ban.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
"wwfc/database"
|
||||
"wwfc/gpcm"
|
||||
)
|
||||
|
||||
func HandleBan(w http.ResponseWriter, r *http.Request) {
|
||||
errorString := handleBanImpl(w, r)
|
||||
if errorString != "" {
|
||||
jsonData, _ := json.Marshal(map[string]string{"error": errorString})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
} else {
|
||||
jsonData, _ := json.Marshal(map[string]string{"success": "true"})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
}
|
||||
}
|
||||
|
||||
func handleBanImpl(w http.ResponseWriter, r *http.Request) string {
|
||||
// TODO: Actual authentication rather than a fixed secret
|
||||
// TODO: Use POST instead of GET
|
||||
|
||||
u, err := url.Parse(r.URL.String())
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
query, err := url.ParseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
if apiSecret == "" || query.Get("secret") != apiSecret {
|
||||
return "Invalid API secret"
|
||||
}
|
||||
|
||||
pidStr := query.Get("pid")
|
||||
if pidStr == "" {
|
||||
return "Missing pid in request"
|
||||
}
|
||||
|
||||
pid, err := strconv.ParseUint(pidStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid pid"
|
||||
}
|
||||
|
||||
tosStr := query.Get("tos")
|
||||
if tosStr == "" {
|
||||
return "Missing tos in request"
|
||||
}
|
||||
|
||||
tos, err := strconv.ParseBool(tosStr)
|
||||
if err != nil {
|
||||
return "Invalid tos"
|
||||
}
|
||||
|
||||
minutes := uint64(0)
|
||||
if query.Get("minutes") != "" {
|
||||
minutesStr := query.Get("minutes")
|
||||
minutes, err = strconv.ParseUint(minutesStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid minutes"
|
||||
}
|
||||
}
|
||||
|
||||
hours := uint64(0)
|
||||
if query.Get("hours") != "" {
|
||||
hoursStr := query.Get("hours")
|
||||
hours, err = strconv.ParseUint(hoursStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid hours"
|
||||
}
|
||||
}
|
||||
|
||||
days := uint64(0)
|
||||
if query.Get("days") != "" {
|
||||
daysStr := query.Get("days")
|
||||
days, err = strconv.ParseUint(daysStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid days"
|
||||
}
|
||||
}
|
||||
|
||||
reason := query.Get("reason")
|
||||
if "reason" == "" {
|
||||
return "Missing ban reason"
|
||||
}
|
||||
|
||||
// reason_hidden is optional
|
||||
reasonHidden := query.Get("reason_hidden")
|
||||
|
||||
moderator := query.Get("moderator")
|
||||
if "moderator" == "" {
|
||||
moderator = "admin"
|
||||
}
|
||||
|
||||
minutes = days*24*60 + hours*60 + minutes
|
||||
if minutes == 0 {
|
||||
return "Missing ban length"
|
||||
}
|
||||
|
||||
length := time.Duration(minutes) * time.Minute
|
||||
|
||||
if !database.BanUser(pool, ctx, uint32(pid), tos, length, reason, reasonHidden, moderator) {
|
||||
return "Failed to ban user"
|
||||
}
|
||||
|
||||
if tos {
|
||||
gpcm.KickPlayer(uint32(pid), "banned")
|
||||
} else {
|
||||
gpcm.KickPlayer(uint32(pid), "restricted")
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
58
api/kick.go
Normal file
58
api/kick.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"wwfc/gpcm"
|
||||
)
|
||||
|
||||
func HandleKick(w http.ResponseWriter, r *http.Request) {
|
||||
errorString := handleKickImpl(w, r)
|
||||
if errorString != "" {
|
||||
jsonData, _ := json.Marshal(map[string]string{"error": errorString})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
} else {
|
||||
jsonData, _ := json.Marshal(map[string]string{"success": "true"})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
}
|
||||
}
|
||||
|
||||
func handleKickImpl(w http.ResponseWriter, r *http.Request) string {
|
||||
// TODO: Actual authentication rather than a fixed secret
|
||||
// TODO: Use POST instead of GET
|
||||
|
||||
u, err := url.Parse(r.URL.String())
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
query, err := url.ParseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
if apiSecret == "" || query.Get("secret") != apiSecret {
|
||||
return "Invalid API secret"
|
||||
}
|
||||
|
||||
pidStr := query.Get("pid")
|
||||
if pidStr == "" {
|
||||
return "Missing pid in request"
|
||||
}
|
||||
|
||||
pid, err := strconv.ParseUint(pidStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid pid"
|
||||
}
|
||||
|
||||
gpcm.KickPlayer(uint32(pid), "moderator_kick")
|
||||
return ""
|
||||
}
|
||||
34
api/main.go
Normal file
34
api/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wwfc/common"
|
||||
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
pool *pgxpool.Pool
|
||||
apiSecret string
|
||||
)
|
||||
|
||||
func StartServer() {
|
||||
// Get config
|
||||
config := common.GetConfig()
|
||||
|
||||
apiSecret = config.APISecret
|
||||
|
||||
// Start SQL
|
||||
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", config.Username, config.Password, config.DatabaseAddress, config.DatabaseName)
|
||||
dbConf, err := pgxpool.ParseConfig(dbString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
58
api/unban.go
Normal file
58
api/unban.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"wwfc/database"
|
||||
)
|
||||
|
||||
func HandleUnban(w http.ResponseWriter, r *http.Request) {
|
||||
errorString := handleUnbanImpl(w, r)
|
||||
if errorString != "" {
|
||||
jsonData, _ := json.Marshal(map[string]string{"error": errorString})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
} else {
|
||||
jsonData, _ := json.Marshal(map[string]string{"success": "true"})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
|
||||
w.Write(jsonData)
|
||||
}
|
||||
}
|
||||
|
||||
func handleUnbanImpl(w http.ResponseWriter, r *http.Request) string {
|
||||
// TODO: Actual authentication rather than a fixed secret
|
||||
// TODO: Use POST instead of GET
|
||||
|
||||
u, err := url.Parse(r.URL.String())
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
query, err := url.ParseQuery(u.RawQuery)
|
||||
if err != nil {
|
||||
return "Bad request"
|
||||
}
|
||||
|
||||
if apiSecret == "" || query.Get("secret") != apiSecret {
|
||||
return "Invalid API secret"
|
||||
}
|
||||
|
||||
pidStr := query.Get("pid")
|
||||
if pidStr == "" {
|
||||
return "Missing pid in request"
|
||||
}
|
||||
|
||||
pid, err := strconv.ParseUint(pidStr, 10, 32)
|
||||
if err != nil {
|
||||
return "Invalid pid"
|
||||
}
|
||||
|
||||
database.UnbanUser(pool, ctx, uint32(pid))
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user