repl.it
@Scoder12/

repl reviver multi

Nodejs

No description

fork
loading
Files
  • index.js
  • repl_config.js
  • Packager files
  • package-lock.json
  • package.json
index.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
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
// Repl Reviver 
// Originally made by mat, improved by Scoder12

// CHANGE THIS STUFF
let REPLS = require("./repl_config.js")
    .filter((repl) => repl.id && repl.url && repl.url.startsWith('http'));

const ownURL = 'https://repl-reviver-multi.scoder12.repl.co'

const apiKey = process.env.key


global.WebSocket = require("ws");

const fetch = require('node-fetch');
const crosis = require('@replit/crosis');
const express = require('express');

const app = express();


if (!apiKey) {
	console.log('HOW TO USE REPL REVIVER')
	console.log('1) fork the repl')
	console.log('2) create a file called .env (that exact name)')
	console.log('3) put key=YOURAPIKEY in .env')
	console.log('4) replace the id key of each entry in repl_config.js with the id of your repl')
	console.log('5) replace the url key of each entry in repl_config.js with the repl.co url of your repl (repeat step 4 and 5 with new objects to add more repls!)');
	console.log('6) replace the ownURL variable with the repl.co url of this repl (to keep it alive)')
	console.log('')
	console.log('How to get an API key: go to https://devs.turbio.repl.co/')
	console.log('How to get a repl id: go to repl.it/data/repls/@YOURUSERNAME/YOURREPLNAME and find the part where it says the id')
	process.exit()
}
console.log('starting')
var connected = {};
var isUp = true


async function connect(replID, timeout) {
	if (connected[replID]) return
	connected[replID] = true;
	const client = new crosis.Client();


	const token = await getToken(replID, apiKey);
    console.log("crosis connecting to", replID);
	await client.connect({ token });

	const packagerCh = client.openChannel({
		service: 'packager3'	
	});
	const interpreterCh = client.openChannel({
		service: 'interp2',
		name: 'interper'
	});

	packagerCh.on('command', async(command) => {
		if (command.state)
			console.log(`packager state ${command.state}`)
		if (command.ok) {
			console.log('packager ok, now running main')
			if (!isUp) {
				await interpreterCh.request({
					runMain: {}
				});
				isUp = true
			}
		}
	});
	interpreterCh.on('command', async(command) => {
	  //console.log('interpreter', command)
		if (command.state === 1) {
			connected[replID] = false
			if (timeout)
				clearTimeout(timeout)
            let repl = REPLS.filter((r) => r.id == replID);
            co_url = repl.length != 1? "??" : repl[0].url;
			console.log(co_url, 'back up!');
			client.close()
			//.catch(() => {
			//	console.log('uhhhh failed to close')
			//})
		}
		var output = command.output
			.replace('\u001b[2J', '[clear]')
		// process.stdout.write(output)
	});

  await packagerCh.request({
		packageInstall: {}
	});


	// 
}


function getToken(replId, apiKey) {
    console.log("getting token");
  return fetch(`https://repl.it/api/v0/repls/${replId}/token`, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ apiKey })
  }).then(res => res.json());
}

function connectWithTimeout(replID) {
    //console.log("connecting...");
	isUp = false
	var assumeDeadTimeout = setTimeout(() => {
		console.log('assumed dead, cleared timeout')
		connected = false
	}, 300000); // 5 mins
	connect(replID, assumeDeadTimeout)
}


function check(replURL, replID) {
    //console.log("fetching", replURL)
    fetch(replURL)
    .catch((e) => {
        console.log(replURL, 'fetch error, disconnected!!!');
        console.log(e);
        connectWithTimeout(replID);
    })
    .then(res => {
        if (res.status == '502') {
            console.log(replURL, '502 error')
            connectWithTimeout(replID);
        } else if (res.status == '500') {
            console.log(replURL, '500')
            connectWithTimeout(replID)
        } else if (res.status == '524') {
            console.log(replURL, '524')
            connectWithTimeout(replID)
        } else {
            isUp = true
            console.log(replURL, 'good', res.status)
        }
    })
}

function checkServerForever() {
    console.log(REPLS.length + ' valid repls');
	console.log('checking server forever')
    REPLS.forEach(function initCheckLoop(repl) {
	    connect(repl.id).catch((e) => {
            if (e && e.message && e.message.startsWith("Channel closed")) {
                return; // ignore that stupid error lol
            }
            console.log('uh', e)
        });
    });
	setInterval(async() => {
        //console.log("fetch self");
		fetch(ownURL).catch(()=>{})
		REPLS.forEach(function checkLoop(repl) {
            check(repl.url, repl.id);
        });
	}, 5000)
}

checkServerForever()

app.get('/', (req, res) => {
  res.send('Hello world!')
});

app.listen(3000, () => {
  console.log('server started');
});
""
Fetching token
?