Browse Source

Added dumb-proxy a simple easy to use proxy with static files

pull/2/head
Darshil Chanpura 2 months ago
parent
commit
b1685c39cf
  1. 39
      cmd/dumb-proxy/main.go
  2. 8
      go.mod
  3. 25
      go.sum
  4. 30
      logger/log_handler.go
  5. 39
      proxy/proxy.go

39
cmd/dumb-proxy/main.go

@ -0,0 +1,39 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"git.dcpri.me/some-fancy-tools/dumb-http/logger"
"git.dcpri.me/some-fancy-tools/dumb-http/proxy"
)
var (
version = "latest"
addr = flag.String("addr", "127.0.0.1:8080", "Address")
root = flag.String("root", ".", "Document root to serve static files")
upstream = flag.String("upstream", "http://127.0.0.1:8081", "Reverse Proxy upstream (backend service)")
proxyPath = flag.String("proxy-path", "/proxy/", "Reverse Proxy path")
stripPrefix = flag.Bool("strip-prefix", false, "Strip Prefix while proxying")
)
func main() {
flag.Parse()
p, err := proxy.New(*upstream, *root)
if err != nil {
log.Fatal(err)
}
p.Routes(*proxyPath, *stripPrefix)
fmt.Printf("Serving with dumb-proxy/%s at http://%s \n", version, *addr)
fmt.Printf(" Root Configuration: http://%s/ -> %q\n", *addr, *root)
if *stripPrefix {
fmt.Printf("Proxy Configuration: http://%s%s -> %s\n", *addr, *proxyPath, *upstream)
} else {
fmt.Printf("Proxy Configuration: http://%s%s -> %s%s\n", *addr, *proxyPath, *upstream, *proxyPath)
}
http.ListenAndServe(*addr, logger.NewLoggingHandler(p, os.Stdout))
}

8
go.mod

@ -1,8 +1,10 @@
module git.dcpri.me/some-fancy-tools/dumb-http
go 1.14
go 1.17
require git.dcpri.me/modules/s3fs v1.0.0
require (
git.dcpri.me/modules/s3fs v1.0.0
github.com/aws/aws-sdk-go v1.33.1 // indirect
github.com/aws/aws-sdk-go v1.40.44 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
)

25
go.sum

@ -1,27 +1,34 @@
git.dcpri.me/modules/s3fs v1.0.0 h1:iivP7hz93acON2WVUy4xde80iOA0vTb4GQCROR2PO1k=
git.dcpri.me/modules/s3fs v1.0.0/go.mod h1:b6dBmk/TsF/Jhb9hs+/zD8QdyGrpD3xJzkiSzk7m3vo=
github.com/aws/aws-sdk-go v1.33.0 h1:Bq5Y6VTLbfnJp1IV8EL/qUU5qO1DYHda/zis/sqevkY=
github.com/aws/aws-sdk-go v1.33.0/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.33.1 h1:yz9XmNzPshz/lhfAZvLfMnIS9HPo8+boGRcWqDVX+T0=
github.com/aws/aws-sdk-go v1.33.1/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.40.44 h1:kECaYybTWYZY5IKHvQMxbE6Wi5Qrb+7hbkV7zQV3Sg8=
github.com/aws/aws-sdk-go v1.40.44/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
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/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
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=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

30
logger/log_handler.go

@ -11,17 +11,11 @@ import (
)
var (
realm string
user string
pass string
user = flag.String("basic-auth-username", "", "Username for basic auth")
pass = flag.String("basic-auth-password", "", "Password for basic auth")
realm = flag.String("basic-auth-realm", "DUMB-HTTP", "Realm for basic auth")
)
func init() {
flag.StringVar(&user, "username", "", "Username for basic auth")
flag.StringVar(&pass, "password", "", "Password for basic auth")
flag.StringVar(&realm, "realm", "DUMB-HTTP", "Realm for basic auth")
}
// LoggingHandler is to be used as wrapper of mux.
type LoggingHandler struct {
handler http.Handler
@ -71,11 +65,11 @@ func (h LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// For getting duration
// startTime := time.Now()
username, password, ok := r.BasicAuth()
if user == "" || pass == "" {
if *user == "" || *pass == "" {
successfulResponse(logRecord, h.handler, h.out, r)
return
}
if user != "" && pass != "" && (!ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1) {
if *user != "" && *pass != "" && (!ok || subtle.ConstantTimeCompare([]byte(*user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(*pass), []byte(password)) != 1) {
unsuccessfulResponse(logRecord, h.handler, h.out, w, r)
return
}
@ -83,22 +77,18 @@ func (h LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func successfulResponse(logRecord *LogRecord, h http.Handler, out io.Writer, r *http.Request) {
startTime := time.Now()
logRecord.time = time.Now()
h.ServeHTTP(logRecord, r)
endTime := time.Now()
logRecord.time = endTime.UTC()
logRecord.duration = endTime.Sub(startTime) / 1000.0
logRecord.duration = time.Since(logRecord.time)
logRecord.Log(out)
}
func unsuccessfulResponse(logRecord *LogRecord, h http.Handler, out io.Writer, w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
logRecord.time = time.Now()
w.Header().Set("WWW-Authenticate", `Basic realm="`+*realm+`"`)
logRecord.statusCode = http.StatusUnauthorized
w.WriteHeader(logRecord.statusCode)
fmt.Fprintln(w, "Unauthorized")
endTime := time.Now()
logRecord.time = endTime.UTC()
logRecord.duration = endTime.Sub(startTime) / 1000.0
logRecord.duration = time.Since(logRecord.time)
logRecord.Log(out)
}

39
proxy/proxy.go

@ -0,0 +1,39 @@
package proxy
import (
"net/http"
"net/http/httputil"
"net/url"
)
type Proxy struct {
mux *http.ServeMux
rp *httputil.ReverseProxy
root string
}
func New(upstream, root string) (*Proxy, error) {
rpURL, err := url.Parse(upstream)
if err != nil {
return nil, err
}
return &Proxy{
mux: http.NewServeMux(),
rp: httputil.NewSingleHostReverseProxy(rpURL),
root: root,
}, nil
}
func (p *Proxy) Routes(proxyPath string, stripPrefix bool) {
if stripPrefix {
p.mux.Handle(proxyPath, http.StripPrefix(proxyPath, p.rp))
} else {
p.mux.Handle(proxyPath, p.rp)
}
p.mux.Handle("/", http.FileServer(http.Dir(p.root)))
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.mux.ServeHTTP(w, r)
}
Loading…
Cancel
Save