-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnet-http.js
96 lines (86 loc) · 2.32 KB
/
qnet-http.js
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
// $Id: qnet-http.js,v 1.3 2019/05/27 02:05:26 rswindell Exp $
// QWK network HTTP[S] client
// Example usage in SCFG->Networks->QWK->Hubs->VERT->Call-out Command Line:
//
// ?qnet-http %s dove.synchro.net YOURPASS
const REVISION = "$Revision: 1.3 $".split(' ')[1];
log(LOG_INFO, "QNET-HTTP " + REVISION + " invoked with options: " + argv.join(' '));
load("http.js");
const HTTP_RESPONSE_SUCCESS = 200;
const HTTP_RESPONSE_NO_CONTENT = 204;
if(argc < 3) {
alert("usage: [-s] <hub-id> <host-name>[:port] <your-password>");
exit();
}
var scheme = "http://";
var userid = system.qwk_id;
if(argv[0] == '-s') {
scheme = "https://";
argv.shift();
}
var hubid = argv[0];
var hostname = argv[1];
var password = argv[2];
var url = format("%s%s/qwk.ssjs", scheme, hostname);
var rep = format("%s%s.rep", system.data_dir, hubid);
var qwk = format("%s%s.qwk", system.data_dir, hubid);
function send_rep(rep)
{
print("Sending " + rep);
var file = new File(rep);
if(!file.open("rb")) {
alert("error " + file.error + " opening " + file.name);
return false;
}
var data = file.read();
file.close();
if(!data) {
alert("No data read from " + file.name);
return false;
}
var http = new HTTPRequest(userid, password);
http.Post(url, data);
if(http.response_code != HTTP_RESPONSE_SUCCESS) {
alert(http.request + " Response: " + http.status_line);
return false;
}
if(!file_remove(rep)) {
log(LOG_ERR, "Error removing file: " + rep);
return false;
}
return true;
}
function receive_qwk(qwk)
{
print("Getting " + qwk);
var file = new File(qwk);
if(!file.open("wb")) {
alert("error " + file.error + " opening " + file.name);
return false;
}
var success = false;
var http = new HTTPRequest(userid, password);
var contents = http.Get(url);
if(http.response_code == HTTP_RESPONSE_NO_CONTENT) {
print("No packet received");
success = true;
}
else if(http.response_code != HTTP_RESPONSE_SUCCESS)
alert(http.request + " Response: " + http.status_line);
else if(!contents)
alert("No data received");
else {
print("Received " + contents.length + " bytes");
file.write(contents);
http.Post(url + '?received=' + file.position, '');
success = (http.response_code == HTTP_RESPONSE_NO_CONTENT);
}
file.close();
return success;
}
if(file_exists(rep)) {
if(!send_rep(rep))
exit(1);
}
if(!receive_qwk(qwk))
exit(1);