You've already forked img-proxy-url-generator
Initial working commit
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.idea/
|
||||||
|
main
|
||||||
|
imgproxy
|
||||||
|
*.cfg
|
||||||
|
img-proxy
|
||||||
|
dist/
|
||||||
104
README.md
Normal file
104
README.md
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
# img-proxy url generator
|
||||||
|
|
||||||
|
img proxy [docs](https://docs.imgproxy.net/usage/processing)
|
||||||
|
|
||||||
|
## build
|
||||||
|
|
||||||
|
you can optionally hard code a path prefix on build
|
||||||
|
```shell
|
||||||
|
go build --ldflags="-X 'git.s.int/rrise/imgproxy/generator.PathPrefix=s3://mybucket'"
|
||||||
|
```
|
||||||
|
|
||||||
|
## config file
|
||||||
|
|
||||||
|
###### params
|
||||||
|
|
||||||
|
[img-proxy]
|
||||||
|
- `key` sha256 hmac key
|
||||||
|
- `salt` sha256 hmac salt
|
||||||
|
- `host` img-proxy server hostname
|
||||||
|
- `encryption-key` aes encryption key
|
||||||
|
- `plain-url` send plain filename. bypasses all encoding
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[img-proxy]
|
||||||
|
key=2c...47
|
||||||
|
salt=27...27
|
||||||
|
host=https://i.fooo.com
|
||||||
|
encryption-key=1c...0b
|
||||||
|
plain-url=1
|
||||||
|
```
|
||||||
|
|
||||||
|
## usage
|
||||||
|
|
||||||
|
generate a plain url with an insecure signature
|
||||||
|
```ini
|
||||||
|
[img-proxy]
|
||||||
|
host=https://i.fooo.com
|
||||||
|
plain-url=1
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png"
|
||||||
|
|
||||||
|
https://i.fooo.com/insecure/raw:1/plain/local:///my-super-image.png
|
||||||
|
```
|
||||||
|
|
||||||
|
generate a plain url with a signature
|
||||||
|
```ini
|
||||||
|
[img-proxy]
|
||||||
|
host=https://i.fooo.com
|
||||||
|
key=23...a4
|
||||||
|
salt=3f...4a
|
||||||
|
plain-url=1
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png"
|
||||||
|
|
||||||
|
https://i.fooo.com/g0ociV0BWaK8JPXtkNsGxdK8IFHbVYczrcmiiv5T9pk/raw:1/plain/local://my-super-image.png
|
||||||
|
```
|
||||||
|
|
||||||
|
generate a hmac url with a signature
|
||||||
|
```ini
|
||||||
|
[img-proxy]
|
||||||
|
host=https://i.fooo.com
|
||||||
|
key=23...a4
|
||||||
|
salt=3f...4a
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png"
|
||||||
|
|
||||||
|
https://i.fooo.com/NIColt6GBjtbquJXAtEMMsptARPw0CdeduEcu-S9Voc/raw:1/bG9jYWw6Ly9teS1zdXBlci1pbWFnZS5wbmc
|
||||||
|
```
|
||||||
|
|
||||||
|
generate an encrypted url
|
||||||
|
```ini
|
||||||
|
[img-proxy]
|
||||||
|
host=https://i.fooo.com
|
||||||
|
key=23...a4
|
||||||
|
salt=3f...4a
|
||||||
|
encryption-key=1c...0b
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png"
|
||||||
|
|
||||||
|
https://i.fooo.com/m3YtaMSgL86qCnfKCnS2i9_vLRmJSogdBx1o86cWbuc/raw:1/enc/F6FAWktv2SAFe5UQwMme0pB6JwKQJVtTI_6Xx-PUfKANdQk0pD1I13NPnv0CvkFT
|
||||||
|
```
|
||||||
|
|
||||||
|
generate a url with params
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png" -p h:200 -p rot:90
|
||||||
|
|
||||||
|
https://i.fooo.com/q-CfgLiuHTXDiZg7vBsUbZB3nkhzfsPgNrK0x20b878/h:200/rot:90/sm:1/enc/DrSKPtr8JkWx_Bf-vuxDTXRXfhrkZKTlPoQE61BzMfG2Mj1mD0qnthPq_Sfk8giv
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
generate a url a format
|
||||||
|
```bash
|
||||||
|
./imgproxy --image "local:///my-super-image.png" --format bmp -p h:200
|
||||||
|
|
||||||
|
https://i.fooo.com/UMkz4OUNw6P9ShLdewuvW3ValMgCt263vZzU5gN57WQ/h:200/sm:1/enc/ECYxMeVBTjRxB7F-jdQ7W_-Fnv4YbmSJIKie-Hdtxd9vsmEKjU1YuWVSzdN97Mod.bmp
|
||||||
|
```
|
||||||
13
build.sh
Executable file
13
build.sh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
go install
|
||||||
|
|
||||||
|
for distro in $(go tool dist list)
|
||||||
|
do
|
||||||
|
arrIN=(${distro//\// })
|
||||||
|
|
||||||
|
if [[ ${arrIN[0]} == 'linux' || ${arrIN[0]} == 'darwin' || ${arrIN[0]} == 'freebsd' || ${arrIN[0]} == 'windows' ]]; then
|
||||||
|
echo "Building $distro..."
|
||||||
|
GOOS=${arrIN[0]} GOARCH=${arrIN[1]} go build --ldflags="-X 'main.Version=$(git describe --tags --abbrev=0)'" -o dist/img_proxy_${arrIN[0]}_${arrIN[1]}
|
||||||
|
fi
|
||||||
|
done
|
||||||
41
generator/format.go
Normal file
41
generator/format.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package generator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Format string
|
||||||
|
|
||||||
|
const (
|
||||||
|
JPG Format = "jpg"
|
||||||
|
PNG Format = "png"
|
||||||
|
BMP Format = "bmp"
|
||||||
|
WEBP Format = "webp"
|
||||||
|
GIF Format = "gif"
|
||||||
|
ICO Format = "ico"
|
||||||
|
DEF Format = "default"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (g *Generator) StringToFormat(string string) (Format, error) {
|
||||||
|
switch strings.ToLower(string) {
|
||||||
|
case "jpg":
|
||||||
|
return JPG, nil
|
||||||
|
case "jpeg":
|
||||||
|
return JPG, nil
|
||||||
|
case "png":
|
||||||
|
return PNG, nil
|
||||||
|
case "bmp":
|
||||||
|
return BMP, nil
|
||||||
|
case "webp":
|
||||||
|
return WEBP, nil
|
||||||
|
case "gif":
|
||||||
|
return GIF, nil
|
||||||
|
case "ico":
|
||||||
|
return ICO, nil
|
||||||
|
case "":
|
||||||
|
return DEF, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("%s is not a valid format", string)
|
||||||
|
}
|
||||||
142
generator/url.go
Normal file
142
generator/url.go
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
package generator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"git.s.int/rrise/imgproxy/printer"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Generator struct {
|
||||||
|
config Config
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Salt []byte
|
||||||
|
saltBin []byte
|
||||||
|
Key []byte
|
||||||
|
keyBin []byte
|
||||||
|
Host string
|
||||||
|
EncryptionKey *string
|
||||||
|
encryptionKeyBin []byte
|
||||||
|
PlainUrl bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var PathPrefix string
|
||||||
|
|
||||||
|
func NewGenerator(config Config) (*Generator, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
gen := new(Generator)
|
||||||
|
gen.config = config
|
||||||
|
|
||||||
|
if gen.config.keyBin, err = hex.DecodeString(string(gen.config.Key)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if gen.config.saltBin, err = hex.DecodeString(string(gen.config.Salt)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if gen.config.EncryptionKey != nil && *gen.config.EncryptionKey != "" {
|
||||||
|
if gen.config.encryptionKeyBin, err = hex.DecodeString(*gen.config.EncryptionKey); err != nil {
|
||||||
|
return nil, fmt.Errorf("Key expected to be hex-encoded string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gen, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) generatePlainUrl(file []byte) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) generateBase64Url(file []byte) string {
|
||||||
|
return base64.RawURLEncoding.EncodeToString(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pkcs7pad(data []byte, blockSize int) []byte {
|
||||||
|
padLen := blockSize - len(data)%blockSize
|
||||||
|
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
|
||||||
|
return append(data, padding...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) generateBaseAesEncUrl(file []byte) (string, error) {
|
||||||
|
c, err := aes.NewCipher(g.config.encryptionKeyBin)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := pkcs7pad(file, aes.BlockSize)
|
||||||
|
|
||||||
|
ciphertext := make([]byte, aes.BlockSize+len(data))
|
||||||
|
iv := ciphertext[:aes.BlockSize]
|
||||||
|
|
||||||
|
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := cipher.NewCBCEncrypter(c, iv)
|
||||||
|
mode.CryptBlocks(ciphertext[aes.BlockSize:], data)
|
||||||
|
|
||||||
|
encryptedURL := g.generateBase64Url(ciphertext)
|
||||||
|
|
||||||
|
return "enc/" + encryptedURL, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Generator) GenerateUrl(file string, params []string, format Format) (string, error) {
|
||||||
|
if params == nil || len(params) == 0 || params[0] == "" {
|
||||||
|
params = []string{"raw:1"}
|
||||||
|
} else {
|
||||||
|
params = append(params, "sm:1")
|
||||||
|
}
|
||||||
|
|
||||||
|
if PathPrefix != "" {
|
||||||
|
file = PathPrefix + file
|
||||||
|
}
|
||||||
|
|
||||||
|
paramString := "/" + strings.Join(params, "/") + "/"
|
||||||
|
|
||||||
|
var url string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if g.config.PlainUrl {
|
||||||
|
url = "plain/" + file
|
||||||
|
} else if g.config.encryptionKeyBin != nil {
|
||||||
|
url, err = g.generateBaseAesEncUrl([]byte(file))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
url = g.generateBase64Url([]byte(file))
|
||||||
|
}
|
||||||
|
|
||||||
|
path := fmt.Sprintf("%s%s", paramString, url)
|
||||||
|
|
||||||
|
if format != DEF {
|
||||||
|
path = path + "." + string(format)
|
||||||
|
}
|
||||||
|
|
||||||
|
var signature string
|
||||||
|
if len(g.config.keyBin) == 0 || len(g.config.saltBin) == 0 {
|
||||||
|
signature = "insecure"
|
||||||
|
|
||||||
|
printer.NewPrinter().LogWarning("Insecure url generated. Provide salt and key to sign and secure url.")
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mac := hmac.New(sha256.New, g.config.keyBin)
|
||||||
|
mac.Write(g.config.saltBin)
|
||||||
|
mac.Write([]byte(path))
|
||||||
|
signature = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s/%s%s", g.config.Host, signature, path), nil
|
||||||
|
}
|
||||||
23
go.mod
Normal file
23
go.mod
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
module git.s.int/rrise/imgproxy
|
||||||
|
|
||||||
|
go 1.21.1
|
||||||
|
|
||||||
|
require github.com/bigkevmcd/go-configparser v0.0.0-20230427073640-c6b631f70126
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/aws/aws-sdk-go v1.49.16 // indirect
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
github.com/charmbracelet/lipgloss v0.10.0 // indirect
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||||
|
github.com/muesli/reflow v0.3.0 // indirect
|
||||||
|
github.com/muesli/termenv v0.15.2 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
|
github.com/urfave/cli/v2 v2.27.1 // indirect
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||||
|
golang.org/x/sys v0.12.0 // indirect
|
||||||
|
)
|
||||||
48
go.sum
Normal file
48
go.sum
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
github.com/aws/aws-sdk-go v1.49.16 h1:KAQwhLg296hfffRdh+itA9p7Nx/3cXS/qOa3uF9ssig=
|
||||||
|
github.com/aws/aws-sdk-go v1.49.16/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
|
github.com/bigkevmcd/go-configparser v0.0.0-20230427073640-c6b631f70126 h1:uru++pUKoS/yYU3Ohq9VItZdK/cT7FFJH/UUjOlxc+s=
|
||||||
|
github.com/bigkevmcd/go-configparser v0.0.0-20230427073640-c6b631f70126/go.mod h1:zqqfbfnDeSdRs1WihmMjSbhb2Ptw8Jbus831xoqiIec=
|
||||||
|
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
|
||||||
|
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||||
|
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||||
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||||
|
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
|
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||||
|
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||||
|
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||||
|
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
|
||||||
|
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
|
||||||
|
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||||
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
154
main.go
Normal file
154
main.go
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.s.int/rrise/imgproxy/generator"
|
||||||
|
"git.s.int/rrise/imgproxy/printer"
|
||||||
|
"github.com/bigkevmcd/go-configparser"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var keyBin, saltBin []byte
|
||||||
|
|
||||||
|
var imgGenerator *generator.Generator
|
||||||
|
|
||||||
|
var Version = "v0.0.0"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
pr := printer.NewPrinter()
|
||||||
|
|
||||||
|
app := &cli.App{
|
||||||
|
Name: "imgproxy",
|
||||||
|
Description: "URL Generator for the img proxy service",
|
||||||
|
DefaultCommand: "generate",
|
||||||
|
Version: Version,
|
||||||
|
Commands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "generate",
|
||||||
|
Usage: "Generate an image from a URL",
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
return run(c, pr)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "image",
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "config",
|
||||||
|
Aliases: []string{"c"},
|
||||||
|
Usage: "Config file to load from",
|
||||||
|
DefaultText: "imgproxy.cfg",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "format",
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Usage: "Convert the image to the specified format",
|
||||||
|
},
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "params",
|
||||||
|
Aliases: []string{"p"},
|
||||||
|
Usage: "Processing options to be passed to the generator ref: https://docs.imgproxy.net/usage/processing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
return run(c, pr)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run(os.Args)
|
||||||
|
if err != nil {
|
||||||
|
pr.LogError(err.Error())
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(c *cli.Context, p *printer.Printer) error {
|
||||||
|
err := initGenerator(c.String("config"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
url, err := signURL(c.String("image"), c.StringSlice("params"), c.String("format"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.LogInfo("Url Generated...")
|
||||||
|
|
||||||
|
println(url)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initGenerator(config string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if config == "" {
|
||||||
|
config = "imgproxy.cfg"
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := configparser.NewConfigParserFromFile(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.HasSection("img-proxy") {
|
||||||
|
return fmt.Errorf("config error - [img-proxy] config required")
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err = p.Get("img-proxy", "key")
|
||||||
|
if config != "" {
|
||||||
|
keyBin = []byte(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err = p.Get("img-proxy", "salt")
|
||||||
|
saltBin = []byte(config)
|
||||||
|
|
||||||
|
hostConf, err := p.Get("img-proxy", "host")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
plainConfig, err := p.Get("img-proxy", "plain-url")
|
||||||
|
|
||||||
|
encKey, err := p.Get("img-proxy", "encryption-key")
|
||||||
|
|
||||||
|
generatorConfig := generator.Config{
|
||||||
|
Salt: saltBin,
|
||||||
|
Key: keyBin,
|
||||||
|
Host: hostConf,
|
||||||
|
EncryptionKey: &encKey,
|
||||||
|
PlainUrl: plainConfig != "",
|
||||||
|
}
|
||||||
|
|
||||||
|
imgGenerator, err = generator.NewGenerator(generatorConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func signURL(file string, params []string, formatS string) (string, error) {
|
||||||
|
format, err := imgGenerator.StringToFormat(formatS)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
url, err := imgGenerator.GenerateUrl(file, params, format)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
19
printer/log.go
Normal file
19
printer/log.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package printer
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func (p *Printer) LogSuccess(message string) {
|
||||||
|
fmt.Println(p.getSuccess().Render("✅ " + message))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Printer) LogWarning(message string) {
|
||||||
|
fmt.Println(p.getWarning().Render("‼️ WARNING: " + message))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Printer) LogInfo(message string) {
|
||||||
|
fmt.Println(p.getInfo().Render("ℹ️ " + message))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Printer) LogError(message string) {
|
||||||
|
fmt.Println(p.getError().Render("❌ ERROR: " + message))
|
||||||
|
}
|
||||||
19
printer/printer.go
Normal file
19
printer/printer.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package printer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Version = "0.0.0"
|
||||||
|
|
||||||
|
type Printer struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPrinter() *Printer {
|
||||||
|
return &Printer{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Printer) PrintTitle() {
|
||||||
|
fmt.Println(p.getBright().Render(fmt.Sprintf("RSA File Encryption Tool %s", Version)))
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
56
printer/styles.go
Normal file
56
printer/styles.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package printer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (*Printer) getBright() lipgloss.Style {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color("#EDEEEDFF")).
|
||||||
|
Background(lipgloss.Color("#424E46FF")).
|
||||||
|
MarginTop(1).
|
||||||
|
PaddingLeft(3).
|
||||||
|
PaddingRight(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Printer) getSuccess() lipgloss.Style {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color("#00FF00")).
|
||||||
|
MarginTop(1).
|
||||||
|
MarginBottom(2).
|
||||||
|
PaddingLeft(2).
|
||||||
|
Width(120)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Printer) getError() lipgloss.Style {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color("#FF0000")).
|
||||||
|
PaddingLeft(2).
|
||||||
|
MarginTop(1).
|
||||||
|
MarginBottom(2).
|
||||||
|
Width(120)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Printer) getWarning() lipgloss.Style {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color("#000")).
|
||||||
|
Background(lipgloss.Color("#aeff00")).
|
||||||
|
PaddingLeft(2).
|
||||||
|
Width(120).
|
||||||
|
MarginTop(1).
|
||||||
|
PaddingTop(1).
|
||||||
|
PaddingBottom(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Printer) getInfo() lipgloss.Style {
|
||||||
|
return lipgloss.NewStyle().
|
||||||
|
Foreground(lipgloss.Color("#4E82B7FF")).
|
||||||
|
PaddingLeft(2).
|
||||||
|
Width(120).
|
||||||
|
PaddingTop(1).
|
||||||
|
PaddingBottom(1)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user