Skip to content

Commit c8c6915

Browse files
committed
revamp
1 parent 29e5913 commit c8c6915

File tree

9 files changed

+557
-55
lines changed

9 files changed

+557
-55
lines changed

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,37 @@ Update your yare code easily from node.js.
55
## Setup
66

77
- Install node.js
8-
- Install tampermonkey
9-
- Install [this](https://raw.githubusercontent.com/swz-gh/yare-sync/main/dist/client.js) userscript
108

119
## Example
1210

1311
```js
1412
let server = require("yare-sync");
1513

16-
server
17-
.updateCode("console.log('test')")
18-
.then(() => {
19-
console.log("Updated code!");
20-
})
21-
.catch((e) => {
22-
console.error("Failed to update code");
23-
});
14+
let username = "your-username";
15+
let password = "VerySecurePassword"; // remember to not store your password in your code
16+
17+
async function main() {
18+
console.log("Logging in as", username);
19+
let acc = await server.login(username, password);
20+
21+
if (!acc) return console.log("Login failed");
22+
23+
console.log("Session id:", acc.session_id);
24+
25+
let games = await server.getGames(username);
26+
27+
console.log("Games:", games);
28+
29+
let successful = await server.sendCode(
30+
"console.log('Hello, World!')",
31+
games,
32+
acc
33+
);
34+
35+
if (successful) {
36+
console.log("Uploaded code!");
37+
}
38+
}
39+
40+
main();
2441
```

dist/client.user.js

Lines changed: 193 additions & 14 deletions
Large diffs are not rendered by default.

dist/wrappers.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
'use strict';
2+
3+
var child_process = require('child_process');
4+
var path = require('path');
5+
var WebSocket = require('ws');
6+
var axios = require('axios');
7+
8+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9+
10+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
11+
var WebSocket__default = /*#__PURE__*/_interopDefaultLegacy(WebSocket);
12+
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
13+
14+
/*! *****************************************************************************
15+
Copyright (c) Microsoft Corporation.
16+
17+
Permission to use, copy, modify, and/or distribute this software for any
18+
purpose with or without fee is hereby granted.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
21+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
22+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
23+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
24+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
25+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
26+
PERFORMANCE OF THIS SOFTWARE.
27+
***************************************************************************** */
28+
29+
function __awaiter(thisArg, _arguments, P, generator) {
30+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31+
return new (P || (P = Promise))(function (resolve, reject) {
32+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35+
step((generator = generator.apply(thisArg, _arguments || [])).next());
36+
});
37+
}
38+
39+
const GAMESERVER = "d1";
40+
const YAREDOMAIN = "yare.io";
41+
function verifySession(acc) {
42+
var _a;
43+
return __awaiter(this, void 0, void 0, function* () {
44+
let failed = false;
45+
let req = yield axios__default['default']
46+
.post(`https://yare.io/session`, {
47+
user_id: acc.user_id,
48+
session_id: acc.session_id,
49+
})
50+
.catch((e) => {
51+
failed = true;
52+
});
53+
if (failed)
54+
return false;
55+
return ((_a = req === null || req === void 0 ? void 0 : req.data) === null || _a === void 0 ? void 0 : _a.username) === acc.user_id;
56+
});
57+
}
58+
function getGames(userID) {
59+
return __awaiter(this, void 0, void 0, function* () {
60+
let req = yield axios__default['default'].get(`https://${YAREDOMAIN}/active-games/${userID}`);
61+
if (req.data.data === "no active games") {
62+
return [];
63+
}
64+
else {
65+
return req.data.data;
66+
}
67+
});
68+
}
69+
function sendCode(code, game, acc) {
70+
return __awaiter(this, void 0, void 0, function* () {
71+
// If its multiple games, run them individually
72+
if (Array.isArray(game)) {
73+
let stillworking = true;
74+
let promises = [];
75+
for (let g of game) {
76+
promises.push(sendCode(code, g, acc).then((result) => {
77+
if (!result)
78+
stillworking = false;
79+
}));
80+
}
81+
yield Promise.all(promises);
82+
// If all were successful then return true
83+
return stillworking;
84+
}
85+
if (!(yield verifySession(acc))) {
86+
return false;
87+
}
88+
let ws = new WebSocket__default['default'](`wss://${YAREDOMAIN}/${GAMESERVER}/${game}`, {
89+
headers: {
90+
"User-Agent": "yare-sync (https://github.com/swz-gh/yare-sync)",
91+
},
92+
});
93+
return new Promise((resolve) => {
94+
// @ts-ignore
95+
ws.on("open", (event) => {
96+
ws.send(JSON.stringify({
97+
u_code: code,
98+
u_id: acc.user_id,
99+
session_id: acc.session_id,
100+
}));
101+
ws.close();
102+
resolve(true);
103+
});
104+
// @ts-ignore
105+
ws.on("error", (e) => {
106+
resolve(false);
107+
});
108+
});
109+
});
110+
}
111+
function login(username, password) {
112+
var _a;
113+
return __awaiter(this, void 0, void 0, function* () {
114+
let req = yield axios__default['default']
115+
.post(`https://${YAREDOMAIN}/validate`, {
116+
user_name: username !== null && username !== void 0 ? username : "INVALID_USER",
117+
password,
118+
})
119+
.catch((e) => {
120+
throw Error("Couldn't log in");
121+
});
122+
if (((_a = req.data) === null || _a === void 0 ? void 0 : _a.user_id) === username) {
123+
return {
124+
user_id: req.data.user_id,
125+
session_id: req.data.data,
126+
};
127+
}
128+
else {
129+
throw Error("Couldn't log in");
130+
}
131+
});
132+
}
133+
/** @deprecated since version 2.0 */
134+
function updateCode(code) {
135+
return __awaiter(this, void 0, void 0, function* () {
136+
return new Promise((res, rej) => {
137+
let fpath = path__default['default'].join(path__default['default'].dirname(__filename), "../dist/server.js ");
138+
let resp = child_process.execSync(`node ${fpath} ${Buffer.from(code, "utf-8").toString("base64")}`).toString();
139+
if (resp.includes("Success!")) {
140+
res(true);
141+
}
142+
else {
143+
rej();
144+
}
145+
});
146+
});
147+
}
148+
module.exports = { updateCode, login, getGames, sendCode };

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "yare-sync",
33
"version": "0.2.1",
44
"description": "Update your yare code from the commandline ",
5-
"main": "serverwrapper.js",
5+
"main": "dist/wrappers.js",
66
"scripts": {
77
"build": "rollup -c",
88
"prepublish": "yarn build"
@@ -20,6 +20,7 @@
2020
"devDependencies": {
2121
"@rollup/plugin-typescript": "^8.2.1",
2222
"@types/express": "^4.17.13",
23+
"@types/ws": "^7.4.7",
2324
"rollup": "^2.51.2",
2425
"rollup-plugin-terser": "^7.0.2",
2526
"rollup-plugin-userscript-metablock": "^0.3.0",
@@ -31,6 +32,8 @@
3132
"dependencies": {
3233
"@rollup/plugin-commonjs": "^19.0.0",
3334
"@rollup/plugin-url": "^6.0.0",
34-
"express": "^4.17.1"
35+
"axios": "^0.21.1",
36+
"express": "^4.17.1",
37+
"uuid": "^8.3.2"
3538
}
3639
}

rollup.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import metablock from "rollup-plugin-userscript-metablock";
66

77
let terserConfig = {
88
format: {
9-
comments: false,
9+
comments: true,
1010
semicolons: false,
1111
},
1212
};
@@ -50,4 +50,12 @@ export default [
5050
},
5151
plugins: [typescript(), terser(terserConfig)],
5252
},
53+
{
54+
input: "src/wrappers/index.ts",
55+
output: {
56+
file: "dist/wrappers.js",
57+
format: "cjs",
58+
},
59+
plugins: [typescript()],
60+
},
5361
];

serverwrapper.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/wrappers/index.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { execSync } from "child_process";
2+
import path from "path";
3+
import WebSocket from "ws";
4+
import axios from "axios";
5+
6+
const GAMESERVER = "d1";
7+
const YAREDOMAIN = "yare.io";
8+
9+
interface userAccount {
10+
user_id: string;
11+
session_id: string;
12+
}
13+
14+
async function verifySession(acc: userAccount): Promise<boolean> {
15+
let failed = false;
16+
let req = await axios
17+
.post(`https://yare.io/session`, {
18+
user_id: acc.user_id,
19+
session_id: acc.session_id,
20+
})
21+
.catch((e) => {
22+
failed = true;
23+
});
24+
if (failed) return false;
25+
return req?.data?.username === acc.user_id;
26+
}
27+
28+
async function getGames(userID: string): Promise<string[]> {
29+
let req = await axios.get(`https://${YAREDOMAIN}/active-games/${userID}`);
30+
if (req.data.data === "no active games") {
31+
return [];
32+
} else {
33+
return req.data.data;
34+
}
35+
}
36+
37+
async function sendCode(
38+
code: string,
39+
game: string | string[],
40+
acc: userAccount
41+
): Promise<boolean> {
42+
// If its multiple games, run them individually
43+
if (Array.isArray(game)) {
44+
let stillworking = true;
45+
let promises: Promise<boolean | void>[] = [];
46+
for (let g of game) {
47+
promises.push(
48+
sendCode(code, g, acc).then((result) => {
49+
if (!result) stillworking = false;
50+
})
51+
);
52+
}
53+
await Promise.all(promises);
54+
// If all were successful then return true
55+
return stillworking;
56+
}
57+
58+
if (!(await verifySession(acc))) {
59+
return false;
60+
}
61+
let ws = new WebSocket(`wss://${YAREDOMAIN}/${GAMESERVER}/${game}`, {
62+
headers: {
63+
"User-Agent": "yare-sync (https://github.com/swz-gh/yare-sync)",
64+
},
65+
});
66+
67+
return new Promise((resolve) => {
68+
// @ts-ignore
69+
ws.on("open", (event: any) => {
70+
ws.send(
71+
JSON.stringify({
72+
u_code: code,
73+
u_id: acc.user_id,
74+
session_id: acc.session_id,
75+
})
76+
);
77+
ws.close();
78+
resolve(true);
79+
});
80+
// @ts-ignore
81+
ws.on("error", (e) => {
82+
resolve(false);
83+
});
84+
});
85+
}
86+
87+
async function login(username: string, password: string): Promise<userAccount> {
88+
let req = await axios
89+
.post(`https://${YAREDOMAIN}/validate`, {
90+
user_name: username ?? "INVALID_USER",
91+
password,
92+
})
93+
.catch((e) => {
94+
throw Error("Couldn't log in");
95+
});
96+
97+
if (req.data?.user_id === username) {
98+
return {
99+
user_id: req.data.user_id,
100+
session_id: req.data.data,
101+
};
102+
} else {
103+
throw Error("Couldn't log in");
104+
}
105+
}
106+
107+
/** @deprecated since version 2.0 */
108+
async function updateCode(code: string) {
109+
return new Promise((res, rej) => {
110+
let fpath = path.join(path.dirname(__filename), "../dist/server.js ");
111+
let resp = execSync(
112+
`node ${fpath} ${Buffer.from(code, "utf-8").toString("base64")}`
113+
).toString();
114+
if (resp.includes("Success!")) {
115+
res(true);
116+
} else {
117+
rej();
118+
}
119+
});
120+
}
121+
122+
module.exports = { updateCode, login, getGames, sendCode, verifySession };

0 commit comments

Comments
 (0)