Skip to content

Commit

Permalink
feat(www): support server name on dynamic keys (#1483)
Browse files Browse the repository at this point in the history
* init commit

* decodeURIComponent

* propose this

* address feedback
  • Loading branch information
daniellacosse authored Nov 30, 2022
1 parent a92b3a3 commit c6e4c34
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/www/app/outline_server_repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ function isDynamicAccessKey(accessKey: string): boolean {

// NOTE: For extracting a name that the user has explicitly set, only.
// (Currenly done by setting the hash on the URI)
function serverNameFromAccessKey(accessKey: string): string {
if (isDynamicAccessKey(accessKey)) {
return new URL(accessKey.replace(/^ssconf:\/\//, 'https://')).hostname;
}
function serverNameFromAccessKey(accessKey: string): string | undefined {
const {hash} = new URL(accessKey.replace(/^ss(?:conf)?:\/\//, 'https://'));

if (!hash) return;

return SHADOWSOCKS_URI.parse(accessKey).tag.data;
return decodeURIComponent(
hash
.slice(1)
.split('&')
.find(keyValuePair => !keyValuePair.includes('='))
);
}

// DEPRECATED: V0 server persistence format.
Expand Down Expand Up @@ -123,7 +128,15 @@ export class OutlineServerRepository implements ServerRepository {
add(accessKey: string) {
this.validateAccessKey(accessKey);

const server = this.createServer(uuidv4(), accessKey, serverNameFromAccessKey(accessKey));
let serverName = serverNameFromAccessKey(accessKey);

if (!serverName && isDynamicAccessKey(accessKey)) {
const {hostname} = new URL(accessKey);

serverName = hostname;
}

const server = this.createServer(uuidv4(), accessKey, serverName);

this.serverById.set(server.id, server);
this.storeServers();
Expand Down

0 comments on commit c6e4c34

Please sign in to comment.