Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/cmd/hightlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ import * as vscode from "vscode";
import { obfuscateScript } from "../obfuscate/callObfuscator";
import { getOutputType } from "../utils/getConfigs";

const ERR_NO_HIGHLIGHT = "Please, highlight a text before obfuscating!"
const ERR_NO_FILE_TO_HIGHLIGHT = "Please open a file before obfuscating!"

export function hightlight(): any {
const text_editor = vscode.window.activeTextEditor;
if (!text_editor) {
vscode.window.showErrorMessage("Please open a file before obfuscating!");
throw Error();
vscode.window.showErrorMessage(ERR_NO_FILE_TO_HIGHLIGHT);
return
}
const selection = text_editor.selection;

if (!selection) {
vscode.window.showErrorMessage(
"Please, highlight a text before obfuscating!"
ERR_NO_HIGHLIGHT
);
throw Error();
return
}

const selectedText = text_editor.document.getText(selection);
if (!selectedText) {
vscode.window.showErrorMessage(
"Please, highlight a text before obfuscating!"
ERR_NO_HIGHLIGHT
);
throw Error();
return
}

obfuscateScript(selectedText, function (code) {
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/obfuscate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getOutputType } from "../utils/getConfigs";
import * as vscode from "vscode";
import { ERR_NO_FILE_TO_OBFUSCATE} from "./utils";

export function obfuscateBody(code: string) {
const outputType = getOutputType();
Expand All @@ -9,7 +10,7 @@ export function obfuscateBody(code: string) {
} else if (outputType == "replace current file") {
const text_editor = vscode.window.activeTextEditor;
if (!text_editor) {
vscode.window.showErrorMessage("Please open a file before obfuscating!");
vscode.window.showErrorMessage(ERR_NO_FILE_TO_OBFUSCATE);
return;
}
// get range object for current editor
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const ERR_NO_HIGHLIGHT = "Please, highlight a text before obfuscating!"
export const ERR_NO_FILE_TO_OBFUSCATE = "Please open a file before obfuscating!"

5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { obfuscateScript } from "./obfuscate/callObfuscator";
import { hightlight } from "./cmd/hightlight";
import { helper } from "./cmd/helper";
import { obfuscateBody } from "./cmd/obfuscate";
import { ERR_NO_FILE_TO_OBFUSCATE } from "./cmd/utils";

let obfuscate = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left
Expand All @@ -16,8 +17,8 @@ export function activate(context: vscode.ExtensionContext) {
obfuscate.show();
vscode.commands.registerCommand("lua.obfuscate", function () {
if (!vscode.window.activeTextEditor) {
vscode.window.showErrorMessage("Please open a file before obfuscating!");
throw new Error("Please open a file before obfuscating!");
vscode.window.showErrorMessage(ERR_NO_FILE_TO_OBFUSCATE);
return
}
obfuscateScript(
vscode.window.activeTextEditor.document.getText(),
Expand Down
88 changes: 13 additions & 75 deletions src/obfuscate/callObfuscator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as vscode from "vscode";
import constants from "../utils/constants.json";
import { parseConfig } from "../utils/getConfigs";
import axios from "axios";
import { AxiosResponse } from "axios";
import { callNewScript } from "src/requests/newScript";
import { callObfuscate } from "src/requests/obfuscate";

export type Callback = (code: string) => void;

Expand All @@ -11,79 +9,19 @@ export function obfuscateScript(script: string, callback: Callback) {
callObfuscator(script, callback);
}

type Response = {code: string|null,message:string|null, sessionId: string|null}

export async function callObfuscator(
script: string,
callback: Callback
callback: Callback,
debug?: boolean
): Promise<void> {
const config = parseConfig();
console.log(constants.obfuscateUrl + "newscript")
console.log(script)
const newScript : AxiosResponse<Response> = await axios
.post(constants.obfuscateUrl + "newscript", script, {
headers: {
"Content-Type": "text/plain",
apikey: constants.apiKey,
},
})
.catch((err) => {
if (err.response.status === 502) {
vscode.window.showErrorMessage("Something went wrong with uploading, try again later!");
throw Error();
}
vscode.window.showErrorMessage(
"Failed to upload script! (Error: " + err.response.status + ")"
);
console.log(err.message);
throw Error();
});
if (newScript.status !== 200) {
vscode.window.showErrorMessage(
"Failed uploading script! (Error: " + newScript.status + ")"
);
throw Error();
}
if (!newScript.data.sessionId) {
vscode.window.showErrorMessage("Failed to upload script!");
throw Error();
}

const obfsucated : AxiosResponse<Response> = await axios
.post(constants.obfuscateUrl + "Obfuscate", config, {
headers: {
"Content-Type": "text/plain",
apikey: constants.apiKey,
sessionId: newScript.data.sessionId,
},
})
.catch((err) => {
console.trace({
err,
config,

headers: {
apikey: constants.apiKey,
sessionId: newScript.data.sessionId,
}
,}
)
if (err.response.status === 502) {
vscode.window.showErrorMessage("Something went wrong, try again later!");
throw Error();
}
vscode.window.showErrorMessage(
"Failed to obfuscate script! (Error: " + err.response.status + ")"
);
throw Error();
});
if (obfsucated.status === 404) {
vscode.window.showErrorMessage("Lua script failed to upload!");
throw Error();
}
if (!obfsucated.data || !obfsucated.data.code) {
vscode.window.showErrorMessage("Obfuscation failed!");
throw Error();
}
callback(obfsucated.data.code);
const newScript = await callNewScript(script)
if(!newScript.sessionId){
return
}
const obfsucated = await callObfuscate(newScript.sessionId, true)
if(!obfsucated || !obfsucated.code || !obfsucated.sessionId){
return
}
callback(obfsucated.code);
}
2 changes: 1 addition & 1 deletion src/obfuscate/deobfuscateErrorReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function deobfuscateErrorReport(
fileContent = await getFileContents(path, uris);
if (!fileContent) {
vscode.window.showErrorMessage("Failed opening file!");
throw new Error("Failed opening file!");
return
}

// only do if 'minified'
Expand Down
49 changes: 49 additions & 0 deletions src/requests/newScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as vscode from "vscode";
import axios, { AxiosResponse } from "axios";
import constants from "../utils/constants.json";
import { ERR_DEFAULT, failedToUpload } from "./utils";

export type Response = {
code: string|null,
message:string|null,
sessionId: string|null,
}

export type ObfuscateResponse = Response & {
debug: boolean| null
variables: Record<string,string>
}

const ENDPOINT = `${constants.obfuscateUrl}newscript`

export async function callNewScript(scriptStr : string): Promise<Response> {
const resp : AxiosResponse<Response> = await axios
.post(ENDPOINT, scriptStr, {
headers: {
"Content-Type": "text/plain",
apikey: constants.apiKey,
},
})
.catch((err) => {
if (err.response.status === 502) {
vscode.window.showErrorMessage(ERR_DEFAULT);
throw Error();
}
vscode.window.showErrorMessage(
failedToUpload(err.response.status)
);
console.log(err.message);
throw Error();
});
if (resp.status !== 200) {
vscode.window.showErrorMessage(
failedToUpload(resp.status)
);
throw Error();
}
if (!resp.data.sessionId) {
vscode.window.showErrorMessage(ERR_DEFAULT);
throw Error();
}
return resp.data
}
61 changes: 61 additions & 0 deletions src/requests/obfuscate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as vscode from "vscode";
import axios, { AxiosResponse } from "axios";
import constants from "../utils/constants.json";
import { parseConfig } from "src/utils/getConfigs";
import { ERR_DEFAULT, ERR_FAILED_OBFUSCATE, failedToObfuscate, failedToUpload } from "./utils";

export type Response = {
code: string|null,
message:string|null,
sessionId: string|null,
}

export type ObfuscateResponse = Response & {
debug: boolean| null
}

const ENDPOINT = `${constants.obfuscateUrl}Obfuscate`

export async function callObfuscate(sessionId: string, debug?: boolean): Promise<ObfuscateResponse|null> {
const config = parseConfig();
if(!config){
return null
}

const obfsucated: AxiosResponse<ObfuscateResponse>|void = await axios
.post(ENDPOINT, {...config, debug }, {
headers: {
"Content-Type": "text/plain",
apikey: constants.apiKey,
sessionId: sessionId,
},
})
.catch((err) => {
console.trace({
err,
config,
headers: {
apikey: constants.apiKey,
sessionId: sessionId,
}
,}
)
if (err.response.status === 502) {
vscode.window.showErrorMessage(ERR_DEFAULT)
}
vscode.window.showErrorMessage(
failedToObfuscate(err.response.status)
)
});

if (!obfsucated || obfsucated.status === 404) {
vscode.window.showErrorMessage(failedToUpload("Lua script failed to upload"))
return null
}
if (!obfsucated.data || !obfsucated.data.code) {
vscode.window.showErrorMessage(ERR_FAILED_OBFUSCATE).then(err=>{throw Error(err)})
return null
}
return obfsucated.data
}

6 changes: 6 additions & 0 deletions src/requests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const ERR_FAILED_UPLOAD = 'Failed to upload script!'
export const ERR_FAILED_OBFUSCATE = 'Failed to obfuscate script!'
export const ERR_DEFAULT = "Something went wrong, try again later!"

export const failedToObfuscate = (msg?:string|number) => `${ERR_FAILED_OBFUSCATE}${msg?`(Error: ${msg} )`:''}`
export const failedToUpload = (msg?:string|number) => `${ERR_FAILED_UPLOAD}${msg?`(Error: ${msg} )`:''}`
4 changes: 2 additions & 2 deletions src/utils/getConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export function getOutputType() {
| "copy to clipboard";
}

export function parseConfig(): Config {
export function parseConfig(): Config|null {
const settings = vscode.workspace.getConfiguration("lua-obfuscator");
if (!settings) {
vscode.window.showErrorMessage("Failed opening settings!");
throw new Error();
return null
}

const config: Config = {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/getFileContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ export function getFileContents(
path: string,
uris: vscode.Uri[]
): Promise<string> {
return new Promise(async (resolve) => {
return new Promise(async (resolve,reject) => {
let fileContent = null;
let openDocument = vscode.workspace.textDocuments.find(
(doc) => doc.uri.toString() === uris.toString()
);
if (openDocument) {
resolve(openDocument.getText());
return resolve(openDocument.getText());
} else {
openDocument = await vscode.workspace.openTextDocument(uris[0]);
if (!openDocument) {
vscode.window.showErrorMessage("Failed opening file!");
throw new Error("Failed opening file!");
return reject()
}
await vscode.window.showTextDocument(openDocument); // open!
fileContent = openDocument.getText();
}
if (!fileContent || fileContent == "") {
vscode.window.showErrorMessage(`Failed reading '${path}'`);
throw new Error(`Failed reading '${path}'`);
return reject()
}
resolve(fileContent);
return resolve(fileContent);
});
}
Empty file added src/utils/utils.ts
Empty file.
10 changes: 8 additions & 2 deletions src/utils/webView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// webview for Wow Lua error parsing
import fs from "fs";
import path from "path";

export function getWebviewContent() {
return fs.readFileSync(__dirname + "\\..\\webView.html", "utf8");
export function getWebviewContent():string{
try{
return fs.readFileSync(path.join(__dirname + "..","webView.html"), "utf8");
}catch(e){
// On mac it might have be lower case (macOS filesystem is not case-sensitive)
return fs.readFileSync(path.join(__dirname + "..","webview.html"), "utf8");
}
}
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.