Skip to content

Commit 3874dcb

Browse files
authored
Merge pull request #114 from SenseUnit/cfg_file
Cfg file
2 parents c2f055c + 362670f commit 3874dcb

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,37 @@ Supported proxy schemes are:
346346
* `url` - actual proxy URL. Note that just like any query string parameter this one has to be URL-encoded to be passed as query string value.
347347
* `ttl` - time to live for cache record. Examples: `15s`, `2m`, `1h`.
348348

349+
## Configuration files
350+
351+
Reading options from configuration file is now supported too! For example, command from one of examples above:
352+
353+
```
354+
dumbproxy \
355+
-bind-address 127.0.0.1:10443 \
356+
-proxyproto \
357+
-auth basicfile://?path=/etc/dumbproxy.htpasswd \
358+
-cert=/etc/letsencrypt/live/proxy.example.com/fullchain.pem \
359+
-key=/etc/letsencrypt/live/proxy.example.com/privkey.pem
360+
```
361+
362+
becomes just
363+
364+
```
365+
dumbproxy -config dp.cfg
366+
```
367+
368+
having dp.cfg file with following content:
369+
370+
```
371+
bind-address 127.0.0.1:10443
372+
proxyproto
373+
auth basicfile://?path=/etc/dumbproxy.htpasswd
374+
cert /etc/letsencrypt/live/proxy.example.com/fullchain.pem
375+
key /etc/letsencrypt/live/proxy.example.com/privkey.pem
376+
```
377+
378+
Configuration format is [RFC 4180](https://www.rfc-editor.org/rfc/rfc4180.html) CSV with space character (`" "`) as a field separator instead of comma, `#` as a comment start character. Lines with only one field are treated as boolean flags, lines with two or more fields are treated as a key and its value, having extra fields joined with space.
379+
349380
## Synopsis
350381

351382
```
@@ -395,6 +426,8 @@ Usage of /home/user/go/bin/dumbproxy:
395426
enable TLS and use certificate
396427
-ciphers string
397428
colon-separated list of enabled ciphers
429+
-config value
430+
read configuration from file with space-separated keys and values
398431
-curves string
399432
colon-separated list of enabled key exchange curves
400433
-deny-dst-addr value

main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"crypto/tls"
77
"encoding/base64"
88
"encoding/binary"
9+
"encoding/csv"
910
"encoding/hex"
1011
"errors"
1112
"flag"
1213
"fmt"
14+
"io"
1315
"log"
1416
"net"
1517
"net/http"
@@ -313,6 +315,7 @@ func parse_args() CLIArgs {
313315
return nil
314316
})
315317
flag.BoolVar(&args.proxyproto, "proxyproto", false, "listen proxy protocol")
318+
flag.Func("config", "read configuration from file with space-separated keys and values", readConfig)
316319
flag.Parse()
317320
args.positionalArgs = flag.Args()
318321
return args
@@ -809,6 +812,50 @@ func prompt(prompt string, secure bool) (string, error) {
809812
return input, nil
810813
}
811814

815+
func readConfig(filename string) error {
816+
f, err := os.Open(filename)
817+
if err != nil {
818+
return fmt.Errorf("unable to open config file %q: %w", filename, err)
819+
}
820+
defer f.Close()
821+
r := csv.NewReader(f)
822+
r.Comma = ' '
823+
r.Comment = '#'
824+
r.FieldsPerRecord = -1
825+
r.TrimLeadingSpace = true
826+
r.ReuseRecord = true
827+
for {
828+
record, err := r.Read()
829+
if err == io.EOF {
830+
break
831+
}
832+
if err != nil {
833+
return fmt.Errorf("configuration file parsing failed: %w", err)
834+
}
835+
switch len(record) {
836+
case 0:
837+
continue
838+
case 1:
839+
if err := flag.Set(record[0], "true"); err != nil {
840+
line, _ := r.FieldPos(0)
841+
return fmt.Errorf("error parsing config file %q at line %d: %w", filename, line, err)
842+
}
843+
case 2:
844+
if err := flag.Set(record[0], record[1]); err != nil {
845+
line, _ := r.FieldPos(0)
846+
return fmt.Errorf("error parsing config file %q at line %d: %w", filename, line, err)
847+
}
848+
default:
849+
unified := strings.Join(record[1:], " ")
850+
if err := flag.Set(record[0], unified); err != nil {
851+
line, _ := r.FieldPos(0)
852+
return fmt.Errorf("error parsing config file %q at line %d: %w", filename, line, err)
853+
}
854+
}
855+
}
856+
return nil
857+
}
858+
812859
func main() {
813860
os.Exit(run())
814861
}

0 commit comments

Comments
 (0)