Skip to content

cli: add support for passing proxy command line parameter #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions CLI/go-build-generic
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

set -euo pipefail

if [[ $# -ne 1 ]]; then
echo "" 1>&2
echo "Compiler for a Go PACKAGE producing GOOS/GOARCH binaries in the" 1>&2
echo "top-level directory (should not be used for releasing)." 1>&2
echo "" 1>&2
echo "usage: $0 PACKAGE..." 1>&2
echo "" 1>&2
echo "Features:" 1>&2
echo "" 1>&2
echo "* automatically sets -tags=ooni_psiphon_config when possible;" 1>&2
echo "" 1>&2
echo "* if GOLANG_EXTRA_FLAGS is set, pass it to the Go compiler." 1>&2
echo "" 1>&2
echo "Example:" 1>&2
echo "" 1>&2
echo " ./CLI/go-build-generic ./internal/cmd/miniooni" 1>&2
echo "" 1>&2
exit 1
fi

if [[ -f ./internal/engine/psiphon-config.json.age &&
-f ./internal/engine/psiphon-config.key ]]; then
OONI_PSIPHON_TAGS=ooni_psiphon_config
else
OONI_PSIPHON_TAGS=""
fi

set -x
for pkg in "$@"; do
go build -tags=$OONI_PSIPHON_TAGS -ldflags="-s -w" ${GOLANG_EXTRA_FLAGS:-} $pkg
done
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ CLI/linux-static-armv7: search/for/docker maybe/copypsiphon
CLI/linux-static-arm64: search/for/docker maybe/copypsiphon
./CLI/go-build-linux-static $(OONI_GO_DOCKER_GOCACHE) arm64 ./cmd/ooniprobe ./internal/cmd/miniooni

#help:
#help: The `make CLI/miniooni` command creates a build of miniooni, for the current
#help: system, putting the binary in the top-level directory.
.PHONY: CLI/miniooni
CLI/miniooni: maybe/copypsiphon search/for/go
./CLI/go-build-generic ./internal/cmd/miniooni

#help:
#help: The `make CLI/ooniprobe` command creates a build of ooniprobe, for the current
#help: system, putting the binary in the top-level directory.
.PHONY: CLI/ooniprobe
CLI/ooniprobe: maybe/copypsiphon search/for/go
./CLI/go-build-generic ./cmd/ooniprobe

#help:
#help: The `make CLI/windows` command builds the ooniprobe and miniooni
#help: command line clients for windows/386 and windows/amd64.
Expand Down
5 changes: 4 additions & 1 deletion cmd/ooniprobe/internal/cli/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func init() {
softwareVersion := Cmd.Flag(
"software-version", "Override the application version",
).Default(version.Version).String()
proxy := Cmd.Flag(
"proxy", "specify a proxy address for speaking to the OONI Probe backend (use: --proxy=psiphon:/// for psiphon)",
).String()

Cmd.PreAction(func(ctx *kingpin.ParseContext) error {
// TODO(bassosimone): we need to properly deprecate --batch
Expand Down Expand Up @@ -78,7 +81,7 @@ func init() {
}

probe := ooni.NewProbe(*configPath, homePath)
err = probe.Init(*softwareName, *softwareVersion)
err = probe.Init(*softwareName, *softwareVersion, *proxy)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ooniprobe/internal/nettests/nettests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newOONIProbe(t *testing.T) *ooni.Probe {
probe := ooni.NewProbe(configPath, homePath)
swName := "ooniprobe-cli-tests"
swVersion := "3.0.0-alpha"
err = probe.Init(swName, swVersion)
err = probe.Init(swName, swVersion, "")
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/ooniprobe/internal/ooni/ooni.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
_ "embed" // because we embed a file
"io/ioutil"
"net/url"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -62,6 +63,7 @@ type Probe struct {

softwareName string
softwareVersion string
proxyURL *url.URL
}

// SetIsBatch sets the value of isBatch.
Expand Down Expand Up @@ -124,7 +126,7 @@ func (p *Probe) ListenForSignals() {
// MaybeListenForStdinClosed will treat any error on stdin just
// like SIGTERM if and only if
//
// os.Getenv("OONI_STDIN_EOF_IMPLIES_SIGTERM") == "true"
// os.Getenv("OONI_STDIN_EOF_IMPLIES_SIGTERM") == "true"
//
// When this feature is enabled, a collateral effect is that we swallow
// whatever is passed to us on the standard input.
Expand All @@ -151,7 +153,7 @@ func (p *Probe) MaybeListenForStdinClosed() {
}

// Init the OONI manager
func (p *Probe) Init(softwareName, softwareVersion string) error {
func (p *Probe) Init(softwareName, softwareVersion, proxy string) error {
var err error

if err = MaybeInitializeHome(p.home); err != nil {
Expand Down Expand Up @@ -197,6 +199,12 @@ func (p *Probe) Init(softwareName, softwareVersion string) error {

p.softwareName = softwareName
p.softwareVersion = softwareVersion
if proxy != "" {
p.proxyURL, err = url.Parse(proxy)
if err != nil {
return errors.Wrap(err, "invalid proxy URL")
}
}
return nil
}

Expand Down Expand Up @@ -228,6 +236,7 @@ func (p *Probe) NewSession(ctx context.Context, runType model.RunType) (*engine.
SoftwareVersion: p.softwareVersion,
TempDir: p.tempDir,
TunnelDir: p.tunnelDir,
ProxyURL: p.proxyURL,
})
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ooniprobe/internal/ooni/ooni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestInit(t *testing.T) {
probe := NewProbe("", ooniHome)
swName := "ooniprobe-cli-tests"
swVersion := "3.0.0-alpha"
if err := probe.Init(swName, swVersion); err != nil {
if err := probe.Init(swName, swVersion, ""); err != nil {
t.Error(err)
t.Fatal("failed to init the context")
}
Expand Down