forked from Jigsaw-Code/outline-apps
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtunnel_store.ts
executable file
·97 lines (88 loc) · 2.89 KB
/
tunnel_store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as fs from 'fs';
import * as path from 'path';
import {ShadowsocksSessionConfig} from '../www/app/tunnel';
// Format to store a tunnel configuration.
export interface SerializableTunnel {
id: string;
config: ShadowsocksSessionConfig;
}
// Persistence layer for a single SerializableTunnel.
export class TunnelStore {
private storagePath: string;
// Creates the store at `storagePath`.
constructor(storagePath: string) {
if (!fs.existsSync(storagePath)) {
fs.mkdirSync(storagePath);
}
// TODO(alalama): rename key to 'tunnel_store' when performing a data migration.
this.storagePath = path.join(storagePath, 'connection_store');
}
// Persists the tunnel to the store. Rejects the promise on failure.
save(tunnel: SerializableTunnel): Promise<void> {
if (!this.isTunnelValid(tunnel)) {
return Promise.reject(new Error('Cannot save invalid tunnel'));
}
return new Promise((resolve, reject) => {
fs.writeFile(this.storagePath, JSON.stringify(tunnel), 'utf8', error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
// Retrieves a tunnel from storage. Rejects the promise if there is none.
load(): Promise<SerializableTunnel> {
return new Promise((resolve, reject) => {
fs.readFile(this.storagePath, 'utf8', (error, data) => {
if (!data) {
reject(error);
return;
}
const tunnel = JSON.parse(data);
if (this.isTunnelValid(tunnel)) {
resolve(tunnel);
} else {
reject(new Error('Cannot load invalid tunnel'));
}
});
});
}
// Deletes the stored tunnel. Rejects the promise on failure.
clear(): Promise<void> {
return new Promise((resolve, reject) => {
if (!fs.existsSync(this.storagePath)) {
resolve();
}
fs.unlink(this.storagePath, error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
// Returns whether `tunnel` and its configuration contain all the required fields.
private isTunnelValid(tunnel: SerializableTunnel) {
const config = tunnel.config;
if (!config || !tunnel.id) {
return false;
}
return config.method && config.password && config.host && config.port;
}
}