Skip to content

Commit fb8aaed

Browse files
authored
fix(autotls): interop nil pointer (2) (#1860)
1 parent fe53764 commit fb8aaed

File tree

8 files changed

+54
-49
lines changed

8 files changed

+54
-49
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ jobs:
2525
cpu: i386
2626
- os: linux-gcc-14
2727
cpu: amd64
28-
- os: macos-14
28+
- os: macos-15
2929
cpu: arm64
3030
- os: windows
3131
cpu: amd64
3232
nim:
3333
- ref: v2.0.16
3434
memory_management: refc
35-
- ref: v2.2.4
35+
- ref: v2.2.6
3636
memory_management: refc
3737
include:
3838
- platform:
@@ -44,8 +44,8 @@ jobs:
4444
builder: ubuntu-24.04
4545
shell: bash
4646
- platform:
47-
os: macos-14
48-
builder: macos-14
47+
os: macos-15
48+
builder: macos-15
4949
shell: bash
5050
- platform:
5151
os: windows

.github/workflows/daily_amd64.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
uses: ./.github/workflows/daily_common.yml
1212
with:
1313
nim: "[
14-
{'ref': 'version-2-0', 'memory_management': 'refc'},
15-
{'ref': 'version-2-2', 'memory_management': 'refc'},
14+
{'ref': 'v2.0.16', 'memory_management': 'refc'},
15+
{'ref': 'v2.2.6', 'memory_management': 'refc'},
1616
{'ref': 'devel', 'memory_management': 'refc'},
1717
]"
1818
cpu: "['amd64']"
@@ -22,8 +22,8 @@ jobs:
2222
with:
2323
pinned_deps: true
2424
nim: "[
25-
{'ref': 'version-2-0', 'memory_management': 'refc'},
26-
{'ref': 'version-2-2', 'memory_management': 'refc'},
25+
{'ref': 'v2.0.16', 'memory_management': 'refc'},
26+
{'ref': 'v2.2.6', 'memory_management': 'refc'},
2727
{'ref': 'devel', 'memory_management': 'refc'},
2828
]"
2929
cpu: "['amd64']"

.github/workflows/daily_common.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
builder: ubuntu-22.04
4343
shell: bash
4444
- os: macos
45-
builder: macos-13
45+
builder: macos-15
4646
shell: bash
4747
- os: windows
4848
builder: windows-2022
@@ -92,7 +92,5 @@ jobs:
9292
run: |
9393
nim --version
9494
nimble --version
95-
96-
export NIMFLAGS="${NIMFLAGS} --mm:${{ matrix.nim.memory_management }}"
97-
nimble testintegration
95+
nim r -d:libp2p_autotls_support -d:chronicles_log_level=DEBUG tests/testintegration.nim
9896

.github/workflows/daily_i386.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
uses: ./.github/workflows/daily_common.yml
1212
with:
1313
nim: "[
14-
{'ref': 'version-2-0', 'memory_management': 'refc'},
15-
{'ref': 'version-2-2', 'memory_management': 'refc'},
14+
{'ref': 'v2.0.16', 'memory_management': 'refc'},
15+
{'ref': 'v2.2.6', 'memory_management': 'refc'},
1616
{'ref': 'devel', 'memory_management': 'refc'},
1717
]"
1818
cpu: "['i386']"
@@ -26,8 +26,8 @@ jobs:
2626
with:
2727
pinned_deps: true
2828
nim: "[
29-
{'ref': 'version-2-0', 'memory_management': 'refc'},
30-
{'ref': 'version-2-2', 'memory_management': 'refc'},
29+
{'ref': 'v2.0.16', 'memory_management': 'refc'},
30+
{'ref': 'v2.2.6', 'memory_management': 'refc'},
3131
{'ref': 'devel', 'memory_management': 'refc'},
3232
]"
3333
cpu: "['i386']"

.github/workflows/examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
shell: bash
3737
os: linux
3838
cpu: amd64
39-
nim_ref: version-2-2
39+
nim_ref: v2.2.6
4040

4141
- name: Restore deps from cache
4242
id: deps-cache

libp2p/utils/ipaddr.nim

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# This file may not be copied, modified, or distributed except according to
88
# those terms.
99

10-
import net, strutils
10+
import net, chronicles, strutils
1111

1212
import ../switch, ../multiaddress, ../multicodec
1313

@@ -17,35 +17,45 @@ proc isIPv4*(ip: IpAddress): bool =
1717
proc isIPv6*(ip: IpAddress): bool =
1818
ip.family == IpAddressFamily.IPv6
1919

20-
proc isPrivate*(ip: string): bool {.raises: [ValueError].} =
21-
ip.startsWith("10.") or
22-
(ip.startsWith("172.") and parseInt(ip.split(".")[1]) in 16 .. 31) or
23-
ip.startsWith("192.168.") or ip.startsWith("127.") or ip.startsWith("169.254.")
20+
proc isPrivate*(ip: string): bool {.raises: [].} =
21+
try:
22+
return
23+
ip.startsWith("10.") or
24+
(ip.startsWith("172.") and parseInt(ip.split(".")[1]) in 16 .. 31) or
25+
ip.startsWith("192.168.") or ip.startsWith("127.") or ip.startsWith("169.254.")
26+
except ValueError:
27+
return false
2428

25-
proc isPrivate*(ip: IpAddress): bool {.raises: [ValueError].} =
29+
proc isPrivate*(ip: IpAddress): bool {.raises: [].} =
2630
isPrivate($ip)
2731

28-
proc isPublic*(ip: string): bool {.raises: [ValueError].} =
32+
proc isPublic*(ip: string): bool {.raises: [].} =
2933
not isPrivate(ip)
3034

31-
proc isPublic*(ip: IpAddress): bool {.raises: [ValueError].} =
35+
proc isPublic*(ip: IpAddress): bool {.raises: [].} =
3236
isPublic($ip)
3337

34-
proc getPublicIPAddress*(): IpAddress {.raises: [OSError, ValueError].} =
38+
proc hasPublicIPAddress*(): bool {.raises: [].} =
3539
let ip =
3640
try:
3741
getPrimaryIPAddr()
38-
except OSError as exc:
39-
raise exc
40-
except ValueError as exc:
41-
raise exc
42-
except Exception as exc:
43-
raise newException(OSError, "Could not get primary IP address")
44-
if not ip.isIPv4():
45-
raise newException(ValueError, "Host does not have an IPv4 address")
46-
if not ip.isPublic():
42+
except CatchableError as e:
43+
error "Unable to get primary ip address", description = e.msg
44+
return false
45+
except Exception as e:
46+
error "Unable to get primary ip address", description = e.msg
47+
return false
48+
debug "Primary IP address", ip = ip, isIPv4 = ip.isIPv4(), isPublic = ip.isPublic()
49+
50+
return ip.isIPv4() and ip.isPublic()
51+
52+
proc getPublicIPAddress*(): IpAddress {.raises: [OSError, ValueError].} =
53+
if not hasPublicIPAddress():
4754
raise newException(ValueError, "Host does not have a public IPv4 address")
48-
ip
55+
try:
56+
return getPrimaryIPAddr()
57+
except Exception as e:
58+
raise newException(OSError, e.msg)
4959

5060
proc ipAddrMatches*(
5161
lookup: MultiAddress, addrs: seq[MultiAddress], ip4: bool = true

tests/testautotls_integration.nim

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ when defined(linux) and defined(amd64):
7575
challenge.dns01.token.len > 0
7676

7777
asyncTest "AutotlsService correctly downloads challenges":
78-
let ip =
79-
try:
80-
getPublicIPAddress()
81-
except CatchableError:
82-
skip() # host doesn't have public IPv4 address
83-
return
78+
if not hasPublicIPAddress():
79+
skip()
80+
return
8481

8582
let switch = SwitchBuilder
8683
.new()
@@ -105,9 +102,10 @@ when defined(linux) and defined(amd64):
105102
for service in switch.services:
106103
if service of AutotlsService:
107104
autotls = AutotlsService(service)
105+
break
106+
108107
if autotls.isNil():
109108
raiseAssert "No Autotls service in switch"
110-
return
111109

112110
# wait for cert to be ready
113111
await autotls.certReady.wait()

tests/transports/testws_integration.nim

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ suite "WebSocket transport integration":
3232
checkTrackers()
3333

3434
asyncTest "switch successfully dials over wss using autotls certificate":
35-
var ipAddress: IpAddress
36-
try:
37-
ipAddress = getPublicIPAddress()
38-
except:
39-
skip() # host doesn't have public IPv4 address
35+
if not hasPublicIPAddress():
36+
skip()
4037
return
4138

39+
let ip = getPublicIPAddress()
40+
4241
let switch1 = SwitchBuilder
4342
.new()
4443
.withRng(newRng())
@@ -77,7 +76,7 @@ suite "WebSocket transport integration":
7776

7877
# generate the domain for the peer
7978
# dashed-ip-add-ress.base36peerid.libp2p.direct
80-
let dashedIpAddr = ($ipAddress).replace(".", "-")
79+
let dashedIpAddr = ($ip).replace(".", "-")
8180
let serverDns =
8281
dashedIpAddr & "." & encodePeerId(switch2.peerInfo.peerId) & "." & AutoTLSDNSServer
8382

0 commit comments

Comments
 (0)