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
Changes from all 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
45 changes: 38 additions & 7 deletions src/shadowbox/server/outline_shadowsocks_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,26 @@ import * as file from '../infrastructure/file';
import * as logging from '../infrastructure/logging';
import {ShadowsocksAccessKey, ShadowsocksServer} from '../model/shadowsocks_server';

/** Represents an outline-ss-server configuration with multiple services. */
export interface OutlineSSServerConfig {
services: {
listeners: {
type: string;
address: string;
}[];
keys: {
id: string;
cipher: string;
secret: string;
}[];
}[];
}

// 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 @@ -81,22 +95,39 @@ export class OutlineShadowsocksServer implements ShadowsocksServer {

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

keysJson.keys.push(key);
const config: OutlineSSServerConfig = {services: []};
const keysByPort: Record<number, ShadowsocksAccessKey[]> = {};
for (const key of validKeys) {
(keysByPort[key.port] ??= []).push(key);
}
for (const port in keysByPort) {
const service = {
listeners: [
{type: 'tcp', address: `[::]:${port}`},
{type: 'udp', address: `[::]:${port}`},
],
keys: keysByPort[port].map((key) => ({
id: key.id,
cipher: key.cipher,
secret: key.secret,
})),
};
config.services.push(service);
}

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
Loading