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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#!/usr/bin/env node
var fs=require("fs"),
path=require("path"),
http=require("http"),
crypto=require("crypto"),
dialog=require("dialog"),
kbd=require("kbd"),
toClipboard=require("to-clipboard");
var HOSTNAME=process.argv[2]||"tomsmeding.com",HTTPPORT=42420;
console.log("Using server "+HOSTNAME);
var lockfiledesc;
var lockfilename="/tmp/.lock_serverstore_client_"+HOSTNAME;
try {lockfiledesc=fs.openSync(lockfilename,"wx");}
catch(e){
var lockedpid=+fs.readFileSync(lockfilename);
var canoverwrite=false;
if(isNaN(lockedpid))canoverwrite=true;
try {process.kill(lockedpid,0);}
catch(e){canoverwrite=true;}
if(!canoverwrite){
console.log("Another copy of the serverstore client with the same hostname seems to be running.");
console.log("If this is not the case, or intentional, delete the file "+lockfilename+" and try again.");
process.exit(1);
}
lockfiledesc=fs.openSync(lockfilename,"w");
}
fs.write(lockfiledesc,process.pid+"\n");
process.on("exit",function(){
fs.closeSync(lockfiledesc);
fs.unlinkSync(lockfilename);
});
var userid="0",password="-";
var HOMEDIR=process.env.HOME||process.env.HOMEPATH||process.env.HOMEDIR||process.cwd();
var WATCHDIR=HOMEDIR+"/Desktop";
var ignored=[];
var currentState=[];
//lots of code taken/modified from tomsmeding/gvajnez
function collectDirState(dir){
if(!dir)dir=directory;
//console.log("collectDirState("+dir+")");
var list=fs.readdirSync(dir);
var result=[];
var statinfo,i,j;
for(i=0;i<list.length;i++){
for(j=0;j<ignored.length;j++){
if(etc.endsWith(path.normalize(dir+"/"+list[i]),"/"+ignored[j]))break;
}
if(j<ignored.length)continue;
statinfo=fs.statSync(dir+"/"+list[i]);
/*if(statinfo.isDirectory()){
result=result.concat(collectDirState(dir+"/"+list[i]));
} else if(statinfo.isFile()){*/
result.push({
name:path.resolve(dir+"/"+list[i]),
mode:statinfo.mode,
mtime:statinfo.mtime.getTime()
});
//}
}
return result;
}
function collectChanges(dir,state){
if(!dir)dir=directory;
var i,j,obj;
var changes=[];
for(i=0;i<state.length;i++){
for(j=0;j<currentState.length;j++){
if(state[i].name==currentState[j].name)break;
}
if(j==currentState.length||
currentState[j].mode!=state[i].mode||
currentState[j].mtime!=state[i].mtime){
//either file didn't exist yet, or metadata has changed
changes.push(state[i]);
}
}
for(i=0;i<currentState.length;i++){
for(j=0;j<state.length;j++){
if(currentState[i].name==state[j].name)break;
}
if(j==state.length){
//file doesn't exist anymore
changes.push(currentState[i]);
}
}
return changes;
}
function handleChanges(changes){
var i,match,namedate,now;
for(i=0;i<changes.length;i++){
if(!fs.existsSync(changes[i].name))continue; //probably delete event
match=changes[i].name.match(/\/Screen Shot (\d{4}-\d{2}-\d{2}) at (\d{2}\.\d{2}\.\d{2}).png$/);
if(!match)continue;
namedate=new Date(match[1]+" "+match[2].replace(/\./g,":"));
if(!namedate)continue;
changes[i].mtime=new Date(changes[i].mtime);
now=new Date();
if((now-changes[i].mtime)/1000>60||(now-namedate)/1000>60)continue; //1 minute limit
setTimeout(function(n){
return function(){sendfile(n);};
}(changes[i].name),100);
}
}
function authhash(challenge,password){
var s=challenge+password;
var hasher=crypto.createHash("sha256");
hasher.update(s);
return hasher.digest("hex");
}
function getchallenge(cb){
console.log("Going to request challenge...");
var req=http.request({
hostname:HOSTNAME,
port:HTTPPORT,
path:"/challenge",
method:"GET",
keepAlive:true //speed up the next request
},function(res){
var body="";
res.on("data",function(data){
body+=data;
});
res.on("end",function(){
if(res.statusCode!=200){
dialog.warn("Could not request challenge! Is your internet connection alive?\n\n"+body);
return;
}
console.log("challenge = "+body);
cb(body);
});
});
req.on("error",function(err){
console.log(err);
});
req.end();
}
function sendfile(fname,retries){
retries=retries!=null?retries:3;
var barefname;
var idx=fname.lastIndexOf("/");
if(idx==-1)barefname=fname;
else barefname=fname.slice(idx+1);
getchallenge(function(challenge){
var req=http.request({
hostname:HOSTNAME,
port:HTTPPORT,
path:"/image/"+userid+"/"+authhash(challenge,password)+"/"+escape(barefname),
method:"POST",
headers:{
"Content-Type":"image/png"
}
},function(res){
var body="";
res.on("data",function(data){
body+=data;
});
res.on("end",function(){
if(res.statusCode!=200){
if(retries>0)sendfile(fname,retries-1);
else dialog.warn("Could not upload image! Are your credentials still okay?\n\n"+body);
return;
}
console.log("Successful upload");
toClipboard.sync(body.trim());
dialog.info(body+"\n(Copied to clipboard.)");
try {
fs.unlinkSync(fname);
} catch(e){
console.log(e);
}
});
});
req.on("error",function(err){
console.log(err);
});
try{req.end(fs.readFileSync(fname));}
catch(e){console.log(e);}
});
}
function userExists(userid,cb){
var req=http.request({
hostname:HOSTNAME,
port:HTTPPORT,
path:"/exists/"+userid,
method:"GET"
},function(res){
if(res.statusCode==200)cb(true);
else if(res.statusCode==404)cb(false);
else {
console.log("Server returned status code "+res.statusCode+" for exists query!");
}
});
req.on("error",function(err){
console.log(err);
});
req.end();
}
function checkLogin(userid,password,cb,retries){
retries=retries!=null?retries:3;
getchallenge(function(challenge){
var req=http.request({
hostname:HOSTNAME,
port:HTTPPORT,
path:"/checklogin/"+userid+"/"+authhash(challenge,password),
method:"GET"
},function(res){
if(res.statusCode==200)cb(true);
else if(res.statusCode==404||res.statusCode==403){
if(retries>0)checkLogin(userid,password,cb,retries-1);
else cb(false);
} else {
console.log("Server returned status code "+res.statusCode+" for checklogin query!");
}
});
req.on("error",function(err){
console.log(err);
});
req.end();
});
}
function registerUser(userid,password){
var req=http.request({
hostname:HOSTNAME,
port:HTTPPORT,
path:"/registerx/"+userid,
method:"POST",
headers:{
"Content-Type":"text/plain"
}
},function(res){
var body="";
res.on("data",function(data){
body+=data;
});
res.on("end",function(){
if(res.statusCode==200)console.log("Successfully registered user "+userid);
else if(res.statusCode==409)console.log("Conflict: "+body);
else console.log("Error: "+body);
});
});
req.on("error",function(err){
console.log(err);
});
req.end(password);
}
process.stdout.write("Username? ");
userid=kbd.getLineSync().replace(/[^a-zA-Z0-9_-]/g,"");
process.stdout.write("Password? ");
(function(){
var hasher=crypto.createHash("sha256");
kbd.setEcho(false);
var passinput=kbd.getLineSync();
hasher.update(passinput);
kbd.setEcho(true);
password=hasher.digest("hex");
})();
console.log("\nChecking existence...");
userExists(userid,function(exists){
if(exists){
checkLogin(userid,password,function(ok){
if(ok)console.log("User login ok.");
else {
console.log("Username or password incorrect!");
process.exit();
}
});
return;
}
process.stdout.write("That username doesn't seem to exist. Register it? [y/N] ");
var response=kbd.getLineSync()[0];
if(response=="y"||response=="Y")registerUser(userid,password);
else {
console.log("Not registered. Exiting.");
process.exit();
}
});
var timeout=null;
var watcher=fs.watch(WATCHDIR,{persistent:true,recursive:false},function(ev,fname){
//console.log("change in directory "+WATCHDIR+" (fname "+fname+")");
if(timeout)return;
timeout=setTimeout(function(){
var newstate=collectDirState(WATCHDIR);
var changes=collectChanges(WATCHDIR,newstate).map(function(o){o.name=o.name.replace(/^\.\//,"");return o;});
currentState=newstate;
if(changes.length!=0)handleChanges(changes);
timeout=null;
//console.log(currentState);
},500);
});
currentState=collectDirState(WATCHDIR);
console.log("-- (Client ready.)");
|