Skip to content
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

refactor(server): use the new service config format #1628

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/shadowbox/model/shadowsocks_server.ts
sbruens marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@
// limitations under the License.

// Parameters required to identify and authenticate connections to a Shadowsocks server.
export interface ShadowsocksServer {
// Updates the server to accept only the given service configs.
update(config: ShadowsocksConfig): Promise<void>;
}

/** Represents the overall Shadowsocks configuration with multiple services. */
export interface ShadowsocksConfig {
services: ShadowsocksService[];
}

/* Represents a Shadowsocks service with its listeners and keys. */
export interface ShadowsocksService {
listeners: ShadowsocksListener[];
keys: ShadowsocksAccessKey[];
}

/* Represents a single listener for a Shadowsocks service. */
export interface ShadowsocksListener {
type: string;
address: string;
}

/* Represents an access key for a Shadowsocks service. */
export interface ShadowsocksAccessKey {
id: string;
port: number;
cipher: string;
secret: string;
}

export interface ShadowsocksServer {
// Updates the server to accept only the given access keys.
update(keys: ShadowsocksAccessKey[]): Promise<void>;
}
14 changes: 8 additions & 6 deletions src/shadowbox/server/mocks/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import {PrometheusClient, QueryResultData} from '../../infrastructure/prometheus_scraper';
import {ShadowsocksAccessKey, ShadowsocksServer} from '../../model/shadowsocks_server';
import {ShadowsocksAccessKey, ShadowsocksConfig, ShadowsocksServer} from '../../model/shadowsocks_server';
import {TextFile} from '../../infrastructure/text_file';

export class InMemoryFile implements TextFile {
Expand All @@ -36,15 +36,17 @@ export class InMemoryFile implements TextFile {
}

export class FakeShadowsocksServer implements ShadowsocksServer {
private accessKeys: ShadowsocksAccessKey[] = [];
private config: ShadowsocksConfig = {services: []};

update(keys: ShadowsocksAccessKey[]) {
this.accessKeys = keys;
update(config: ShadowsocksConfig) {
this.config = config;
return Promise.resolve();
}

getAccessKeys() {
return this.accessKeys;
getAccessKeys(): ShadowsocksAccessKey[] {
return this.config.services.reduce((acc, service) => {
return acc.concat(service.keys);
}, [] as ShadowsocksAccessKey[]);
}
}

Expand Down
33 changes: 17 additions & 16 deletions src/shadowbox/server/outline_shadowsocks_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import * as path from 'path';

import * as file from '../infrastructure/file';
import * as logging from '../infrastructure/logging';
import {ShadowsocksAccessKey, ShadowsocksServer} from '../model/shadowsocks_server';
import {ShadowsocksAccessKey, ShadowsocksServer, ShadowsocksConfig} from '../model/shadowsocks_server';

// Runs outline-ss-server.
export class OutlineShadowsocksServer implements ShadowsocksServer {
private ssProcess: child_process.ChildProcess;
private ipCountryFilename?: string;
private ipAsnFilename?: string;
private isAsnMetricsEnabled = false;
private isReplayProtectionEnabled = false;

/**
Expand Down Expand Up @@ -66,10 +65,10 @@ export class OutlineShadowsocksServer implements ShadowsocksServer {
}

// Promise is resolved after the outline-ss-config config is updated and the SIGHUP sent.
// Keys may not be active yet.
// Listeners and keys may not be active yet.
// TODO(fortuna): Make promise resolve when keys are ready.
update(keys: ShadowsocksAccessKey[]): Promise<void> {
return this.writeConfigFile(keys).then(() => {
update(config: ShadowsocksConfig): Promise<void> {
return this.writeConfigFile(config).then(() => {
if (!this.ssProcess) {
this.start();
return Promise.resolve();
Expand All @@ -79,24 +78,26 @@ export class OutlineShadowsocksServer implements ShadowsocksServer {
});
}

private writeConfigFile(keys: ShadowsocksAccessKey[]): Promise<void> {
private writeConfigFile(config: ShadowsocksConfig): Promise<void> {
return new Promise((resolve, reject) => {
const keysJson = {keys: [] as ShadowsocksAccessKey[]};
for (const key of keys) {
if (!isAeadCipher(key.cipher)) {
logging.error(
`Cipher ${key.cipher} for access key ${key.id} is not supported: use an AEAD cipher instead.`
);
continue;
for (const service of config.services) {
const filteredKeys: ShadowsocksAccessKey[] = [];
for (const key of service.keys) {
if (!isAeadCipher(key.cipher)) {
logging.error(
`Cipher ${key.cipher} for access key ${key.id} is not supported: use an AEAD cipher instead.`
);
continue;
}
filteredKeys.push(key);
}

keysJson.keys.push(key);
service.keys = filteredKeys;
}

mkdirp.sync(path.dirname(this.configFilename));

try {
file.atomicWriteFileSync(this.configFilename, jsyaml.safeDump(keysJson, {sortKeys: true}));
file.atomicWriteFileSync(this.configFilename, jsyaml.safeDump(config, {sortKeys: true}));
resolve();
} catch (error) {
reject(error);
Expand Down
33 changes: 25 additions & 8 deletions src/shadowbox/server/server_access_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
ProxyParams,
} from '../model/access_key';
import * as errors from '../model/errors';
import {ShadowsocksServer} from '../model/shadowsocks_server';
import {ShadowsocksConfig, ShadowsocksServer, ShadowsocksService} from '../model/shadowsocks_server';
import {PrometheusManagerMetrics} from './manager_metrics';

// The format as json of access keys in the config file.
Expand Down Expand Up @@ -322,17 +322,34 @@ export class ServerAccessKeyRepository implements AccessKeyRepository {
}

private updateServer(): Promise<void> {
const serverAccessKeys = this.accessKeys
const config: ShadowsocksConfig = {services: []}

// Group access keys by port.
const keysByPort = this.accessKeys
.filter((key) => !key.reachedDataLimit)
.map((key) => {
return {
.reduce((acc, key) => {
const port = key.proxyParams.portNumber;
(acc[port] ??= []).push(key);
return acc;
}, {} as Record<number, typeof this.accessKeys>);

// Create services for each port.
for (const port in keysByPort) {
const service: ShadowsocksService = {
listeners: [
{ type: 'tcp', address: `[::]:${port}` },
{ type: 'udp', address: `[::]:${port}` },
],
keys: keysByPort[port].map((key) => ({
id: key.id,
port: key.proxyParams.portNumber,
cipher: key.proxyParams.encryptionMethod,
secret: key.proxyParams.password,
};
});
return this.shadowsocksServer.update(serverAccessKeys);
})),
};
config.services.push(service);
}

return this.shadowsocksServer.update(config);
}

private loadAccessKeys(): AccessKey[] {
Expand Down
Loading