server/browser_launch.go (5580B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "crypto/rand" 6 "crypto/sha256" 7 "database/sql" 8 "errors" 9 "fmt" 10 mathrand "math/rand/v2" 11 "net/http" 12 "sync" 13 "time" 14 ) 15 16 // Browser cookies identify both pending launches and saved accounts. 17 func browserCredential(r *http.Request) string { 18 cookie, err := r.Cookie("ao_client") 19 if err != nil { 20 return "" 21 } 22 return fmt.Sprintf("%x", sha256.Sum256([]byte(cookie.Value))) 23 } 24 25 func (server *server) browserPlayerID(credential string) (uint32, error) { 26 if credential == "" { 27 return 0, nil 28 } 29 var id uint32 30 err := server.db.QueryRow("SELECT id FROM characters WHERE id<1000000 AND browser_token_hash=?", credential).Scan(&id) 31 if err == sql.ErrNoRows { 32 return 0, nil 33 } 34 return id, err 35 } 36 37 func (state *server) browserLaunch(w http.ResponseWriter, r *http.Request) { 38 if state.rejectFull(w, r) { 39 return 40 } 41 var id uint32 42 var token string 43 err := state.accountOperation(func(coordinator *worldCoordinator) error { 44 credential := browserCredential(r) 45 var err error 46 id, err = state.browserSessionID(credential) 47 if err != nil { 48 return err 49 } 50 if err := checkGameBan(coordinator.db, id, browserRemoteAddress(r)); err != nil { 51 return err 52 } 53 if r.URL.Query().Get("create") == "1" { 54 if state.browserVisitors.lookup(credential) != 0 || id == 0 { 55 return errBrowserSessionChanged 56 } 57 if coordinator.browserCreations == nil { 58 coordinator.browserCreations = make(map[string]*browserCreationCredentials) 59 } 60 coordinator.browserCreations[credential] = &browserCreationCredentials{} 61 } else if id != 0 { 62 // Returning to the game cancels an unfinished native creator. 63 delete(coordinator.browserCreations, credential) 64 } 65 if id != 0 { 66 return nil 67 } 68 69 token = rand.Text() 70 reservation := new(playerIDReservation) 71 if err := coordinator.commitWrite(reservation); err != nil { 72 return err 73 } 74 id = uint32(*reservation) 75 state.browserVisitors.mutex.Lock() 76 for credential, launch := range state.browserVisitors.pending { 77 if time.Now().After(launch.expires) { 78 delete(state.browserVisitors.pending, credential) 79 } 80 } 81 state.browserVisitors.pending[fmt.Sprintf("%x", sha256.Sum256([]byte(token)))] = browserVisitorLaunch{id, time.Now().Add(time.Hour)} 82 state.browserVisitors.mutex.Unlock() 83 return nil 84 }) 85 if err != nil { 86 status := http.StatusInternalServerError 87 if errors.Is(err, errGameBanned) { 88 status = http.StatusForbidden 89 } 90 if errors.Is(err, errBrowserSessionChanged) { 91 status = http.StatusUnauthorized 92 } 93 http.Error(w, err.Error(), status) 94 return 95 } 96 if token != "" { 97 state.setBrowserCookie(w, r, token) 98 } 99 w.Header().Set("Content-Type", "text/plain") 100 w.Header().Set("Cache-Control", "no-store") 101 if r.URL.Query().Get("create") == "1" { 102 // Character zero selects the original creator and its login connection. 103 w.Header().Set("X-AO-World-Port", fmt.Sprint(state.worldPort)) 104 fmt.Fprintf(w, "Clientr.exe localtest localtest 0 2130706433 %d 0 0", state.loginPort) 105 return 106 } 107 fmt.Fprintf(w, "Clientr.exe localtest localtest %d 2130706433 %d 0 0", id, state.worldPort) 108 } 109 110 // Launches reserve an identity, but create no character or inventory until the 111 // AO handshake. Expired launches are discarded when another launch arrives. 112 type browserVisitorLaunch struct { 113 id uint32 114 expires time.Time 115 } 116 117 type browserVisitors struct { 118 mutex sync.Mutex 119 pending map[string]browserVisitorLaunch 120 } 121 122 func (visitors *browserVisitors) lookup(credential string) uint32 { 123 visitors.mutex.Lock() 124 defer visitors.mutex.Unlock() 125 launch := visitors.pending[credential] 126 if time.Now().After(launch.expires) { 127 delete(visitors.pending, credential) 128 return 0 129 } 130 return launch.id 131 } 132 133 func (server *server) browserSessionID(credential string) (uint32, error) { 134 id, err := server.browserPlayerID(credential) 135 if err != nil || id != 0 { 136 return id, err 137 } 138 return server.browserVisitors.lookup(credential), nil 139 } 140 141 type playerIDReservation uint32 142 143 func (reservation *playerIDReservation) persist(tx *sql.Tx) error { 144 id, err := allocatePlayerID(tx) 145 *reservation = playerIDReservation(id) 146 return err 147 } 148 149 func (coordinator *worldCoordinator) completeBrowserLaunch(id uint32, credential, address string) (*character, error) { 150 if coordinator.browserVisitors.lookup(credential) != id || id == 0 { 151 return nil, nil 152 } 153 if err := checkGameBan(coordinator.db, id, address); err != nil { 154 return nil, err 155 } 156 player := &character{running: true, level: 1, profession: 1, 157 browserTokenHash: credential, 158 playfield: 705, position: [3]float32{960 + float32(mathrand.IntN(49)-24)/8, 16.5, 510 + float32(mathrand.IntN(49)-24)/8}, 159 rotation: [4]float32{0, 0.9238795, 0, -0.3826834}} 160 // Use complete native Solitus head choices with their matching skin race. 161 appearance := appearanceFields{Race: 1, Fatness: 1, Breed: 1, Sex: uint16(2 + mathrand.IntN(2)), Side: 2} 162 var heads []gamedata.PlayerHead 163 for _, head := range gamedata.PlayerAppearances.Heads { 164 if head.Breed == appearance.Breed && head.Sex == appearance.Sex { 165 heads = append(heads, head) 166 } 167 } 168 head := heads[mathrand.IntN(len(heads))] 169 player.headMesh, appearance.Race = head.Mesh, head.Race 170 player.appearance = appearance.packed() 171 player.stats = defaultCharacterStats(appearance.Breed) 172 player.health = player.maximumHealth() 173 player.id = id 174 if err := coordinator.saveVisitor(player, address); err != nil { 175 return nil, err 176 } 177 coordinator.browserVisitors.mutex.Lock() 178 delete(coordinator.browserVisitors.pending, credential) 179 coordinator.browserVisitors.mutex.Unlock() 180 return player, nil 181 }