From 6f12a531c3d052bbc8c0ad18becf920cbd30df65 Mon Sep 17 00:00:00 2001 From: Claudio d'Angelis Date: Fri, 11 Mar 2022 19:16:05 +0100 Subject: [PATCH] Set port through environment variable * Allow for setting port through environment variable * minor refactoring Co-authored-by: Johannes Arnold --- README.md | 20 ++++++++++++++------ config/config.go | 12 +++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 70ed2d0..a6a9feb 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Receive files from mobile: # Installation ## Install the latest development version with Go - + _Note: it requires go 1.8_ go get github.com/claudiodangelis/qrcp @@ -97,7 +97,7 @@ qrcp --help Download the latest Windows .tar.gz archive from the [Releases page](https://github.com/claudiodangelis/qrcp/releases) and extract the EXE file. -### Scoop +### Scoop If you use [Scoop](https://scoop.sh/) for package management on Windows, you can install qrcp with the following one-liner: @@ -127,7 +127,7 @@ If you use [Homebrew](https://brew.sh) for package management on macOS, you can ``` brew install qrcp ``` - + # Usage ## Send files @@ -190,7 +190,7 @@ qrcp config Note: if some network interfaces are not showing up, use the `--list-all-interfaces` flag to suppress the interfaces' filter. ```sh -qrcp --list-all-interfaces config +qrcp --list-all-interfaces config ``` @@ -204,11 +204,19 @@ qrcp --config /tmp/qrcp.json MyDocument.pdf ### Port -By default `qrcp` listens on a random port. Pass the `--port` (or `-p`) flag to choose a specific one: +By default `qrcp` listens on a random port. Set the `QRCP_PORT` environment variable or pass the `--port` (or `-p`) flag to choose a specific one: + +```sh +export QRCP_PORT=8080 +qrcp MyDocument +``` + +Or: ```sh qrcp --port 8080 MyDocument.pdf ``` + ### Network Interface `qrcp` will try to automatically find the suitable network interface to use for the transfers. If more than one suitable interface is found, it asks you to choose one. @@ -224,7 +232,7 @@ qrcp -i tun0 MyDocument.dpf ``` -You can also use a special interface name, `any`, which binds the web server to `0.0.0.0`, making the web server visible by everyone on any network, even from an external network. +You can also use a special interface name, `any`, which binds the web server to `0.0.0.0`, making the web server visible by everyone on any network, even from an external network. This is useful when you want to transfer files from your Amazon EC2, Digital Ocean Droplet, Google Cloud Platform Compute Instance or any other VPS. diff --git a/config/config.go b/config/config.go index f03157f..eab3281 100644 --- a/config/config.go +++ b/config/config.go @@ -354,10 +354,16 @@ func New(path string, opts Options) (Config, error) { cfg.FQDN = opts.FQDN } if opts.Port != 0 { - if opts.Port > 65535 { - return cfg, errors.New("invalid value for port") - } cfg.Port = opts.Port + } else if portVal, ok := os.LookupEnv("QRCP_PORT"); ok { + port, err := strconv.Atoi(portVal) + if err != nil { + return cfg, errors.New("could not parse port from environment variable QRCP_PORT") + } + cfg.Port = port + } + if cfg.Port != 0 && !govalidator.IsPort(fmt.Sprintf("%d", cfg.Port)) { + return cfg, fmt.Errorf("%d is not a valid port", cfg.Port) } if opts.KeepAlive { cfg.KeepAlive = true