Code re-organization
Some checks failed
Go Tests / build (1.22.x) (push) Failing after 1m27s

This commit is contained in:
2025-01-26 18:20:45 -05:00
parent b93d1c29b8
commit 85938a2def
21 changed files with 685 additions and 348 deletions

View File

@@ -6,30 +6,33 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/siteworxpro/img-proxy-url-generator/config"
)
type Config struct {
AwsKey string
AwsSecret string
AwsRole string
Bucket string
}
type Service struct {
s3 *s3.S3
bucket string
}
func NewClient(config *Config) *Service {
func NewClient(config *config.Config) *Service {
var accessCredentials *credentials.Credentials
staticCredentials := credentials.NewStaticCredentials(config.Aws.AwsKey, config.Aws.AwsSecret, config.Aws.AwsToken)
awsSession := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(config.AwsKey, config.AwsSecret, ""),
Credentials: staticCredentials,
Region: aws.String("us-east-1"),
}))
assumeRoleCredentials := stscreds.NewCredentials(awsSession, config.AwsRole)
if config.Aws.AwsRole != "" {
assumeRoleCredentials := stscreds.NewCredentials(awsSession, config.Aws.AwsRole)
accessCredentials = assumeRoleCredentials
} else {
accessCredentials = staticCredentials
}
return &Service{
s3: s3.New(awsSession, &aws.Config{Credentials: assumeRoleCredentials}),
bucket: config.Bucket,
s3: s3.New(awsSession, &aws.Config{Credentials: accessCredentials}),
bucket: config.Aws.AwsBucket,
}
}

View File

@@ -37,6 +37,10 @@ func (s *Service) ListBucketContents(continuationToken *string) (*BucketList, er
}
for _, item := range v2.Contents {
if *item.Size == 0 {
continue
}
image := Image{
Name: *item.Key,
S3Path: "s3://" + s.bucket + "/" + *item.Key,

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
go install
go mod tidy
for distro in $(go tool dist list)
do

43
commands/decrypt.go Normal file
View File

@@ -0,0 +1,43 @@
package commands
import (
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"github.com/siteworxpro/img-proxy-url-generator/printer"
"github.com/urfave/cli/v2"
)
func DecryptCommand() *cli.Command {
return &cli.Command{
Name: "decrypt",
Usage: "decrypt an image url contents",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Required: true,
},
},
Action: func(c *cli.Context) error {
pr := printer.NewPrinter()
cfg, err := config.NewConfig(c.String("config"))
if err != nil {
return err
}
ig, err := generator.NewGenerator(cfg)
if err != nil {
return err
}
plain, err := ig.Decrypt(c.String("url"))
if err != nil {
return err
}
pr.LogSuccess(plain)
return nil
},
}
}

79
commands/generate.go Normal file
View File

@@ -0,0 +1,79 @@
package commands
import (
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"github.com/siteworxpro/img-proxy-url-generator/printer"
"github.com/urfave/cli/v2"
)
func GenerateCommand() *cli.Command {
return &cli.Command{
Name: "generate",
Usage: "Generate an image from a URL",
Action: runGenerate,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "image",
Aliases: []string{"i"},
Required: true,
},
&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",
},
},
}
}
func runGenerate(c *cli.Context) error {
p := printer.NewPrinter()
_, err := config.NewConfig(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 signURL(file string, params []string, formatS string) (string, error) {
cfg := config.GetConfig()
if cfg == nil {
return "", fmt.Errorf("config not loaded")
}
ig, err := generator.NewGenerator(cfg)
if err != nil {
return "", err
}
format, err := ig.StringToFormat(formatS)
if err != nil {
return "", err
}
url, err := ig.GenerateUrl(file, params, format)
if err != nil {
return "", err
}
return url, nil
}

53
commands/grpc.go Normal file
View File

@@ -0,0 +1,53 @@
package commands
import (
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/config"
proto "github.com/siteworxpro/img-proxy-url-generator/grpc"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"log"
"net"
)
func GrpcCommand() *cli.Command {
return &cli.Command{
Name: "grpc",
Usage: "Start a grpc service",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "Port to listen on",
Required: false,
Value: 9000,
},
},
Action: func(c *cli.Context) error {
cfg, err := config.NewConfig(c.String("config"))
if err != nil {
return err
}
s := grpc.NewServer()
addr := fmt.Sprintf(":%d", c.Int("port"))
println("listening on", addr)
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
svc, err := proto.NewService(cfg)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
proto.RegisterGeneratorServer(s, svc)
err = s.Serve(lis)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
return nil
},
}
}

22
commands/report.go Normal file
View File

@@ -0,0 +1,22 @@
package commands
import (
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/report"
"github.com/urfave/cli/v2"
)
func ReportCommand() *cli.Command {
return &cli.Command{
Name: "report",
Usage: "Generate usage report",
Action: func(c *cli.Context) error {
cf, err := config.NewConfig(c.String("config"))
if err != nil {
return err
}
return report.Handle(cf)
},
}
}

124
commands/server.go Normal file
View File

@@ -0,0 +1,124 @@
package commands
import (
"encoding/json"
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/aws"
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"github.com/siteworxpro/img-proxy-url-generator/printer"
"github.com/urfave/cli/v2"
"html/template"
"log"
"net/http"
"os"
"strings"
)
type jsonRequest struct {
Image string `json:"image"`
Params []string `json:"params"`
Format string `json:"format"`
}
func ServerCommand() *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start a webserver for s3 file browsing and the web service",
Action: func(c *cli.Context) error {
p := printer.NewPrinter()
return startServer(c, p)
},
}
}
func startServer(c *cli.Context, p *printer.Printer) error {
cfg, err := config.NewConfig(c.String("config"))
if err != nil {
return err
}
ig, err := generator.NewGenerator(cfg)
if err != nil {
return err
}
_, err = os.Stat("./templates")
if !os.IsNotExist(err) {
awsClient := aws.NewClient(cfg)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
contToken := r.URL.Query().Get("next")
var next *string
if contToken == "" {
next = nil
} else {
next = &contToken
}
contents, err := awsClient.ListBucketContents(next)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
for i, content := range contents.Images {
contents.Images[i].Url, _ = ig.GenerateUrl("s3://"+cfg.Aws.AwsBucket+"/"+content.Name, []string{"pr:sq"}, "")
contents.Images[i].Download, _ = ig.GenerateUrl("s3://"+cfg.Aws.AwsBucket+"/"+content.Name, []string{""}, "")
}
file, _ := os.ReadFile("./templates/index.gohtml")
tmpl := template.Must(template.New("index").Parse(string(file)))
err = tmpl.Execute(w, contents)
if err != nil {
println(err.Error())
}
})
}
http.HandleFunc("/generate", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
bodyContents := make([]byte, r.ContentLength)
_, _ = r.Body.Read(bodyContents)
jr := jsonRequest{}
err = json.Unmarshal(bodyContents, &jr)
if err != nil {
println(err.Error())
w.WriteHeader(500)
return
}
url, err := signURL(jr.Image, jr.Params, jr.Format)
if err != nil {
println(err.Error())
w.WriteHeader(500)
return
}
log.Println(fmt.Sprintf("%s - [%s] - (%s)", jr.Image, strings.Join(jr.Params, ","), url))
_, _ = w.Write([]byte(url))
})
p.LogSuccess("Starting http server on port 8080. http://localhost:8080")
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
return nil
}

24
config/aws.go Normal file
View File

@@ -0,0 +1,24 @@
package config
import "github.com/bigkevmcd/go-configparser"
type awsConfig struct {
AwsKey string
AwsSecret string
AwsToken string
AwsRegion string
AwsBucket string
AwsRole string
}
func getAwsConfig(p *configparser.ConfigParser) *awsConfig {
ac := &awsConfig{}
ac.AwsKey, _ = p.Get("aws", "key")
ac.AwsSecret, _ = p.Get("aws", "secret")
ac.AwsToken, _ = p.Get("aws", "token")
ac.AwsRegion, _ = p.Get("aws", "region")
ac.AwsBucket, _ = p.Get("aws", "bucket")
ac.AwsRole, _ = p.Get("aws", "role")
return ac
}

54
config/config.go Normal file
View File

@@ -0,0 +1,54 @@
package config
import (
"github.com/bigkevmcd/go-configparser"
"sync"
)
type Config struct {
initializeOnce sync.Once
Generator *generatorConfig
Aws *awsConfig
Redis *redisConfig
}
var c *Config
func GetConfig() *Config {
if c == nil {
return nil
}
return c
}
// NewConfig returns a new Config struct
func NewConfig(path string) (*Config, error) {
if path == "" {
path = "imgproxy.cfg"
}
p, err := configparser.NewConfigParserFromFile(path)
if err != nil {
return nil, err
}
c = &Config{}
gc, err := getGeneratorConfig(p)
if err != nil {
return nil, err
}
c.Generator = gc
if p.HasSection("aws") {
c.Aws = getAwsConfig(p)
}
if p.HasSection("redis") {
c.Redis = getRedisConfig(p)
}
return c, nil
}

43
config/generator.go Normal file
View File

@@ -0,0 +1,43 @@
package config
import (
"fmt"
"github.com/bigkevmcd/go-configparser"
)
type generatorConfig struct {
Salt []byte
Key []byte
Host string
EncryptionKey string
PlainUrl bool
}
func getGeneratorConfig(p *configparser.ConfigParser) (*generatorConfig, error) {
var config string
var err error
gc := &generatorConfig{}
if !p.HasSection("img-proxy") {
return nil, fmt.Errorf("config error - [img-proxy] config required")
}
config, _ = p.Get("img-proxy", "key")
gc.Key = []byte(config)
config, _ = p.Get("img-proxy", "salt")
gc.Salt = []byte(config)
if config, err = p.Get("img-proxy", "host"); err != nil {
return nil, err
}
gc.Host = config
config, _ = p.Get("img-proxy", "plain-url")
gc.PlainUrl = config == "true" || config == "1"
config, _ = p.Get("img-proxy", "encryption-key")
gc.EncryptionKey = config
return gc, nil
}

19
config/redis.go Normal file
View File

@@ -0,0 +1,19 @@
package config
import "github.com/bigkevmcd/go-configparser"
type redisConfig struct {
Host string
Port string
Password string
DB string
}
func getRedisConfig(p *configparser.ConfigParser) *redisConfig {
rc := &redisConfig{}
rc.Host, _ = p.Get("redis", "host")
rc.Port, _ = p.Get("redis", "port")
rc.Password, _ = p.Get("redis", "password")
rc.DB, _ = p.Get("redis", "db")
return rc
}

View File

@@ -16,7 +16,7 @@ func pkcs7pad(data []byte, blockSize int) []byte {
}
func (g *Generator) Decrypt(s string) (string, error) {
c, err := aes.NewCipher(g.config.encryptionKeyBin)
c, err := aes.NewCipher(g.encryptionKey)
if err != nil {
return "", err
}
@@ -36,7 +36,7 @@ func (g *Generator) Decrypt(s string) (string, error) {
}
func (g *Generator) generateBaseAesEncUrl(file []byte) (string, error) {
c, err := aes.NewCipher(g.config.encryptionKeyBin)
c, err := aes.NewCipher(g.encryptionKey)
if err != nil {
return "", err
}

View File

@@ -9,14 +9,14 @@ import (
func (g *Generator) generateSignature(path string) string {
var signature string
if len(g.config.keyBin) == 0 || len(g.config.saltBin) == 0 {
if len(g.keyBin) == 0 || len(g.salt) == 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 := hmac.New(sha256.New, g.keyBin)
mac.Write(g.salt)
mac.Write([]byte(path))
signature = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
}

View File

@@ -3,42 +3,33 @@ package generator
import (
"encoding/hex"
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/config"
"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
keyBin []byte
salt []byte
encryptionKey []byte
}
var PathPrefix string
func NewGenerator(config Config) (*Generator, error) {
func NewGenerator(config *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 {
if gen.keyBin, err = hex.DecodeString(string(config.Generator.Key)); err != nil {
return nil, err
}
if gen.config.saltBin, err = hex.DecodeString(string(gen.config.Salt)); err != nil {
if gen.salt, err = hex.DecodeString(string(config.Generator.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 {
if config.Generator.EncryptionKey != "" {
if gen.encryptionKey, err = hex.DecodeString(config.Generator.EncryptionKey); err != nil {
return nil, fmt.Errorf("key expected to be hex-encoded string")
}
}
@@ -62,9 +53,9 @@ func (g *Generator) GenerateUrl(file string, params []string, format Format) (st
var url string
var err error
if g.config.PlainUrl {
if config.GetConfig().Generator.PlainUrl {
url, _ = g.generatePlainUrl(file)
} else if g.config.encryptionKeyBin != nil {
} else if g.encryptionKey != nil {
url, err = g.generateBaseAesEncUrl([]byte(file))
} else {
url, _ = g.generateBase64Url([]byte(file))
@@ -82,5 +73,5 @@ func (g *Generator) GenerateUrl(file string, params []string, format Format) (st
signature := g.generateSignature(path)
return fmt.Sprintf("%s/%s%s", g.config.Host, signature, path), nil
return fmt.Sprintf("%s/%s%s", config.GetConfig().Generator.Host, signature, path), nil
}

3
go.mod
View File

@@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go v1.55.5
github.com/bigkevmcd/go-configparser v0.0.0-20240808124832-fc81059ea0bd
github.com/charmbracelet/lipgloss v1.0.0
github.com/redis/go-redis/v9 v9.7.0
github.com/urfave/cli/v2 v2.27.5
google.golang.org/grpc v1.69.2
google.golang.org/protobuf v1.36.1
@@ -13,8 +14,10 @@ require (
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/x/ansi v0.6.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // 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.20 // indirect

46
go.sum
View File

@@ -2,24 +2,38 @@ github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
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/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/bigkevmcd/go-configparser v0.0.0-20240808124832-fc81059ea0bd h1:MsTk4yo6KVYdulsDscuH4AwiZN1CyuCJAg59EWE7HPQ=
github.com/bigkevmcd/go-configparser v0.0.0-20240808124832-fc81059ea0bd/go.mod h1:vzEQfW+A1T+AMJmTIX+SXNLNECHOM7GEinHhw0IjykI=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.6.0 h1:qOznutrb93gx9oMiGf7caF7bqqubh6YIM0SWKyA08pA=
github.com/charmbracelet/x/ansi v0.6.0/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
@@ -38,6 +52,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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
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=
@@ -48,29 +64,27 @@ github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f h1:C1QccEa9kUwvMgEUORqQD9S17QesQijxjZ84sO82mfo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 h1:TqExAhdPaB60Ux47Cn0oLV07rGnxZzIsaRhQaqS666A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=
google.golang.org/grpc v1.68.0/go.mod h1:fmSPC5AsjSBCK54MyHRx48kpOti1/jRfOlwEWywNjWA=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@@ -3,6 +3,7 @@ package grpc
import (
"context"
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"log"
"strings"
@@ -13,8 +14,13 @@ type GeneratorService struct {
imgGenerator *generator.Generator
}
func NewService(imgGenerator *generator.Generator) *GeneratorService {
return &GeneratorService{imgGenerator: imgGenerator}
func NewService(config *config.Config) (*GeneratorService, error) {
g, err := generator.NewGenerator(config)
if err != nil {
return nil, err
}
return &GeneratorService{imgGenerator: g}, nil
}
func (s *GeneratorService) Generate(c context.Context, r *UrlRequest) (*UrlResponse, error) {

297
main.go
View File

@@ -1,138 +1,24 @@
package main
import (
"encoding/json"
"fmt"
"github.com/bigkevmcd/go-configparser"
"github.com/siteworxpro/img-proxy-url-generator/aws"
"github.com/siteworxpro/img-proxy-url-generator/generator"
proto "github.com/siteworxpro/img-proxy-url-generator/grpc"
cliCommands "github.com/siteworxpro/img-proxy-url-generator/commands"
"github.com/siteworxpro/img-proxy-url-generator/printer"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"html/template"
"log"
"net"
"net/http"
"os"
"strings"
)
var keyBin, saltBin []byte
var imgGenerator *generator.Generator
var Version = "v0.0.0"
var awsConfig aws.Config
type jsonRequest struct {
Image string `json:"image"`
Params []string `json:"params"`
Format string `json:"format"`
}
func main() {
pr := printer.NewPrinter()
var commands []*cli.Command
commands = append(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: "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",
},
},
})
commands = append(commands, &cli.Command{
Name: "server",
Usage: "Start a webserver for s3 file browsing and the web service",
Action: func(c *cli.Context) error {
return startServer(c, pr)
},
})
commands = append(commands, &cli.Command{
Name: "grpc",
Usage: "Start a grpc service",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "Port to listen on",
Required: false,
Value: 9000,
},
},
Action: func(c *cli.Context) error {
err := initGenerator(c.String("config"))
if err != nil {
return err
}
s := grpc.NewServer()
addr := fmt.Sprintf(":%d", c.Int("port"))
println("listening on", addr)
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
proto.RegisterGeneratorServer(s, proto.NewService(imgGenerator))
err = s.Serve(lis)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
return nil
},
})
commands = append(commands, &cli.Command{
Name: "decrypt",
Usage: "decrypt an image url contents",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Required: true,
},
},
Action: func(c *cli.Context) error {
err := initGenerator(c.String("config"))
if err != nil {
return err
}
plain, err := imgGenerator.Decrypt(c.String("url"))
if err != nil {
return err
}
pr.LogSuccess(plain)
return nil
},
})
commands = append(commands, cliCommands.GenerateCommand())
commands = append(commands, cliCommands.ServerCommand())
commands = append(commands, cliCommands.ReportCommand())
commands = append(commands, cliCommands.GrpcCommand())
commands = append(commands, cliCommands.DecryptCommand())
app := &cli.App{
Name: "img-proxy-url-generator",
@@ -140,9 +26,6 @@ func main() {
DefaultCommand: "generate",
Version: Version,
Commands: commands,
Action: func(c *cli.Context) error {
return run(c, pr)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
@@ -160,171 +43,3 @@ func main() {
os.Exit(1)
}
}
func startServer(c *cli.Context, p *printer.Printer) error {
err := initGenerator(c.String("config"))
if err != nil {
return err
}
_, err = os.Stat("./templates")
if !os.IsNotExist(err) {
awsClient := aws.NewClient(&awsConfig)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
contToken := r.URL.Query().Get("next")
var next *string
if contToken == "" {
next = nil
} else {
next = &contToken
}
contents, err := awsClient.ListBucketContents(next)
if err != nil {
return
}
for i, content := range contents.Images {
contents.Images[i].Url, _ = signURL("s3://"+awsConfig.Bucket+"/"+content.Name, []string{"pr:sq"}, "")
contents.Images[i].Download, _ = signURL("s3://"+awsConfig.Bucket+"/"+content.Name, []string{""}, "")
}
file, _ := os.ReadFile("./templates/index.gohtml")
tmpl := template.Must(template.New("index").Parse(string(file)))
err = tmpl.Execute(w, contents)
if err != nil {
println(err.Error())
}
})
}
http.HandleFunc("/generate", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
return
}
bodyContents := make([]byte, r.ContentLength)
_, _ = r.Body.Read(bodyContents)
jr := jsonRequest{}
err = json.Unmarshal(bodyContents, &jr)
if err != nil {
println(err.Error())
w.WriteHeader(500)
return
}
url, err := signURL(jr.Image, jr.Params, jr.Format)
if err != nil {
println(err.Error())
w.WriteHeader(500)
return
}
log.Println(fmt.Sprintf("%s - [%s] - (%s)", jr.Image, strings.Join(jr.Params, ","), url))
_, _ = w.Write([]byte(url))
})
p.LogSuccess("Starting http server on port 8080. http://localhost:8080")
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
return nil
}
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
}
if p.HasSection("aws") {
awsConfig.AwsSecret, _ = p.Get("aws", "secret")
awsConfig.AwsKey, _ = p.Get("aws", "key")
awsConfig.AwsRole, _ = p.Get("aws", "role")
awsConfig.Bucket, _ = p.Get("aws", "bucket")
}
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
}

65
redis/client.go Normal file
View File

@@ -0,0 +1,65 @@
package redis
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"github.com/siteworxpro/img-proxy-url-generator/config"
"strconv"
)
type Redis struct {
initialized redisStatus
client *redis.Client
}
type redisStatus uint8
const (
redisStatusUninitialized redisStatus = iota
redisStatusInitialized
)
var singleton *Redis
func New(config *config.Config) (*Redis, error) {
if singleton != nil && singleton.initialized == redisStatusUninitialized {
return singleton, nil
}
db, err := strconv.ParseInt(config.Redis.DB, 10, 64)
if err != nil {
db = 0
}
port := config.Redis.Port
if port == "" {
port = "6379"
}
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", config.Redis.Host, port),
DB: int(db),
Password: config.Redis.Password,
})
_, err = rdb.Ping(context.Background()).Result()
if err != nil {
return nil, fmt.Errorf("failed to connect to redis: %w", err)
}
singleton = &Redis{
initialized: redisStatusInitialized,
client: rdb,
}
return singleton, nil
}
func (r *Redis) GetClient() *redis.Client {
return r.client
}
func (r *Redis) Close() error {
return r.client.Close()
}

75
report/command.go Normal file
View File

@@ -0,0 +1,75 @@
package report
import (
"context"
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/siteworxpro/img-proxy-url-generator/aws"
"github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator"
"github.com/siteworxpro/img-proxy-url-generator/redis"
"sort"
"strconv"
"time"
)
const lastAccessKey = "imgproxy:%s:last_access"
const requestsKey = "imgproxy:%s:requests"
var rowStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF"))
var headerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Bold(true)
func Handle(config *config.Config) error {
a := aws.NewClient(config)
r, err := redis.New(config)
ig, err := generator.NewGenerator(config)
if err != nil {
return err
}
var continuationToken *string
list, err := a.ListBucketContents(continuationToken)
if err != nil {
return err
}
var rows [][]string
for list.StartAfter != "" {
for _, image := range list.Images {
dlUrl, err := ig.GenerateUrl(image.S3Path, []string{}, generator.DEF)
if err != nil {
return err
}
lastAccessedS, err := r.GetClient().Get(context.Background(), fmt.Sprintf(lastAccessKey, image.S3Path)).Result()
lastAccessedI, _ := strconv.ParseInt(lastAccessedS, 10, 64)
lastAccessed := time.Unix(lastAccessedI, 0)
requestsCount, err := r.GetClient().Get(context.Background(), fmt.Sprintf(requestsKey, image.S3Path)).Result()
rows = append(rows, []string{image.S3Path, requestsCount, lastAccessed.Format(time.DateTime), dlUrl})
}
continuationToken = &list.StartAfter
list, err = a.ListBucketContents(continuationToken)
}
// sort by last accessed
sort.Slice(rows, func(i, j int) bool {
return rows[i][2] > rows[j][2]
})
t := table.New().StyleFunc(func(row int, col int) lipgloss.Style {
switch {
case row == 0:
return headerStyle
default:
return rowStyle
}
}).
Headers("Image", "Times Accessed", "Last Accessed", "URL").
Rows(rows...)
fmt.Println(t)
return nil
}