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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
namespace Tmate
{
public interface SessionInterface : Object
{
public abstract signal void started (string socket);
public abstract signal void stopped ();
public abstract signal void address_changed (SessionType session, string address);
public abstract signal void error (string summery, string message = "");
public abstract signal void joined (string mate, int mates);
public abstract signal void left (string mate, int mates);
public abstract bool start(Config? config = null);
public abstract bool stop();
}
[SingleInstance]
public class Session: Object, SessionInterface
{
private IOChannel? stdout = null;
private Pid? pid = null;
private uint error_count = 0;
public bool terminate = true;
public SessionAddress address { get; construct; }
public string? socket { get; private set;}
public Config? config { get; private set;}
construct {
address = new SessionAddress();
reset();
address.clear();
error.connect_after(() => {
if(error_count++ < 5) return;
stop();
});
}
private void address_set(SessionType session, string? address)
{
if (SessionType.SSH in session || SessionType.HTTP in session) {
debug("CALL: %s\t%s", session.to_string(), address);
if(address != null || address != "")
this.address.add(session, address);
if(this.address[session] != null)
address_changed (session, this.address[session]);
}
}
private void reset()
{
stdout = null;
pid = null;
socket = null;
error_count = 0;
address.clear();
delete_config();
}
internal void delete_config()
{
if(null == config) return;
config.delete();
config = null;
}
private bool send(string[] args)
{
if(pid == null)
return false;
var command = new GenericArray<string>();
command.data = {
"/usr/bin/tmate",
"-S", socket
};
foreach(var arg in args)
command.add(arg);
try {
return Process.spawn_sync(null, command.data, null,
STDERR_TO_DEV_NULL|STDOUT_TO_DEV_NULL,
null, null, null, null);
} catch (SpawnError e) {
return false;
}
}
private void restart()
{
if(terminate) {
stop();
return;
}
started(socket);
address_set(SSH, null);
address_set(HTTP, null);
address_set(SSH | READONLY, null);
address_set(HTTP | READONLY, null);
}
public bool stop()
{
debug("stopping!");
var ret = send({"kill-server"});
if (! send({"has"})) {
debug("stopped!");
Idle.add(() => {
stopped();
return false;
});
}
return ret;
}
public bool start(Config? config = null)
{
if(null != pid) {
started(socket);
return true;
}
int out_fd;
var args = new GenericArray<string>();
args.data = {"/usr/bin/tmate", "-F"};
if(null != config) {
this.config = config;
args.add("-f");
args.add(config.get_path());
}
try {
Process.spawn_async_with_pipes(
null, args.data, null,
STDERR_TO_DEV_NULL | DO_NOT_REAP_CHILD,
null, out pid, null,
out out_fd, null
);
stdout = new IOChannel.unix_new(out_fd);
stdout.add_watch(IOCondition.IN | IOCondition.HUP, (channel, condition) => {
if (condition == IOCondition.HUP)
return false;
try {
string line;
channel.read_line (out line, null, null);
Stdout.Token token = (new Stdout()).parse(line);
debug(token.to_string());
switch(token.class) {
case SOCKET:
socket = token.list[0];
return true;
case INIT:
started (socket);
delete_config();
return true;
case ADDRESS:
address_set(
SessionType.from_string(
token.list[0], token.list[1]
), token.list[2]);
return true;
case RESTART:
restart();
return true;
case TERM:
info (_("Session terminated."));
stopped();
return false;
case ERROR:
this.error(token.list[0], token.list[1]);
return true;
case COMMENT:
return true; // not needed.
case MATES:
return true;// TODO: handle mates join/leave
case UNKNOWN:
debug("%s", token.list[0]);
return true;
}
} catch (IOChannelError e) {
print ("IOChannelError: %s\n", e.message);
return false;
} catch (ConvertError e) {
print ("ConvertError: %s\n", e.message);
return false;
}
return true;
});
ChildWatch.add(pid, (pid, status) => {
Process.close_pid(pid);
reset();
stopped();
});
return true;
} catch (SpawnError e) {
warning("Error: %s", e.message);
this.error(e.message);
reset();
stopped();
return false;
}
}
}
}
|