Skip to content

Commit

Permalink
feat: add configurable broker
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Jul 6, 2022
1 parent 54f9e0a commit cd27371
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Caddyfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
localhost {
route * {
packetframe_httpgate always
packetframe_httpgate https://httpgate-broker.packetframe.com always
respond "test"
}
}
133 changes: 0 additions & 133 deletions httpgate/broker/main.go

This file was deleted.

6 changes: 2 additions & 4 deletions httpgate/broker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"strings"
)

const broker = "http://localhost:8080"

// newHash requests a new hash from the broker
func newHash() (string, error) {
func newHash(broker string) (string, error) {
resp, err := http.Get(broker + "/new")
if err != nil {
return "", err
Expand All @@ -26,7 +24,7 @@ func newHash() (string, error) {
}

// validate verifies a token/hash against the broker
func validate(httpGate string) (bool, error) {
func validate(broker, httpGate string) (bool, error) {
parts := strings.Split(httpGate, ":")
if len(parts) != 2 {
return false, fmt.Errorf("invalid token")
Expand Down
13 changes: 10 additions & 3 deletions httpgate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func init() {
// HTTPGate represents the HTTP gate module
type HTTPGate struct {
// Client challenge intensity mode
Mode string `json:"mode,omitempty"`
Mode string `json:"mode,omitempty"`
Broker string `json:"broker,omitempty"`

indexTemplate *template.Template
logger *zap.Logger
Expand Down Expand Up @@ -65,6 +66,9 @@ func (p *HTTPGate) Validate() error {
default:
return fmt.Errorf("invalid mode: %s", p.Mode)
}
if p.Broker == "" {
return fmt.Errorf("broker is required")
}
return nil
}

Expand All @@ -83,7 +87,7 @@ func (p HTTPGate) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
forceChallenge := false

if httpGate != "" {
ok, err := validate(httpGate)
ok, err := validate(p.Broker, httpGate)
if err != nil {
p.internalServerError(err)
return next.ServeHTTP(w, r) // fail open
Expand All @@ -102,7 +106,7 @@ func (p HTTPGate) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
}

if forceChallenge || p.shouldChallenge(r) {
h, err := newHash()
h, err := newHash(p.Broker)
if err != nil {
p.internalServerError(err)
return next.ServeHTTP(w, r) // fail open
Expand All @@ -126,6 +130,9 @@ func (p *HTTPGate) UnmarshalCaddyfile(_ *caddyfile.Dispenser) error {
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var p HTTPGate
for h.Next() {
if !h.Args(&p.Broker) {
return nil, h.ArgErr()
}
if !h.Args(&p.Mode) {
return nil, h.ArgErr()
}
Expand Down

0 comments on commit cd27371

Please sign in to comment.