/
server.ts
163 lines (147 loc) · 3.81 KB
/
server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
X11 server channel.
TODO:
- [ ] other user activity
- [ ] when stopping project, kill xpra's
*/
import { spawn, SpawnOptions } from "child_process";
import { callback } from "awaiting";
const { abspath } = require("smc-util-node/misc_node");
const { path_split } = require("smc-util/misc");
import { clone } from "underscore";
const x11_channels = {};
class X11Channel {
logger: any;
display: number;
path: string;
name: string;
channel: any;
clip: any;
constructor({
primus,
path,
name,
logger,
display
}: {
primus: any;
path: string;
name: string;
logger: any;
display: number;
}) {
this.logger = logger;
this.log("creating new x11 channel");
this.display = display; // needed for copy/paste support
this.path = path;
this.name = name;
this.channel = primus.channel(this.name);
this.init_handlers();
}
log(...args): void {
this.logger.debug(`x11 channel ${this.path} -- `, ...args);
}
new_connection(spark: any): void {
if (this.channel === undefined) {
return;
}
// Now handle the connection
this.log(`new connection from ${spark.address.ip} -- ${spark.id}`);
spark.on("data", async data => {
try {
await this.handle_data(spark, data);
} catch (err) {
spark.write({ error: `error handling command -- ${err}` });
}
});
}
init_handlers(): void {
this.channel.on("connection", this.new_connection.bind(this));
}
async handle_data(_, data): Promise<void> {
this.log("handle_data ", data);
if (typeof data !== "object") {
return; // nothing defined yet
}
switch (data.cmd) {
case "paste":
await this.paste(data.value, data.wid ? data.wid : 0);
break;
case "launch":
await this.launch(data.command, data.args);
break;
default:
throw Error("WARNING: unknown command -- " + data.cmd);
}
}
async paste(value: string, wid: number): Promise<void> {
this.log("paste", value, wid);
await this.set_clipboard(value);
await this.cause_paste(wid);
}
async set_clipboard(value: string): Promise<void> {
this.log("set_clipboard to string of length", value.length);
const p = spawn("xclip", [
"-selection",
"clipboard",
"-d",
`:${this.display}`
]);
p.stdin.write(value);
p.stdin.end();
// wait for exit event.
await callback(cb => p.on("exit", cb));
}
cause_paste(wid: number): void {
this.log("paste to window ", wid);
const env = { DISPLAY: `:${this.display}` };
const args: string[] = ["key", "Control_L+v"];
if (wid) {
args.push("--window");
args.push(`${wid}`);
}
this.log("xdotool", args);
spawn("xdotool", args, { env }).on("close", code => {
console.log(`xdotool exited with code ${code}`);
});
}
// launch a command and detach -- used to start x11 applications running.
launch(command: string, args?: string[]): void {
const env = clone(process.env);
env.DISPLAY = `:${this.display}`;
const cwd = this.get_cwd();
const options: SpawnOptions = { cwd, env, detached: true, stdio: "ignore" };
args = args != null ? args : [];
try {
const sub = spawn(command, args, options);
sub.unref();
} catch (err) {
this.channel.write({
error: `error launching ${command} -- ${err}`
});
return;
}
}
private get_cwd(): string {
return path_split(abspath(this.path)).head; // containing path
}
}
export async function x11_channel(
_: any,
primus: any,
logger: any,
path: string,
display: number
): Promise<string> {
const name = `x11:${path}`;
if (x11_channels[name] === undefined) {
x11_channels[name] = new X11Channel({
primus,
path,
name,
logger,
display
});
}
return name;
}