Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisf committed Feb 10, 2020
0 parents commit 0bce2cb
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .config.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yourauthstring
https://yourdomain.com/reverseproxylocation/
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Curtis Fowler

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.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SimpleZWS

SimpleZWS is a *simple* image uploader and URL "hider" for personal use. Many large chat platforms will clean ZWS characters from a URL, but it works on Discord, where I am using this.

## Installation

Clone the repo

```bash
git clone https://github.com/caf203/simplezws && cd simplezws
```

Populate your config (with your editor of choice)

```bash
mv .config.example .config
```

Create the image directory

```bash
mkdir images
```

## Usage

Use your tool of choice to POST to the base URL you provided in your config. This is how I do:

```bash
maim -s --format=png /dev/stdout | curl -H "Authorization: yourauthstring" -F 'data=@-' https://curtisf.dev/u | xclip -selection clipboard
```

The response returned will be your base URL + the ZWS characters following it.

## License
[MIT](https://choosealicense.com/licenses/mit/)
154 changes: 154 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"fmt"
"log"
"net/http"
"io/ioutil"
"io"
"os"
"strings"
"strconv"
)

var authString = ""
var baseURL = ""

func stringToBin(s string) (binString string) {
for _, c := range s {
binString = fmt.Sprintf("%s%.8b",binString, c)
}
return
}

func binaryToZWS(s string) (zwsString string) {
for _, c := range s {
if string(c) == "1" {
zwsString = zwsString + "\u200d" // zero-width joiner space
} else if (string(c) == "0") {
zwsString = zwsString + "\u200c" // zero-width non-joiner space
}
}
return
}

func ChunkString(s string, chunkSize int) []string {
var chunks []string
runes := []rune(s)

if len(runes) == 0 {
return []string{s}
}

for i := 0; i < len(runes); i += chunkSize {
nn := i + chunkSize
if nn > len(runes) {
nn = len(runes)
}
chunks = append(chunks, string(runes[i:nn]))
}
return chunks
}

func zwsToPath(zws string) (path string) {
var binaryString = ""
for _, c := range zws {
if string(c) == "\u200d" { // zero-width joiner space
binaryString = binaryString + "1"
} else if (string(c) == "\u200c") {// zero-width non-joiner space
binaryString = binaryString + "0"
}
}
var chunks = ChunkString(binaryString, 8)
for _, c := range chunks {
var decConv, err = strconv.ParseInt(c, 2, 64)
if err != nil {
panic(err)
}
path += string(decConv)
}
return
}

func EntryPoint(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
var zwsStr = strings.Replace(r.URL.Path, "/", "", 1)
var decodedPath = zwsToPath(zwsStr)
fmt.Printf("Serving: %s\n", decodedPath)
image, err := os.Open(decodedPath)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "image/png")
io.Copy(w, image)
case http.MethodPost:
w.Header().Set("Content-Type", "application/text")
if r.Header.Get("Authorization") == "" {
log.Println("Missing authorization header")
http.Error(w, "Missing authorization header.", http.StatusUnauthorized)
return
}
if r.Header.Get("Authorization") != authString {
log.Println("Bad token: " + r.Header.Get("Authorization"))
http.Error(w, "Invalid token!", http.StatusForbidden)
return
}
r.ParseMultipartForm(10 << 20) // Accept up to 10mb
file, handler, err := r.FormFile("data")

if err != nil {
fmt.Println("There was an error while getting the image from the request")
fmt.Println(err)
http.Error(w, "Missing image named data!", http.StatusBadRequest)
return
}
defer file.Close()

fmt.Printf("Uploaded File: %+v\n", handler.Filename)
fmt.Printf("File Size: %+v\n", handler.Size)

tempFile, err := ioutil.TempFile("images", "upload-*.png")
fmt.Printf("Saving upload as %s\n", tempFile.Name())
var zwsRep = binaryToZWS(stringToBin(tempFile.Name()))
if err != nil {
fmt.Println("Error while making temporary image file")
fmt.Println(err)
http.Error(w, "Server error", http.StatusInternalServerError)
return
}

fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("Error while reading image bytes from upload")
fmt.Println(err)
http.Error(w, "Bad image!", http.StatusBadRequest)
return
}
tempFile.Write(fileBytes)
fmt.Fprintf(w, "%s%s", baseURL, zwsRep) // return just a string for easy xclip copying
}
}

func main() {
authData, err := ioutil.ReadFile(".config")
if err != nil {
log.Println("Could not open .auth file, exiting!")
panic(err)
}
lines := strings.Split(string(authData), "\n")
if len(lines) != 2 {
fmt.Printf("Invalid config - first line must be the authorization string and second must be the base URL. Found %d lines\n", len(lines))
os.Exit(1)
}
authString = strings.TrimSuffix(lines[0], "\n")
baseURL = strings.TrimSuffix(lines[1], "\n")
if baseURL == "" || authString == "" {
log.Println("Invalid config - first line must be the authorization string and second must be the base URL")
os.Exit(1)
}
log.Println("Populated authentication information - content will be served at " + baseURL)
log.Println("Serving on port 8080")
http.HandleFunc("/", EntryPoint)
log.Fatal(http.ListenAndServe(":8080", nil))
}

0 comments on commit 0bce2cb

Please sign in to comment.