CI / Lint, test and scan (push) Successful in 1m41s
Restructure the daemon to follow the project's Go coding standards and
expand its features.
Layout and tooling
- cmd/uberbringer/main.go is now thin; all logic lives under internal/.
- Module path is git.anomalous.dev/57_Wolve/uberbringer, go 1.26, every
dependency upgraded, vendor/ removed.
- staticcheck, govulncheck and goimports pinned as go tools; add a
Makefile, CI workflow, goreleaser v2 config and Alpine Dockerfile.
Features
- Background print queue with job status (GET /api/v1/jobs/:id), send
retries with backoff, and graceful shutdown.
- New {"elements": [...]} job format on POST /api/v1/print. Breaking:
replaces {"data": [...]}, and the response is now JSON 202.
- In-house ESC/POS library replacing justinmichaelvieira/escpos: sizes
1-8, Font B, CODE39/CODE128, QR, banded raster images, DLE EOT status.
- UTF-8 text transcoded to CP437 (configurable) with word-aware wrapping.
- Images from URL or base64, scaled and dithered; URL fetches refuse
internal addresses (SSRF guard).
- Fiber v3 HTTP server, TypeID (UUIDv7) job and request IDs, /healthz
with live printer status, `secret` and `token` subcommands.
Fixes
- Jobs are validated before anything is sent; a failed printer dial no
longer panics.
- Request logs never contain tokens; Sentry no longer receives the
Authorization header or print contents.
- Rate limiting is per client behind nginx (X-Real-IP).
- install.sh writes the [printer] section, runs the service as a
dedicated user and uses a current nginx TLS config.
config.toml is no longer tracked; start from configs/config.example.toml.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
5.4 KiB
Go
212 lines
5.4 KiB
Go
package escpos
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Symbology identifies a one-dimensional barcode type. Values are the GS k
|
|
// function B type codes.
|
|
type Symbology byte
|
|
|
|
// Supported barcode symbologies.
|
|
const (
|
|
UPCA Symbology = 65
|
|
UPCE Symbology = 66
|
|
EAN13 Symbology = 67
|
|
EAN8 Symbology = 68
|
|
CODE39 Symbology = 69
|
|
CODE128 Symbology = 73
|
|
)
|
|
|
|
// HRIPosition places the human-readable interpretation (the digits) relative
|
|
// to a barcode.
|
|
type HRIPosition byte
|
|
|
|
// HRIPosition values for GS H.
|
|
const (
|
|
HRINone HRIPosition = 0
|
|
HRIAbove HRIPosition = 1
|
|
HRIBelow HRIPosition = 2
|
|
HRIBoth HRIPosition = 3
|
|
)
|
|
|
|
// Barcode module width limits for GS w.
|
|
const (
|
|
MinBarcodeWidth = 2
|
|
MaxBarcodeWidth = 6
|
|
)
|
|
|
|
const code39Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -.$/+%"
|
|
|
|
// ParseSymbology returns the symbology named by name, e.g. "ean13".
|
|
func ParseSymbology(name string) (Symbology, error) {
|
|
switch strings.ToLower(strings.TrimSpace(name)) {
|
|
case "upca", "upc-a":
|
|
return UPCA, nil
|
|
case "upce", "upc-e":
|
|
return UPCE, nil
|
|
case "ean13", "ean-13", "jan13":
|
|
return EAN13, nil
|
|
case "ean8", "ean-8", "jan8":
|
|
return EAN8, nil
|
|
case "code39", "code-39":
|
|
return CODE39, nil
|
|
case "code128", "code-128":
|
|
return CODE128, nil
|
|
}
|
|
return 0, fmt.Errorf("%w: unknown barcode symbology %q", ErrInvalidArgument, name)
|
|
}
|
|
|
|
// String returns the canonical symbology name.
|
|
func (s Symbology) String() string {
|
|
switch s {
|
|
case UPCA:
|
|
return "upca"
|
|
case UPCE:
|
|
return "upce"
|
|
case EAN13:
|
|
return "ean13"
|
|
case EAN8:
|
|
return "ean8"
|
|
case CODE39:
|
|
return "code39"
|
|
case CODE128:
|
|
return "code128"
|
|
}
|
|
return fmt.Sprintf("Symbology(%d)", byte(s))
|
|
}
|
|
|
|
// ValidateBarcode reports whether data can be encoded as sym. Where data
|
|
// includes the check digit, the check digit is verified.
|
|
func ValidateBarcode(sym Symbology, data string) error {
|
|
invalid := func(format string, args ...any) error {
|
|
return fmt.Errorf("%w: %s: "+format, append([]any{ErrInvalidArgument, sym}, args...)...)
|
|
}
|
|
switch sym {
|
|
case UPCA:
|
|
if !isDigits(data) || (len(data) != 11 && len(data) != 12) {
|
|
return invalid("want 11 or 12 digits")
|
|
}
|
|
if len(data) == 12 && !validCheckDigit(data) {
|
|
return invalid("check digit is wrong")
|
|
}
|
|
case UPCE:
|
|
switch {
|
|
case !isDigits(data):
|
|
return invalid("want digits only")
|
|
case len(data) >= 6 && len(data) <= 8:
|
|
case (len(data) == 11 || len(data) == 12) && data[0] == '0':
|
|
default:
|
|
return invalid("want 6-8 digits, or 11-12 digits starting with 0")
|
|
}
|
|
case EAN13:
|
|
if !isDigits(data) || (len(data) != 12 && len(data) != 13) {
|
|
return invalid("want 12 or 13 digits")
|
|
}
|
|
if len(data) == 13 && !validCheckDigit(data) {
|
|
return invalid("check digit is wrong")
|
|
}
|
|
case EAN8:
|
|
if !isDigits(data) || (len(data) != 7 && len(data) != 8) {
|
|
return invalid("want 7 or 8 digits")
|
|
}
|
|
if len(data) == 8 && !validCheckDigit(data) {
|
|
return invalid("check digit is wrong")
|
|
}
|
|
case CODE39:
|
|
if len(data) < 1 || len(data) > 255 {
|
|
return invalid("want 1-255 characters")
|
|
}
|
|
for _, r := range data {
|
|
if !strings.ContainsRune(code39Charset, r) {
|
|
return invalid("character %q not allowed (use 0-9, A-Z, space and -.$/+%%)", r)
|
|
}
|
|
}
|
|
case CODE128:
|
|
if len(data) < 1 {
|
|
return invalid("data is empty")
|
|
}
|
|
for _, r := range data {
|
|
if r < 0x20 || r > 0x7E {
|
|
return invalid("character %q not allowed (printable ASCII only)", r)
|
|
}
|
|
}
|
|
if len(code128Payload(data)) > 255 {
|
|
return invalid("data is too long")
|
|
}
|
|
default:
|
|
return fmt.Errorf("%w: unknown barcode symbology %d", ErrInvalidArgument, byte(sym))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// BarcodeHeight sets the barcode height in dots (GS h n).
|
|
func (b *Builder) BarcodeHeight(dots uint8) {
|
|
b.buf.Write([]byte{gs, 'h', dots})
|
|
}
|
|
|
|
// BarcodeWidth sets the barcode module width, [MinBarcodeWidth] to
|
|
// [MaxBarcodeWidth] (GS w n).
|
|
func (b *Builder) BarcodeWidth(module int) error {
|
|
if module < MinBarcodeWidth || module > MaxBarcodeWidth {
|
|
return fmt.Errorf("%w: barcode width %d must be within %d-%d",
|
|
ErrInvalidArgument, module, MinBarcodeWidth, MaxBarcodeWidth)
|
|
}
|
|
b.buf.Write([]byte{gs, 'w', byte(module)})
|
|
return nil
|
|
}
|
|
|
|
// BarcodeHRI sets where the human-readable digits are printed (GS H n).
|
|
func (b *Builder) BarcodeHRI(pos HRIPosition) {
|
|
b.buf.Write([]byte{gs, 'H', byte(pos)})
|
|
}
|
|
|
|
// Barcode validates data and appends a barcode (GS k m n d1...dn). CODE128
|
|
// data is printed in code set B, so callers pass plain text.
|
|
func (b *Builder) Barcode(sym Symbology, data string) error {
|
|
if err := ValidateBarcode(sym, data); err != nil {
|
|
return err
|
|
}
|
|
payload := []byte(data)
|
|
if sym == CODE128 {
|
|
payload = code128Payload(data)
|
|
}
|
|
b.buf.Write([]byte{gs, 'k', byte(sym), byte(len(payload))})
|
|
b.buf.Write(payload)
|
|
return nil
|
|
}
|
|
|
|
// code128Payload selects code set B and escapes '{', which the printer
|
|
// treats as the start of a code set or function character.
|
|
func code128Payload(data string) []byte {
|
|
return []byte("{B" + strings.ReplaceAll(data, "{", "{{"))
|
|
}
|
|
|
|
func isDigits(s string) bool {
|
|
if s == "" {
|
|
return false
|
|
}
|
|
for i := range len(s) {
|
|
if s[i] < '0' || s[i] > '9' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// validCheckDigit verifies the GS1 mod-10 check digit in the last position of
|
|
// a UPC-A, EAN-13 or EAN-8 number.
|
|
func validCheckDigit(code string) bool {
|
|
sum := 0
|
|
body := code[:len(code)-1]
|
|
for i := range len(body) {
|
|
digit := int(body[len(body)-1-i] - '0')
|
|
if i%2 == 0 {
|
|
digit *= 3
|
|
}
|
|
sum += digit
|
|
}
|
|
return (10-sum%10)%10 == int(code[len(code)-1]-'0')
|
|
}
|