Skip to content

Commit

Permalink
chore: tidy up logs, config, and some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbekhen committed Sep 21, 2023
1 parent 78a2a20 commit f341795
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ ADDR=
PROTO=
PEM=
KEY=
TIMEOUT=
TIMEOUT=
AUTH=
TZ=
6 changes: 4 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ builds:
- CGO_ENABLED=0
goos:
- linux
- darwin
dockers:
- image_templates:
- "ghcr.io/ryanbekhen/nanoproxy:{{ .Version }}"
Expand All @@ -38,9 +37,12 @@ nfpms:
contents:
- src: systemd/nanoproxy.service
dst: /etc/systemd/system/nanoproxy.service
type: "config|noreplace"
- src: .env.sample
dst: /etc/nanoproxy/nanoproxy.env
dst: /etc/nanoproxy/nanoproxy
type: "config|noreplace"
formats:
- apk
- deb
- rpm

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/ryanbekhen/nanoproxy?cache=v1)](https://goreportcard.com/report/github.com/vladopajic/go-test-coverage)

NanoProxy is a lightweight HTTP proxy server designed to provide basic proxying functionality.
It supports handling HTTP requests, tunneling, and follows redirects.
It supports handling HTTP requests and tunneling for HTTPS. NanoProxy is written in Go and built on top of FastHTTP.

> ⚠️ **Notice:** NanoProxy is currently in pre-production stage. While it provides essential proxying capabilities,
> please be aware that it is still under active development. Full backward compatibility is not guaranteed until
Expand Down Expand Up @@ -116,6 +116,8 @@ You can modify the behavior of NanoProxy by adjusting the command line flags whe
- `-key`: Path to the private key file for TLS.
- `-proto`: Proxy protocol `http` or `https`. If set to `https`, the `-pem` and `-key` flags must be set.
- `-timeout`: Timeout duration for tunneling connections (default: 15 seconds).
- `-auth`: Basic authentication credentials in the form of `username:password`.
- `-debug`: Enable debug mode.

You can set the configuration using environment variables. Create a file
at `/etc/nanoproxy/nanoproxy.env` and add the desired values:
Expand All @@ -126,6 +128,8 @@ PROTO=http
PEM=server.pem
KEY=server.key
TIMEOUT=15s
AUTH=user:pass
TZ=Asia/Jakarta
```

Modify these flags or environment variables according to your requirements.
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
Addr string
TunnelTimeout time.Duration
BasicAuth string
Debug bool
}

func New() *Config {
Expand All @@ -23,6 +24,7 @@ func New() *Config {
flag.StringVar(&c.Addr, "addr", ":8080", "proxy listen address (default :8080)")
flag.DurationVar(&c.TunnelTimeout, "timeout", time.Second*15, "tunnel timeout (default 15s)")
flag.StringVar(&c.BasicAuth, "auth", "", "basic auth (username:password)")
flag.BoolVar(&c.Debug, "debug", false, "debug mode")
flag.Parse()

if os.Getenv("PEM") != "" {
Expand Down
15 changes: 9 additions & 6 deletions nanoproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ func main() {
loc, _ := time.LoadLocation(os.Getenv("TZ"))
time.Local = loc

logger := log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
}).With().Timestamp().Logger()
logLevel := zerolog.InfoLevel
if cfg.Debug {
logLevel = zerolog.DebugLevel
}

logger := log.Level(logLevel).Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
With().Timestamp().Logger()

// validate protocol is http or https only
if cfg.Proto != "http" && cfg.Proto != "https" {
Expand All @@ -28,8 +31,8 @@ func main() {

srv := webproxy.New(cfg.BasicAuth, cfg.TunnelTimeout, logger)
server := &fasthttp.Server{
Handler: srv.Handler,
ReadTimeout: 15 * time.Second,
Handler: srv.Handler,
Logger: &logger,
}

logger.Info().Msg("Listening on " + cfg.Addr)
Expand Down
4 changes: 2 additions & 2 deletions systemd/nanoproxy.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Description=NanoProxy is a simple reverse proxy written in Go
After=network.target

[Service]
EnvironmentFile=/etc/nanoproxy/nanoproxy.env
EnvironmentFile=/etc/nanoproxy/nanoproxy
ExecStart=/usr/bin/nanoproxy
WorkingDirectory=/usr/bin
WorkingDirectory=/etc/nanoproxy/
Restart=always
User=root

Expand Down
5 changes: 4 additions & 1 deletion webproxy/webproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (s *WebProxy) Handler(ctx *fasthttp.RequestCtx) {
if s.BasicAuth != "" {
proxyAuth := string(ctx.Request.Header.Peek("Proxy-Authorization"))
if proxyAuth == "" {
ctx.Error("Unauthorized", fasthttp.StatusProxyAuthRequired)
ctx.Logger().Printf("Requires authentication")
ctx.Error("Requires authentication", fasthttp.StatusProxyAuthRequired)
return
}

Expand All @@ -54,11 +55,13 @@ func (s *WebProxy) Handler(ctx *fasthttp.RequestCtx) {
// read the decoded username:password
decoded, err := io.ReadAll(base64Io)
if err != nil {
ctx.Logger().Printf("Decoding error: %s", err.Error())
ctx.Error(err.Error(), fasthttp.StatusBadRequest)
return
}

if string(decoded) != s.BasicAuth {
ctx.Logger().Printf("Unauthorized")
ctx.Error("Unauthorized", fasthttp.StatusUnauthorized)
return
}
Expand Down

0 comments on commit f341795

Please sign in to comment.