aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/admin_moderation_test.go (4821B)


      1 package main
      2 
      3 import (
      4 	"errors"
      5 	"net"
      6 	"net/http"
      7 	"net/http/httptest"
      8 	"testing"
      9 )
     10 
     11 func TestAdminKickAndCharacterBan(t *testing.T) {
     12 	state := newTestServer(t)
     13 	visitor := accountVisitor(t, state)
     14 	host, peer := net.Pipe()
     15 	defer host.Close()
     16 	defer peer.Close()
     17 	state.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{Conn: host}}
     18 	credential := state.characters[1].browserTokenHash
     19 	shutdown := startTestService(t, state)
     20 	handler := state.httpHandler()
     21 	login := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin&password_confirm=admin", nil)
     22 	cookie := login.Result().Cookies()[0]
     23 
     24 	kick := adminRequest(handler, "POST", "/admin/players/1/kick", "", cookie)
     25 	if launch := adminRequest(handler, "POST", "/browser-launch", "", visitor); launch.Code != http.StatusOK {
     26 		t.Fatal("kick blocked a fresh login", launch.Code)
     27 	}
     28 	query := &zoneConnectionQuery{player: 1}
     29 	callTestZone(t, state, 1, query)
     30 	ban := adminRequest(handler, "POST", "/admin/players/1/ban", "", cookie)
     31 	blocked := adminRequest(handler, "POST", "/browser-launch", "", visitor)
     32 	zone, err := state.world.owner(1)
     33 	if err != nil {
     34 		t.Fatal(err)
     35 	}
     36 	denied := zone.joinWorldClient(&worldClient{id: 1, conn: &combatTestConn{}, browserTokenHash: credential}, 1)
     37 	if err := shutdown(); err != nil {
     38 		t.Fatal(err)
     39 	}
     40 	cold := newTestInventoryServer(t)
     41 	cold.baseURL = "/"
     42 	stillBanned := adminRequest(cold.httpHandler(), "POST", "/browser-launch", "", visitor)
     43 	unban := adminRequest(cold.httpHandler(), "POST", "/admin/players/1/unban", "", cookie)
     44 	allowed := adminRequest(cold.httpHandler(), "POST", "/browser-launch", "", visitor)
     45 
     46 	if kick.Code != http.StatusSeeOther || query.connected || ban.Code != http.StatusSeeOther || !errors.Is(denied, errGameBanned) ||
     47 		blocked.Code != http.StatusForbidden || stillBanned.Code != http.StatusForbidden || unban.Code != http.StatusSeeOther || allowed.Code != http.StatusOK {
     48 		t.Fatal(kick.Code, query.connected, ban.Code, denied, blocked.Code, stillBanned.Code, unban.Code, allowed.Code)
     49 	}
     50 }
     51 
     52 func TestAdminIPBanAppliesToSharedAddressAndSurvivesDeletion(t *testing.T) {
     53 	state, _ := newTeamWorld(t)
     54 	state.baseURL = "/"
     55 	for id, client := range state.clients {
     56 		if id > 3 {
     57 			delete(state.clients, id)
     58 			continue
     59 		}
     60 		host, peer := net.Pipe()
     61 		defer host.Close()
     62 		defer peer.Close()
     63 		ip := "192.0.2.1:8765"
     64 		if id == 3 {
     65 			ip = "192.0.2.3:8765"
     66 		}
     67 		client.conn = &browserGameConn{Conn: &combatTestConn{Conn: host}, peer: browserPeerAddress(ip)}
     68 		if err := state.savePlayerIP(id, ip); err != nil {
     69 			t.Fatal(err)
     70 		}
     71 	}
     72 	startTestService(t, state)
     73 	handler := adminTestHandler(state)
     74 
     75 	banned := adminRequest(handler, "POST", "/admin/players/1/ban-ip", "ip=192.0.2.1", nil)
     76 	var connected []bool
     77 	for _, id := range []uint32{1, 2, 3} {
     78 		query := &zoneConnectionQuery{player: id}
     79 		callTestZone(t, state, id, query)
     80 		connected = append(connected, query.connected)
     81 	}
     82 	deleted := adminRequest(handler, "POST", "/admin/players/1/delete", "", nil)
     83 	fresh := adminRequest(state.httpHandler(), "POST", "/browser-launch", "", nil)
     84 	unbanned := adminRequest(handler, "POST", "/admin/ip-bans/unban", "ip=192.0.2.1", nil)
     85 	allowed := adminRequest(state.httpHandler(), "POST", "/browser-launch", "", nil)
     86 
     87 	if banned.Code != http.StatusSeeOther || connected[0] || connected[1] || !connected[2] || deleted.Code != http.StatusSeeOther ||
     88 		fresh.Code != http.StatusForbidden || unbanned.Code != http.StatusSeeOther || allowed.Code != http.StatusOK {
     89 		t.Fatal(banned.Code, connected, deleted.Code, fresh.Code, unbanned.Code, allowed.Code)
     90 	}
     91 }
     92 
     93 func TestAdminIPBanChecksForwardedBrowserAddress(t *testing.T) {
     94 	state := newTestServer(t)
     95 	if _, err := state.db.Exec("INSERT INTO ip_bans VALUES ('2001:db8::7')"); err != nil {
     96 		t.Fatal(err)
     97 	}
     98 	handler := state.httpHandler()
     99 	for _, test := range []struct {
    100 		name, peer string
    101 		forwarded  []string
    102 		want       int
    103 	}{
    104 		{"local proxy", "127.0.0.1:4000", []string{"192.0.2.99, 2001:db8::7"}, http.StatusForbidden},
    105 		{"multiple header lines", "[::1]:4000", []string{"192.0.2.99", "2001:db8::7"}, http.StatusForbidden},
    106 		{"allowed peer through proxy", "[::1]:4000", []string{"2001:db8::7, 192.0.2.99"}, http.StatusOK},
    107 		{"direct banned peer", "[2001:db8::7]:4000", []string{"192.0.2.99"}, http.StatusForbidden},
    108 		{"direct allowed peer", "192.0.2.99:4000", []string{"2001:db8::7"}, http.StatusOK},
    109 	} {
    110 		t.Run(test.name, func(t *testing.T) {
    111 			request := httptest.NewRequest("POST", "/browser-launch", nil)
    112 			request.RemoteAddr = test.peer
    113 			request.Header["X-Forwarded-For"] = test.forwarded
    114 			response := httptest.NewRecorder()
    115 
    116 			handler.ServeHTTP(response, request)
    117 
    118 			if response.Code != test.want {
    119 				t.Fatalf("launch returned %d, want %d", response.Code, test.want)
    120 			}
    121 		})
    122 	}
    123 }