Skip to content

Commit

Permalink
update docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Aug 21, 2023
1 parent 38528af commit 68fb4c1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 35 deletions.
50 changes: 25 additions & 25 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -292,31 +292,31 @@ Usage:
go-camo [OPTIONS]
Application Options:
-k, --key= HMAC key
-H, --header= Add additional header to each response. This option can be used multiple times to add multiple headers
--listen= Address:Port to bind to for HTTP (default: 0.0.0.0:8080)
--ssl-listen= Address:Port to bind to for HTTPS/SSL/TLS
--socket-listen= Path for unix domain socket to bind to for HTTP
--ssl-key= ssl private key (key.pem) path
--ssl-cert= ssl cert (cert.pem) path
--max-size= Max allowed response size (KB)
--timeout= Upstream request timeout (default: 4s)
--max-redirects= Maximum number of redirects to follow (default: 3)
--metrics Enable Prometheus compatible metrics endpoint
--no-log-ts Do not add a timestamp to logging
--log-json Log in JSON format
--no-fk Disable frontend http keep-alive support
--no-bk Disable backend http keep-alive support
--allow-content-video Additionally allow 'video/*' content
--allow-content-audio Additionally allow 'audio/*' content
--allow-credential-urls Allow urls to contain user/pass credentials
--enable-extra-headers Enable support for additional headers as part of hmac signed url data
--filter-ruleset= Text file containing filtering rules (one per line)
--server-name= Value to use for the HTTP server field (default: go-camo)
--expose-server-version Include the server version in the HTTP server response header
--enable-xfwd4 Enable x-forwarded-for passthrough/generation
-v, --verbose Show verbose (debug) log level output
-V, --version Print version and exit; specify twice to show license information
-k, --key= HMAC key
-H, --header= Add additional header to each response. This option can be used multiple times to add multiple headers
--listen= Address:Port to bind to for HTTP (default: 0.0.0.0:8080)
--ssl-listen= Address:Port to bind to for HTTPS/SSL/TLS
--socket-listen= Path for unix domain socket to bind to for HTTP
--ssl-key= ssl private key (key.pem) path
--ssl-cert= ssl cert (cert.pem) path
--max-size= Max allowed response size (KB)
--timeout= Upstream request timeout (default: 4s)
--max-redirects= Maximum number of redirects to follow (default: 3)
--metrics Enable Prometheus compatible metrics endpoint
--no-log-ts Do not add a timestamp to logging
--log-json Log in JSON format
--no-fk Disable frontend http keep-alive support
--no-bk Disable backend http keep-alive support
--allow-content-video Additionally allow 'video/*' content
--allow-content-audio Additionally allow 'audio/*' content
--allow-credential-urls Allow urls to contain user/pass credentials
--enable-extra-headers Enable support for extra headers as part of hmac signed url data
--filter-ruleset= Text file containing filtering rules (one per line)
--server-name= Value to use for the HTTP server field (default: go-camo)
--expose-server-version Include the server version in the HTTP server response header
--enable-xfwd4 Enable x-forwarded-for passthrough/generation
-v, --verbose Show verbose (debug) log level output
-V, --version Print version and exit; specify twice to show license information
Help Options:
-h, --help Show this help message
Expand Down
2 changes: 1 addition & 1 deletion cmd/url-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *EncodeCommand) Execute(args []string) error {

// DecodeCommand holds command options for the decode command
type DecodeCommand struct {
PrintHeaders bool `short:"x" long:"print-headers" description:"Print any encoded addition headers, if present"`
PrintHeaders bool `short:"x" long:"print-headers" description:"Print any encoded extra headers, if present"`
Positional struct {
Url string `positional-arg-name:"URL"`
} `positional-args:"yes" required:"true"`
Expand Down
46 changes: 41 additions & 5 deletions examples/python-base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hashlib
import hmac
import base64
import json


CAMO_HOST = 'https://img.example.com'
Expand All @@ -13,13 +14,48 @@
def camo_url(hmac_key, image_url):
if image_url.startswith("https:"):
return image_url
b64digest = base64.urlsafe_b64encode(
hmac.new(hmac_key, image_url, hashlib.sha1).digest()).strip('=')
b64url = base64.urlsafe_b64encode(image_url).strip('=')
requrl = '%s/%s/%s' % (CAMO_HOST, b64digest, b64url)

mac = hmac.new(hmac_key.encode('utf-8'), image_url.encode('utf-8'), hashlib.sha1)
b64digest = base64.urlsafe_b64encode(mac.digest()).strip(b'=')
b64url = base64.urlsafe_b64encode(image_url.encode('utf-8')).strip(b'=')
requrl = '%s/%s/%s' % (
CAMO_HOST,
b64digest.decode('utf-8'),
b64url.decode('utf-8'),
)
return requrl

def camo_url_w_extra_headers(hmac_key, image_url, extra_headers=None):
if image_url.startswith("https:"):
return image_url

print camo_url("test", "http://golang.org/doc/gopher/frontpage.png")
json_headers = json.dumps(extra_headers).encode('utf-8')
mac = hmac.new(hmac_key.encode('utf-8'), digestmod=hashlib.sha1)
mac.update(image_url.encode('utf-8'))
mac.update(json_headers)

b64digest = base64.urlsafe_b64encode(mac.digest()).strip(b'=')
b64url = base64.urlsafe_b64encode(image_url.encode('utf-8')).strip(b'=')
b64headers = base64.urlsafe_b64encode(json_headers).strip(b'=')
requrl = '%s/%s/%s/%s' % (
CAMO_HOST,
b64digest.decode('utf-8'),
b64url.decode('utf-8'),
b64headers.decode('utf-8'),
)
return requrl


print(
camo_url("test", "http://golang.org/doc/gopher/frontpage.png")
)
# 'https://img.example.org/D23vHLFHsOhPOcvdxeoQyAJTpvM/aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n'

print(
camo_url_w_extra_headers(
"test",
"http://golang.org/doc/gopher/frontpage.png",
{"content-disposition": "attachment; filename=\"image.png\""}
)
)

2 changes: 1 addition & 1 deletion examples/python-hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def camo_url(hmac_key, image_url):
return requrl


print camo_url("test", "http://golang.org/doc/gopher/frontpage.png")
print(camo_url("test", "http://golang.org/doc/gopher/frontpage.png"))
# 'https://img.example.org/0f6def1cb147b0e84f39cbddc5ea10c80253a6f3/687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67'
4 changes: 2 additions & 2 deletions man/go-camo.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ go-camo -k BEEFBEEFBEEF \
== EXTRA_HEADERS

When this option is supplied, extra headers may be included with the url payload as part of url generation.
These extra headers will then be returned as part of the client request.
These extra headers will then be returned as part of the response.

These extra headers are part of a 4th encoded url path component, signed under the hmac signature.
The underlying data is a json map of headers.

This is useful for setting content-disposition headers on certain types of responses.
An example use-case is setting content-disposition headers on specific image urls.

== METRICS

Expand Down
2 changes: 1 addition & 1 deletion man/url-tool.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Available encode options:
Available decode options:

*-x*, *--print-headers*::
Print any encoded addition headers, if present.
Print any encoded extra headers, if present.
--

== EXAMPLES
Expand Down

0 comments on commit 68fb4c1

Please sign in to comment.