Skip to content

Commit dec9022

Browse files
Merge pull request #13 from 404NotFoundIndonesia/feature/1-javascript-obfuscator
Initial JavaScript Obfuscator
2 parents ed6f338 + 735a812 commit dec9022

File tree

23 files changed

+854
-6
lines changed

23 files changed

+854
-6
lines changed

.github/workflows/go-unit-test.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Go Unit Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- develop
7+
8+
jobs:
9+
build-and-test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: 1.23.4
20+
21+
- name: Install Go Dependencies
22+
run: go mod tidy
23+
24+
- name: Install Node.js
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: 20.12.0
28+
29+
- name: Install JS Dependencies
30+
run: npm install --prefix frontend
31+
32+
- name: Build Frontend to be embedded
33+
run: npm run build --prefix frontend
34+
35+
- name: Run tests
36+
run: go test -v -cover ./...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build/bin
22
node_modules
33
frontend/dist
4+
.idea/

backend/filesystem/dialog.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package filesystem
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/wailsapp/wails/v2/pkg/runtime"
7+
"io/fs"
8+
"os/exec"
9+
"path/filepath"
10+
goRuntime "runtime"
11+
"strings"
12+
)
13+
14+
type Dialog struct {
15+
ctx context.Context
16+
}
17+
18+
func NewDialog() *Dialog {
19+
return &Dialog{}
20+
}
21+
22+
func (d *Dialog) SetContext(ctx context.Context) {
23+
d.ctx = ctx
24+
}
25+
26+
func (d *Dialog) OpenMultipleFilesDialog() ([]string, error) {
27+
return runtime.OpenMultipleFilesDialog(d.ctx, runtime.OpenDialogOptions{
28+
Title: "Select file(s)",
29+
Filters: []runtime.FileFilter{
30+
{DisplayName: "JavaScript", Pattern: "*.js"},
31+
{DisplayName: "PHP", Pattern: "*.php"},
32+
{DisplayName: "All Files", Pattern: "*"},
33+
},
34+
ShowHiddenFiles: true,
35+
})
36+
}
37+
38+
func (d *Dialog) OpenDirectoryDialog() ([]string, error) {
39+
path, err := runtime.OpenDirectoryDialog(d.ctx, runtime.OpenDialogOptions{
40+
Title: "Select folder",
41+
})
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
if path == "" {
47+
return nil, errors.New("could not open folder")
48+
}
49+
50+
var files []string
51+
err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
52+
if err != nil {
53+
return err
54+
}
55+
56+
if !d.IsDir() && (strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".php")) {
57+
files = append(files, path)
58+
}
59+
60+
return nil
61+
})
62+
return files, err
63+
}
64+
65+
func (d *Dialog) OpenFileLocation(path string) error {
66+
currOS := goRuntime.GOOS
67+
command, ok := map[string]string{
68+
"windows": "explorer",
69+
"darwin": "open",
70+
"linux": "xdg-open",
71+
}[currOS]
72+
if !ok {
73+
return errors.New("unsupported OS: " + currOS)
74+
}
75+
76+
return exec.Command(command, filepath.Dir(path)).Run()
77+
}
78+
79+
func (d *Dialog) MessageInfoDialog(title string, message string) error {
80+
_, err := runtime.MessageDialog(d.ctx, runtime.MessageDialogOptions{
81+
Title: title,
82+
Message: message,
83+
Type: runtime.InfoDialog,
84+
})
85+
86+
return err
87+
}

backend/filesystem/file_manager.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package filesystem
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
type FileManager struct {
11+
ctx context.Context
12+
}
13+
14+
func NewFileManager() *FileManager {
15+
return &FileManager{}
16+
}
17+
18+
func (f *FileManager) SetContext(ctx context.Context) {
19+
f.ctx = ctx
20+
}
21+
22+
func (f *FileManager) ReadFile(path string) (string, error) {
23+
contents, err := os.ReadFile(path)
24+
if err != nil {
25+
return "", err
26+
}
27+
28+
return string(contents), nil
29+
}
30+
31+
func (f *FileManager) WriteFile(path string, content string) error {
32+
if _, err := os.Stat(path); os.IsNotExist(err) {
33+
if err = os.WriteFile(path, []byte(content), 0644); err != nil {
34+
return err
35+
}
36+
37+
return nil
38+
}
39+
40+
extension := filepath.Ext(path)
41+
filename := path[:len(path)-len(extension)]
42+
index := 1
43+
for {
44+
path = fmt.Sprintf("%s_%d%s", filename, index, extension)
45+
if _, ok := os.Stat(path); os.IsNotExist(ok) {
46+
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
47+
return err
48+
}
49+
50+
return nil
51+
}
52+
index++
53+
}
54+
}

backend/obfuscator/config.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package obfuscator
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
type Config struct {
11+
SingleLineOutput bool `json:"single_line_output"`
12+
StringLiteral bool `json:"string_literal"`
13+
LoopStatement bool `json:"loop_statement"`
14+
IfStatement bool `json:"if_statement"`
15+
ConstantName bool `json:"constant_name"`
16+
VariableName bool `json:"variable_name"`
17+
FunctionName bool `json:"function_name"`
18+
RemoveComments bool `json:"remove_comments"`
19+
path string
20+
}
21+
22+
func NewConfig() Config {
23+
var config Config
24+
25+
err := config.load()
26+
if os.IsNotExist(err) {
27+
if err = config.save(); err != nil {
28+
log.Fatal(err)
29+
}
30+
} else if err != nil {
31+
log.Fatal(err)
32+
}
33+
34+
log.Println("Hyperion config:", config.path)
35+
36+
return config
37+
}
38+
39+
func (config *Config) load() error {
40+
configDir, err := os.UserConfigDir()
41+
if err != nil {
42+
return err
43+
}
44+
45+
appConfigDir := filepath.Join(configDir, "hyperion")
46+
_, err = os.Stat(appConfigDir)
47+
if os.IsNotExist(err) {
48+
if err = os.MkdirAll(appConfigDir, os.ModePerm); err != nil {
49+
return err
50+
}
51+
}
52+
53+
config.path = filepath.Join(appConfigDir, "config.json")
54+
if _, err = os.Stat(config.path); err != nil {
55+
return err
56+
}
57+
58+
data, err := os.ReadFile(config.path)
59+
if err != nil {
60+
return err
61+
}
62+
63+
if err = json.Unmarshal(data, &config); err != nil {
64+
return err
65+
}
66+
67+
return nil
68+
}
69+
70+
func (config *Config) save() error {
71+
data, err := json.MarshalIndent(config, "", " ")
72+
if err != nil {
73+
return err
74+
}
75+
76+
return os.WriteFile(config.path, data, os.ModePerm)
77+
}
78+
79+
func (config *Config) Save(c Config) {
80+
log.Println("Saving obfuscation configuration", c)
81+
}
82+
83+
func (config *Config) GetConfig() Config {
84+
return *config
85+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const constantName = 'constantName';
2+
const CONSTANT_NAME = 'CONSTANT_NAME';
3+
const constant_name = 'constant_name';
4+
const ConstantName = 'ConstantName';
5+
6+
let variableName1 = 'VARIABLE_NAME_1';
7+
var variableName2 = 'VARIABLE_NAME_2';
8+
let variableName3, variableName4;
9+
var variableName5, variableName6;
10+
let variableName7,variableName8 ,variableName9 , variableName10;
11+
12+
// REGULAR FUNCTION WITHOUT RETURN
13+
function functionName1 () {
14+
// constantName
15+
// console.log("DECEPTION: constantNameCONSTANT_NAME constant_name-ConstantName");
16+
console.log("DECEPTION: constantNameCONSTANT_NAME constant_name-ConstantName"); // Just string
17+
console.log(`DECEPTION: ${constantName} ${CONSTANT_NAME}`); // This is constant, bro
18+
console.log('DECEPTION: ${CONSTANT_NAME}'); // String
19+
console.log("DECEPTION: ${constant_name}"); // String again
20+
console.log(constantName, CONSTANT_NAME, constant_name, ConstantName);
21+
}
22+
23+
// ARROW FUNCTION WITHOUT RETURN
24+
const functionName2 = () => {
25+
// variableName6
26+
// console.log("DECEPTION: variableName1 variableName6variableName4");
27+
console.log("DECEPTION: variableName3 variableName2variableName5"); // Just string
28+
console.log(`DECEPTION: ${variableName6} ${variableName2}`); // This is variable, bro
29+
console.log('DECEPTION: ${variableName6}'); // String
30+
console.log("DECEPTION: ${variableName6}"); // String again
31+
console.log(variableName6, variableName5, variableName2, variableName3, variableName4);
32+
};
33+
34+
/**
35+
* functionName3 - regular function to sum parameters
36+
* @param params1 {int}
37+
* @param params2 {int}
38+
* @returns {int}
39+
*/
40+
function functionName3 (params1, params2) {
41+
return params1 + params2;
42+
}
43+
44+
/**
45+
* functionName4 - arrow function to sum parameters
46+
* @param params1
47+
* @param params2
48+
* @returns {*}
49+
*/
50+
const functionName4 = (params1, params2) => params1 + params2;
51+
52+
// ARROW FUNCTION WITHOUT PARENTHESES AND BRACKETS
53+
const functionName5 = params => console.log(params);
54+
55+
// REGULAR FUNCTION WITH SPREAD PARAMETER
56+
function functionName6 (...params) {
57+
for (const param of params) {
58+
console.log(param);
59+
}
60+
}
61+
62+
functionName1();
63+
functionName2();
64+
const functionName3Result = functionName3(10, 20);
65+
let functionName4Result = functionName4(20, 10);
66+
functionName5('DONE');
67+
functionName6('A', 'N', 'J', 'A', 'Y');
68+
69+
/** LAST STATEMENT **/
70+
if (functionName3Result === functionName4Result) {
71+
console.log('FUNCTION RESULT IS IDENTICAL');
72+
}
73+
74+

0 commit comments

Comments
 (0)