-
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(skymp5-client): make server settings more flexible #2347
Merged
Pospelove
merged 7 commits into
skyrim-multiplayer:main
from
nic11:client-server-settings
Feb 19, 2025
+179
−82
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
72f039b
feat(skymp5-client): make server settings more flexible
nic11 55479dd
upd
nic11 4d64152
upd
nic11 daaa194
upd
nic11 a704e52
rm comment about async
nic11 ee18043
Update index.ts
Pospelove 9325f2f
Update index.ts
Pospelove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { HttpClient, HttpHeaders, HttpResponse, printConsole } from "skyrimPlatform"; | ||
import { AuthService } from "./authService"; | ||
import { ClientListener, CombinedController, Sp } from "./clientListener"; | ||
|
||
interface IHttpClient { | ||
get(path: string, options?: { headers?: HttpHeaders }): Promise<HttpResponse>; | ||
post(path: string, options: { body: string, contentType: string, headers?: HttpHeaders }): Promise<HttpResponse>; | ||
} | ||
|
||
interface TargetPeer { | ||
host: string; | ||
port: number; | ||
} | ||
|
||
export class SettingsService extends ClientListener { | ||
constructor(private sp: Sp, private controller: CombinedController) { | ||
super(); | ||
} | ||
|
||
public getServerMasterKey() { | ||
let masterKey = this.sp.settings["skymp5-client"]["server-master-key"]; | ||
if (!masterKey) { | ||
masterKey = this.sp.settings["skymp5-client"]["master-key"]; | ||
} | ||
if (!masterKey) { | ||
masterKey = this.sp.settings["skymp5-client"]["server-ip"] + ":" + this.sp.settings["skymp5-client"]["server-port"]; | ||
} | ||
return masterKey; | ||
} | ||
|
||
// TODO: make async? | ||
public getMasterUrl() { | ||
return this.normalizeUrl((this.sp.settings["skymp5-client"]["master"] as string) || "https://gateway.skymp.net"); | ||
} | ||
|
||
public async makeMasterApiClient(): Promise<IHttpClient> { | ||
const addr = this.getMasterUrl(); | ||
return new HttpClient(addr); | ||
} | ||
|
||
public async getTargetPeer(): Promise<TargetPeer> { | ||
const masterApiClient = await this.makeMasterApiClient(); | ||
const masterKey = this.getServerMasterKey(); | ||
try { | ||
let headers: HttpHeaders = {}; | ||
let session = this.controller.lookupListener(AuthService).readAuthDataFromDisk()?.session; | ||
if (session) { | ||
headers['X-Session'] = session; | ||
} | ||
|
||
const res = await masterApiClient.get(`/api/servers/${masterKey}/serverinfo`, { headers }); | ||
if (res.status != 200) { | ||
throw new Error(`status ${res.status}`); | ||
} | ||
const resJson = JSON.parse(res.body); | ||
return { | ||
host: resJson.host, | ||
port: resJson.port, | ||
}; | ||
} catch (e) { | ||
printConsole(`Server info request failed, falling back; error: ${e}`); | ||
} | ||
|
||
return { | ||
host: this.sp.settings['skymp5-client']['server-ip'] as string, | ||
port: this.sp.settings['skymp5-client']['server-port'] as number, | ||
}; | ||
} | ||
|
||
private normalizeUrl(url: string) { | ||
if (url.endsWith('/')) { | ||
return url.slice(0, url.length - 1); | ||
} | ||
return url; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promises are not yet supported in main menu