From 4399c4e02ec253275117abfc00884bc8ebb276ba Mon Sep 17 00:00:00 2001 From: Andreas Schulte <0x0001f346@pm.me> Date: Mon, 3 Feb 2025 20:48:19 +0100 Subject: [PATCH] first commit --- .gitignore | 2 ++ LICENSE | 9 ++++++ README.md | 22 +++++++++++++ build/.gitkeep | 0 core/core.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++ core/core_test.go | 44 ++++++++++++++++++++++++++ go.mod | 3 ++ handler/get.go | 25 +++++++++++++++ handler/post.go | 52 +++++++++++++++++++++++++++++++ main.go | 42 +++++++++++++++++++++++++ 10 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build/.gitkeep create mode 100644 core/core.go create mode 100644 core/core_test.go create mode 100644 go.mod create mode 100644 handler/get.go create mode 100644 handler/post.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1464c71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/* +!/build/.gitkeep \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6bab383 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2025 Andreas Schulte + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f82bd0d --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# clickNgo + +A minimalistic implementation of **Click'n'Load 2** (as used by [JDownloader](https://jdownloader.org/knowledge/wiki/glossary/cnl2)) written in **Go**. The project is intentionally kept small, follows the **KISS principle**, and has **zero external dependencies**. + +## Installation + +To get started with clickNgo, follow these steps: + +```bash +git clone https://git.0x0001f346.de/andreas/clickNgo.git +cd clickNgo +go build -o ./build/ . +``` + +## Usage + +Once built, you can run clickNgo with the following command: +```bash +build/clickNgo +``` + +Simply start the application, click on a corresponding button in your browser, and admire the decrypted links in your terminal. \ No newline at end of file diff --git a/build/.gitkeep b/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/core/core.go b/core/core.go new file mode 100644 index 0000000..2cc9118 --- /dev/null +++ b/core/core.go @@ -0,0 +1,79 @@ +package core + +import ( + "crypto/aes" + "crypto/cipher" + "encoding/base64" + "encoding/hex" + "fmt" + "net/url" + "regexp" + "strings" + "unicode" +) + +func DecryptURLs(jk string, crypted string) ([]string, error) { + key, err := convertJKToKey(jk) + if err != nil { + return []string{}, err + } + + ciphertext, err := base64.StdEncoding.DecodeString(crypted) + if err != nil { + return []string{}, err + } + + block, err := aes.NewCipher([]byte(key)) + if err != nil { + return []string{}, err + } + + if len(ciphertext)%aes.BlockSize != 0 { + return []string{}, fmt.Errorf("ciphertext has invalid length") + } + + mode := cipher.NewCBCDecrypter(block, []byte(key)) + mode.CryptBlocks(ciphertext, ciphertext) + + return ExtractURLsFromString(fmt.Sprintf("%s", ciphertext)), nil +} + +func ExtractURLsFromString(s string) []string { + URLs := []string{} + + for _, line := range strings.Split(s, "\n") { + line = strings.TrimSpace(line) + + line = strings.Map(func(r rune) rune { + if unicode.IsPrint(r) { + return r + } + return -1 + }, line) + + parsedURL, err := url.Parse(line) + if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" { + continue + } + + URLs = append(URLs, line) + } + + return URLs +} + +func convertJKToKey(jk string) (string, error) { + re := regexp.MustCompile(`return '(\d{32})'`) + matches := re.FindStringSubmatch(jk) + + if len(matches) < 2 { + return "", fmt.Errorf("no key found for jk: %s", jk) + } + + base10key, err := hex.DecodeString(matches[1]) + if err != nil { + return "", fmt.Errorf("jk is invalid: %s", jk) + } + + return fmt.Sprintf("%s", base10key), nil +} diff --git a/core/core_test.go b/core/core_test.go new file mode 100644 index 0000000..8cf71bd --- /dev/null +++ b/core/core_test.go @@ -0,0 +1,44 @@ +package core + +import "testing" + +func TestConvertJKToKey(t *testing.T) { + type args struct { + jk string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "36323334313036343130323934373732", + args: args{ + jk: "function f(){ return '36323334313036343130323934373732';}", + }, + want: "6234106410294772", + wantErr: false, + }, + { + name: "36373434353538393131373730353833", + args: args{ + jk: "function f(){ return '36373434353538393131373730353833';}", + }, + want: "6744558911770583", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := convertJKToKey(tt.args.jk) + if (err != nil) != tt.wantErr { + t.Errorf("convertJKToKey() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("convertJKToKey() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1d8a755 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.0x0001f346.de/andreas/clickNgo + +go 1.23.5 diff --git a/handler/get.go b/handler/get.go new file mode 100644 index 0000000..1adc65d --- /dev/null +++ b/handler/get.go @@ -0,0 +1,25 @@ +package handler + +import ( + "fmt" + "net/http" +) + +func GETIndex(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + fmt.Fprintln(w, "clickNgo") +} + +func GETJdcheckJS(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "text/javascript") + fmt.Fprintln(w, "jdownloader=true; var version='17461';") +} diff --git a/handler/post.go b/handler/post.go new file mode 100644 index 0000000..2e3cb02 --- /dev/null +++ b/handler/post.go @@ -0,0 +1,52 @@ +package handler + +import ( + "fmt" + "net/http" + + "git.0x0001f346.de/andreas/clickNgo/core" +) + +func POSTFlashAdd(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + r.ParseForm() + URLs := core.ExtractURLsFromString(r.PostFormValue("urls")) + if len(URLs) == 0 { + http.Error(w, "400 Bad Request", http.StatusBadRequest) + return + } + + printURLs(URLs) + + fmt.Fprintln(w, "OK") +} + +func POSTFlashAddcrypted2(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + r.ParseForm() + URLs, err := core.DecryptURLs(r.PostFormValue("jk"), r.PostFormValue("crypted")) + if err != nil || len(URLs) == 0 { + http.Error(w, "400 Bad Request", http.StatusBadRequest) + return + } + + printURLs(URLs) + + fmt.Fprintln(w, "OK") +} + +func printURLs(URLs []string) { + for _, url := range URLs { + fmt.Println(url) + } + + fmt.Println("") +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..63f38e1 --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "git.0x0001f346.de/andreas/clickNgo/handler" +) + +func main() { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT) + + server := &http.Server{ + Addr: ":9666", + } + + go func() { + http.HandleFunc("/", handler.GETIndex) + http.HandleFunc("/jdcheck.js", handler.GETJdcheckJS) + http.HandleFunc("/flash/add", handler.POSTFlashAdd) + http.HandleFunc("/flash/addcrypted2", handler.POSTFlashAddcrypted2) + + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Printf("[Error] %v\n", err) + } + }() + + <-sigChan + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + + server.Shutdown(ctx) + + os.Exit(0) +}