Skip to content

Commit 50782a7

Browse files
authored
Add custom listen address support (#6)
1 parent 96816de commit 50782a7

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

.github/workflows/integration-tests.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
2323
- name: Configure Aproxy
2424
run: |
25-
sudo snap set aproxy proxy=squid.internal:3128
25+
sudo snap set aproxy proxy=squid.internal:3128 listen=:23403
2626
sudo nft -f - << EOF
2727
define default-ip = $(ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via \K\S+') | grep -oP 'src \K\S+')
2828
define private-ips = { 10.0.0.0/8, 127.0.0.1/8, 172.16.0.0/12, 192.168.0.0/16 }
@@ -31,12 +31,12 @@ jobs:
3131
table ip aproxy {
3232
chain prerouting {
3333
type nat hook prerouting priority dstnat; policy accept;
34-
ip daddr != \$private-ips tcp dport { 80, 443 } counter dnat to \$default-ip:8443
34+
ip daddr != \$private-ips tcp dport { 80, 443 } counter dnat to \$default-ip:23403
3535
}
3636
3737
chain output {
3838
type nat hook output priority -100; policy accept;
39-
ip daddr != \$private-ips tcp dport { 80, 443 } counter dnat to \$default-ip:8443
39+
ip daddr != \$private-ips tcp dport { 80, 443 } counter dnat to \$default-ip:23403
4040
}
4141
}
4242
EOF

aproxy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"golang.org/x/crypto/cryptobyte"
2424
)
2525

26-
var version = "0.2.1"
26+
var version = "0.2.2"
2727

2828
// PrereadConn is a wrapper around net.Conn that supports pre-reading from the underlying connection.
2929
// Any Read before the EndPreread can be undone and read again by calling the EndPreread function.
@@ -374,16 +374,17 @@ func HandleConn(conn net.Conn, proxy string) {
374374

375375
func main() {
376376
proxyFlag := flag.String("proxy", "", "upstream HTTP proxy address in the 'host:port' format")
377+
listenFlag := flag.String("listen", ":8443", "the address and port on which the server will listen")
377378
flag.Parse()
378-
listenAddr := &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 8443}
379+
listenAddr := *listenFlag
379380
ctx := context.Background()
380381
signal.NotifyContext(ctx, os.Interrupt)
381382
listenConfig := new(net.ListenConfig)
382-
listener, err := listenConfig.Listen(ctx, "tcp", fmt.Sprintf(":8443"))
383+
listener, err := listenConfig.Listen(ctx, "tcp", listenAddr)
383384
if err != nil {
384-
log.Fatalf("failed to listen on %s", listenAddr.String())
385+
log.Fatalf("failed to listen on %#v", listenAddr)
385386
}
386-
slog.Info("start listening on 0.0.0.0:8443")
387+
slog.Info(fmt.Sprintf("start listening on %s", listenAddr))
387388
proxy := *proxyFlag
388389
if proxy == "" {
389390
log.Fatalf("no upstearm proxy specified")

snap/hooks/configure

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
mkdir -p $SNAP_COMMON/cert
5-
4+
[ -z "$(snapctl get listen)" ] && snapctl set listen=":8443"
65

76
validate_proxy() {
87
local hostport="$1"
@@ -25,8 +24,8 @@ validate_proxy() {
2524
return 0
2625
}
2726

28-
2927
proxy="$(snapctl get proxy)"
28+
listen="$(snapctl get listen)"
3029

3130
if [ -z "${proxy}" ]; then
3231
echo "set upstream proxy using \`snap set aproxy proxy=example:1234\`"
@@ -35,7 +34,7 @@ fi
3534

3635
validate_proxy "$proxy"
3736

38-
echo "--proxy $proxy" > $SNAP_DATA/args
37+
echo "--proxy $proxy --listen $listen" > $SNAP_DATA/args
3938

4039
snapctl stop ${SNAP_NAME}.aproxy
4140
snapctl start ${SNAP_NAME}.aproxy --enable

snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aproxy
2-
version: 0.2.1
2+
version: 0.2.2
33
summary: Transparent proxy for HTTP and HTTPS/TLS connections.
44
description: |
55
Aproxy is a transparent proxy for HTTP and HTTPS/TLS connections. By

0 commit comments

Comments
 (0)