mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 22:55:06 +03:00
Merge branch 'develop' into develop
This commit is contained in:
commit
d66ddf7b96
59 changed files with 2197 additions and 2030 deletions
140
.github/workflows/ci.yml
vendored
140
.github/workflows/ci.yml
vendored
|
@ -17,7 +17,7 @@ jobs:
|
|||
steps:
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
go-version: 1.19
|
||||
- uses: actions/checkout@v3
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v3
|
||||
|
@ -51,7 +51,7 @@ jobs:
|
|||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
goversion: ["1.17", "1.18"]
|
||||
goversion: ["1.17", "1.18", "1.19"]
|
||||
|
||||
name: Build & Test (Linux, Go ${{ matrix.goversion }})
|
||||
needs: [lint]
|
||||
|
@ -75,7 +75,7 @@ jobs:
|
|||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
goversion: ["1.17", "1.18"]
|
||||
goversion: ["1.17", "1.18", "1.19"]
|
||||
|
||||
name: Build & Test (Windows, Go ${{ matrix.goversion }})
|
||||
needs: [lint]
|
||||
|
@ -99,7 +99,7 @@ jobs:
|
|||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
goversion: ["1.17", "1.18"]
|
||||
goversion: ["1.17", "1.18", "1.19"]
|
||||
|
||||
name: Build & Test (macOS, Go ${{ matrix.goversion }})
|
||||
needs: [lint]
|
||||
|
@ -129,135 +129,3 @@ jobs:
|
|||
uses: re-actors/alls-green@release/v1
|
||||
with:
|
||||
jobs: ${{ toJSON(needs) }}
|
||||
|
||||
build-packages-debian:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["amd64", "i386", "mips", "mipsel", "armhf", "armel", "arm64"]
|
||||
|
||||
name: Create Package (Debian, ${{ matrix.pkgarch }})
|
||||
needs: [tests-ok]
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
PKGARCH: ${{ matrix.pkgarch }}
|
||||
run: sh contrib/deb/generate.sh
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Debian package (${{ matrix.pkgarch }})
|
||||
path: "*.deb"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-macos:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["amd64", "arm64"]
|
||||
|
||||
name: Create Package (macOS, ${{ matrix.pkgarch }})
|
||||
needs: [tests-ok]
|
||||
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
PKGARCH: ${{ matrix.pkgarch }}
|
||||
run: sh contrib/macos/create-pkg.sh
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: macOS package (${{ matrix.pkgarch }})
|
||||
path: "*.pkg"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-windows:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["x64", "x86", "arm", "arm64"]
|
||||
|
||||
name: Create Package (Windows, ${{ matrix.pkgarch }})
|
||||
needs: [tests-ok]
|
||||
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
|
||||
- name: Build package
|
||||
run: sh contrib/msi/build-msi.sh ${{ matrix.pkgarch }}
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Windows package (${{ matrix.pkgarch }})
|
||||
path: "*.msi"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-router:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["edgerouter-x", "edgerouter-lite", "vyos-amd64", "vyos-i386"]
|
||||
|
||||
name: Create Package (Router, ${{ matrix.pkgarch }})
|
||||
needs: [tests-ok]
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
path: yggdrasil
|
||||
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
repository: neilalexander/vyatta-yggdrasil
|
||||
path: vyatta-yggdrasil
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.18
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
BUILDDIR_YGG: /home/runner/work/yggdrasil-go/yggdrasil-go/yggdrasil
|
||||
run: cd /home/runner/work/yggdrasil-go/yggdrasil-go/vyatta-yggdrasil && ./build-${{ matrix.pkgarch }}
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Router package (${{ matrix.pkgarch }})
|
||||
path: "/home/runner/work/yggdrasil-go/yggdrasil-go/vyatta-yggdrasil/*.deb"
|
||||
if-no-files-found: error
|
||||
|
|
137
.github/workflows/pkg.yml
vendored
Normal file
137
.github/workflows/pkg.yml
vendored
Normal file
|
@ -0,0 +1,137 @@
|
|||
name: Packages
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build-packages-debian:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["amd64", "i386", "mips", "mipsel", "armhf", "armel", "arm64"]
|
||||
|
||||
name: Package (Debian, ${{ matrix.pkgarch }})
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
PKGARCH: ${{ matrix.pkgarch }}
|
||||
run: sh contrib/deb/generate.sh
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Debian package (${{ matrix.pkgarch }})
|
||||
path: "*.deb"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-macos:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["amd64", "arm64"]
|
||||
|
||||
name: Package (macOS, ${{ matrix.pkgarch }})
|
||||
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
PKGARCH: ${{ matrix.pkgarch }}
|
||||
run: sh contrib/macos/create-pkg.sh
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: macOS package (${{ matrix.pkgarch }})
|
||||
path: "*.pkg"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-windows:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["x64", "x86", "arm", "arm64"]
|
||||
|
||||
name: Package (Windows, ${{ matrix.pkgarch }})
|
||||
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Build package
|
||||
run: sh contrib/msi/build-msi.sh ${{ matrix.pkgarch }}
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Windows package (${{ matrix.pkgarch }})
|
||||
path: "*.msi"
|
||||
if-no-files-found: error
|
||||
|
||||
build-packages-router:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
pkgarch: ["edgerouter-x", "edgerouter-lite", "vyos-amd64", "vyos-i386"]
|
||||
|
||||
name: Package (Router, ${{ matrix.pkgarch }})
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
path: yggdrasil
|
||||
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
repository: neilalexander/vyatta-yggdrasil
|
||||
path: vyatta-yggdrasil
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.19
|
||||
|
||||
- name: Build package
|
||||
env:
|
||||
BUILDDIR_YGG: /home/runner/work/yggdrasil-go/yggdrasil-go/yggdrasil
|
||||
run: cd /home/runner/work/yggdrasil-go/yggdrasil-go/vyatta-yggdrasil && ./build-${{ matrix.pkgarch }}
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: Router package (${{ matrix.pkgarch }})
|
||||
path: "/home/runner/work/yggdrasil-go/yggdrasil-go/vyatta-yggdrasil/*.deb"
|
||||
if-no-files-found: error
|
195
CHANGELOG.md
195
CHANGELOG.md
|
@ -1,4 +1,5 @@
|
|||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
||||
|
@ -25,89 +26,116 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- in case of vulnerabilities.
|
||||
-->
|
||||
|
||||
## [0.4.4] - 2022-07-07
|
||||
|
||||
### Fixed
|
||||
|
||||
- ICMPv6 "Packet Too Big" payload size has been increased, which should fix Path MTU Discovery (PMTUD) when two nodes have different `IfMTU` values configured
|
||||
- A crash has been fixed when handling debug packet responses
|
||||
- `yggdrasilctl getSelf` should now report coordinates correctly again
|
||||
|
||||
### Changed
|
||||
|
||||
- Go 1.17 is now required to build Yggdrasil
|
||||
|
||||
## [0.4.3] - 2022-02-06
|
||||
|
||||
### Added
|
||||
|
||||
- `bytes_sent`, `bytes_recvd` and `uptime` have been added to `getPeers`
|
||||
- Clearer logging when connections are rejected due to incompatible peer versions
|
||||
|
||||
### Fixed
|
||||
|
||||
- Latency-based parent selection tiebreak is now reliable on platforms even with low timer resolution
|
||||
- Tree distance calculation offsets have been corrected
|
||||
|
||||
## [0.4.2] - 2021-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Reverted a dependency update which resulted in problems building with Go 1.16 and running on Windows
|
||||
|
||||
## [0.4.1] - 2021-11-03
|
||||
|
||||
### Added
|
||||
|
||||
- TLS peerings now support Server Name Indication (SNI)
|
||||
- The SNI is sent automatically if the peering URI contains a DNS name
|
||||
- A custom SNI can be specified by adding the `?sni=domain.com` parameter to the peering URI
|
||||
- The SNI is sent automatically if the peering URI contains a DNS name
|
||||
- A custom SNI can be specified by adding the `?sni=domain.com` parameter to the peering URI
|
||||
- A new `ipv6rwc` API package now implements the IPv6-specific logic separate from the `tun` package
|
||||
|
||||
### Fixed
|
||||
|
||||
- A crash when calculating the partial public key for very high IPv6 addresses has been fixed
|
||||
- A crash due to a concurrent map write has been fixed
|
||||
- A crash due to missing TUN configuration has been fixed
|
||||
- A race condition in the keystore code has been fixed
|
||||
|
||||
## [0.4.0] - 2021-07-04
|
||||
|
||||
### Added
|
||||
|
||||
- New routing scheme, which is backwards incompatible with previous versions of Yggdrasil
|
||||
- The wire protocol version number, exchanged as part of the peer setup handshake, has been increased to 0.4
|
||||
- Nodes running this new version **will not** be able to peer with earlier versions of Yggdrasil
|
||||
- Please note that **the network may be temporarily unstable** while infrastructure is being upgraded to the new release
|
||||
- The wire protocol version number, exchanged as part of the peer setup handshake, has been increased to 0.4
|
||||
- Nodes running this new version **will not** be able to peer with earlier versions of Yggdrasil
|
||||
- Please note that **the network may be temporarily unstable** while infrastructure is being upgraded to the new release
|
||||
- TLS connections now use public key pinning
|
||||
- If no public key was already pinned, then the public key received as part of the TLS handshake is pinned to the connection
|
||||
- The public key received as part of the handshake is checked against the pinned keys, and if no match is found, the connection is rejected
|
||||
- If no public key was already pinned, then the public key received as part of the TLS handshake is pinned to the connection
|
||||
- The public key received as part of the handshake is checked against the pinned keys, and if no match is found, the connection is rejected
|
||||
|
||||
### Changed
|
||||
|
||||
- IP addresses are now derived from ed25519 public (signing) keys
|
||||
- Previously, addresses were derived from a hash of X25519 (Diffie-Hellman) keys
|
||||
- Importantly, this means that **all internal IPv6 addresses will change with this release** — this will affect anyone running public services or relying on Yggdrasil for remote access
|
||||
- Previously, addresses were derived from a hash of X25519 (Diffie-Hellman) keys
|
||||
- Importantly, this means that **all internal IPv6 addresses will change with this release** — this will affect anyone running public services or relying on Yggdrasil for remote access
|
||||
- It is now recommended to peer over TLS
|
||||
- Link-local peers from multicast peer discovery will now connect over TLS, with the key from the multicast beacon pinned to the connection
|
||||
- `socks://` peers now expect the destination endpoint to be a `tls://` listener, instead of a `tcp://` listener
|
||||
- Link-local peers from multicast peer discovery will now connect over TLS, with the key from the multicast beacon pinned to the connection
|
||||
- `socks://` peers now expect the destination endpoint to be a `tls://` listener, instead of a `tcp://` listener
|
||||
- Multicast peer discovery is now more configurable
|
||||
- There are separate configuration options to control if beacons are sent, what port to listen on for incoming connections (if sending beacons), and whether or not to listen for beacons from other nodes (and open connections when receiving a beacon)
|
||||
- Each configuration entry in the list specifies a regular expression to match against interface names
|
||||
- If an interface matches multiple regex in the list, it will use the settings for the first entry in the list that it matches with
|
||||
- There are separate configuration options to control if beacons are sent, what port to listen on for incoming connections (if sending beacons), and whether or not to listen for beacons from other nodes (and open connections when receiving a beacon)
|
||||
- Each configuration entry in the list specifies a regular expression to match against interface names
|
||||
- If an interface matches multiple regex in the list, it will use the settings for the first entry in the list that it matches with
|
||||
- The session and routing code has been entirely redesigned and rewritten
|
||||
- This is still an early work-in-progress, so the code hasn't been as well tested or optimized as the old code base — please bear with us for these next few releases as we work through any bugs or issues
|
||||
- Generally speaking, we expect to see reduced bandwidth use and improved reliability with the new design, especially in cases where nodes move around or change peerings frequently
|
||||
- Cryptographic sessions no longer use a single shared (ephemeral) secret for the entire life of the session. Keys are now rotated regularly for ongoing sessions (currently rotated at least once per round trip exchange of traffic, subject to change in future releases)
|
||||
- Source routing has been added. Under normal circumstances, this is what is used to forward session traffic (e.g. the user's IPv6 traffic)
|
||||
- DHT-based routing has been added. This is used when the sender does not know a source route to the destination. Forwarding through the DHT is less efficient, but the only information that it requires the sender to know is the destination node's (static) key. This is primarily used during the key exchange at session setup, or as a temporary fallback when a source route fails due to changes in the network
|
||||
- The new DHT design is no longer RPC-based, does not support crawling and does not inherently allow nodes to look up the owner of an arbitrary key. Responding to lookups is now implemented at the application level and a response is only sent if the destination key matches the node's `/128` IP or `/64` prefix
|
||||
- The greedy routing scheme, used to forward all traffic in previous releases, is now only used for protocol traffic (i.e. DHT setup and source route discovery)
|
||||
- The routing logic now lives in a [standalone library](https://github.com/Arceliar/ironwood). You are encouraged **not** to use it, as it's still considered pre-alpha, but it's available for those who want to experiment with the new routing algorithm in other contexts
|
||||
- Session MTUs may be slightly lower now, in order to accommodate large packet headers if required
|
||||
- This is still an early work-in-progress, so the code hasn't been as well tested or optimized as the old code base — please bear with us for these next few releases as we work through any bugs or issues
|
||||
- Generally speaking, we expect to see reduced bandwidth use and improved reliability with the new design, especially in cases where nodes move around or change peerings frequently
|
||||
- Cryptographic sessions no longer use a single shared (ephemeral) secret for the entire life of the session. Keys are now rotated regularly for ongoing sessions (currently rotated at least once per round trip exchange of traffic, subject to change in future releases)
|
||||
- Source routing has been added. Under normal circumstances, this is what is used to forward session traffic (e.g. the user's IPv6 traffic)
|
||||
- DHT-based routing has been added. This is used when the sender does not know a source route to the destination. Forwarding through the DHT is less efficient, but the only information that it requires the sender to know is the destination node's (static) key. This is primarily used during the key exchange at session setup, or as a temporary fallback when a source route fails due to changes in the network
|
||||
- The new DHT design is no longer RPC-based, does not support crawling and does not inherently allow nodes to look up the owner of an arbitrary key. Responding to lookups is now implemented at the application level and a response is only sent if the destination key matches the node's `/128` IP or `/64` prefix
|
||||
- The greedy routing scheme, used to forward all traffic in previous releases, is now only used for protocol traffic (i.e. DHT setup and source route discovery)
|
||||
- The routing logic now lives in a [standalone library](https://github.com/Arceliar/ironwood). You are encouraged **not** to use it, as it's still considered pre-alpha, but it's available for those who want to experiment with the new routing algorithm in other contexts
|
||||
- Session MTUs may be slightly lower now, in order to accommodate large packet headers if required
|
||||
- Many of the admin functions available over `yggdrasilctl` have been changed or removed as part of rewrites to the code
|
||||
- Several remote `debug` functions have been added temporarily, to allow for crawling and census gathering during the transition to the new version, but we intend to remove this at some point in the (possibly distant) future
|
||||
- The list of available functions will likely be expanded in future releases
|
||||
- Several remote `debug` functions have been added temporarily, to allow for crawling and census gathering during the transition to the new version, but we intend to remove this at some point in the (possibly distant) future
|
||||
- The list of available functions will likely be expanded in future releases
|
||||
- The configuration file format has been updated in response to the changed/removed features
|
||||
|
||||
### Removed
|
||||
|
||||
- Tunnel routing (a.k.a. crypto-key routing or "CKR") has been removed
|
||||
- It was far too easy to accidentally break routing altogether by capturing the route to peers with the TUN adapter
|
||||
- We recommend tunnelling an existing standard over Yggdrasil instead (e.g. `ip6gre`, `ip6gretap` or other similar encapsulations, using Yggdrasil IPv6 addresses as the tunnel endpoints)
|
||||
- All `TunnelRouting` configuration options will no longer take effect
|
||||
- It was far too easy to accidentally break routing altogether by capturing the route to peers with the TUN adapter
|
||||
- We recommend tunnelling an existing standard over Yggdrasil instead (e.g. `ip6gre`, `ip6gretap` or other similar encapsulations, using Yggdrasil IPv6 addresses as the tunnel endpoints)
|
||||
- All `TunnelRouting` configuration options will no longer take effect
|
||||
- Session firewall has been removed
|
||||
- This was never a true firewall — it didn't behave like a stateful IP firewall, often allowed return traffic unexpectedly and was simply a way to prevent a node from being flooded with unwanted sessions, so the name could be misleading and usually lead to a false sense of security
|
||||
- Due to design changes, the new code needs to address the possible memory exhaustion attacks in other ways and a single configurable list no longer makes sense
|
||||
- Users who want a firewall or other packet filter mechansim should configure something supported by their OS instead (e.g. `ip6tables`)
|
||||
- All `SessionFirewall` configuration options will no longer take effect
|
||||
- This was never a true firewall — it didn't behave like a stateful IP firewall, often allowed return traffic unexpectedly and was simply a way to prevent a node from being flooded with unwanted sessions, so the name could be misleading and usually lead to a false sense of security
|
||||
- Due to design changes, the new code needs to address the possible memory exhaustion attacks in other ways and a single configurable list no longer makes sense
|
||||
- Users who want a firewall or other packet filter mechansim should configure something supported by their OS instead (e.g. `ip6tables`)
|
||||
- All `SessionFirewall` configuration options will no longer take effect
|
||||
- `SIGHUP` handling to reload the configuration at runtime has been removed
|
||||
- It was not obvious which parts of the configuration could be reloaded at runtime, and which required the application to be killed and restarted to take effect
|
||||
- Reloading the config without restarting was also a delicate and bug-prone process, and was distracting from more important developments
|
||||
- `SIGHUP` will be handled normally (i.e. by exiting)
|
||||
- It was not obvious which parts of the configuration could be reloaded at runtime, and which required the application to be killed and restarted to take effect
|
||||
- Reloading the config without restarting was also a delicate and bug-prone process, and was distracting from more important developments
|
||||
- `SIGHUP` will be handled normally (i.e. by exiting)
|
||||
- `cmd/yggrasilsim` has been removed, and is unlikely to return to this repository
|
||||
|
||||
## [0.3.16] - 2021-03-18
|
||||
|
||||
### Added
|
||||
|
||||
- New simulation code under `cmd/yggdrasilsim` (work-in-progress)
|
||||
|
||||
### Changed
|
||||
|
||||
- Multi-threading in the switch
|
||||
- Swich lookups happen independently for each (incoming) peer connection, instead of being funneled to a single dedicated switch worker
|
||||
- Packets are queued for each (outgoing) peer connection, instead of being handled by a single dedicated switch worker
|
||||
|
@ -123,13 +151,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Upgrade build to Go 1.16
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed a bug where the connection listener could exit prematurely due to resoruce exhaustion (if e.g. too many connections were opened)
|
||||
- Fixed DefaultIfName for OpenBSD (`/dev/tun0` -> `tun0`)
|
||||
- Fixed an issue where a peer could sometimes never be added to the switch
|
||||
- Fixed a goroutine leak that could occur if a peer with an open connection continued to spam additional connection attempts
|
||||
|
||||
## [0.3.15] - 2020-09-27
|
||||
|
||||
### Added
|
||||
|
||||
- Support for pinning remote public keys in peering strings has been added, e.g.
|
||||
- By signing public key: `tcp://host:port?ed25519=key`
|
||||
- By encryption public key: `tcp://host:port?curve25519=key`
|
||||
|
@ -139,25 +170,32 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Added support for SOCKS proxy authentication, e.g. `socks://user@password:host/...`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Some bugs in the multicast code that could cause unnecessary CPU usage have been fixed
|
||||
- A possible multicast deadlock on macOS when enumerating interfaces has been fixed
|
||||
- A deadlock in the connection code has been fixed
|
||||
- Updated HJSON dependency that caused some build problems
|
||||
|
||||
### Changed
|
||||
|
||||
- `DisconnectPeer` and `RemovePeer` have been separated and implemented properly now
|
||||
- Less nodes are stored in the DHT now, reducing ambient network traffic and possible instability
|
||||
- Default config file for FreeBSD is now at `/usr/local/etc/yggdrasil.conf` instead of `/etc/yggdrasil.conf`
|
||||
|
||||
## [0.3.14] - 2020-03-28
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixes a memory leak that may occur if packets are incorrectly never removed from a switch queue
|
||||
|
||||
### Changed
|
||||
|
||||
- Make DHT searches a bit more reliable by tracking the 16 most recently visited nodes
|
||||
|
||||
## [0.3.13] - 2020-02-21
|
||||
|
||||
### Added
|
||||
|
||||
- Support for the Wireguard TUN driver, which now replaces Water and provides far better support and performance on Windows
|
||||
- Windows `.msi` installer files are now supported (bundling the Wireguard TUN driver)
|
||||
- NodeInfo code is now actorised, should be more reliable
|
||||
|
@ -165,6 +203,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- The Yggdrasil API now supports dialing a remote node using the public key instead of the Node ID
|
||||
|
||||
### Changed
|
||||
|
||||
- The `-loglevel` command line parameter is now cumulative and automatically includes all levels below the one specified
|
||||
- DHT search code has been significantly simplified and processes rumoured nodes in parallel, speeding up search time
|
||||
- DHT search results are now sorted
|
||||
|
@ -172,26 +211,32 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- The Yggdrasil API now returns public keys instead of node IDs when querying for local and remote addresses
|
||||
|
||||
### Fixed
|
||||
|
||||
- The multicast code no longer panics when shutting down the node
|
||||
- A potential OOB error when calculating IPv4 flow labels (when tunnel routing is enabled) has been fixed
|
||||
- A bug resulting in incorrect idle notifications in the switch should now be fixed
|
||||
- MTUs are now using a common datatype throughout the codebase
|
||||
|
||||
### Removed
|
||||
|
||||
- TAP mode has been removed entirely, since it is no longer supported with the Wireguard TUN package. Please note that if you are using TAP mode, you may need to revise your config!
|
||||
- NetBSD support has been removed until the Wireguard TUN package supports NetBSD
|
||||
|
||||
## [0.3.12] - 2019-11-24
|
||||
|
||||
### Added
|
||||
|
||||
- New API functions `SetMaximumSessionMTU` and `GetMaximumSessionMTU`
|
||||
- New command line parameters `-address` and `-subnet` for getting the address/subnet from the config file, for use with `-useconffile` or `-useconf`
|
||||
- A warning is now produced in the Yggdrasil output at startup when the MTU in the config is invalid or has been adjusted for some reason
|
||||
|
||||
### Changed
|
||||
|
||||
- On Linux, outgoing `InterfacePeers` connections now use `SO_BINDTODEVICE` to prefer an outgoing interface
|
||||
- The `genkeys` utility is now in `cmd` rather than `misc`
|
||||
|
||||
### Fixed
|
||||
|
||||
- A data race condition has been fixed when updating session coordinates
|
||||
- A crash when shutting down when no multicast interfaces are configured has been fixed
|
||||
- A deadlock when calling `AddPeer` multiple times has been fixed
|
||||
|
@ -200,10 +245,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- The MTU calculation now correctly accounts for ethernet headers when running in TAP mode
|
||||
|
||||
## [0.3.11] - 2019-10-25
|
||||
|
||||
### Added
|
||||
|
||||
- Support for TLS listeners and peers has been added, allowing the use of `tls://host:port` in `Peers`, `InterfacePeers` and `Listen` configuration settings - this allows hiding Yggdrasil peerings inside regular TLS connections
|
||||
|
||||
### Changed
|
||||
|
||||
- Go 1.13 or later is now required for building Yggdrasil
|
||||
- Some exported API functions have been updated to work with standard Go interfaces:
|
||||
- `net.Conn` instead of `yggdrasil.Conn`
|
||||
|
@ -213,27 +261,35 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Multicast module reloading behaviour has been improved
|
||||
|
||||
### Fixed
|
||||
|
||||
- An incorrectly held mutex in the crypto-key routing code has been fixed
|
||||
- Multicast module no longer opens a listener socket if no multicast interfaces are configured
|
||||
|
||||
## [0.3.10] - 2019-10-10
|
||||
|
||||
### Added
|
||||
|
||||
- The core library now includes several unit tests for peering and `yggdrasil.Conn` connections
|
||||
|
||||
### Changed
|
||||
|
||||
- On recent Linux kernels, Yggdrasil will now set the `tcp_congestion_control` algorithm used for its own TCP sockets to [BBR](https://github.com/google/bbr), which reduces latency under load
|
||||
- The systemd service configuration in `contrib` (and, by extension, some of our packages) now attempts to load the `tun` module, in case TUN/TAP support is available but not loaded, and it restricts Yggdrasil to the `CAP_NET_ADMIN` capability for managing the TUN/TAP adapter, rather than letting it do whatever the (typically `root`) user can do
|
||||
|
||||
### Fixed
|
||||
|
||||
- The `yggdrasil.Conn.RemoteAddr()` function no longer blocks, fixing a deadlock when CKR is used while under heavy load
|
||||
|
||||
## [0.3.9] - 2019-09-27
|
||||
|
||||
### Added
|
||||
|
||||
- Yggdrasil will now complain more verbosely when a peer URI is incorrectly formatted
|
||||
- Soft-shutdown methods have been added, allowing a node to shut down gracefully when terminated
|
||||
- New multicast interval logic which sends multicast beacons more often when Yggdrasil is first started to increase the chance of finding nearby nodes quickly after startup
|
||||
|
||||
### Changed
|
||||
|
||||
- The switch now buffers packets more eagerly in an attempt to give the best link a chance to send, which appears to reduce packet reordering when crossing aggregate sets of peerings
|
||||
- Substantial amounts of the codebase have been refactored to use the actor model, which should substantially reduce the chance of deadlocks
|
||||
- Nonce tracking in sessions has been modified so that memory usage is reduced whilst still only allowing duplicate packets within a small window
|
||||
|
@ -242,6 +298,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- The maximum queue size is now managed exclusively by the switch rather than by the core
|
||||
|
||||
### Fixed
|
||||
|
||||
- The broken `hjson-go` dependency which affected builds of the previous version has now been resolved in the module manifest
|
||||
- Some minor memory leaks in the switch have been fixed, which improves memory usage on mobile builds
|
||||
- A memory leak in the add-peer loop has been fixed
|
||||
|
@ -253,10 +310,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- A panic which could occur when the TUN/TAP interface reads an undersized/corrupted packet has been fixed
|
||||
|
||||
### Removed
|
||||
|
||||
- A number of legacy debug functions have now been removed and a number of exported API functions are now better documented
|
||||
|
||||
## [0.3.8] - 2019-08-21
|
||||
|
||||
### Changed
|
||||
|
||||
- Yggdrasil can now send multiple packets from the switch at once, which results in improved throughput with smaller packets or lower MTUs
|
||||
- Performance has been slightly improved by not allocating cancellations where not necessary
|
||||
- Crypto-key routing options have been renamed for clarity
|
||||
|
@ -269,20 +329,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- New nonce tracking should help to reduce the number of packets dropped as a result of multiple/aggregate paths or congestion control in the switch
|
||||
|
||||
### Fixed
|
||||
|
||||
- A deadlock was fixed in the session code which could result in Yggdrasil failing to pass traffic after some time
|
||||
|
||||
### Security
|
||||
|
||||
- Address verification was not strict enough, which could result in a malicious session sending traffic with unexpected or spoofed source or destination addresses which Yggdrasil could fail to reject
|
||||
- Versions `0.3.6` and `0.3.7` are vulnerable - users of these versions should upgrade as soon as possible
|
||||
- Versions `0.3.5` and earlier are not affected
|
||||
|
||||
## [0.3.7] - 2019-08-14
|
||||
|
||||
### Changed
|
||||
|
||||
- The switch should now forward packets along a single path more consistently in cases where congestion is low and multiple equal-length paths exist, which should improve stability and result in fewer out-of-order packets
|
||||
- Sessions should now be more tolerant of out-of-order packets, by replacing a bitmask with a variable sized heap+map structure to track recently received nonces, which should reduce the number of packets dropped due to reordering when multiple paths are used or multiple independent flows are transmitted through the same session
|
||||
- The admin socket can no longer return a dotfile representation of the known parts of the network, this could be rebuilt by clients using information from `getSwitchPeers`,`getDHT` and `getSessions`
|
||||
|
||||
### Fixed
|
||||
|
||||
- A number of significant performance regressions introduced in version 0.3.6 have been fixed, resulting in better performance
|
||||
- Flow labels are now used to prioritise traffic flows again correctly
|
||||
- In low-traffic scenarios where there are multiple peerings between a pair of nodes, Yggdrasil now prefers the most active peering instead of the least active, helping to reduce packet reordering
|
||||
|
@ -296,13 +361,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- A number of minor allocation and pointer fixes
|
||||
|
||||
## [0.3.6] - 2019-08-03
|
||||
|
||||
### Added
|
||||
|
||||
- Yggdrasil now has a public API with interfaces such as `yggdrasil.ConnDialer`, `yggdrasil.ConnListener` and `yggdrasil.Conn` for using Yggdrasil as a transport directly within applications
|
||||
- Session gatekeeper functions, part of the API, which can be used to control whether to allow or reject incoming or outgoing sessions dynamically (compared to the previous fixed whitelist/blacklist approach)
|
||||
- Support for logging to files or syslog (where supported)
|
||||
- Platform defaults now include the ability to set sane defaults for multicast interfaces
|
||||
|
||||
### Changed
|
||||
|
||||
- Following a massive refactoring exercise, Yggdrasil's codebase has now been broken out into modules
|
||||
- Core node functionality in the `yggdrasil` package with a public API
|
||||
- This allows Yggdrasil to be integrated directly into other applications and used as a transport
|
||||
|
@ -315,6 +383,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Upstream dependency references have been updated, which includes a number of fixes in the Water library
|
||||
|
||||
### Fixed
|
||||
|
||||
- Multicast discovery is no longer disabled if the nominated interfaces aren't available on the system yet, e.g. during boot
|
||||
- Multicast interfaces are now re-evaluated more frequently so that Yggdrasil doesn't need to be restarted to use interfaces that have become available since startup
|
||||
- Admin socket error cases are now handled better
|
||||
|
@ -329,12 +398,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Lots of small bug tweaks and clean-ups throughout the codebase
|
||||
|
||||
## [0.3.5] - 2019-03-13
|
||||
|
||||
### Fixed
|
||||
|
||||
- The `AllowedEncryptionPublicKeys` option has now been fixed to handle incoming connections properly and no longer blocks outgoing connections (this was broken in v0.3.4)
|
||||
- Multicast TCP listeners will now be stopped correctly when the link-local address on the interface changes or disappears altogether
|
||||
|
||||
## [0.3.4] - 2019-03-12
|
||||
|
||||
### Added
|
||||
|
||||
- Support for multiple listeners (although currently only TCP listeners are supported)
|
||||
- New multicast behaviour where each multicast interface is given its own link-local listener and does not depend on the `Listen` configuration
|
||||
- Blocking detection in the switch to avoid parenting a blocked peer
|
||||
|
@ -345,6 +418,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Added `LinkLocalTCPPort` option for controlling the port number that link-local TCP listeners will listen on by default when setting up `MulticastInterfaces` (a node restart is currently required for changes to `LinkLocalTCPPort` to take effect - it cannot be updated by reloading config during runtime)
|
||||
|
||||
### Changed
|
||||
|
||||
- The `Listen` configuration statement is now an array instead of a string
|
||||
- The `Listen` configuration statement should now conform to the same formatting as peers with the protocol prefix, e.g. `tcp://[::]:0`
|
||||
- Session workers are now non-blocking
|
||||
|
@ -353,6 +427,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Peer forwarding is now prioritised instead of randomised
|
||||
|
||||
### Fixed
|
||||
|
||||
- Admin socket `getTunTap` call now returns properly instead of claiming no interface is enabled in all cases
|
||||
- Handling of `getRoutes` etc in `yggdrasilctl` is now working
|
||||
- Local interface names are no longer leaked in multicast packets
|
||||
|
@ -360,7 +435,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Yggdrasil now correctly responds to multicast interfaces going up and down during runtime
|
||||
|
||||
## [0.3.3] - 2019-02-18
|
||||
|
||||
### Added
|
||||
|
||||
- Dynamic reconfiguration, which allows reloading the configuration file to make changes during runtime by sending a `SIGHUP` signal (note: this only works with `-useconffile` and not `-useconf` and currently reconfiguring TUN/TAP is not supported)
|
||||
- Support for building Yggdrasil as an iOS or Android framework if the appropriate tools (e.g. `gomobile`/`gobind` + SDKs) are available
|
||||
- Connection contexts used for TCP connections which allow more exotic socket options to be set, e.g.
|
||||
|
@ -369,26 +446,33 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Flexible logging support, which allows for logging at different levels of verbosity
|
||||
|
||||
### Changed
|
||||
|
||||
- Switch changes to improve parent selection
|
||||
- Node configuration is now stored centrally, rather than having fragments/copies distributed at startup time
|
||||
- Significant refactoring in various areas, including for link types (TCP, AWDL etc), generic streams and adapters
|
||||
- macOS builds through CircleCI are now 64-bit only
|
||||
|
||||
### Fixed
|
||||
|
||||
- Simplified `systemd` service now in `contrib`
|
||||
|
||||
### Removed
|
||||
|
||||
- `ReadTimeout` option is now deprecated
|
||||
|
||||
## [0.3.2] - 2018-12-26
|
||||
|
||||
### Added
|
||||
|
||||
- The admin socket is now multithreaded, greatly improving performance of the crawler and allowing concurrent lookups to take place
|
||||
- The ability to hide NodeInfo defaults through either setting the `NodeInfoPrivacy` option or through setting individual `NodeInfo` attributes to `null`
|
||||
|
||||
### Changed
|
||||
|
||||
- The `armhf` build now targets ARMv6 instead of ARMv7, adding support for Raspberry Pi Zero and other older models, amongst others
|
||||
|
||||
### Fixed
|
||||
|
||||
- DHT entries are now populated using a copy in memory to fix various potential DHT bugs
|
||||
- DHT traffic should now throttle back exponentially to reduce idle traffic
|
||||
- Adjust how nodes are inserted into the DHT which should help to reduce some incorrect DHT traffic
|
||||
|
@ -396,7 +480,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- In TUN mode, ICMPv6 packets are now ignored whereas they were incorrectly processed before
|
||||
|
||||
## [0.3.1] - 2018-12-17
|
||||
|
||||
### Added
|
||||
|
||||
- Build name and version is now imprinted onto the binaries if available/specified during build
|
||||
- Ability to disable admin socket with `AdminListen: none`
|
||||
- `AF_UNIX` domain sockets for the admin socket
|
||||
|
@ -406,18 +492,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Adds flags `-c`, `-l` and `-t` to `build` script for specifying `GCFLAGS`, `LDFLAGS` or whether to keep symbol/DWARF tables
|
||||
|
||||
### Changed
|
||||
|
||||
- Default `AdminListen` in newly generated config is now `unix:///var/run/yggdrasil.sock`
|
||||
- Formatting of `getRoutes` in the admin socket has been improved
|
||||
- Debian package now adds `yggdrasil` group to assist with `AF_UNIX` admin socket permissions
|
||||
- Crypto, address and other utility code refactored into separate Go packages
|
||||
|
||||
### Fixed
|
||||
|
||||
- Switch peer convergence is now much faster again (previously it was taking up to a minute once the peering was established)
|
||||
- `yggdrasilctl` is now less prone to crashing when parameters are specified incorrectly
|
||||
- Panic fixed when `Peers` or `InterfacePeers` was commented out
|
||||
|
||||
## [0.3.0] - 2018-12-12
|
||||
|
||||
### Added
|
||||
|
||||
- Crypto-key routing support for tunnelling both IPv4 and IPv6 over Yggdrasil
|
||||
- Add advanced `SwitchOptions` in configuration file for tuning the switch
|
||||
- Add `dhtPing` to the admin socket to aid in crawling the network
|
||||
|
@ -430,6 +520,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- `yggdrasilctl` now returns more useful logging in the event of a fatal error
|
||||
|
||||
### Changed
|
||||
|
||||
- Switched to Chord DHT (instead of Kademlia, although still compatible at the protocol level)
|
||||
- The `AdminListen` option and `yggdrasilctl` now default to `unix:///var/run/yggdrasil.sock` on BSDs, macOS and Linux
|
||||
- Cleaned up some of the parameter naming in the admin socket
|
||||
|
@ -439,12 +530,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- `yggdrasilctl` now has more useful help text (with `-help` or when no arguments passed)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Memory leaks in the DHT fixed
|
||||
- Crash fixed where the ICMPv6 NDP goroutine would incorrectly start in TUN mode
|
||||
- Removing peers from the switch table if they stop sending switch messages but keep the TCP connection alive
|
||||
|
||||
## [0.2.7] - 2018-10-13
|
||||
|
||||
### Added
|
||||
|
||||
- Session firewall, which makes it possible to control who can open sessions with your node
|
||||
- Add `getSwitchQueues` to admin socket
|
||||
- Add `InterfacePeers` for configuring static peerings via specific network interfaces
|
||||
|
@ -452,81 +546,106 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- FreeBSD service script in `contrib`
|
||||
|
||||
## Changed
|
||||
|
||||
- CircleCI builds are now built with Go 1.11 instead of Go 1.9
|
||||
|
||||
## Fixed
|
||||
|
||||
- Race condition in the switch table, reported by trn
|
||||
- Debug builds are now tested by CircleCI as well as platform release builds
|
||||
- Port number fixed on admin graph from unknown nodes
|
||||
|
||||
## [0.2.6] - 2018-07-31
|
||||
|
||||
### Added
|
||||
|
||||
- Configurable TCP timeouts to assist in peering over Tor/I2P
|
||||
- Prefer IPv6 flow label when extending coordinates to sort backpressure queues
|
||||
- `arm64` builds through CircleCI
|
||||
|
||||
### Changed
|
||||
|
||||
- Sort dot graph links by integer value
|
||||
|
||||
## [0.2.5] - 2018-07-19
|
||||
|
||||
### Changed
|
||||
|
||||
- Make `yggdrasilctl` less case sensitive
|
||||
- More verbose TCP disconnect messages
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed debug builds
|
||||
- Cap maximum MTU on Linux in TAP mode
|
||||
- Process successfully-read TCP traffic before checking for / handling errors (fixes EOF behavior)
|
||||
|
||||
## [0.2.4] - 2018-07-08
|
||||
|
||||
### Added
|
||||
|
||||
- Support for UNIX domain sockets for the admin socket using `unix:///path/to/file.sock`
|
||||
- Centralised platform-specific defaults
|
||||
|
||||
### Changed
|
||||
|
||||
- Backpressure tuning, including reducing resource consumption
|
||||
|
||||
### Fixed
|
||||
|
||||
- macOS local ping bug, which previously prevented you from pinging your own `utun` adapter's IPv6 address
|
||||
|
||||
## [0.2.3] - 2018-06-29
|
||||
|
||||
### Added
|
||||
|
||||
- Begin keeping changelog (incomplete and possibly inaccurate information before this point).
|
||||
- Build RPMs in CircleCI using alien. This provides package support for Fedora, Red Hat Enterprise Linux, CentOS and other RPM-based distributions.
|
||||
|
||||
### Changed
|
||||
|
||||
- Local backpressure improvements.
|
||||
- Change `box_pub_key` to `key` in admin API for simplicity.
|
||||
- Session cleanup.
|
||||
|
||||
## [0.2.2] - 2018-06-21
|
||||
|
||||
### Added
|
||||
|
||||
- Add `yggdrasilconf` utility for testing with the `vyatta-yggdrasil` package.
|
||||
- Add a randomized retry delay after TCP disconnects, to prevent synchronization livelocks.
|
||||
|
||||
### Changed
|
||||
|
||||
- Update build script to strip by default, which significantly reduces the size of the binary.
|
||||
- Add debug `-d` and UPX `-u` flags to the `build` script.
|
||||
- Start pprof in debug builds based on an environment variable (e.g. `PPROFLISTEN=localhost:6060`), instead of a flag.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fix typo in big-endian BOM so that both little-endian and big-endian UTF-16 files are detected correctly.
|
||||
|
||||
## [0.2.1] - 2018-06-15
|
||||
|
||||
### Changed
|
||||
|
||||
- The address range was moved from `fd00::/8` to `200::/7`. This range was chosen as it is marked as deprecated. The change prevents overlap with other ULA privately assigned ranges.
|
||||
|
||||
### Fixed
|
||||
|
||||
- UTF-16 detection conversion for configuration files, which can particularly be a problem on Windows 10 if a configuration file is generated from within PowerShell.
|
||||
- Fixes to the Debian package control file.
|
||||
- Fixes to the launchd service for macOS.
|
||||
- Fixes to the DHT and switch.
|
||||
|
||||
## [0.2.0] - 2018-06-13
|
||||
|
||||
### Added
|
||||
|
||||
- Exchange version information during connection setup, to prevent connections with incompatible versions.
|
||||
|
||||
### Changed
|
||||
|
||||
- Wire format changes (backwards incompatible).
|
||||
- Less maintenance traffic per peer.
|
||||
- Exponential back-off for DHT maintenance traffic (less maintenance traffic for known good peers).
|
||||
|
@ -534,18 +653,24 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
- Use local queue sizes for a sort of local-only backpressure routing, instead of the removed bandwidth estimates, when deciding where to send a packet.
|
||||
|
||||
### Removed
|
||||
|
||||
- UDP peering, this may be added again if/when a better implementation appears.
|
||||
- Per peer bandwidth estimation, as this has been replaced with an early local backpressure implementation.
|
||||
|
||||
## [0.1.0] - 2018-02-01
|
||||
|
||||
### Added
|
||||
|
||||
- Adopt semantic versioning.
|
||||
|
||||
### Changed
|
||||
|
||||
- Wire format changes (backwards incompatible).
|
||||
- Many other undocumented changes leading up to this release and before the next one.
|
||||
|
||||
## [0.0.1] - 2017-12-28
|
||||
|
||||
### Added
|
||||
|
||||
- First commit.
|
||||
- Initial public release.
|
||||
|
|
|
@ -8,10 +8,11 @@ import (
|
|||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
|
@ -27,18 +28,17 @@ import (
|
|||
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/tuntap"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||
)
|
||||
|
||||
type node struct {
|
||||
core core.Core
|
||||
config *config.NodeConfig
|
||||
tuntap *tuntap.TunAdapter
|
||||
core *core.Core
|
||||
tun *tun.TunAdapter
|
||||
multicast *multicast.Multicast
|
||||
admin *admin.AdminSocket
|
||||
}
|
||||
|
@ -51,10 +51,10 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf
|
|||
var err error
|
||||
if useconffile != "" {
|
||||
// Read the file from the filesystem
|
||||
conf, err = ioutil.ReadFile(useconffile)
|
||||
conf, err = os.ReadFile(useconffile)
|
||||
} else {
|
||||
// Read the file from stdin.
|
||||
conf, err = ioutil.ReadAll(os.Stdin)
|
||||
conf, err = io.ReadAll(os.Stdin)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -81,48 +81,6 @@ func readConfig(log *log.Logger, useconf bool, useconffile string, normaliseconf
|
|||
if err := hjson.Unmarshal(conf, &dat); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Check if we have old field names
|
||||
if _, ok := dat["TunnelRouting"]; ok {
|
||||
log.Warnln("WARNING: Tunnel routing is no longer supported")
|
||||
}
|
||||
if old, ok := dat["SigningPrivateKey"]; ok {
|
||||
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option has been renamed to \"PrivateKey\"")
|
||||
if _, ok := dat["PrivateKey"]; !ok {
|
||||
if privstr, err := hex.DecodeString(old.(string)); err == nil {
|
||||
priv := ed25519.PrivateKey(privstr)
|
||||
pub := priv.Public().(ed25519.PublicKey)
|
||||
dat["PrivateKey"] = hex.EncodeToString(priv[:])
|
||||
dat["PublicKey"] = hex.EncodeToString(pub[:])
|
||||
} else {
|
||||
log.Warnln("WARNING: The \"SigningPrivateKey\" configuration option contains an invalid value and will be ignored")
|
||||
}
|
||||
}
|
||||
}
|
||||
if oldmc, ok := dat["MulticastInterfaces"]; ok {
|
||||
if oldmcvals, ok := oldmc.([]interface{}); ok {
|
||||
var newmc []config.MulticastInterfaceConfig
|
||||
for _, oldmcval := range oldmcvals {
|
||||
if str, ok := oldmcval.(string); ok {
|
||||
newmc = append(newmc, config.MulticastInterfaceConfig{
|
||||
Regex: str,
|
||||
Beacon: true,
|
||||
Listen: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
if newmc != nil {
|
||||
if oldport, ok := dat["LinkLocalTCPPort"]; ok {
|
||||
// numbers parse to float64 by default
|
||||
if port, ok := oldport.(float64); ok {
|
||||
for idx := range newmc {
|
||||
newmc[idx].Port = uint16(port)
|
||||
}
|
||||
}
|
||||
}
|
||||
dat["MulticastInterfaces"] = newmc
|
||||
}
|
||||
}
|
||||
}
|
||||
// Sanitise the config
|
||||
confJson, err := json.Marshal(dat)
|
||||
if err != nil {
|
||||
|
@ -261,7 +219,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||
return
|
||||
case args.autoconf:
|
||||
// Use an autoconf-generated config, this will give us random keys and
|
||||
// port numbers, and will use an automatically selected TUN/TAP interface.
|
||||
// port numbers, and will use an automatically selected TUN interface.
|
||||
cfg = defaults.GenerateConfig()
|
||||
case args.useconffile != "" || args.useconf:
|
||||
// Read the configuration from either stdin or from the filesystem
|
||||
|
@ -327,45 +285,86 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||
fmt.Println(ipnet.String())
|
||||
}
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
// Setup the Yggdrasil node itself. The node{} type includes a Core, so we
|
||||
// don't need to create this manually.
|
||||
n := node{config: cfg}
|
||||
// Now start Yggdrasil - this starts the DHT, router, switch and other core
|
||||
// components needed for Yggdrasil to operate
|
||||
if err = n.core.Start(cfg, logger); err != nil {
|
||||
logger.Errorln("An error occurred during startup")
|
||||
panic(err)
|
||||
n := &node{}
|
||||
|
||||
// Setup the Yggdrasil node itself.
|
||||
{
|
||||
sk, err := hex.DecodeString(cfg.PrivateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
options := []core.SetupOption{}
|
||||
for _, addr := range cfg.Listen {
|
||||
options = append(options, core.ListenAddress(addr))
|
||||
}
|
||||
for _, peer := range cfg.Peers {
|
||||
options = append(options, core.Peer{URI: peer})
|
||||
}
|
||||
for intf, peers := range cfg.InterfacePeers {
|
||||
for _, peer := range peers {
|
||||
options = append(options, core.Peer{URI: peer, SourceInterface: intf})
|
||||
}
|
||||
}
|
||||
for _, allowed := range cfg.AllowedPublicKeys {
|
||||
k, err := hex.DecodeString(allowed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
options = append(options, core.AllowedPublicKey(k[:]))
|
||||
}
|
||||
if n.core, err = core.New(sk[:], logger, options...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
// Register the session firewall gatekeeper function
|
||||
// Allocate our modules
|
||||
n.admin = &admin.AdminSocket{}
|
||||
n.multicast = &multicast.Multicast{}
|
||||
n.tuntap = &tuntap.TunAdapter{}
|
||||
// Start the admin socket
|
||||
if err := n.admin.Init(&n.core, cfg, logger, nil); err != nil {
|
||||
logger.Errorln("An error occurred initialising admin socket:", err)
|
||||
} else if err := n.admin.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting admin socket:", err)
|
||||
|
||||
// Setup the admin socket.
|
||||
{
|
||||
options := []admin.SetupOption{
|
||||
admin.ListenAddress(cfg.AdminListen),
|
||||
}
|
||||
if n.admin, err = admin.New(n.core, logger, options...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if n.admin != nil {
|
||||
n.admin.SetupAdminHandlers()
|
||||
}
|
||||
}
|
||||
n.admin.SetupAdminHandlers(n.admin)
|
||||
// Start the multicast interface
|
||||
if err := n.multicast.Init(&n.core, cfg, logger, nil); err != nil {
|
||||
logger.Errorln("An error occurred initialising multicast:", err)
|
||||
} else if err := n.multicast.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting multicast:", err)
|
||||
|
||||
// Setup the multicast module.
|
||||
{
|
||||
options := []multicast.SetupOption{}
|
||||
for _, intf := range cfg.MulticastInterfaces {
|
||||
options = append(options, multicast.MulticastInterface{
|
||||
Regex: regexp.MustCompile(intf.Regex),
|
||||
Beacon: intf.Beacon,
|
||||
Listen: intf.Listen,
|
||||
Port: intf.Port,
|
||||
})
|
||||
}
|
||||
if n.multicast, err = multicast.New(n.core, logger, options...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if n.admin != nil && n.multicast != nil {
|
||||
n.multicast.SetupAdminHandlers(n.admin)
|
||||
}
|
||||
}
|
||||
n.multicast.SetupAdminHandlers(n.admin)
|
||||
// Start the TUN/TAP interface
|
||||
rwc := ipv6rwc.NewReadWriteCloser(&n.core)
|
||||
if err := n.tuntap.Init(rwc, cfg, logger, nil); err != nil {
|
||||
logger.Errorln("An error occurred initialising TUN/TAP:", err)
|
||||
} else if err := n.tuntap.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting TUN/TAP:", err)
|
||||
|
||||
// Setup the TUN module.
|
||||
{
|
||||
options := []tun.SetupOption{
|
||||
tun.InterfaceName(cfg.IfName),
|
||||
tun.InterfaceMTU(cfg.IfMTU),
|
||||
}
|
||||
if n.tun, err = tun.New(ipv6rwc.NewReadWriteCloser(n.core), logger, options...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if n.admin != nil && n.tun != nil {
|
||||
n.tun.SetupAdminHandlers(n.admin)
|
||||
}
|
||||
}
|
||||
n.tuntap.SetupAdminHandlers(n.admin)
|
||||
|
||||
// Make some nice output that tells us what our IPv6 address and subnet are.
|
||||
// This is just logged to stdout for the user.
|
||||
address := n.core.Address()
|
||||
|
@ -384,7 +383,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||
func (n *node) shutdown() {
|
||||
_ = n.admin.Stop()
|
||||
_ = n.multicast.Stop()
|
||||
_ = n.tuntap.Stop()
|
||||
_ = n.tun.Stop()
|
||||
n.core.Stop()
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
|
@ -15,9 +14,9 @@ import (
|
|||
)
|
||||
|
||||
type CmdLineEnv struct {
|
||||
args []string
|
||||
endpoint, server string
|
||||
injson, verbose, ver bool
|
||||
args []string
|
||||
endpoint, server string
|
||||
injson, ver bool
|
||||
}
|
||||
|
||||
func newCmdLineEnv() CmdLineEnv {
|
||||
|
@ -47,7 +46,6 @@ func (cmdLineEnv *CmdLineEnv) parseFlagsAndArgs() {
|
|||
|
||||
server := flag.String("endpoint", cmdLineEnv.endpoint, "Admin socket endpoint")
|
||||
injson := flag.Bool("json", false, "Output in JSON format (as opposed to pretty-print)")
|
||||
verbose := flag.Bool("v", false, "Verbose output (includes public keys)")
|
||||
ver := flag.Bool("version", false, "Prints the version of this build")
|
||||
|
||||
flag.Parse()
|
||||
|
@ -55,13 +53,12 @@ func (cmdLineEnv *CmdLineEnv) parseFlagsAndArgs() {
|
|||
cmdLineEnv.args = flag.Args()
|
||||
cmdLineEnv.server = *server
|
||||
cmdLineEnv.injson = *injson
|
||||
cmdLineEnv.verbose = *verbose
|
||||
cmdLineEnv.ver = *ver
|
||||
}
|
||||
|
||||
func (cmdLineEnv *CmdLineEnv) setEndpoint(logger *log.Logger) {
|
||||
if cmdLineEnv.server == cmdLineEnv.endpoint {
|
||||
if config, err := ioutil.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
|
||||
if config, err := os.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
|
||||
if bytes.Equal(config[0:2], []byte{0xFF, 0xFE}) ||
|
||||
bytes.Equal(config[0:2], []byte{0xFE, 0xFF}) {
|
||||
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
|
||||
|
|
|
@ -10,15 +10,17 @@ import (
|
|||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||
)
|
||||
|
||||
type admin_info map[string]interface{}
|
||||
|
||||
func main() {
|
||||
// makes sure we can use defer and still return an error code to the OS
|
||||
os.Exit(run())
|
||||
|
@ -54,103 +56,13 @@ func run() int {
|
|||
|
||||
cmdLineEnv.setEndpoint(logger)
|
||||
|
||||
conn := connect(cmdLineEnv.endpoint, logger)
|
||||
logger.Println("Connected")
|
||||
defer conn.Close()
|
||||
|
||||
decoder := json.NewDecoder(conn)
|
||||
encoder := json.NewEncoder(conn)
|
||||
send := make(admin_info)
|
||||
recv := make(admin_info)
|
||||
|
||||
for c, a := range cmdLineEnv.args {
|
||||
if c == 0 {
|
||||
if strings.HasPrefix(a, "-") {
|
||||
logger.Printf("Ignoring flag %s as it should be specified before other parameters\n", a)
|
||||
continue
|
||||
}
|
||||
logger.Printf("Sending request: %v\n", a)
|
||||
send["request"] = a
|
||||
continue
|
||||
}
|
||||
tokens := strings.Split(a, "=")
|
||||
if len(tokens) == 1 {
|
||||
send[tokens[0]] = true
|
||||
} else if len(tokens) > 2 {
|
||||
send[tokens[0]] = strings.Join(tokens[1:], "=")
|
||||
} else if len(tokens) == 2 {
|
||||
if i, err := strconv.Atoi(tokens[1]); err == nil {
|
||||
logger.Printf("Sending parameter %s: %d\n", tokens[0], i)
|
||||
send[tokens[0]] = i
|
||||
} else {
|
||||
switch strings.ToLower(tokens[1]) {
|
||||
case "true":
|
||||
send[tokens[0]] = true
|
||||
case "false":
|
||||
send[tokens[0]] = false
|
||||
default:
|
||||
send[tokens[0]] = tokens[1]
|
||||
}
|
||||
logger.Printf("Sending parameter %s: %v\n", tokens[0], send[tokens[0]])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := encoder.Encode(&send); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
logger.Printf("Request sent")
|
||||
|
||||
if err := decoder.Decode(&recv); err == nil {
|
||||
logger.Printf("Response received")
|
||||
if recv["status"] == "error" {
|
||||
if err, ok := recv["error"]; ok {
|
||||
fmt.Println("Admin socket returned an error:", err)
|
||||
} else {
|
||||
fmt.Println("Admin socket returned an error but didn't specify any error text")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if _, ok := recv["request"]; !ok {
|
||||
fmt.Println("Missing request in response (malformed response?)")
|
||||
return 1
|
||||
}
|
||||
if _, ok := recv["response"]; !ok {
|
||||
fmt.Println("Missing response body (malformed response?)")
|
||||
return 1
|
||||
}
|
||||
res := recv["response"].(map[string]interface{})
|
||||
|
||||
if cmdLineEnv.injson {
|
||||
if json, err := json.MarshalIndent(res, "", " "); err == nil {
|
||||
fmt.Println(string(json))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
handleAll(recv, cmdLineEnv.verbose)
|
||||
} else {
|
||||
logger.Println("Error receiving response:", err)
|
||||
}
|
||||
|
||||
if v, ok := recv["status"]; ok && v != "success" {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func connect(endpoint string, logger *log.Logger) net.Conn {
|
||||
var conn net.Conn
|
||||
|
||||
u, err := url.Parse(endpoint)
|
||||
|
||||
u, err := url.Parse(cmdLineEnv.endpoint)
|
||||
if err == nil {
|
||||
switch strings.ToLower(u.Scheme) {
|
||||
case "unix":
|
||||
logger.Println("Connecting to UNIX socket", endpoint[7:])
|
||||
conn, err = net.Dial("unix", endpoint[7:])
|
||||
logger.Println("Connecting to UNIX socket", cmdLineEnv.endpoint[7:])
|
||||
conn, err = net.Dial("unix", cmdLineEnv.endpoint[7:])
|
||||
case "tcp":
|
||||
logger.Println("Connecting to TCP socket", u.Host)
|
||||
conn, err = net.Dial("tcp", u.Host)
|
||||
|
@ -160,298 +72,208 @@ func connect(endpoint string, logger *log.Logger) net.Conn {
|
|||
}
|
||||
} else {
|
||||
logger.Println("Connecting to TCP socket", u.Host)
|
||||
conn, err = net.Dial("tcp", endpoint)
|
||||
conn, err = net.Dial("tcp", cmdLineEnv.endpoint)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return conn
|
||||
}
|
||||
logger.Println("Connected")
|
||||
defer conn.Close()
|
||||
|
||||
func handleAll(recv map[string]interface{}, verbose bool) {
|
||||
req := recv["request"].(map[string]interface{})
|
||||
res := recv["response"].(map[string]interface{})
|
||||
|
||||
switch strings.ToLower(req["request"].(string)) {
|
||||
case "dot":
|
||||
handleDot(res)
|
||||
case "list", "getpeers", "getswitchpeers", "getdht", "getsessions", "dhtping":
|
||||
handleVariousInfo(res, verbose)
|
||||
case "gettuntap", "settuntap":
|
||||
handleGetAndSetTunTap(res)
|
||||
case "getself":
|
||||
handleGetSelf(res, verbose)
|
||||
case "getswitchqueues":
|
||||
handleGetSwitchQueues(res)
|
||||
case "addpeer", "removepeer", "addallowedencryptionpublickey", "removeallowedencryptionpublickey", "addsourcesubnet", "addroute", "removesourcesubnet", "removeroute":
|
||||
handleAddsAndRemoves(res)
|
||||
case "getallowedencryptionpublickeys":
|
||||
handleGetAllowedEncryptionPublicKeys(res)
|
||||
case "getmulticastinterfaces":
|
||||
handleGetMulticastInterfaces(res)
|
||||
case "getsourcesubnets":
|
||||
handleGetSourceSubnets(res)
|
||||
case "getroutes":
|
||||
handleGetRoutes(res)
|
||||
case "settunnelrouting":
|
||||
fallthrough
|
||||
case "gettunnelrouting":
|
||||
handleGetTunnelRouting(res)
|
||||
default:
|
||||
if json, err := json.MarshalIndent(recv["response"], "", " "); err == nil {
|
||||
decoder := json.NewDecoder(conn)
|
||||
encoder := json.NewEncoder(conn)
|
||||
send := &admin.AdminSocketRequest{}
|
||||
recv := &admin.AdminSocketResponse{}
|
||||
args := map[string]string{}
|
||||
for c, a := range cmdLineEnv.args {
|
||||
if c == 0 {
|
||||
if strings.HasPrefix(a, "-") {
|
||||
logger.Printf("Ignoring flag %s as it should be specified before other parameters\n", a)
|
||||
continue
|
||||
}
|
||||
logger.Printf("Sending request: %v\n", a)
|
||||
send.Name = a
|
||||
continue
|
||||
}
|
||||
tokens := strings.SplitN(a, "=", 2)
|
||||
switch {
|
||||
case len(tokens) == 1:
|
||||
logger.Println("Ignoring invalid argument:", a)
|
||||
default:
|
||||
args[tokens[0]] = tokens[1]
|
||||
}
|
||||
}
|
||||
if send.Arguments, err = json.Marshal(args); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := encoder.Encode(&send); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logger.Printf("Request sent")
|
||||
if err := decoder.Decode(&recv); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if recv.Status == "error" {
|
||||
if err := recv.Error; err != "" {
|
||||
fmt.Println("Admin socket returned an error:", err)
|
||||
} else {
|
||||
fmt.Println("Admin socket returned an error but didn't specify any error text")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if cmdLineEnv.injson {
|
||||
if json, err := json.MarshalIndent(recv.Response, "", " "); err == nil {
|
||||
fmt.Println(string(json))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func handleDot(res map[string]interface{}) {
|
||||
fmt.Println(res["dot"])
|
||||
}
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAutoFormatHeaders(false)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetTablePadding("\t") // pad with tabs
|
||||
table.SetNoWhiteSpace(true)
|
||||
table.SetAutoWrapText(false)
|
||||
|
||||
func handleVariousInfo(res map[string]interface{}, verbose bool) {
|
||||
maxWidths := make(map[string]int)
|
||||
var keyOrder []string
|
||||
keysOrdered := false
|
||||
|
||||
for _, tlv := range res {
|
||||
for slk, slv := range tlv.(map[string]interface{}) {
|
||||
if !keysOrdered {
|
||||
for k := range slv.(map[string]interface{}) {
|
||||
if !verbose {
|
||||
if k == "box_pub_key" || k == "box_sig_key" || k == "nodeinfo" || k == "was_mtu_fixed" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
keyOrder = append(keyOrder, fmt.Sprint(k))
|
||||
}
|
||||
sort.Strings(keyOrder)
|
||||
keysOrdered = true
|
||||
}
|
||||
for k, v := range slv.(map[string]interface{}) {
|
||||
if len(fmt.Sprint(slk)) > maxWidths["key"] {
|
||||
maxWidths["key"] = len(fmt.Sprint(slk))
|
||||
}
|
||||
if len(fmt.Sprint(v)) > maxWidths[k] {
|
||||
maxWidths[k] = len(fmt.Sprint(v))
|
||||
if maxWidths[k] < len(k) {
|
||||
maxWidths[k] = len(k)
|
||||
}
|
||||
}
|
||||
switch strings.ToLower(send.Name) {
|
||||
case "list":
|
||||
var resp admin.ListResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Command", "Arguments", "Description"})
|
||||
for _, entry := range resp.List {
|
||||
for i := range entry.Fields {
|
||||
entry.Fields[i] = entry.Fields[i] + "=..."
|
||||
}
|
||||
table.Append([]string{entry.Command, strings.Join(entry.Fields, ", "), entry.Description})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "getself":
|
||||
var resp admin.GetSelfResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.Append([]string{"Build name:", resp.BuildName})
|
||||
table.Append([]string{"Build version:", resp.BuildVersion})
|
||||
table.Append([]string{"IPv6 address:", resp.IPAddress})
|
||||
table.Append([]string{"IPv6 subnet:", resp.Subnet})
|
||||
table.Append([]string{"Coordinates:", fmt.Sprintf("%v", resp.Coords)})
|
||||
table.Append([]string{"Public key:", resp.PublicKey})
|
||||
table.Render()
|
||||
|
||||
case "getpeers":
|
||||
var resp admin.GetPeersResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Port", "Public Key", "IP Address", "Uptime", "RX", "TX", "URI"})
|
||||
for _, peer := range resp.Peers {
|
||||
table.Append([]string{
|
||||
fmt.Sprintf("%d", peer.Port),
|
||||
peer.PublicKey,
|
||||
peer.IPAddress,
|
||||
(time.Duration(peer.Uptime) * time.Second).String(),
|
||||
peer.RXBytes.String(),
|
||||
peer.TXBytes.String(),
|
||||
peer.Remote,
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "getdht":
|
||||
var resp admin.GetDHTResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Public Key", "IP Address", "Port", "Rest"})
|
||||
for _, dht := range resp.DHT {
|
||||
table.Append([]string{
|
||||
dht.PublicKey,
|
||||
dht.IPAddress,
|
||||
fmt.Sprintf("%d", dht.Port),
|
||||
fmt.Sprintf("%d", dht.Rest),
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "getpaths":
|
||||
var resp admin.GetPathsResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Public Key", "IP Address", "Path"})
|
||||
for _, p := range resp.Paths {
|
||||
table.Append([]string{
|
||||
p.PublicKey,
|
||||
p.IPAddress,
|
||||
fmt.Sprintf("%v", p.Path),
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "getsessions":
|
||||
var resp admin.GetSessionsResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Public Key", "IP Address", "Uptime", "RX", "TX"})
|
||||
for _, p := range resp.Sessions {
|
||||
table.Append([]string{
|
||||
p.PublicKey,
|
||||
p.IPAddress,
|
||||
(time.Duration(p.Uptime) * time.Second).String(),
|
||||
p.RXBytes.String(),
|
||||
p.TXBytes.String(),
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "getnodeinfo":
|
||||
var resp core.GetNodeInfoResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, v := range resp {
|
||||
fmt.Println(string(v))
|
||||
break
|
||||
}
|
||||
|
||||
if len(keyOrder) > 0 {
|
||||
fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", "")
|
||||
for _, v := range keyOrder {
|
||||
fmt.Printf("%-"+fmt.Sprint(maxWidths[v])+"s ", v)
|
||||
}
|
||||
fmt.Println()
|
||||
case "getmulticastinterfaces":
|
||||
var resp multicast.GetMulticastInterfacesResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Interface"})
|
||||
for _, p := range resp.Interfaces {
|
||||
table.Append([]string{p})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
for slk, slv := range tlv.(map[string]interface{}) {
|
||||
fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", slk)
|
||||
for _, k := range keyOrder {
|
||||
preformatted := slv.(map[string]interface{})[k]
|
||||
var formatted string
|
||||
switch k {
|
||||
case "bytes_sent", "bytes_recvd":
|
||||
formatted = fmt.Sprintf("%d", uint(preformatted.(float64)))
|
||||
case "uptime", "last_seen":
|
||||
seconds := uint(preformatted.(float64)) % 60
|
||||
minutes := uint(preformatted.(float64)/60) % 60
|
||||
hours := uint(preformatted.(float64) / 60 / 60)
|
||||
formatted = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
|
||||
default:
|
||||
formatted = fmt.Sprint(preformatted)
|
||||
}
|
||||
fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", formatted)
|
||||
}
|
||||
fmt.Println()
|
||||
case "gettun":
|
||||
var resp tun.GetTUNResponse
|
||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.Append([]string{"TUN enabled:", fmt.Sprintf("%#v", resp.Enabled)})
|
||||
if resp.Enabled {
|
||||
table.Append([]string{"Interface name:", resp.Name})
|
||||
table.Append([]string{"Interface MTU:", fmt.Sprintf("%d", resp.MTU)})
|
||||
}
|
||||
table.Render()
|
||||
|
||||
case "addpeer", "removepeer":
|
||||
|
||||
default:
|
||||
fmt.Println(string(recv.Response))
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetAndSetTunTap(res map[string]interface{}) {
|
||||
for k, v := range res {
|
||||
fmt.Println("Interface name:", k)
|
||||
if mtu, ok := v.(map[string]interface{})["mtu"].(float64); ok {
|
||||
fmt.Println("Interface MTU:", mtu)
|
||||
}
|
||||
if tap_mode, ok := v.(map[string]interface{})["tap_mode"].(bool); ok {
|
||||
fmt.Println("TAP mode:", tap_mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetSelf(res map[string]interface{}, verbose bool) {
|
||||
for k, v := range res["self"].(map[string]interface{}) {
|
||||
if buildname, ok := v.(map[string]interface{})["build_name"].(string); ok && buildname != "unknown" {
|
||||
fmt.Println("Build name:", buildname)
|
||||
}
|
||||
if buildversion, ok := v.(map[string]interface{})["build_version"].(string); ok && buildversion != "unknown" {
|
||||
fmt.Println("Build version:", buildversion)
|
||||
}
|
||||
fmt.Println("IPv6 address:", k)
|
||||
if subnet, ok := v.(map[string]interface{})["subnet"].(string); ok {
|
||||
fmt.Println("IPv6 subnet:", subnet)
|
||||
}
|
||||
if boxSigKey, ok := v.(map[string]interface{})["key"].(string); ok {
|
||||
fmt.Println("Public key:", boxSigKey)
|
||||
}
|
||||
if coords, ok := v.(map[string]interface{})["coords"].(string); ok {
|
||||
fmt.Println("Coords:", coords)
|
||||
}
|
||||
if verbose {
|
||||
if nodeID, ok := v.(map[string]interface{})["node_id"].(string); ok {
|
||||
fmt.Println("Node ID:", nodeID)
|
||||
}
|
||||
if boxPubKey, ok := v.(map[string]interface{})["box_pub_key"].(string); ok {
|
||||
fmt.Println("Public encryption key:", boxPubKey)
|
||||
}
|
||||
if boxSigKey, ok := v.(map[string]interface{})["box_sig_key"].(string); ok {
|
||||
fmt.Println("Public signing key:", boxSigKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetSwitchQueues(res map[string]interface{}) {
|
||||
maximumqueuesize := float64(4194304)
|
||||
portqueues := make(map[float64]float64)
|
||||
portqueuesize := make(map[float64]float64)
|
||||
portqueuepackets := make(map[float64]float64)
|
||||
v := res["switchqueues"].(map[string]interface{})
|
||||
if queuecount, ok := v["queues_count"].(float64); ok {
|
||||
fmt.Printf("Active queue count: %d queues\n", uint(queuecount))
|
||||
}
|
||||
if queuesize, ok := v["queues_size"].(float64); ok {
|
||||
fmt.Printf("Active queue size: %d bytes\n", uint(queuesize))
|
||||
}
|
||||
if highestqueuecount, ok := v["highest_queues_count"].(float64); ok {
|
||||
fmt.Printf("Highest queue count: %d queues\n", uint(highestqueuecount))
|
||||
}
|
||||
if highestqueuesize, ok := v["highest_queues_size"].(float64); ok {
|
||||
fmt.Printf("Highest queue size: %d bytes\n", uint(highestqueuesize))
|
||||
}
|
||||
if m, ok := v["maximum_queues_size"].(float64); ok {
|
||||
maximumqueuesize = m
|
||||
fmt.Printf("Maximum queue size: %d bytes\n", uint(maximumqueuesize))
|
||||
}
|
||||
if queues, ok := v["queues"].([]interface{}); ok {
|
||||
if len(queues) != 0 {
|
||||
fmt.Println("Active queues:")
|
||||
for _, v := range queues {
|
||||
queueport := v.(map[string]interface{})["queue_port"].(float64)
|
||||
queuesize := v.(map[string]interface{})["queue_size"].(float64)
|
||||
queuepackets := v.(map[string]interface{})["queue_packets"].(float64)
|
||||
queueid := v.(map[string]interface{})["queue_id"].(string)
|
||||
portqueues[queueport]++
|
||||
portqueuesize[queueport] += queuesize
|
||||
portqueuepackets[queueport] += queuepackets
|
||||
queuesizepercent := (100 / maximumqueuesize) * queuesize
|
||||
fmt.Printf("- Switch port %d, Stream ID: %v, size: %d bytes (%d%% full), %d packets\n",
|
||||
uint(queueport), []byte(queueid), uint(queuesize),
|
||||
uint(queuesizepercent), uint(queuepackets))
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(portqueuesize) > 0 && len(portqueuepackets) > 0 {
|
||||
fmt.Println("Aggregated statistics by switchport:")
|
||||
for k, v := range portqueuesize {
|
||||
queuesizepercent := (100 / (portqueues[k] * maximumqueuesize)) * v
|
||||
fmt.Printf("- Switch port %d, size: %d bytes (%d%% full), %d packets\n",
|
||||
uint(k), uint(v), uint(queuesizepercent), uint(portqueuepackets[k]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleAddsAndRemoves(res map[string]interface{}) {
|
||||
if _, ok := res["added"]; ok {
|
||||
for _, v := range res["added"].([]interface{}) {
|
||||
fmt.Println("Added:", fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
if _, ok := res["not_added"]; ok {
|
||||
for _, v := range res["not_added"].([]interface{}) {
|
||||
fmt.Println("Not added:", fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
if _, ok := res["removed"]; ok {
|
||||
for _, v := range res["removed"].([]interface{}) {
|
||||
fmt.Println("Removed:", fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
if _, ok := res["not_removed"]; ok {
|
||||
for _, v := range res["not_removed"].([]interface{}) {
|
||||
fmt.Println("Not removed:", fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetAllowedEncryptionPublicKeys(res map[string]interface{}) {
|
||||
if _, ok := res["allowed_box_pubs"]; !ok {
|
||||
fmt.Println("All connections are allowed")
|
||||
} else if res["allowed_box_pubs"] == nil {
|
||||
fmt.Println("All connections are allowed")
|
||||
} else {
|
||||
fmt.Println("Connections are allowed only from the following public box keys:")
|
||||
for _, v := range res["allowed_box_pubs"].([]interface{}) {
|
||||
fmt.Println("-", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetMulticastInterfaces(res map[string]interface{}) {
|
||||
if _, ok := res["multicast_interfaces"]; !ok {
|
||||
fmt.Println("No multicast interfaces found")
|
||||
} else if res["multicast_interfaces"] == nil {
|
||||
fmt.Println("No multicast interfaces found")
|
||||
} else {
|
||||
fmt.Println("Multicast peer discovery is active on:")
|
||||
for _, v := range res["multicast_interfaces"].([]interface{}) {
|
||||
fmt.Println("-", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetSourceSubnets(res map[string]interface{}) {
|
||||
if _, ok := res["source_subnets"]; !ok {
|
||||
fmt.Println("No source subnets found")
|
||||
} else if res["source_subnets"] == nil {
|
||||
fmt.Println("No source subnets found")
|
||||
} else {
|
||||
fmt.Println("Source subnets:")
|
||||
for _, v := range res["source_subnets"].([]interface{}) {
|
||||
fmt.Println("-", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetRoutes(res map[string]interface{}) {
|
||||
if routes, ok := res["routes"].(map[string]interface{}); !ok {
|
||||
fmt.Println("No routes found")
|
||||
} else {
|
||||
if res["routes"] == nil || len(routes) == 0 {
|
||||
fmt.Println("No routes found")
|
||||
} else {
|
||||
fmt.Println("Routes:")
|
||||
for k, v := range routes {
|
||||
if pv, ok := v.(string); ok {
|
||||
fmt.Println("-", k, " via ", pv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetTunnelRouting(res map[string]interface{}) {
|
||||
if enabled, ok := res["enabled"].(bool); !ok {
|
||||
fmt.Println("Tunnel routing is disabled")
|
||||
} else if !enabled {
|
||||
fmt.Println("Tunnel routing is disabled")
|
||||
} else {
|
||||
fmt.Println("Tunnel routing is enabled")
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
|
||||
"github.com/gologme/log"
|
||||
|
||||
|
@ -25,10 +26,10 @@ import (
|
|||
// functions. Note that in the case of iOS we handle reading/writing to/from TUN
|
||||
// in Swift therefore we use the "dummy" TUN interface instead.
|
||||
type Yggdrasil struct {
|
||||
core core.Core
|
||||
core *core.Core
|
||||
iprwc *ipv6rwc.ReadWriteCloser
|
||||
config *config.NodeConfig
|
||||
multicast multicast.Multicast
|
||||
multicast *multicast.Multicast
|
||||
log MobileLogger
|
||||
}
|
||||
|
||||
|
@ -48,27 +49,58 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
|||
if err := json.Unmarshal(configjson, &m.config); err != nil {
|
||||
return err
|
||||
}
|
||||
m.config.IfName = "none"
|
||||
if err := m.core.Start(m.config, logger); err != nil {
|
||||
logger.Errorln("An error occured starting Yggdrasil:", err)
|
||||
return err
|
||||
// Setup the Yggdrasil node itself.
|
||||
{
|
||||
sk, err := hex.DecodeString(m.config.PrivateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
options := []core.SetupOption{}
|
||||
for _, peer := range m.config.Peers {
|
||||
options = append(options, core.Peer{URI: peer})
|
||||
}
|
||||
for intf, peers := range m.config.InterfacePeers {
|
||||
for _, peer := range peers {
|
||||
options = append(options, core.Peer{URI: peer, SourceInterface: intf})
|
||||
}
|
||||
}
|
||||
for _, allowed := range m.config.AllowedPublicKeys {
|
||||
k, err := hex.DecodeString(allowed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
options = append(options, core.AllowedPublicKey(k[:]))
|
||||
}
|
||||
m.core, err = core.New(sk[:], logger, options...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Setup the multicast module.
|
||||
if len(m.config.MulticastInterfaces) > 0 {
|
||||
var err error
|
||||
options := []multicast.SetupOption{}
|
||||
for _, intf := range m.config.MulticastInterfaces {
|
||||
options = append(options, multicast.MulticastInterface{
|
||||
Regex: regexp.MustCompile(intf.Regex),
|
||||
Beacon: intf.Beacon,
|
||||
Listen: intf.Listen,
|
||||
Port: intf.Port,
|
||||
})
|
||||
}
|
||||
m.multicast, err = multicast.New(m.core, logger, options...)
|
||||
if err != nil {
|
||||
logger.Errorln("An error occurred starting multicast:", err)
|
||||
}
|
||||
}
|
||||
|
||||
mtu := m.config.IfMTU
|
||||
m.iprwc = ipv6rwc.NewReadWriteCloser(&m.core)
|
||||
m.iprwc = ipv6rwc.NewReadWriteCloser(m.core)
|
||||
if m.iprwc.MaxMTU() < mtu {
|
||||
mtu = m.iprwc.MaxMTU()
|
||||
}
|
||||
m.iprwc.SetMTU(mtu)
|
||||
if len(m.config.MulticastInterfaces) > 0 {
|
||||
if err := m.multicast.Init(&m.core, m.config, logger, nil); err != nil {
|
||||
logger.Errorln("An error occurred initialising multicast:", err)
|
||||
return err
|
||||
}
|
||||
if err := m.multicast.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting multicast:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -139,18 +171,18 @@ func (m *Yggdrasil) GetCoordsString() string {
|
|||
|
||||
func (m *Yggdrasil) GetPeersJSON() (result string) {
|
||||
peers := []struct {
|
||||
core.Peer
|
||||
core.PeerInfo
|
||||
IP string
|
||||
}{}
|
||||
for _, v := range m.core.GetPeers() {
|
||||
a := address.AddrForKey(v.Key)
|
||||
ip := net.IP(a[:]).String()
|
||||
peers = append(peers, struct {
|
||||
core.Peer
|
||||
core.PeerInfo
|
||||
IP string
|
||||
}{
|
||||
Peer: v,
|
||||
IP: ip,
|
||||
PeerInfo: v,
|
||||
IP: ip,
|
||||
})
|
||||
}
|
||||
if res, err := json.Marshal(peers); err == nil {
|
||||
|
|
23
go.mod
23
go.mod
|
@ -3,7 +3,7 @@ module github.com/yggdrasil-network/yggdrasil-go
|
|||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/Arceliar/ironwood v0.0.0-20220409035209-b7f71f05435a
|
||||
github.com/Arceliar/ironwood v0.0.0-20220924160422-ed4b6d4750b6
|
||||
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979
|
||||
github.com/cheggaaa/pb/v3 v3.0.8
|
||||
github.com/gologme/log v1.2.0
|
||||
|
@ -12,24 +12,27 @@ require (
|
|||
github.com/kardianos/minwinsvc v1.0.0
|
||||
github.com/mitchellh/mapstructure v1.4.1
|
||||
github.com/vishvananda/netlink v1.1.0
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816
|
||||
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3
|
||||
golang.org/x/sys v0.0.0-20211102192858-4dd72447c267
|
||||
golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
|
||||
golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b
|
||||
golang.zx2c4.com/wireguard v0.0.0-20211017052713-f87e87af0d9a
|
||||
golang.zx2c4.com/wireguard/windows v0.4.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/tools v0.1.12 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/VividCortex/ewma v1.2.0 // indirect
|
||||
github.com/fatih/color v1.12.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||
github.com/mattn/go-isatty v0.0.13 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
|
||||
golang.org/x/mod v0.4.2 // indirect
|
||||
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
)
|
||||
|
|
31
go.sum
31
go.sum
|
@ -1,5 +1,5 @@
|
|||
github.com/Arceliar/ironwood v0.0.0-20220409035209-b7f71f05435a h1:yfbnOyqPcx2gi5cFIJ2rlPz5M6rFPHT/c8FgZmFjCdc=
|
||||
github.com/Arceliar/ironwood v0.0.0-20220409035209-b7f71f05435a/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk=
|
||||
github.com/Arceliar/ironwood v0.0.0-20220924160422-ed4b6d4750b6 h1:iwL6nm2ibyuHXYimRNtFof7RJfe8JB+6CPDskV7K7gA=
|
||||
github.com/Arceliar/ironwood v0.0.0-20220924160422-ed4b6d4750b6/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk=
|
||||
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979 h1:WndgpSW13S32VLQ3ugUxx2EnnWmgba1kCqPkd4Gk1yQ=
|
||||
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979/go.mod h1:6Lkn+/zJilRMsKmbmG1RPoamiArC6HS73xbwRyp3UyI=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
|
@ -26,11 +26,14 @@ github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
|
|||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
|
||||
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
|
||||
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
|
@ -40,6 +43,7 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17
|
|||
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f h1:p4VB7kIXpOQvVn1ZaTIVp+3vuYAXFe3OJEvjbUYJLaA=
|
||||
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
|
||||
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
|
@ -50,11 +54,12 @@ golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9t
|
|||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816 h1:jhDgkcu3yQ4tasBZ+1YwDmK7eFmuVf1w1k+NGGGxfmE=
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
|
||||
golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 h1:3vUV5x5+3LfQbgk7paCM6INOaJG9xXQbn79xoNkwfIk=
|
||||
golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
|
||||
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
|
||||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
|
@ -62,10 +67,11 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
|
|||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20210927181540-4e4d966f7476/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3 h1:VrJZAjbekhoRn7n5FBujY31gboH+iB3pdLxn3gE9FjU=
|
||||
golang.org/x/net v0.0.0-20211101193420-4a448f8816b3/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
@ -81,23 +87,26 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211102192858-4dd72447c267 h1:7zYaz3tjChtpayGDzu6H0hDAUM5zIGA2XW7kRNgQ0jc=
|
||||
golang.org/x/sys v0.0.0-20211102192858-4dd72447c267/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b h1:NXqSWXSRUSCaFuvitrWtU169I3876zRTalMRbfd6LL0=
|
||||
golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b/go.mod h1:EFNZuWvGYxIRUEX+K8UmCFwYmZjqcrnq15ZuVldZkZ0=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
|
||||
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 h1:YuekqPskqwCCPM79F1X5Dhv4ezTCj+Ki1oNwiafxkA0=
|
||||
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
|
||||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.zx2c4.com/wireguard v0.0.0-20211012062646-82d2aa87aa62/go.mod h1:id8Oh3eCCmpj9uVGWVjsUAl6UPX5ysMLzu6QxJU2UOU=
|
||||
golang.zx2c4.com/wireguard v0.0.0-20211017052713-f87e87af0d9a h1:tTbyylK9/D3u/wEP26Vx7L700UpY48nhioJWZM1vhZw=
|
||||
|
|
|
@ -108,7 +108,7 @@ func SubnetForKey(publicKey ed25519.PublicKey) *Subnet {
|
|||
}
|
||||
var snet Subnet
|
||||
copy(snet[:], addr[:])
|
||||
prefix := GetPrefix()
|
||||
prefix := GetPrefix() // nolint:staticcheck
|
||||
snet[len(prefix)-1] |= 0x01
|
||||
return &snet
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ func SubnetForKey(publicKey ed25519.PublicKey) *Subnet {
|
|||
// This is used for key lookup.
|
||||
func (a *Address) GetKey() ed25519.PublicKey {
|
||||
var key [ed25519.PublicKeySize]byte
|
||||
prefix := GetPrefix()
|
||||
prefix := GetPrefix() // nolint:staticcheck
|
||||
ones := int(a[len(prefix)])
|
||||
for idx := 0; idx < ones; idx++ {
|
||||
key[idx/8] |= 0x80 >> byte(idx%8)
|
||||
|
|
12
src/admin/addpeer.go
Normal file
12
src/admin/addpeer.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package admin
|
||||
|
||||
type AddPeerRequest struct {
|
||||
Uri string `json:"uri"`
|
||||
Sintf string `json:"interface,omitempty"`
|
||||
}
|
||||
|
||||
type AddPeerResponse struct{}
|
||||
|
||||
func (a *AdminSocket) addPeerHandler(req *AddPeerRequest, res *AddPeerResponse) error {
|
||||
return a.core.AddPeer(req.Uri, req.Sintf)
|
||||
}
|
|
@ -7,55 +7,63 @@ import (
|
|||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gologme/log"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
)
|
||||
|
||||
// TODO: Add authentication
|
||||
|
||||
type AdminSocket struct {
|
||||
core *core.Core
|
||||
log *log.Logger
|
||||
listenaddr string
|
||||
listener net.Listener
|
||||
handlers map[string]handler
|
||||
done chan struct{}
|
||||
core *core.Core
|
||||
log core.Logger
|
||||
listener net.Listener
|
||||
handlers map[string]handler
|
||||
done chan struct{}
|
||||
config struct {
|
||||
listenaddr ListenAddress
|
||||
}
|
||||
}
|
||||
|
||||
type AdminSocketRequest struct {
|
||||
Name string `json:"request"`
|
||||
Arguments json.RawMessage `json:"arguments,omitempty"`
|
||||
KeepAlive bool `json:"keepalive,omitempty"`
|
||||
}
|
||||
|
||||
type AdminSocketResponse struct {
|
||||
Status string `json:"status"`
|
||||
Request struct {
|
||||
Name string `json:"request"`
|
||||
KeepAlive bool `json:"keepalive"`
|
||||
} `json:"request"`
|
||||
Response interface{} `json:"response"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Request json.RawMessage `json:"request"`
|
||||
Response json.RawMessage `json:"response"`
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
desc string // What does the endpoint do?
|
||||
args []string // List of human-readable argument names
|
||||
handler core.AddHandlerFunc // First is input map, second is output
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
List map[string]ListEntry `json:"list"`
|
||||
List []ListEntry `json:"list"`
|
||||
}
|
||||
|
||||
type ListEntry struct {
|
||||
Fields []string `json:"fields"`
|
||||
Command string `json:"command"`
|
||||
Description string `json:"description"`
|
||||
Fields []string `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// AddHandler is called for each admin function to add the handler and help documentation to the API.
|
||||
func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc core.AddHandlerFunc) error {
|
||||
func (a *AdminSocket) AddHandler(name, desc string, args []string, handlerfunc core.AddHandlerFunc) error {
|
||||
if _, ok := a.handlers[strings.ToLower(name)]; ok {
|
||||
return errors.New("handler already exists")
|
||||
}
|
||||
a.handlers[strings.ToLower(name)] = handler{
|
||||
desc: desc,
|
||||
args: args,
|
||||
handler: handlerfunc,
|
||||
}
|
||||
|
@ -63,100 +71,142 @@ func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc core.Ad
|
|||
}
|
||||
|
||||
// Init runs the initial admin setup.
|
||||
func (a *AdminSocket) Init(c *core.Core, nc *config.NodeConfig, log *log.Logger, options interface{}) error {
|
||||
a.core = c
|
||||
a.log = log
|
||||
a.handlers = make(map[string]handler)
|
||||
nc.RLock()
|
||||
a.listenaddr = nc.AdminListen
|
||||
nc.RUnlock()
|
||||
a.done = make(chan struct{})
|
||||
close(a.done) // Start in a done / not-started state
|
||||
_ = a.AddHandler("list", []string{}, func(_ json.RawMessage) (interface{}, error) {
|
||||
res := &ListResponse{
|
||||
List: map[string]ListEntry{},
|
||||
}
|
||||
func New(c *core.Core, log core.Logger, opts ...SetupOption) (*AdminSocket, error) {
|
||||
a := &AdminSocket{
|
||||
core: c,
|
||||
log: log,
|
||||
handlers: make(map[string]handler),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
a._applyOption(opt)
|
||||
}
|
||||
if a.config.listenaddr == "none" || a.config.listenaddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
_ = a.AddHandler("list", "List available commands", []string{}, func(_ json.RawMessage) (interface{}, error) {
|
||||
res := &ListResponse{}
|
||||
for name, handler := range a.handlers {
|
||||
res.List[name] = ListEntry{
|
||||
Fields: handler.args,
|
||||
}
|
||||
res.List = append(res.List, ListEntry{
|
||||
Command: name,
|
||||
Description: handler.desc,
|
||||
Fields: handler.args,
|
||||
})
|
||||
}
|
||||
sort.SliceStable(res.List, func(i, j int) bool {
|
||||
return strings.Compare(res.List[i].Command, res.List[j].Command) < 0
|
||||
})
|
||||
return res, nil
|
||||
})
|
||||
return a.core.SetAdmin(a)
|
||||
a.done = make(chan struct{})
|
||||
go a.listen()
|
||||
return a, a.core.SetAdmin(a)
|
||||
}
|
||||
|
||||
func (a *AdminSocket) SetupAdminHandlers(na *AdminSocket) {
|
||||
_ = a.AddHandler("getSelf", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetSelfRequest{}
|
||||
res := &GetSelfResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getSelfHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
_ = a.AddHandler("getPeers", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetPeersRequest{}
|
||||
res := &GetPeersResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getPeersHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
_ = a.AddHandler("getDHT", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetDHTRequest{}
|
||||
res := &GetDHTResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getDHTHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
_ = a.AddHandler("getPaths", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetPathsRequest{}
|
||||
res := &GetPathsResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getPathsHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
_ = a.AddHandler("getSessions", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetSessionsRequest{}
|
||||
res := &GetSessionsResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getSessionsHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
func (a *AdminSocket) SetupAdminHandlers() {
|
||||
_ = a.AddHandler(
|
||||
"getSelf", "Show details about this node", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetSelfRequest{}
|
||||
res := &GetSelfResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getSelfHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"getPeers", "Show directly connected peers", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetPeersRequest{}
|
||||
res := &GetPeersResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getPeersHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"getDHT", "Show known DHT entries", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetDHTRequest{}
|
||||
res := &GetDHTResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getDHTHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"getPaths", "Show established paths through this node", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetPathsRequest{}
|
||||
res := &GetPathsResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getPathsHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"getSessions", "Show established traffic sessions with remote nodes", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetSessionsRequest{}
|
||||
res := &GetSessionsResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.getSessionsHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"addPeer", "Add a peer to the peer list", []string{"uri", "interface"},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &AddPeerRequest{}
|
||||
res := &AddPeerResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.addPeerHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
_ = a.AddHandler(
|
||||
"removePeer", "Remove a peer from the peer list", []string{"uri", "interface"},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &RemovePeerRequest{}
|
||||
res := &RemovePeerResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := a.removePeerHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
//_ = a.AddHandler("getNodeInfo", []string{"key"}, t.proto.nodeinfo.nodeInfoAdminHandler)
|
||||
//_ = a.AddHandler("debug_remoteGetSelf", []string{"key"}, t.proto.getSelfHandler)
|
||||
//_ = a.AddHandler("debug_remoteGetPeers", []string{"key"}, t.proto.getPeersHandler)
|
||||
//_ = a.AddHandler("debug_remoteGetDHT", []string{"key"}, t.proto.getDHTHandler)
|
||||
}
|
||||
|
||||
// Start runs the admin API socket to listen for / respond to admin API calls.
|
||||
func (a *AdminSocket) Start() error {
|
||||
if a.listenaddr != "none" && a.listenaddr != "" {
|
||||
a.done = make(chan struct{})
|
||||
go a.listen()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsStarted returns true if the module has been started.
|
||||
func (a *AdminSocket) IsStarted() bool {
|
||||
select {
|
||||
|
@ -171,6 +221,9 @@ func (a *AdminSocket) IsStarted() bool {
|
|||
|
||||
// Stop will stop the admin API and close the socket.
|
||||
func (a *AdminSocket) Stop() error {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
if a.listener != nil {
|
||||
select {
|
||||
case <-a.done:
|
||||
|
@ -184,31 +237,32 @@ func (a *AdminSocket) Stop() error {
|
|||
|
||||
// listen is run by start and manages API connections.
|
||||
func (a *AdminSocket) listen() {
|
||||
u, err := url.Parse(a.listenaddr)
|
||||
listenaddr := string(a.config.listenaddr)
|
||||
u, err := url.Parse(listenaddr)
|
||||
if err == nil {
|
||||
switch strings.ToLower(u.Scheme) {
|
||||
case "unix":
|
||||
if _, err := os.Stat(a.listenaddr[7:]); err == nil {
|
||||
a.log.Debugln("Admin socket", a.listenaddr[7:], "already exists, trying to clean up")
|
||||
if _, err := net.DialTimeout("unix", a.listenaddr[7:], time.Second*2); err == nil || err.(net.Error).Timeout() {
|
||||
a.log.Errorln("Admin socket", a.listenaddr[7:], "already exists and is in use by another process")
|
||||
if _, err := os.Stat(listenaddr[7:]); err == nil {
|
||||
a.log.Debugln("Admin socket", listenaddr[7:], "already exists, trying to clean up")
|
||||
if _, err := net.DialTimeout("unix", listenaddr[7:], time.Second*2); err == nil || err.(net.Error).Timeout() {
|
||||
a.log.Errorln("Admin socket", listenaddr[7:], "already exists and is in use by another process")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
if err := os.Remove(a.listenaddr[7:]); err == nil {
|
||||
a.log.Debugln(a.listenaddr[7:], "was cleaned up")
|
||||
if err := os.Remove(listenaddr[7:]); err == nil {
|
||||
a.log.Debugln(listenaddr[7:], "was cleaned up")
|
||||
} else {
|
||||
a.log.Errorln(a.listenaddr[7:], "already exists and was not cleaned up:", err)
|
||||
a.log.Errorln(listenaddr[7:], "already exists and was not cleaned up:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
a.listener, err = net.Listen("unix", a.listenaddr[7:])
|
||||
a.listener, err = net.Listen("unix", listenaddr[7:])
|
||||
if err == nil {
|
||||
switch a.listenaddr[7:8] {
|
||||
switch listenaddr[7:8] {
|
||||
case "@": // maybe abstract namespace
|
||||
default:
|
||||
if err := os.Chmod(a.listenaddr[7:], 0660); err != nil {
|
||||
a.log.Warnln("WARNING:", a.listenaddr[:7], "may have unsafe permissions!")
|
||||
if err := os.Chmod(listenaddr[7:], 0660); err != nil {
|
||||
a.log.Warnln("WARNING:", listenaddr[:7], "may have unsafe permissions!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,10 +270,10 @@ func (a *AdminSocket) listen() {
|
|||
a.listener, err = net.Listen("tcp", u.Host)
|
||||
default:
|
||||
// err = errors.New(fmt.Sprint("protocol not supported: ", u.Scheme))
|
||||
a.listener, err = net.Listen("tcp", a.listenaddr)
|
||||
a.listener, err = net.Listen("tcp", listenaddr)
|
||||
}
|
||||
} else {
|
||||
a.listener, err = net.Listen("tcp", a.listenaddr)
|
||||
a.listener, err = net.Listen("tcp", listenaddr)
|
||||
}
|
||||
if err != nil {
|
||||
a.log.Errorf("Admin socket failed to listen: %v", err)
|
||||
|
@ -271,37 +325,58 @@ func (a *AdminSocket) handleRequest(conn net.Conn) {
|
|||
for {
|
||||
var err error
|
||||
var buf json.RawMessage
|
||||
_ = decoder.Decode(&buf)
|
||||
var req AdminSocketRequest
|
||||
var resp AdminSocketResponse
|
||||
resp.Status = "success"
|
||||
if err = json.Unmarshal(buf, &resp.Request); err == nil {
|
||||
if resp.Request.Name == "" {
|
||||
resp.Status = "error"
|
||||
resp.Response = &ErrorResponse{
|
||||
Error: "No request specified",
|
||||
}
|
||||
} else if h, ok := a.handlers[strings.ToLower(resp.Request.Name)]; ok {
|
||||
resp.Response, err = h.handler(buf)
|
||||
if err != nil {
|
||||
resp.Status = "error"
|
||||
resp.Response = &ErrorResponse{
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp.Status = "error"
|
||||
resp.Response = &ErrorResponse{
|
||||
Error: fmt.Sprintf("Unknown action '%s', try 'list' for help", resp.Request.Name),
|
||||
}
|
||||
if err := func() error {
|
||||
if err = decoder.Decode(&buf); err != nil {
|
||||
return fmt.Errorf("Failed to find request")
|
||||
}
|
||||
if err = json.Unmarshal(buf, &req); err != nil {
|
||||
return fmt.Errorf("Failed to unmarshal request")
|
||||
}
|
||||
if req.Name == "" {
|
||||
return fmt.Errorf("No request specified")
|
||||
}
|
||||
reqname := strings.ToLower(req.Name)
|
||||
handler, ok := a.handlers[reqname]
|
||||
if !ok {
|
||||
return fmt.Errorf("Unknown action '%s', try 'list' for help", reqname)
|
||||
}
|
||||
res, err := handler.handler(req.Arguments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Response, err = json.Marshal(res); err != nil {
|
||||
return fmt.Errorf("Failed to marshal response: %w", err)
|
||||
}
|
||||
resp.Status = "success"
|
||||
return nil
|
||||
}(); err != nil {
|
||||
resp.Status = "error"
|
||||
resp.Error = err.Error()
|
||||
}
|
||||
if err = encoder.Encode(resp); err != nil {
|
||||
a.log.Debugln("Encode error:", err)
|
||||
}
|
||||
if !resp.Request.KeepAlive {
|
||||
if !req.KeepAlive {
|
||||
break
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type DataUnit uint64
|
||||
|
||||
func (d DataUnit) String() string {
|
||||
switch {
|
||||
case d > 1024*1024*1024*1024:
|
||||
return fmt.Sprintf("%2.ftb", float64(d)/1024/1024/1024/1024)
|
||||
case d > 1024*1024*1024:
|
||||
return fmt.Sprintf("%2.fgb", float64(d)/1024/1024/1024)
|
||||
case d > 1024*1024:
|
||||
return fmt.Sprintf("%2.fmb", float64(d)/1024/1024)
|
||||
default:
|
||||
return fmt.Sprintf("%2.fkb", float64(d)/1024)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package admin
|
|||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
)
|
||||
|
@ -10,25 +12,30 @@ import (
|
|||
type GetDHTRequest struct{}
|
||||
|
||||
type GetDHTResponse struct {
|
||||
DHT map[string]DHTEntry `json:"dht"`
|
||||
DHT []DHTEntry `json:"dht"`
|
||||
}
|
||||
|
||||
type DHTEntry struct {
|
||||
IPAddress string `json:"address"`
|
||||
PublicKey string `json:"key"`
|
||||
Port uint64 `json:"port"`
|
||||
Rest uint64 `json:"rest"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getDHTHandler(req *GetDHTRequest, res *GetDHTResponse) error {
|
||||
res.DHT = map[string]DHTEntry{}
|
||||
for _, d := range a.core.GetDHT() {
|
||||
dht := a.core.GetDHT()
|
||||
res.DHT = make([]DHTEntry, 0, len(dht))
|
||||
for _, d := range dht {
|
||||
addr := address.AddrForKey(d.Key)
|
||||
so := net.IP(addr[:]).String()
|
||||
res.DHT[so] = DHTEntry{
|
||||
res.DHT = append(res.DHT, DHTEntry{
|
||||
IPAddress: net.IP(addr[:]).String(),
|
||||
PublicKey: hex.EncodeToString(d.Key[:]),
|
||||
Port: d.Port,
|
||||
Rest: d.Rest,
|
||||
}
|
||||
})
|
||||
}
|
||||
sort.SliceStable(res.DHT, func(i, j int) bool {
|
||||
return strings.Compare(res.DHT[i].PublicKey, res.DHT[j].PublicKey) < 0
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package admin
|
|||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
)
|
||||
|
@ -11,23 +13,28 @@ type GetPathsRequest struct {
|
|||
}
|
||||
|
||||
type GetPathsResponse struct {
|
||||
Paths map[string]PathEntry `json:"paths"`
|
||||
Paths []PathEntry `json:"paths"`
|
||||
}
|
||||
|
||||
type PathEntry struct {
|
||||
IPAddress string `json:"address"`
|
||||
PublicKey string `json:"key"`
|
||||
Path []uint64 `json:"path"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getPathsHandler(req *GetPathsRequest, res *GetPathsResponse) error {
|
||||
res.Paths = map[string]PathEntry{}
|
||||
for _, p := range a.core.GetPaths() {
|
||||
paths := a.core.GetPaths()
|
||||
res.Paths = make([]PathEntry, 0, len(paths))
|
||||
for _, p := range paths {
|
||||
addr := address.AddrForKey(p.Key)
|
||||
so := net.IP(addr[:]).String()
|
||||
res.Paths[so] = PathEntry{
|
||||
res.Paths = append(res.Paths, PathEntry{
|
||||
IPAddress: net.IP(addr[:]).String(),
|
||||
PublicKey: hex.EncodeToString(p.Key),
|
||||
Path: p.Path,
|
||||
}
|
||||
})
|
||||
}
|
||||
sort.SliceStable(res.Paths, func(i, j int) bool {
|
||||
return strings.Compare(res.Paths[i].PublicKey, res.Paths[j].PublicKey) < 0
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package admin
|
|||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"sort"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
)
|
||||
|
@ -11,33 +12,38 @@ type GetPeersRequest struct {
|
|||
}
|
||||
|
||||
type GetPeersResponse struct {
|
||||
Peers map[string]PeerEntry `json:"peers"`
|
||||
Peers []PeerEntry `json:"peers"`
|
||||
}
|
||||
|
||||
type PeerEntry struct {
|
||||
IPAddress string `json:"address"`
|
||||
PublicKey string `json:"key"`
|
||||
Port uint64 `json:"port"`
|
||||
Coords []uint64 `json:"coords"`
|
||||
Remote string `json:"remote"`
|
||||
RXBytes uint64 `json:"bytes_recvd"`
|
||||
TXBytes uint64 `json:"bytes_sent"`
|
||||
RXBytes DataUnit `json:"bytes_recvd"`
|
||||
TXBytes DataUnit `json:"bytes_sent"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getPeersHandler(req *GetPeersRequest, res *GetPeersResponse) error {
|
||||
res.Peers = map[string]PeerEntry{}
|
||||
for _, p := range a.core.GetPeers() {
|
||||
peers := a.core.GetPeers()
|
||||
res.Peers = make([]PeerEntry, 0, len(peers))
|
||||
for _, p := range peers {
|
||||
addr := address.AddrForKey(p.Key)
|
||||
so := net.IP(addr[:]).String()
|
||||
res.Peers[so] = PeerEntry{
|
||||
res.Peers = append(res.Peers, PeerEntry{
|
||||
IPAddress: net.IP(addr[:]).String(),
|
||||
PublicKey: hex.EncodeToString(p.Key),
|
||||
Port: p.Port,
|
||||
Coords: p.Coords,
|
||||
Remote: p.Remote,
|
||||
RXBytes: p.RXBytes,
|
||||
TXBytes: p.TXBytes,
|
||||
RXBytes: DataUnit(p.RXBytes),
|
||||
TXBytes: DataUnit(p.TXBytes),
|
||||
Uptime: p.Uptime.Seconds(),
|
||||
}
|
||||
})
|
||||
}
|
||||
sort.Slice(res.Peers, func(i, j int) bool {
|
||||
return res.Peers[i].Port < res.Peers[j].Port
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -9,28 +9,22 @@ import (
|
|||
type GetSelfRequest struct{}
|
||||
|
||||
type GetSelfResponse struct {
|
||||
Self map[string]SelfEntry `json:"self"`
|
||||
}
|
||||
|
||||
type SelfEntry struct {
|
||||
BuildName string `json:"build_name"`
|
||||
BuildVersion string `json:"build_version"`
|
||||
PublicKey string `json:"key"`
|
||||
IPAddress string `json:"address"`
|
||||
Coords []uint64 `json:"coords"`
|
||||
Subnet string `json:"subnet"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getSelfHandler(req *GetSelfRequest, res *GetSelfResponse) error {
|
||||
res.Self = make(map[string]SelfEntry)
|
||||
self := a.core.GetSelf()
|
||||
addr := a.core.Address().String()
|
||||
snet := a.core.Subnet()
|
||||
res.Self[addr] = SelfEntry{
|
||||
BuildName: version.BuildName(),
|
||||
BuildVersion: version.BuildVersion(),
|
||||
PublicKey: hex.EncodeToString(self.Key[:]),
|
||||
Subnet: snet.String(),
|
||||
Coords: self.Coords,
|
||||
}
|
||||
res.BuildName = version.BuildName()
|
||||
res.BuildVersion = version.BuildVersion()
|
||||
res.PublicKey = hex.EncodeToString(self.Key[:])
|
||||
res.IPAddress = a.core.Address().String()
|
||||
res.Subnet = snet.String()
|
||||
res.Coords = self.Coords
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package admin
|
|||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
)
|
||||
|
@ -10,21 +12,32 @@ import (
|
|||
type GetSessionsRequest struct{}
|
||||
|
||||
type GetSessionsResponse struct {
|
||||
Sessions map[string]SessionEntry `json:"sessions"`
|
||||
Sessions []SessionEntry `json:"sessions"`
|
||||
}
|
||||
|
||||
type SessionEntry struct {
|
||||
PublicKey string `json:"key"`
|
||||
IPAddress string `json:"address"`
|
||||
PublicKey string `json:"key"`
|
||||
RXBytes DataUnit `json:"bytes_recvd"`
|
||||
TXBytes DataUnit `json:"bytes_sent"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getSessionsHandler(req *GetSessionsRequest, res *GetSessionsResponse) error {
|
||||
res.Sessions = map[string]SessionEntry{}
|
||||
for _, s := range a.core.GetSessions() {
|
||||
sessions := a.core.GetSessions()
|
||||
res.Sessions = make([]SessionEntry, 0, len(sessions))
|
||||
for _, s := range sessions {
|
||||
addr := address.AddrForKey(s.Key)
|
||||
so := net.IP(addr[:]).String()
|
||||
res.Sessions[so] = SessionEntry{
|
||||
res.Sessions = append(res.Sessions, SessionEntry{
|
||||
IPAddress: net.IP(addr[:]).String(),
|
||||
PublicKey: hex.EncodeToString(s.Key[:]),
|
||||
}
|
||||
RXBytes: DataUnit(s.RXBytes),
|
||||
TXBytes: DataUnit(s.TXBytes),
|
||||
Uptime: s.Uptime.Seconds(),
|
||||
})
|
||||
}
|
||||
sort.SliceStable(res.Sessions, func(i, j int) bool {
|
||||
return strings.Compare(res.Sessions[i].PublicKey, res.Sessions[j].PublicKey) < 0
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
16
src/admin/options.go
Normal file
16
src/admin/options.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package admin
|
||||
|
||||
func (c *AdminSocket) _applyOption(opt SetupOption) {
|
||||
switch v := opt.(type) {
|
||||
case ListenAddress:
|
||||
c.config.listenaddr = v
|
||||
}
|
||||
}
|
||||
|
||||
type SetupOption interface {
|
||||
isSetupOption()
|
||||
}
|
||||
|
||||
type ListenAddress string
|
||||
|
||||
func (a ListenAddress) isSetupOption() {}
|
12
src/admin/removepeer.go
Normal file
12
src/admin/removepeer.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package admin
|
||||
|
||||
type RemovePeerRequest struct {
|
||||
Uri string `json:"uri"`
|
||||
Sintf string `json:"interface,omitempty"`
|
||||
}
|
||||
|
||||
type RemovePeerResponse struct{}
|
||||
|
||||
func (a *AdminSocket) removePeerHandler(req *RemovePeerRequest, res *RemovePeerResponse) error {
|
||||
return a.core.RemovePeer(req.Uri, req.Sintf)
|
||||
}
|
|
@ -19,16 +19,14 @@ package config
|
|||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// NodeConfig is the main configuration structure, containing configuration
|
||||
// options that are necessary for an Yggdrasil node to run. You will need to
|
||||
// supply one of these structs to the Yggdrasil core when starting a node.
|
||||
type NodeConfig struct {
|
||||
sync.RWMutex `json:"-"`
|
||||
Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."`
|
||||
InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ tls://a.b.c.d:e ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."`
|
||||
InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ \"tls://a.b.c.d:e\" ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."`
|
||||
Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntls://0.0.0.0:0 or tls://[::]:0 to listen on all interfaces."`
|
||||
AdminListen string `comment:"Listen address for admin connections. Default is to listen for local\nconnections either on TCP/9001 or a UNIX socket depending on your\nplatform. Use this value for yggdrasilctl -endpoint=X. To disable\nthe admin socket, use the value \"none\" instead."`
|
||||
MulticastInterfaces []MulticastInterfaceConfig `comment:"Configuration for which interfaces multicast peer discovery should be\nenabled on. Each entry in the list should be a json object which may\ncontain Regex, Beacon, Listen, and Port. Regex is a regular expression\nwhich is matched against an interface name, and interfaces use the\nfirst configuration that they match gainst. Beacon configures whether\nor not the node should send link-local multicast beacons to advertise\ntheir presence, while listening for incoming connections on Port.\nListen controls whether or not the node listens for multicast beacons\nand opens outgoing connections."`
|
||||
|
|
226
src/core/api.go
226
src/core/api.go
|
@ -2,32 +2,24 @@ package core
|
|||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
//"encoding/hex"
|
||||
"encoding/json"
|
||||
//"errors"
|
||||
//"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
//"sort"
|
||||
//"time"
|
||||
|
||||
"github.com/gologme/log"
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
//"github.com/Arceliar/phony"
|
||||
)
|
||||
|
||||
type Self struct {
|
||||
type SelfInfo struct {
|
||||
Key ed25519.PublicKey
|
||||
Root ed25519.PublicKey
|
||||
Coords []uint64
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
type PeerInfo struct {
|
||||
Key ed25519.PublicKey
|
||||
Root ed25519.PublicKey
|
||||
Coords []uint64
|
||||
|
@ -38,23 +30,26 @@ type Peer struct {
|
|||
Uptime time.Duration
|
||||
}
|
||||
|
||||
type DHTEntry struct {
|
||||
type DHTEntryInfo struct {
|
||||
Key ed25519.PublicKey
|
||||
Port uint64
|
||||
Rest uint64
|
||||
}
|
||||
|
||||
type PathEntry struct {
|
||||
type PathEntryInfo struct {
|
||||
Key ed25519.PublicKey
|
||||
Path []uint64
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Key ed25519.PublicKey
|
||||
type SessionInfo struct {
|
||||
Key ed25519.PublicKey
|
||||
RXBytes uint64
|
||||
TXBytes uint64
|
||||
Uptime time.Duration
|
||||
}
|
||||
|
||||
func (c *Core) GetSelf() Self {
|
||||
var self Self
|
||||
func (c *Core) GetSelf() SelfInfo {
|
||||
var self SelfInfo
|
||||
s := c.PacketConn.PacketConn.Debug.GetSelf()
|
||||
self.Key = s.Key
|
||||
self.Root = s.Root
|
||||
|
@ -62,17 +57,17 @@ func (c *Core) GetSelf() Self {
|
|||
return self
|
||||
}
|
||||
|
||||
func (c *Core) GetPeers() []Peer {
|
||||
var peers []Peer
|
||||
func (c *Core) GetPeers() []PeerInfo {
|
||||
var peers []PeerInfo
|
||||
names := make(map[net.Conn]string)
|
||||
c.links.mutex.Lock()
|
||||
for _, info := range c.links.links {
|
||||
names[info.conn] = info.lname
|
||||
}
|
||||
c.links.mutex.Unlock()
|
||||
phony.Block(&c.links, func() {
|
||||
for _, info := range c.links._links {
|
||||
names[info.conn] = info.lname
|
||||
}
|
||||
})
|
||||
ps := c.PacketConn.PacketConn.Debug.GetPeers()
|
||||
for _, p := range ps {
|
||||
var info Peer
|
||||
var info PeerInfo
|
||||
info.Key = p.Key
|
||||
info.Root = p.Root
|
||||
info.Coords = p.Coords
|
||||
|
@ -91,11 +86,11 @@ func (c *Core) GetPeers() []Peer {
|
|||
return peers
|
||||
}
|
||||
|
||||
func (c *Core) GetDHT() []DHTEntry {
|
||||
var dhts []DHTEntry
|
||||
func (c *Core) GetDHT() []DHTEntryInfo {
|
||||
var dhts []DHTEntryInfo
|
||||
ds := c.PacketConn.PacketConn.Debug.GetDHT()
|
||||
for _, d := range ds {
|
||||
var info DHTEntry
|
||||
var info DHTEntryInfo
|
||||
info.Key = d.Key
|
||||
info.Port = d.Port
|
||||
info.Rest = d.Rest
|
||||
|
@ -104,11 +99,11 @@ func (c *Core) GetDHT() []DHTEntry {
|
|||
return dhts
|
||||
}
|
||||
|
||||
func (c *Core) GetPaths() []PathEntry {
|
||||
var paths []PathEntry
|
||||
func (c *Core) GetPaths() []PathEntryInfo {
|
||||
var paths []PathEntryInfo
|
||||
ps := c.PacketConn.PacketConn.Debug.GetPaths()
|
||||
for _, p := range ps {
|
||||
var info PathEntry
|
||||
var info PathEntryInfo
|
||||
info.Key = p.Key
|
||||
info.Path = p.Path
|
||||
paths = append(paths, info)
|
||||
|
@ -116,12 +111,15 @@ func (c *Core) GetPaths() []PathEntry {
|
|||
return paths
|
||||
}
|
||||
|
||||
func (c *Core) GetSessions() []Session {
|
||||
var sessions []Session
|
||||
func (c *Core) GetSessions() []SessionInfo {
|
||||
var sessions []SessionInfo
|
||||
ss := c.PacketConn.Debug.GetSessions()
|
||||
for _, s := range ss {
|
||||
var info Session
|
||||
var info SessionInfo
|
||||
info.Key = s.Key
|
||||
info.RXBytes = s.RX
|
||||
info.TXBytes = s.TX
|
||||
info.Uptime = s.Uptime
|
||||
sessions = append(sessions, info)
|
||||
}
|
||||
return sessions
|
||||
|
@ -130,8 +128,17 @@ func (c *Core) GetSessions() []Session {
|
|||
// Listen starts a new listener (either TCP or TLS). The input should be a url.URL
|
||||
// parsed from a string of the form e.g. "tcp://a.b.c.d:e". In the case of a
|
||||
// link-local address, the interface should be provided as the second argument.
|
||||
func (c *Core) Listen(u *url.URL, sintf string) (*TcpListener, error) {
|
||||
return c.links.tcp.listenURL(u, sintf)
|
||||
func (c *Core) Listen(u *url.URL, sintf string) (*Listener, error) {
|
||||
switch u.Scheme {
|
||||
case "tcp":
|
||||
return c.links.tcp.listen(u, sintf)
|
||||
case "tls":
|
||||
return c.links.tls.listen(u, sintf)
|
||||
case "unix":
|
||||
return c.links.unix.listen(u, sintf)
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognised scheme %q", u.Scheme)
|
||||
}
|
||||
}
|
||||
|
||||
// Address gets the IPv6 address of the Yggdrasil node. This is always a /128
|
||||
|
@ -159,92 +166,73 @@ func (c *Core) Subnet() net.IPNet {
|
|||
// may be useful if you want to redirect the output later. Note that this
|
||||
// expects a Logger from the github.com/gologme/log package and not from Go's
|
||||
// built-in log package.
|
||||
func (c *Core) SetLogger(log *log.Logger) {
|
||||
func (c *Core) SetLogger(log Logger) {
|
||||
c.log = log
|
||||
}
|
||||
|
||||
// AddPeer adds a peer. This should be specified in the peer URI format, e.g.:
|
||||
// tcp://a.b.c.d:e
|
||||
// socks://a.b.c.d:e/f.g.h.i:j
|
||||
//
|
||||
// tcp://a.b.c.d:e
|
||||
// socks://a.b.c.d:e/f.g.h.i:j
|
||||
//
|
||||
// This adds the peer to the peer list, so that they will be called again if the
|
||||
// connection drops.
|
||||
/*
|
||||
func (c *Core) AddPeer(addr string, sintf string) error {
|
||||
if err := c.CallPeer(addr, sintf); err != nil {
|
||||
// TODO: We maybe want this to write the peer to the persistent
|
||||
// configuration even if a connection attempt fails, but first we'll need to
|
||||
// move the code to check the peer URI so that we don't deliberately save a
|
||||
// peer with a known bad URI. Loading peers from config should really do the
|
||||
// same thing too but I don't think that happens today
|
||||
func (c *Core) AddPeer(uri string, sourceInterface string) error {
|
||||
var known bool
|
||||
phony.Block(c, func() {
|
||||
_, known = c.config._peers[Peer{uri, sourceInterface}]
|
||||
})
|
||||
if known {
|
||||
return fmt.Errorf("peer already configured")
|
||||
}
|
||||
u, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.config.Mutex.Lock()
|
||||
defer c.config.Mutex.Unlock()
|
||||
if sintf == "" {
|
||||
for _, peer := range c.config.Current.Peers {
|
||||
if peer == addr {
|
||||
return errors.New("peer already added")
|
||||
}
|
||||
}
|
||||
c.config.Current.Peers = append(c.config.Current.Peers, addr)
|
||||
} else {
|
||||
if _, ok := c.config.Current.InterfacePeers[sintf]; ok {
|
||||
for _, peer := range c.config.Current.InterfacePeers[sintf] {
|
||||
if peer == addr {
|
||||
return errors.New("peer already added")
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, ok := c.config.Current.InterfacePeers[sintf]; !ok {
|
||||
c.config.Current.InterfacePeers[sintf] = []string{addr}
|
||||
} else {
|
||||
c.config.Current.InterfacePeers[sintf] = append(c.config.Current.InterfacePeers[sintf], addr)
|
||||
}
|
||||
info, err := c.links.call(u, sourceInterface)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (c *Core) RemovePeer(addr string, sintf string) error {
|
||||
if sintf == "" {
|
||||
for i, peer := range c.config.Current.Peers {
|
||||
if peer == addr {
|
||||
c.config.Current.Peers = append(c.config.Current.Peers[:i], c.config.Current.Peers[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if _, ok := c.config.Current.InterfacePeers[sintf]; ok {
|
||||
for i, peer := range c.config.Current.InterfacePeers[sintf] {
|
||||
if peer == addr {
|
||||
c.config.Current.InterfacePeers[sintf] = append(c.config.Current.InterfacePeers[sintf][:i], c.config.Current.InterfacePeers[sintf][i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic("TODO") // Get the net.Conn to this peer (if any) and close it
|
||||
c.peers.Act(nil, func() {
|
||||
ports := c.peers.ports
|
||||
for _, peer := range ports {
|
||||
if addr == peer.intf.name() {
|
||||
c.peers._removePeer(peer)
|
||||
}
|
||||
}
|
||||
phony.Block(c, func() {
|
||||
c.config._peers[Peer{uri, sourceInterface}] = &info
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// RemovePeer removes a peer. The peer should be specified in URI format, see AddPeer.
|
||||
// The peer is not disconnected immediately.
|
||||
func (c *Core) RemovePeer(uri string, sourceInterface string) error {
|
||||
var err error
|
||||
phony.Block(c, func() {
|
||||
peer := Peer{uri, sourceInterface}
|
||||
linkInfo, ok := c.config._peers[peer]
|
||||
if !ok {
|
||||
err = fmt.Errorf("peer not configured")
|
||||
return
|
||||
}
|
||||
if ok && linkInfo != nil {
|
||||
c.links.Act(nil, func() {
|
||||
if link := c.links._links[*linkInfo]; link != nil {
|
||||
_ = link.close()
|
||||
}
|
||||
})
|
||||
}
|
||||
delete(c.config._peers, peer)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// CallPeer calls a peer once. This should be specified in the peer URI format,
|
||||
// e.g.:
|
||||
// tcp://a.b.c.d:e
|
||||
// socks://a.b.c.d:e/f.g.h.i:j
|
||||
//
|
||||
// tcp://a.b.c.d:e
|
||||
// socks://a.b.c.d:e/f.g.h.i:j
|
||||
//
|
||||
// This does not add the peer to the peer list, so if the connection drops, the
|
||||
// peer will not be called again automatically.
|
||||
func (c *Core) CallPeer(u *url.URL, sintf string) error {
|
||||
return c.links.call(u, sintf)
|
||||
_, err := c.links.call(u, sintf)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Core) PublicKey() ed25519.PublicKey {
|
||||
|
@ -254,7 +242,7 @@ func (c *Core) PublicKey() ed25519.PublicKey {
|
|||
// Hack to get the admin stuff working, TODO something cleaner
|
||||
|
||||
type AddHandler interface {
|
||||
AddHandler(name string, args []string, handlerfunc AddHandlerFunc) error
|
||||
AddHandler(name, desc string, args []string, handlerfunc AddHandlerFunc) error
|
||||
}
|
||||
|
||||
type AddHandlerFunc func(json.RawMessage) (interface{}, error)
|
||||
|
@ -262,16 +250,28 @@ type AddHandlerFunc func(json.RawMessage) (interface{}, error)
|
|||
// SetAdmin must be called after Init and before Start.
|
||||
// It sets the admin handler for NodeInfo and the Debug admin functions.
|
||||
func (c *Core) SetAdmin(a AddHandler) error {
|
||||
if err := a.AddHandler("getNodeInfo", []string{"key"}, c.proto.nodeinfo.nodeInfoAdminHandler); err != nil {
|
||||
if err := a.AddHandler(
|
||||
"getNodeInfo", "Request nodeinfo from a remote node by its public key", []string{"key"},
|
||||
c.proto.nodeinfo.nodeInfoAdminHandler,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AddHandler("debug_remoteGetSelf", []string{"key"}, c.proto.getSelfHandler); err != nil {
|
||||
if err := a.AddHandler(
|
||||
"debug_remoteGetSelf", "Debug use only", []string{"key"},
|
||||
c.proto.getSelfHandler,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AddHandler("debug_remoteGetPeers", []string{"key"}, c.proto.getPeersHandler); err != nil {
|
||||
if err := a.AddHandler(
|
||||
"debug_remoteGetPeers", "Debug use only", []string{"key"},
|
||||
c.proto.getPeersHandler,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AddHandler("debug_remoteGetDHT", []string{"key"}, c.proto.getDHTHandler); err != nil {
|
||||
if err := a.AddHandler(
|
||||
"debug_remoteGetDHT", "Debug use only", []string{"key"},
|
||||
c.proto.getDHTHandler,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
189
src/core/core.go
189
src/core/core.go
|
@ -3,10 +3,8 @@ package core
|
|||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
@ -16,8 +14,6 @@ import (
|
|||
"github.com/Arceliar/phony"
|
||||
"github.com/gologme/log"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||
)
|
||||
|
||||
|
@ -29,62 +25,86 @@ type Core struct {
|
|||
// guarantee that it will be covered by the mutex
|
||||
phony.Inbox
|
||||
*iwe.PacketConn
|
||||
config *config.NodeConfig // Config
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
secret ed25519.PrivateKey
|
||||
public ed25519.PublicKey
|
||||
links links
|
||||
proto protoHandler
|
||||
log *log.Logger
|
||||
log Logger
|
||||
addPeerTimer *time.Timer
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
config struct {
|
||||
_peers map[Peer]*linkInfo // configurable after startup
|
||||
_listeners map[ListenAddress]struct{} // configurable after startup
|
||||
nodeinfo NodeInfo // immutable after startup
|
||||
nodeinfoPrivacy NodeInfoPrivacy // immutable after startup
|
||||
_allowedPublicKeys map[[32]byte]struct{} // configurable after startup
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Core) _init() error {
|
||||
// TODO separate init and start functions
|
||||
// Init sets up structs
|
||||
// Start launches goroutines that depend on structs being set up
|
||||
// This is pretty much required to completely avoid race conditions
|
||||
c.config.RLock()
|
||||
defer c.config.RUnlock()
|
||||
func New(secret ed25519.PrivateKey, logger Logger, opts ...SetupOption) (*Core, error) {
|
||||
c := &Core{
|
||||
log: logger,
|
||||
}
|
||||
if name := version.BuildName(); name != "unknown" {
|
||||
c.log.Infoln("Build name:", name)
|
||||
}
|
||||
if version := version.BuildVersion(); version != "unknown" {
|
||||
c.log.Infoln("Build version:", version)
|
||||
}
|
||||
c.ctx, c.cancel = context.WithCancel(context.Background())
|
||||
// Take a copy of the private key so that it is in our own memory space.
|
||||
if len(secret) != ed25519.PrivateKeySize {
|
||||
return nil, fmt.Errorf("private key is incorrect length")
|
||||
}
|
||||
c.secret = make(ed25519.PrivateKey, ed25519.PrivateKeySize)
|
||||
copy(c.secret, secret)
|
||||
c.public = secret.Public().(ed25519.PublicKey)
|
||||
var err error
|
||||
if c.PacketConn, err = iwe.NewPacketConn(c.secret); err != nil {
|
||||
return nil, fmt.Errorf("error creating encryption: %w", err)
|
||||
}
|
||||
c.config._peers = map[Peer]*linkInfo{}
|
||||
c.config._listeners = map[ListenAddress]struct{}{}
|
||||
c.config._allowedPublicKeys = map[[32]byte]struct{}{}
|
||||
for _, opt := range opts {
|
||||
c._applyOption(opt)
|
||||
}
|
||||
if c.log == nil {
|
||||
c.log = log.New(ioutil.Discard, "", 0)
|
||||
c.log = log.New(io.Discard, "", 0)
|
||||
}
|
||||
|
||||
sigPriv, err := hex.DecodeString(c.config.PrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(sigPriv) < ed25519.PrivateKeySize {
|
||||
return errors.New("PrivateKey is incorrect length")
|
||||
}
|
||||
|
||||
c.secret = ed25519.PrivateKey(sigPriv)
|
||||
c.public = c.secret.Public().(ed25519.PublicKey)
|
||||
// TODO check public against current.PublicKey, error if they don't match
|
||||
|
||||
c.PacketConn, err = iwe.NewPacketConn(c.secret)
|
||||
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
|
||||
c.proto.init(c)
|
||||
if err := c.proto.nodeinfo.setNodeInfo(c.config.NodeInfo, c.config.NodeInfoPrivacy); err != nil {
|
||||
return fmt.Errorf("setNodeInfo: %w", err)
|
||||
if err := c.links.init(c); err != nil {
|
||||
return nil, fmt.Errorf("error initialising links: %w", err)
|
||||
}
|
||||
return err
|
||||
if err := c.proto.nodeinfo.setNodeInfo(c.config.nodeinfo, bool(c.config.nodeinfoPrivacy)); err != nil {
|
||||
return nil, fmt.Errorf("error setting node info: %w", err)
|
||||
}
|
||||
for listenaddr := range c.config._listeners {
|
||||
u, err := url.Parse(string(listenaddr))
|
||||
if err != nil {
|
||||
c.log.Errorf("Invalid listener URI %q specified, ignoring\n", listenaddr)
|
||||
continue
|
||||
}
|
||||
if _, err = c.links.listen(u, ""); err != nil {
|
||||
c.log.Errorf("Failed to start listener %q: %s\n", listenaddr, err)
|
||||
}
|
||||
}
|
||||
c.Act(nil, c._addPeerLoop)
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// If any static peers were provided in the configuration above then we should
|
||||
// configure them. The loop ensures that disconnected peers will eventually
|
||||
// be reconnected with.
|
||||
func (c *Core) _addPeerLoop() {
|
||||
c.config.RLock()
|
||||
defer c.config.RUnlock()
|
||||
|
||||
if c.addPeerTimer == nil {
|
||||
select {
|
||||
case <-c.ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
// Add peers from the Peers section
|
||||
for _, peer := range c.config.Peers {
|
||||
for peer := range c.config._peers {
|
||||
go func(peer string, intf string) {
|
||||
u, err := url.Parse(peer)
|
||||
if err != nil {
|
||||
|
@ -93,22 +113,7 @@ func (c *Core) _addPeerLoop() {
|
|||
if err := c.CallPeer(u, intf); err != nil {
|
||||
c.log.Errorln("Failed to add peer:", err)
|
||||
}
|
||||
}(peer, "") // TODO: this should be acted and not in a goroutine?
|
||||
}
|
||||
|
||||
// Add peers from the InterfacePeers section
|
||||
for intf, intfpeers := range c.config.InterfacePeers {
|
||||
for _, peer := range intfpeers {
|
||||
go func(peer string, intf string) {
|
||||
u, err := url.Parse(peer)
|
||||
if err != nil {
|
||||
c.log.Errorln("Failed to parse peer url:", peer, err)
|
||||
}
|
||||
if err := c.CallPeer(u, intf); err != nil {
|
||||
c.log.Errorln("Failed to add peer:", err)
|
||||
}
|
||||
}(peer, intf) // TODO: this should be acted and not in a goroutine?
|
||||
}
|
||||
}(peer.URI, peer.SourceInterface) // TODO: this should be acted and not in a goroutine?
|
||||
}
|
||||
|
||||
c.addPeerTimer = time.AfterFunc(time.Minute, func() {
|
||||
|
@ -116,75 +121,24 @@ func (c *Core) _addPeerLoop() {
|
|||
})
|
||||
}
|
||||
|
||||
// Start starts up Yggdrasil using the provided config.NodeConfig, and outputs
|
||||
// debug logging through the provided log.Logger. The started stack will include
|
||||
// TCP and UDP sockets, a multicast discovery socket, an admin socket, router,
|
||||
// switch and DHT node. A config.NodeState is returned which contains both the
|
||||
// current and previous configurations (from reconfigures).
|
||||
func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (err error) {
|
||||
phony.Block(c, func() {
|
||||
err = c._start(nc, log)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// This function is unsafe and should only be ran by the core actor.
|
||||
func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) error {
|
||||
c.log = log
|
||||
c.config = nc
|
||||
|
||||
if name := version.BuildName(); name != "unknown" {
|
||||
c.log.Infoln("Build name:", name)
|
||||
}
|
||||
if version := version.BuildVersion(); version != "unknown" {
|
||||
c.log.Infoln("Build version:", version)
|
||||
}
|
||||
|
||||
c.log.Infoln("Starting up...")
|
||||
if err := c._init(); err != nil {
|
||||
c.log.Errorln("Failed to initialize core")
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.links.init(c); err != nil {
|
||||
c.log.Errorln("Failed to start link interfaces")
|
||||
return err
|
||||
}
|
||||
|
||||
c.addPeerTimer = time.AfterFunc(0, func() {
|
||||
c.Act(nil, c._addPeerLoop)
|
||||
})
|
||||
|
||||
c.log.Infoln("Startup complete")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop shuts down the Yggdrasil node.
|
||||
func (c *Core) Stop() {
|
||||
phony.Block(c, func() {
|
||||
c.log.Infoln("Stopping...")
|
||||
c._close()
|
||||
_ = c._close()
|
||||
c.log.Infoln("Stopped")
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Core) Close() error {
|
||||
var err error
|
||||
phony.Block(c, func() {
|
||||
err = c._close()
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// This function is unsafe and should only be ran by the core actor.
|
||||
func (c *Core) _close() error {
|
||||
c.ctxCancel()
|
||||
c.cancel()
|
||||
c.links.shutdown()
|
||||
err := c.PacketConn.Close()
|
||||
if c.addPeerTimer != nil {
|
||||
c.addPeerTimer.Stop()
|
||||
c.addPeerTimer = nil
|
||||
}
|
||||
_ = c.links.stop()
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -237,3 +191,16 @@ func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
Printf(string, ...interface{})
|
||||
Println(...interface{})
|
||||
Infof(string, ...interface{})
|
||||
Infoln(...interface{})
|
||||
Warnf(string, ...interface{})
|
||||
Warnln(...interface{})
|
||||
Errorf(string, ...interface{})
|
||||
Errorln(...interface{})
|
||||
Debugf(string, ...interface{})
|
||||
Debugln(...interface{})
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package core
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
|
@ -9,21 +10,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gologme/log"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
)
|
||||
|
||||
// GenerateConfig produces default configuration with suitable modifications for tests.
|
||||
func GenerateConfig() *config.NodeConfig {
|
||||
cfg := defaults.GenerateConfig()
|
||||
cfg.AdminListen = "none"
|
||||
cfg.Listen = []string{"tcp://127.0.0.1:0"}
|
||||
cfg.IfName = "none"
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// GetLoggerWithPrefix creates a new logger instance with prefix.
|
||||
// If verbose is set to true, three log levels are enabled: "info", "warn", "error".
|
||||
func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
|
||||
|
@ -40,13 +28,19 @@ func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
|
|||
// CreateAndConnectTwo creates two nodes. nodeB connects to nodeA.
|
||||
// Verbosity flag is passed to logger.
|
||||
func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core) {
|
||||
nodeA = new(Core)
|
||||
if err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose)); err != nil {
|
||||
var err error
|
||||
var skA, skB ed25519.PrivateKey
|
||||
if _, skA, err = ed25519.GenerateKey(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
nodeB = new(Core)
|
||||
if err := nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose)); err != nil {
|
||||
if _, skB, err = ed25519.GenerateKey(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logger := GetLoggerWithPrefix("", false)
|
||||
if nodeA, err = New(skA, logger, ListenAddress("tcp://127.0.0.1:0")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if nodeB, err = New(skB, logger, ListenAddress("tcp://127.0.0.1:0")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ package core
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
|
|
345
src/core/link.go
345
src/core/link.go
|
@ -1,7 +1,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
@ -9,31 +9,25 @@ import (
|
|||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
//"sync/atomic"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
"golang.org/x/net/proxy"
|
||||
//"github.com/Arceliar/phony" // TODO? use instead of mutexes
|
||||
)
|
||||
|
||||
type links struct {
|
||||
core *Core
|
||||
mutex sync.RWMutex // protects links below
|
||||
links map[linkInfo]*link
|
||||
tcp tcp // TCP interface support
|
||||
stopped chan struct{}
|
||||
// TODO timeout (to remove from switch), read from config.ReadTimeout
|
||||
phony.Inbox
|
||||
core *Core
|
||||
tcp *linkTCP // TCP interface support
|
||||
tls *linkTLS // TLS interface support
|
||||
unix *linkUNIX // UNIX interface support
|
||||
socks *linkSOCKS // SOCKS interface support
|
||||
_links map[linkInfo]*link // *link is nil if connection in progress
|
||||
}
|
||||
|
||||
// linkInfo is used as a map key
|
||||
type linkInfo struct {
|
||||
key keyArray
|
||||
linkType string // Type of link, e.g. TCP, AWDL
|
||||
local string // Local name or address
|
||||
remote string // Remote name or address
|
||||
|
@ -47,147 +41,215 @@ type link struct {
|
|||
info linkInfo
|
||||
incoming bool
|
||||
force bool
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
type linkOptions struct {
|
||||
pinnedEd25519Keys map[keyArray]struct{}
|
||||
}
|
||||
|
||||
type Listener struct {
|
||||
net.Listener
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
func (l *Listener) Close() error {
|
||||
err := l.Listener.Close()
|
||||
<-l.closed
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *links) init(c *Core) error {
|
||||
l.core = c
|
||||
l.mutex.Lock()
|
||||
l.links = make(map[linkInfo]*link)
|
||||
l.mutex.Unlock()
|
||||
l.stopped = make(chan struct{})
|
||||
l.tcp = l.newLinkTCP()
|
||||
l.tls = l.newLinkTLS(l.tcp)
|
||||
l.unix = l.newLinkUNIX()
|
||||
l.socks = l.newLinkSOCKS()
|
||||
l._links = make(map[linkInfo]*link)
|
||||
|
||||
if err := l.tcp.init(l); err != nil {
|
||||
c.log.Errorln("Failed to start TCP interface")
|
||||
return err
|
||||
}
|
||||
var listeners []ListenAddress
|
||||
phony.Block(c, func() {
|
||||
listeners = make([]ListenAddress, 0, len(c.config._listeners))
|
||||
for listener := range c.config._listeners {
|
||||
listeners = append(listeners, listener)
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *links) call(u *url.URL, sintf string) error {
|
||||
//u, err := url.Parse(uri)
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
|
||||
//}
|
||||
tcpOpts := tcpOptions{}
|
||||
if pubkeys, ok := u.Query()["key"]; ok && len(pubkeys) > 0 {
|
||||
tcpOpts.pinnedEd25519Keys = make(map[keyArray]struct{})
|
||||
for _, pubkey := range pubkeys {
|
||||
if sigPub, err := hex.DecodeString(pubkey); err == nil {
|
||||
var sigPubKey keyArray
|
||||
copy(sigPubKey[:], sigPub)
|
||||
tcpOpts.pinnedEd25519Keys[sigPubKey] = struct{}{}
|
||||
}
|
||||
func (l *links) shutdown() {
|
||||
phony.Block(l.tcp, func() {
|
||||
for l := range l.tcp._listeners {
|
||||
_ = l.Close()
|
||||
}
|
||||
})
|
||||
phony.Block(l.tls, func() {
|
||||
for l := range l.tls._listeners {
|
||||
_ = l.Close()
|
||||
}
|
||||
})
|
||||
phony.Block(l.unix, func() {
|
||||
for l := range l.unix._listeners {
|
||||
_ = l.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (l *links) isConnectedTo(info linkInfo) bool {
|
||||
var isConnected bool
|
||||
phony.Block(l, func() {
|
||||
_, isConnected = l._links[info]
|
||||
})
|
||||
return isConnected
|
||||
}
|
||||
|
||||
func (l *links) call(u *url.URL, sintf string) (linkInfo, error) {
|
||||
info := linkInfoFor(u.Scheme, sintf, u.Host)
|
||||
if l.isConnectedTo(info) {
|
||||
return info, nil
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "tcp":
|
||||
l.tcp.call(u.Host, tcpOpts, sintf)
|
||||
case "socks":
|
||||
tcpOpts.socksProxyAddr = u.Host
|
||||
if u.User != nil {
|
||||
tcpOpts.socksProxyAuth = &proxy.Auth{}
|
||||
tcpOpts.socksProxyAuth.User = u.User.Username()
|
||||
tcpOpts.socksProxyAuth.Password, _ = u.User.Password()
|
||||
options := linkOptions{
|
||||
pinnedEd25519Keys: map[keyArray]struct{}{},
|
||||
}
|
||||
for _, pubkey := range u.Query()["key"] {
|
||||
sigPub, err := hex.DecodeString(pubkey)
|
||||
if err != nil {
|
||||
return info, fmt.Errorf("pinned key contains invalid hex characters")
|
||||
}
|
||||
tcpOpts.upgrade = l.tcp.tls.forDialer // TODO make this configurable
|
||||
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
||||
l.tcp.call(pathtokens[0], tcpOpts, sintf)
|
||||
var sigPubKey keyArray
|
||||
copy(sigPubKey[:], sigPub)
|
||||
options.pinnedEd25519Keys[sigPubKey] = struct{}{}
|
||||
}
|
||||
switch info.linkType {
|
||||
case "tcp":
|
||||
go func() {
|
||||
if err := l.tcp.dial(u, options, sintf); err != nil {
|
||||
l.core.log.Warnf("Failed to dial TCP %s: %s\n", u.Host, err)
|
||||
}
|
||||
}()
|
||||
|
||||
case "socks":
|
||||
go func() {
|
||||
if err := l.socks.dial(u, options); err != nil {
|
||||
l.core.log.Warnf("Failed to dial SOCKS %s: %s\n", u.Host, err)
|
||||
}
|
||||
}()
|
||||
|
||||
case "tls":
|
||||
tcpOpts.upgrade = l.tcp.tls.forDialer
|
||||
// SNI headers must contain hostnames and not IP addresses, so we must make sure
|
||||
// that we do not populate the SNI with an IP literal. We do this by splitting
|
||||
// the host-port combo from the query option and then seeing if it parses to an
|
||||
// IP address successfully or not.
|
||||
var tlsSNI string
|
||||
if sni := u.Query().Get("sni"); sni != "" {
|
||||
if net.ParseIP(sni) == nil {
|
||||
tcpOpts.tlsSNI = sni
|
||||
tlsSNI = sni
|
||||
}
|
||||
}
|
||||
// If the SNI is not configured still because the above failed then we'll try
|
||||
// again but this time we'll use the host part of the peering URI instead.
|
||||
if tcpOpts.tlsSNI == "" {
|
||||
if tlsSNI == "" {
|
||||
if host, _, err := net.SplitHostPort(u.Host); err == nil && net.ParseIP(host) == nil {
|
||||
tcpOpts.tlsSNI = host
|
||||
tlsSNI = host
|
||||
}
|
||||
}
|
||||
l.tcp.call(u.Host, tcpOpts, sintf)
|
||||
go func() {
|
||||
if err := l.tls.dial(u, options, sintf, tlsSNI); err != nil {
|
||||
l.core.log.Warnf("Failed to dial TLS %s: %s\n", u.Host, err)
|
||||
}
|
||||
}()
|
||||
|
||||
case "unix":
|
||||
go func() {
|
||||
if err := l.unix.dial(u, options, sintf); err != nil {
|
||||
l.core.log.Warnf("Failed to dial UNIX %s: %s\n", u.Host, err)
|
||||
}
|
||||
}()
|
||||
|
||||
default:
|
||||
return errors.New("unknown call scheme: " + u.Scheme)
|
||||
return info, errors.New("unknown call scheme: " + u.Scheme)
|
||||
}
|
||||
return nil
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (l *links) create(conn net.Conn, name, linkType, local, remote string, incoming, force bool, options linkOptions) (*link, error) {
|
||||
// Technically anything unique would work for names, but let's pick something human readable, just for debugging
|
||||
func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
|
||||
var listener *Listener
|
||||
var err error
|
||||
switch u.Scheme {
|
||||
case "tcp":
|
||||
listener, err = l.tcp.listen(u, sintf)
|
||||
case "tls":
|
||||
listener, err = l.tls.listen(u, sintf)
|
||||
case "unix":
|
||||
listener, err = l.unix.listen(u, sintf)
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognised scheme %q", u.Scheme)
|
||||
}
|
||||
return listener, err
|
||||
}
|
||||
|
||||
func (l *links) create(conn net.Conn, name string, info linkInfo, incoming, force bool, options linkOptions) error {
|
||||
intf := link{
|
||||
conn: &linkConn{
|
||||
Conn: conn,
|
||||
up: time.Now(),
|
||||
},
|
||||
lname: name,
|
||||
links: l,
|
||||
options: options,
|
||||
info: linkInfo{
|
||||
linkType: linkType,
|
||||
local: local,
|
||||
remote: remote,
|
||||
},
|
||||
lname: name,
|
||||
links: l,
|
||||
options: options,
|
||||
info: info,
|
||||
incoming: incoming,
|
||||
force: force,
|
||||
}
|
||||
return &intf, nil
|
||||
}
|
||||
|
||||
func (l *links) stop() error {
|
||||
close(l.stopped)
|
||||
if err := l.tcp.stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
if err := intf.handler(); err != nil {
|
||||
l.core.log.Errorf("Link handler %s error (%s): %s", name, conn.RemoteAddr(), err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (intf *link) handler() (chan struct{}, error) {
|
||||
// TODO split some of this into shorter functions, so it's easier to read, and for the FIXME duplicate peer issue mentioned later
|
||||
defer intf.conn.Close()
|
||||
func (intf *link) handler() error {
|
||||
defer intf.conn.Close() // nolint:errcheck
|
||||
|
||||
// Don't connect to this link more than once.
|
||||
if intf.links.isConnectedTo(intf.info) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Mark the connection as in progress.
|
||||
phony.Block(intf.links, func() {
|
||||
intf.links._links[intf.info] = nil
|
||||
})
|
||||
|
||||
// When we're done, clean up the connection entry.
|
||||
defer phony.Block(intf.links, func() {
|
||||
delete(intf.links._links, intf.info)
|
||||
})
|
||||
|
||||
meta := version_getBaseMetadata()
|
||||
meta.key = intf.links.core.public
|
||||
metaBytes := meta.encode()
|
||||
// TODO timeouts on send/recv (goroutine for send/recv, channel select w/ timer)
|
||||
var err error
|
||||
if !util.FuncTimeout(30*time.Second, func() {
|
||||
var n int
|
||||
n, err = intf.conn.Write(metaBytes)
|
||||
if err == nil && n != len(metaBytes) {
|
||||
err = errors.New("incomplete metadata send")
|
||||
}
|
||||
}) {
|
||||
return nil, errors.New("timeout on metadata send")
|
||||
if err := intf.conn.SetDeadline(time.Now().Add(time.Second * 6)); err != nil {
|
||||
return fmt.Errorf("failed to set handshake deadline: %w", err)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
n, err := intf.conn.Write(metaBytes)
|
||||
switch {
|
||||
case err != nil:
|
||||
return fmt.Errorf("write handshake: %w", err)
|
||||
case err == nil && n != len(metaBytes):
|
||||
return fmt.Errorf("incomplete handshake send")
|
||||
}
|
||||
if !util.FuncTimeout(30*time.Second, func() {
|
||||
var n int
|
||||
n, err = io.ReadFull(intf.conn, metaBytes)
|
||||
if err == nil && n != len(metaBytes) {
|
||||
err = errors.New("incomplete metadata recv")
|
||||
}
|
||||
}) {
|
||||
return nil, errors.New("timeout on metadata recv")
|
||||
if _, err = io.ReadFull(intf.conn, metaBytes); err != nil {
|
||||
return fmt.Errorf("read handshake: %w", err)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err = intf.conn.SetDeadline(time.Time{}); err != nil {
|
||||
return fmt.Errorf("failed to clear handshake deadline: %w", err)
|
||||
}
|
||||
meta = version_metadata{}
|
||||
base := version_getBaseMetadata()
|
||||
if !meta.decode(metaBytes) {
|
||||
return nil, errors.New("failed to decode metadata")
|
||||
return errors.New("failed to decode metadata")
|
||||
}
|
||||
if !meta.check() {
|
||||
var connectError string
|
||||
|
@ -202,25 +264,23 @@ func (intf *link) handler() (chan struct{}, error) {
|
|||
fmt.Sprintf("%d.%d", base.ver, base.minorVer),
|
||||
fmt.Sprintf("%d.%d", meta.ver, meta.minorVer),
|
||||
)
|
||||
return nil, errors.New("remote node is incompatible version")
|
||||
return errors.New("remote node is incompatible version")
|
||||
}
|
||||
// Check if the remote side matches the keys we expected. This is a bit of a weak
|
||||
// check - in future versions we really should check a signature or something like that.
|
||||
if pinned := intf.options.pinnedEd25519Keys; pinned != nil {
|
||||
if pinned := intf.options.pinnedEd25519Keys; len(pinned) > 0 {
|
||||
var key keyArray
|
||||
copy(key[:], meta.key)
|
||||
if _, allowed := pinned[key]; !allowed {
|
||||
intf.links.core.log.Errorf("Failed to connect to node: %q sent ed25519 key that does not match pinned keys", intf.name())
|
||||
return nil, fmt.Errorf("failed to connect: host sent ed25519 key that does not match pinned keys")
|
||||
return fmt.Errorf("failed to connect: host sent ed25519 key that does not match pinned keys")
|
||||
}
|
||||
}
|
||||
// Check if we're authorized to connect to this key / IP
|
||||
intf.links.core.config.RLock()
|
||||
allowed := intf.links.core.config.AllowedPublicKeys
|
||||
intf.links.core.config.RUnlock()
|
||||
allowed := intf.links.core.config._allowedPublicKeys
|
||||
isallowed := len(allowed) == 0
|
||||
for _, k := range allowed {
|
||||
if k == hex.EncodeToString(meta.key) { // TODO: this is yuck
|
||||
for k := range allowed {
|
||||
if bytes.Equal(k[:], meta.key) {
|
||||
isallowed = true
|
||||
break
|
||||
}
|
||||
|
@ -228,56 +288,51 @@ func (intf *link) handler() (chan struct{}, error) {
|
|||
if intf.incoming && !intf.force && !isallowed {
|
||||
intf.links.core.log.Warnf("%s connection from %s forbidden: AllowedEncryptionPublicKeys does not contain key %s",
|
||||
strings.ToUpper(intf.info.linkType), intf.info.remote, hex.EncodeToString(meta.key))
|
||||
intf.close()
|
||||
return nil, nil
|
||||
_ = intf.close()
|
||||
return fmt.Errorf("forbidden connection")
|
||||
}
|
||||
// Check if we already have a link to this node
|
||||
copy(intf.info.key[:], meta.key)
|
||||
intf.links.mutex.Lock()
|
||||
if oldIntf, isIn := intf.links.links[intf.info]; isIn {
|
||||
intf.links.mutex.Unlock()
|
||||
// FIXME we should really return an error and let the caller block instead
|
||||
// That lets them do things like close connections on its own, avoid printing a connection message in the first place, etc.
|
||||
intf.links.core.log.Debugln("DEBUG: found existing interface for", intf.name())
|
||||
return oldIntf.closed, nil
|
||||
} else {
|
||||
intf.closed = make(chan struct{})
|
||||
intf.links.links[intf.info] = intf
|
||||
defer func() {
|
||||
intf.links.mutex.Lock()
|
||||
delete(intf.links.links, intf.info)
|
||||
intf.links.mutex.Unlock()
|
||||
close(intf.closed)
|
||||
}()
|
||||
intf.links.core.log.Debugln("DEBUG: registered interface for", intf.name())
|
||||
}
|
||||
intf.links.mutex.Unlock()
|
||||
themAddr := address.AddrForKey(ed25519.PublicKey(intf.info.key[:]))
|
||||
themAddrString := net.IP(themAddr[:]).String()
|
||||
themString := fmt.Sprintf("%s@%s", themAddrString, intf.info.remote)
|
||||
|
||||
phony.Block(intf.links, func() {
|
||||
intf.links._links[intf.info] = intf
|
||||
})
|
||||
|
||||
remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String()
|
||||
remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote)
|
||||
localStr := intf.conn.LocalAddr()
|
||||
intf.links.core.log.Infof("Connected %s: %s, source %s",
|
||||
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
||||
// Run the handler
|
||||
err = intf.links.core.HandleConn(ed25519.PublicKey(intf.info.key[:]), intf.conn)
|
||||
strings.ToUpper(intf.info.linkType), remoteStr, localStr)
|
||||
|
||||
// TODO don't report an error if it's just a 'use of closed network connection'
|
||||
if err != nil {
|
||||
if err = intf.links.core.HandleConn(meta.key, intf.conn); err != nil && err != io.EOF {
|
||||
intf.links.core.log.Infof("Disconnected %s: %s, source %s; error: %s",
|
||||
strings.ToUpper(intf.info.linkType), themString, intf.info.local, err)
|
||||
strings.ToUpper(intf.info.linkType), remoteStr, localStr, err)
|
||||
} else {
|
||||
intf.links.core.log.Infof("Disconnected %s: %s, source %s",
|
||||
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
||||
strings.ToUpper(intf.info.linkType), remoteStr, localStr)
|
||||
}
|
||||
return nil, err
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (intf *link) close() {
|
||||
intf.conn.Close()
|
||||
func (intf *link) close() error {
|
||||
return intf.conn.Close()
|
||||
}
|
||||
|
||||
func (intf *link) name() string {
|
||||
return intf.lname
|
||||
}
|
||||
|
||||
func linkInfoFor(linkType, sintf, remote string) linkInfo {
|
||||
if h, _, err := net.SplitHostPort(remote); err == nil {
|
||||
remote = h
|
||||
}
|
||||
return linkInfo{
|
||||
linkType: linkType,
|
||||
local: sintf,
|
||||
remote: remote,
|
||||
}
|
||||
}
|
||||
|
||||
type linkConn struct {
|
||||
// tx and rx are at the beginning of the struct to ensure 64-bit alignment
|
||||
// on 32-bit platforms, see https://pkg.go.dev/sync/atomic#pkg-note-BUG
|
||||
|
|
52
src/core/link_socks.go
Normal file
52
src/core/link_socks.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
type linkSOCKS struct {
|
||||
*links
|
||||
}
|
||||
|
||||
func (l *links) newLinkSOCKS() *linkSOCKS {
|
||||
lt := &linkSOCKS{
|
||||
links: l,
|
||||
}
|
||||
return lt
|
||||
}
|
||||
|
||||
func (l *linkSOCKS) dial(url *url.URL, options linkOptions) error {
|
||||
info := linkInfoFor("socks", "", url.Path)
|
||||
if l.links.isConnectedTo(info) {
|
||||
return fmt.Errorf("duplicate connection attempt")
|
||||
}
|
||||
proxyAuth := &proxy.Auth{}
|
||||
proxyAuth.User = url.User.Username()
|
||||
proxyAuth.Password, _ = url.User.Password()
|
||||
dialer, err := proxy.SOCKS5("tcp", url.Host, proxyAuth, proxy.Direct)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to configure proxy")
|
||||
}
|
||||
pathtokens := strings.Split(strings.Trim(url.Path, "/"), "/")
|
||||
conn, err := dialer.Dial("tcp", pathtokens[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.handler(url.String(), info, conn, options, false)
|
||||
}
|
||||
|
||||
func (l *linkSOCKS) handler(name string, info linkInfo, conn net.Conn, options linkOptions, incoming bool) error {
|
||||
return l.links.create(
|
||||
conn, // connection
|
||||
name, // connection name
|
||||
info, // connection info
|
||||
incoming, // not incoming
|
||||
false, // not forced
|
||||
options, // connection options
|
||||
)
|
||||
}
|
183
src/core/link_tcp.go
Normal file
183
src/core/link_tcp.go
Normal file
|
@ -0,0 +1,183 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
)
|
||||
|
||||
type linkTCP struct {
|
||||
phony.Inbox
|
||||
*links
|
||||
listener *net.ListenConfig
|
||||
_listeners map[*Listener]context.CancelFunc
|
||||
}
|
||||
|
||||
func (l *links) newLinkTCP() *linkTCP {
|
||||
lt := &linkTCP{
|
||||
links: l,
|
||||
listener: &net.ListenConfig{
|
||||
KeepAlive: -1,
|
||||
},
|
||||
_listeners: map[*Listener]context.CancelFunc{},
|
||||
}
|
||||
lt.listener.Control = lt.tcpContext
|
||||
return lt
|
||||
}
|
||||
|
||||
func (l *linkTCP) dial(url *url.URL, options linkOptions, sintf string) error {
|
||||
info := linkInfoFor("tcp", sintf, strings.SplitN(url.Host, "%", 2)[0])
|
||||
if l.links.isConnectedTo(info) {
|
||||
return fmt.Errorf("duplicate connection attempt")
|
||||
}
|
||||
addr, err := net.ResolveTCPAddr("tcp", url.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addr.Zone = sintf
|
||||
dialer, err := l.dialerFor(addr.String(), sintf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn, err := dialer.DialContext(l.core.ctx, "tcp", addr.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.handler(url.String(), info, conn, options, false)
|
||||
}
|
||||
|
||||
func (l *linkTCP) listen(url *url.URL, sintf string) (*Listener, error) {
|
||||
ctx, cancel := context.WithCancel(l.core.ctx)
|
||||
hostport := url.Host
|
||||
if sintf != "" {
|
||||
if host, port, err := net.SplitHostPort(hostport); err == nil {
|
||||
hostport = fmt.Sprintf("[%s%%%s]:%s", host, sintf, port)
|
||||
}
|
||||
}
|
||||
listener, err := l.listener.Listen(ctx, "tcp", hostport)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
entry := &Listener{
|
||||
Listener: listener,
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
phony.Block(l, func() {
|
||||
l._listeners[entry] = cancel
|
||||
})
|
||||
l.core.log.Printf("TCP listener started on %s", listener.Addr())
|
||||
go func() {
|
||||
defer phony.Block(l, func() {
|
||||
delete(l._listeners, entry)
|
||||
})
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
cancel()
|
||||
break
|
||||
}
|
||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
||||
name := fmt.Sprintf("tls://%s", addr)
|
||||
info := linkInfoFor("tcp", sintf, strings.SplitN(addr.IP.String(), "%", 2)[0])
|
||||
if err = l.handler(name, info, conn, linkOptions{}, true); err != nil {
|
||||
l.core.log.Errorln("Failed to create inbound link:", err)
|
||||
}
|
||||
}
|
||||
_ = listener.Close()
|
||||
close(entry.closed)
|
||||
l.core.log.Printf("TCP listener stopped on %s", listener.Addr())
|
||||
}()
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (l *linkTCP) handler(name string, info linkInfo, conn net.Conn, options linkOptions, incoming bool) error {
|
||||
return l.links.create(
|
||||
conn, // connection
|
||||
name, // connection name
|
||||
info, // connection info
|
||||
incoming, // not incoming
|
||||
false, // not forced
|
||||
options, // connection options
|
||||
)
|
||||
}
|
||||
|
||||
// Returns the address of the listener.
|
||||
func (l *linkTCP) getAddr() *net.TCPAddr {
|
||||
// TODO: Fix this, because this will currently only give a single address
|
||||
// to multicast.go, which obviously is not great, but right now multicast.go
|
||||
// doesn't have the ability to send more than one address in a packet either
|
||||
var addr *net.TCPAddr
|
||||
phony.Block(l, func() {
|
||||
for listener := range l._listeners {
|
||||
addr = listener.Addr().(*net.TCPAddr)
|
||||
}
|
||||
})
|
||||
return addr
|
||||
}
|
||||
|
||||
func (l *linkTCP) dialerFor(saddr, sintf string) (*net.Dialer, error) {
|
||||
dst, err := net.ResolveTCPAddr("tcp", saddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dst.IP.IsLinkLocalUnicast() {
|
||||
dst.Zone = sintf
|
||||
if dst.Zone == "" {
|
||||
return nil, fmt.Errorf("link-local address requires a zone")
|
||||
}
|
||||
}
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Second * 5,
|
||||
KeepAlive: -1,
|
||||
Control: l.tcpContext,
|
||||
}
|
||||
if sintf != "" {
|
||||
dialer.Control = l.getControl(sintf)
|
||||
ief, err := net.InterfaceByName(sintf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("interface %q not found", sintf)
|
||||
}
|
||||
if ief.Flags&net.FlagUp == 0 {
|
||||
return nil, fmt.Errorf("interface %q is not up", sintf)
|
||||
}
|
||||
addrs, err := ief.Addrs()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("interface %q addresses not available: %w", sintf, err)
|
||||
}
|
||||
for addrindex, addr := range addrs {
|
||||
src, _, err := net.ParseCIDR(addr.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if !src.IsGlobalUnicast() && !src.IsLinkLocalUnicast() {
|
||||
continue
|
||||
}
|
||||
bothglobal := src.IsGlobalUnicast() == dst.IP.IsGlobalUnicast()
|
||||
bothlinklocal := src.IsLinkLocalUnicast() == dst.IP.IsLinkLocalUnicast()
|
||||
if !bothglobal && !bothlinklocal {
|
||||
continue
|
||||
}
|
||||
if (src.To4() != nil) != (dst.IP.To4() != nil) {
|
||||
continue
|
||||
}
|
||||
if bothglobal || bothlinklocal || addrindex == len(addrs)-1 {
|
||||
dialer.LocalAddr = &net.TCPAddr{
|
||||
IP: src,
|
||||
Port: 0,
|
||||
Zone: sintf,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if dialer.LocalAddr == nil {
|
||||
return nil, fmt.Errorf("no suitable source address found on interface %q", sintf)
|
||||
}
|
||||
}
|
||||
return dialer, nil
|
||||
}
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
// WARNING: This context is used both by net.Dialer and net.Listen in tcp.go
|
||||
|
||||
func (t *tcp) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
func (t *linkTCP) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
var control error
|
||||
var recvanyif error
|
||||
|
||||
|
@ -28,6 +28,6 @@ func (t *tcp) tcpContext(network, address string, c syscall.RawConn) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *tcp) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
func (t *linkTCP) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
return t.tcpContext
|
||||
}
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
// WARNING: This context is used both by net.Dialer and net.Listen in tcp.go
|
||||
|
||||
func (t *tcp) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
func (t *linkTCP) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
var control error
|
||||
var bbr error
|
||||
|
||||
|
@ -31,7 +31,7 @@ func (t *tcp) tcpContext(network, address string, c syscall.RawConn) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (t *tcp) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
func (t *linkTCP) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
return func(network, address string, c syscall.RawConn) error {
|
||||
var err error
|
||||
btd := func(fd uintptr) {
|
|
@ -9,10 +9,10 @@ import (
|
|||
|
||||
// WARNING: This context is used both by net.Dialer and net.Listen in tcp.go
|
||||
|
||||
func (t *tcp) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
func (t *linkTCP) tcpContext(network, address string, c syscall.RawConn) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *tcp) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
func (t *linkTCP) getControl(sintf string) func(string, string, syscall.RawConn) error {
|
||||
return t.tcpContext
|
||||
}
|
171
src/core/link_tls.go
Normal file
171
src/core/link_tls.go
Normal file
|
@ -0,0 +1,171 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
)
|
||||
|
||||
type linkTLS struct {
|
||||
phony.Inbox
|
||||
*links
|
||||
tcp *linkTCP
|
||||
listener *net.ListenConfig
|
||||
config *tls.Config
|
||||
_listeners map[*Listener]context.CancelFunc
|
||||
}
|
||||
|
||||
func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS {
|
||||
lt := &linkTLS{
|
||||
links: l,
|
||||
tcp: tcp,
|
||||
listener: &net.ListenConfig{
|
||||
Control: tcp.tcpContext,
|
||||
KeepAlive: -1,
|
||||
},
|
||||
_listeners: map[*Listener]context.CancelFunc{},
|
||||
}
|
||||
var err error
|
||||
lt.config, err = lt.generateConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return lt
|
||||
}
|
||||
|
||||
func (l *linkTLS) dial(url *url.URL, options linkOptions, sintf, sni string) error {
|
||||
info := linkInfoFor("tls", sintf, strings.SplitN(url.Host, "%", 2)[0])
|
||||
if l.links.isConnectedTo(info) {
|
||||
return fmt.Errorf("duplicate connection attempt")
|
||||
}
|
||||
addr, err := net.ResolveTCPAddr("tcp", url.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addr.Zone = sintf
|
||||
dialer, err := l.tcp.dialerFor(addr.String(), sintf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tlsconfig := l.config.Clone()
|
||||
tlsconfig.ServerName = sni
|
||||
tlsdialer := &tls.Dialer{
|
||||
NetDialer: dialer,
|
||||
Config: tlsconfig,
|
||||
}
|
||||
conn, err := tlsdialer.DialContext(l.core.ctx, "tcp", addr.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.handler(url.String(), info, conn, options, false)
|
||||
}
|
||||
|
||||
func (l *linkTLS) listen(url *url.URL, sintf string) (*Listener, error) {
|
||||
ctx, cancel := context.WithCancel(l.core.ctx)
|
||||
hostport := url.Host
|
||||
if sintf != "" {
|
||||
if host, port, err := net.SplitHostPort(hostport); err == nil {
|
||||
hostport = fmt.Sprintf("[%s%%%s]:%s", host, sintf, port)
|
||||
}
|
||||
}
|
||||
listener, err := l.listener.Listen(ctx, "tcp", hostport)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
tlslistener := tls.NewListener(listener, l.config)
|
||||
entry := &Listener{
|
||||
Listener: tlslistener,
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
phony.Block(l, func() {
|
||||
l._listeners[entry] = cancel
|
||||
})
|
||||
l.core.log.Printf("TLS listener started on %s", listener.Addr())
|
||||
go func() {
|
||||
defer phony.Block(l, func() {
|
||||
delete(l._listeners, entry)
|
||||
})
|
||||
for {
|
||||
conn, err := tlslistener.Accept()
|
||||
if err != nil {
|
||||
cancel()
|
||||
break
|
||||
}
|
||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
||||
name := fmt.Sprintf("tls://%s", addr)
|
||||
info := linkInfoFor("tls", sintf, strings.SplitN(addr.IP.String(), "%", 2)[0])
|
||||
if err = l.handler(name, info, conn, linkOptions{}, true); err != nil {
|
||||
l.core.log.Errorln("Failed to create inbound link:", err)
|
||||
}
|
||||
}
|
||||
_ = tlslistener.Close()
|
||||
close(entry.closed)
|
||||
l.core.log.Printf("TLS listener stopped on %s", listener.Addr())
|
||||
}()
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (l *linkTLS) generateConfig() (*tls.Config, error) {
|
||||
certBuf := &bytes.Buffer{}
|
||||
|
||||
// TODO: because NotAfter is finite, we should add some mechanism to
|
||||
// regenerate the certificate and restart the listeners periodically
|
||||
// for nodes with very high uptimes. Perhaps regenerate certs and restart
|
||||
// listeners every few months or so.
|
||||
cert := x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
Subject: pkix.Name{
|
||||
CommonName: hex.EncodeToString(l.links.core.public[:]),
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().Add(time.Hour * 24 * 365),
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
|
||||
certbytes, err := x509.CreateCertificate(rand.Reader, &cert, &cert, l.links.core.public, l.links.core.secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := pem.Encode(certBuf, &pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: certbytes,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rootCAs := x509.NewCertPool()
|
||||
rootCAs.AppendCertsFromPEM(certbytes)
|
||||
|
||||
return &tls.Config{
|
||||
RootCAs: rootCAs,
|
||||
Certificates: []tls.Certificate{
|
||||
{
|
||||
Certificate: [][]byte{certbytes},
|
||||
PrivateKey: l.links.core.secret,
|
||||
},
|
||||
},
|
||||
InsecureSkipVerify: true,
|
||||
MinVersion: tls.VersionTLS13,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *linkTLS) handler(name string, info linkInfo, conn net.Conn, options linkOptions, incoming bool) error {
|
||||
return l.tcp.handler(name, info, conn, options, incoming)
|
||||
}
|
98
src/core/link_unix.go
Normal file
98
src/core/link_unix.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
)
|
||||
|
||||
type linkUNIX struct {
|
||||
phony.Inbox
|
||||
*links
|
||||
dialer *net.Dialer
|
||||
listener *net.ListenConfig
|
||||
_listeners map[*Listener]context.CancelFunc
|
||||
}
|
||||
|
||||
func (l *links) newLinkUNIX() *linkUNIX {
|
||||
lt := &linkUNIX{
|
||||
links: l,
|
||||
dialer: &net.Dialer{
|
||||
Timeout: time.Second * 5,
|
||||
KeepAlive: -1,
|
||||
},
|
||||
listener: &net.ListenConfig{
|
||||
KeepAlive: -1,
|
||||
},
|
||||
_listeners: map[*Listener]context.CancelFunc{},
|
||||
}
|
||||
return lt
|
||||
}
|
||||
|
||||
func (l *linkUNIX) dial(url *url.URL, options linkOptions, _ string) error {
|
||||
info := linkInfoFor("unix", "", url.Path)
|
||||
if l.links.isConnectedTo(info) {
|
||||
return fmt.Errorf("duplicate connection attempt")
|
||||
}
|
||||
addr, err := net.ResolveUnixAddr("unix", url.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn, err := l.dialer.DialContext(l.core.ctx, "unix", addr.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.handler(url.String(), info, conn, options, false)
|
||||
}
|
||||
|
||||
func (l *linkUNIX) listen(url *url.URL, _ string) (*Listener, error) {
|
||||
ctx, cancel := context.WithCancel(l.core.ctx)
|
||||
listener, err := l.listener.Listen(ctx, "unix", url.Path)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
entry := &Listener{
|
||||
Listener: listener,
|
||||
closed: make(chan struct{}),
|
||||
}
|
||||
phony.Block(l, func() {
|
||||
l._listeners[entry] = cancel
|
||||
})
|
||||
l.core.log.Printf("UNIX listener started on %s", listener.Addr())
|
||||
go func() {
|
||||
defer phony.Block(l, func() {
|
||||
delete(l._listeners, entry)
|
||||
})
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
cancel()
|
||||
break
|
||||
}
|
||||
info := linkInfoFor("unix", "", url.String())
|
||||
if err = l.handler(url.String(), info, conn, linkOptions{}, true); err != nil {
|
||||
l.core.log.Errorln("Failed to create inbound link:", err)
|
||||
}
|
||||
}
|
||||
_ = listener.Close()
|
||||
close(entry.closed)
|
||||
l.core.log.Printf("UNIX listener stopped on %s", listener.Addr())
|
||||
}()
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (l *linkUNIX) handler(name string, info linkInfo, conn net.Conn, options linkOptions, incoming bool) error {
|
||||
return l.links.create(
|
||||
conn, // connection
|
||||
name, // connection name
|
||||
info, // connection info
|
||||
incoming, // not incoming
|
||||
false, // not forced
|
||||
options, // connection options
|
||||
)
|
||||
}
|
|
@ -4,16 +4,13 @@ import (
|
|||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
iwt "github.com/Arceliar/ironwood/types"
|
||||
"github.com/Arceliar/phony"
|
||||
|
||||
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||
)
|
||||
|
||||
|
@ -154,18 +151,21 @@ func (m *nodeinfo) _sendRes(key keyArray) {
|
|||
type GetNodeInfoRequest struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
type GetNodeInfoResponse map[string]interface{}
|
||||
type GetNodeInfoResponse map[string]json.RawMessage
|
||||
|
||||
func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error) {
|
||||
var req GetNodeInfoRequest
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Key == "" {
|
||||
return nil, fmt.Errorf("No remote public key supplied")
|
||||
}
|
||||
var key keyArray
|
||||
var kbs []byte
|
||||
var err error
|
||||
if kbs, err = hex.DecodeString(req.Key); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Failed to decode public key: %w", err)
|
||||
}
|
||||
copy(key[:], kbs)
|
||||
ch := make(chan []byte, 1)
|
||||
|
@ -176,14 +176,14 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
|
|||
defer timer.Stop()
|
||||
select {
|
||||
case <-timer.C:
|
||||
return nil, errors.New("timeout")
|
||||
return nil, errors.New("Timed out waiting for response")
|
||||
case info := <-ch:
|
||||
var msg json.RawMessage
|
||||
if err := msg.UnmarshalJSON(info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ip := net.IP(address.AddrForKey(kbs)[:])
|
||||
res := GetNodeInfoResponse{ip.String(): msg}
|
||||
key := hex.EncodeToString(kbs[:])
|
||||
res := GetNodeInfoResponse{key: msg}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
|
41
src/core/options.go
Normal file
41
src/core/options.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
)
|
||||
|
||||
func (c *Core) _applyOption(opt SetupOption) {
|
||||
switch v := opt.(type) {
|
||||
case Peer:
|
||||
c.config._peers[v] = nil
|
||||
case ListenAddress:
|
||||
c.config._listeners[v] = struct{}{}
|
||||
case NodeInfo:
|
||||
c.config.nodeinfo = v
|
||||
case NodeInfoPrivacy:
|
||||
c.config.nodeinfoPrivacy = v
|
||||
case AllowedPublicKey:
|
||||
pk := [32]byte{}
|
||||
copy(pk[:], v)
|
||||
c.config._allowedPublicKeys[pk] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
type SetupOption interface {
|
||||
isSetupOption()
|
||||
}
|
||||
|
||||
type ListenAddress string
|
||||
type Peer struct {
|
||||
URI string
|
||||
SourceInterface string
|
||||
}
|
||||
type NodeInfo map[string]interface{}
|
||||
type NodeInfoPrivacy bool
|
||||
type AllowedPublicKey ed25519.PublicKey
|
||||
|
||||
func (a ListenAddress) isSetupOption() {}
|
||||
func (a Peer) isSetupOption() {}
|
||||
func (a NodeInfo) isSetupOption() {}
|
||||
func (a NodeInfoPrivacy) isSetupOption() {}
|
||||
func (a AllowedPublicKey) isSetupOption() {}
|
|
@ -65,10 +65,16 @@ func (p *protoHandler) handleProto(from phony.Actor, key keyArray, bs []byte) {
|
|||
case typeProtoNodeInfoResponse:
|
||||
p.nodeinfo.handleRes(p, key, bs[1:])
|
||||
case typeProtoDebug:
|
||||
p._handleDebug(key, bs[1:])
|
||||
p.handleDebug(from, key, bs[1:])
|
||||
}
|
||||
}
|
||||
|
||||
func (p *protoHandler) handleDebug(from phony.Actor, key keyArray, bs []byte) {
|
||||
p.Act(from, func() {
|
||||
p._handleDebug(key, bs)
|
||||
})
|
||||
}
|
||||
|
||||
func (p *protoHandler) _handleDebug(key keyArray, bs []byte) {
|
||||
if len(bs) == 0 {
|
||||
return
|
||||
|
|
418
src/core/tcp.go
418
src/core/tcp.go
|
@ -1,418 +0,0 @@
|
|||
package core
|
||||
|
||||
// This sends packets to peers using TCP as a transport
|
||||
// It's generally better tested than the UDP implementation
|
||||
// Using it regularly is insane, but I find TCP easier to test/debug with it
|
||||
// Updating and optimizing the UDP version is a higher priority
|
||||
|
||||
// TODO:
|
||||
// Something needs to make sure we're getting *valid* packets
|
||||
// Could be used to DoS (connect, give someone else's keys, spew garbage)
|
||||
// I guess the "peer" part should watch for link packets, disconnect?
|
||||
|
||||
// TCP connections start with a metadata exchange.
|
||||
// It involves exchanging version numbers and crypto keys
|
||||
// See version.go for version metadata format
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
//"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
)
|
||||
|
||||
const default_timeout = 6 * time.Second
|
||||
|
||||
// The TCP listener and information about active TCP connections, to avoid duplication.
|
||||
type tcp struct {
|
||||
links *links
|
||||
waitgroup sync.WaitGroup
|
||||
mutex sync.Mutex // Protecting the below
|
||||
listeners map[string]*TcpListener
|
||||
calls map[string]struct{}
|
||||
conns map[linkInfo](chan struct{})
|
||||
tls tcptls
|
||||
}
|
||||
|
||||
// TcpListener is a stoppable TCP listener interface. These are typically
|
||||
// returned from calls to the ListenTCP() function and are also used internally
|
||||
// to represent listeners created by the "Listen" configuration option and for
|
||||
// multicast interfaces.
|
||||
type TcpListener struct {
|
||||
Listener net.Listener
|
||||
opts tcpOptions
|
||||
stop chan struct{}
|
||||
}
|
||||
|
||||
type TcpUpgrade struct {
|
||||
upgrade func(c net.Conn, o *tcpOptions) (net.Conn, error)
|
||||
name string
|
||||
}
|
||||
|
||||
type tcpOptions struct {
|
||||
linkOptions
|
||||
upgrade *TcpUpgrade
|
||||
socksProxyAddr string
|
||||
socksProxyAuth *proxy.Auth
|
||||
socksPeerAddr string
|
||||
tlsSNI string
|
||||
}
|
||||
|
||||
func (l *TcpListener) Stop() {
|
||||
defer func() { _ = recover() }()
|
||||
close(l.stop)
|
||||
}
|
||||
|
||||
// Wrapper function to set additional options for specific connection types.
|
||||
func (t *tcp) setExtraOptions(c net.Conn) {
|
||||
switch sock := c.(type) {
|
||||
case *net.TCPConn:
|
||||
_ = sock.SetNoDelay(true)
|
||||
// TODO something for socks5
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the address of the listener.
|
||||
func (t *tcp) getAddr() *net.TCPAddr {
|
||||
// TODO: Fix this, because this will currently only give a single address
|
||||
// to multicast.go, which obviously is not great, but right now multicast.go
|
||||
// doesn't have the ability to send more than one address in a packet either
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
for _, l := range t.listeners {
|
||||
return l.Listener.Addr().(*net.TCPAddr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initializes the struct.
|
||||
func (t *tcp) init(l *links) error {
|
||||
t.links = l
|
||||
t.tls.init(t)
|
||||
t.mutex.Lock()
|
||||
t.calls = make(map[string]struct{})
|
||||
t.conns = make(map[linkInfo](chan struct{}))
|
||||
t.listeners = make(map[string]*TcpListener)
|
||||
t.mutex.Unlock()
|
||||
|
||||
t.links.core.config.RLock()
|
||||
defer t.links.core.config.RUnlock()
|
||||
for _, listenaddr := range t.links.core.config.Listen {
|
||||
u, err := url.Parse(listenaddr)
|
||||
if err != nil {
|
||||
t.links.core.log.Errorln("Failed to parse listener: listener", listenaddr, "is not correctly formatted, ignoring")
|
||||
}
|
||||
if _, err := t.listenURL(u, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *tcp) stop() error {
|
||||
t.mutex.Lock()
|
||||
for _, listener := range t.listeners {
|
||||
listener.Stop()
|
||||
}
|
||||
t.mutex.Unlock()
|
||||
t.waitgroup.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *tcp) listenURL(u *url.URL, sintf string) (*TcpListener, error) {
|
||||
var listener *TcpListener
|
||||
var err error
|
||||
hostport := u.Host // Used for tcp and tls
|
||||
if len(sintf) != 0 {
|
||||
host, port, err := net.SplitHostPort(hostport)
|
||||
if err == nil {
|
||||
hostport = fmt.Sprintf("[%s%%%s]:%s", host, sintf, port)
|
||||
}
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "tcp":
|
||||
listener, err = t.listen(hostport, nil)
|
||||
case "tls":
|
||||
listener, err = t.listen(hostport, t.tls.forListener)
|
||||
default:
|
||||
t.links.core.log.Errorln("Failed to add listener: listener", u.String(), "is not correctly formatted, ignoring")
|
||||
}
|
||||
return listener, err
|
||||
}
|
||||
|
||||
func (t *tcp) listen(listenaddr string, upgrade *TcpUpgrade) (*TcpListener, error) {
|
||||
var err error
|
||||
|
||||
ctx := t.links.core.ctx
|
||||
lc := net.ListenConfig{
|
||||
Control: t.tcpContext,
|
||||
}
|
||||
listener, err := lc.Listen(ctx, "tcp", listenaddr)
|
||||
if err == nil {
|
||||
l := TcpListener{
|
||||
Listener: listener,
|
||||
opts: tcpOptions{upgrade: upgrade},
|
||||
stop: make(chan struct{}),
|
||||
}
|
||||
t.waitgroup.Add(1)
|
||||
go t.listener(&l, listenaddr)
|
||||
return &l, nil
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Runs the listener, which spawns off goroutines for incoming connections.
|
||||
func (t *tcp) listener(l *TcpListener, listenaddr string) {
|
||||
defer t.waitgroup.Done()
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
// Track the listener so that we can find it again in future
|
||||
t.mutex.Lock()
|
||||
if _, isIn := t.listeners[listenaddr]; isIn {
|
||||
t.mutex.Unlock()
|
||||
l.Listener.Close()
|
||||
return
|
||||
}
|
||||
callproto := "TCP"
|
||||
if l.opts.upgrade != nil {
|
||||
callproto = strings.ToUpper(l.opts.upgrade.name)
|
||||
}
|
||||
t.listeners[listenaddr] = l
|
||||
t.mutex.Unlock()
|
||||
// And here we go!
|
||||
defer func() {
|
||||
t.links.core.log.Infoln("Stopping", callproto, "listener on:", l.Listener.Addr().String())
|
||||
l.Listener.Close()
|
||||
t.mutex.Lock()
|
||||
delete(t.listeners, listenaddr)
|
||||
t.mutex.Unlock()
|
||||
}()
|
||||
t.links.core.log.Infoln("Listening for", callproto, "on:", l.Listener.Addr().String())
|
||||
go func() {
|
||||
<-l.stop
|
||||
l.Listener.Close()
|
||||
}()
|
||||
defer l.Stop()
|
||||
for {
|
||||
sock, err := l.Listener.Accept()
|
||||
if err != nil {
|
||||
t.links.core.log.Errorln("Failed to accept connection:", err)
|
||||
select {
|
||||
case <-l.stop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
time.Sleep(time.Second) // So we don't busy loop
|
||||
continue
|
||||
}
|
||||
t.waitgroup.Add(1)
|
||||
options := l.opts
|
||||
go t.handler(sock, true, options)
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if we already are calling this address
|
||||
func (t *tcp) startCalling(saddr string) bool {
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
_, isIn := t.calls[saddr]
|
||||
t.calls[saddr] = struct{}{}
|
||||
return !isIn
|
||||
}
|
||||
|
||||
// Checks if a connection already exists.
|
||||
// If not, it adds it to the list of active outgoing calls (to block future attempts) and dials the address.
|
||||
// If the dial is successful, it launches the handler.
|
||||
// When finished, it removes the outgoing call, so reconnection attempts can be made later.
|
||||
// This all happens in a separate goroutine that it spawns.
|
||||
func (t *tcp) call(saddr string, options tcpOptions, sintf string) {
|
||||
go func() {
|
||||
callname := saddr
|
||||
callproto := "TCP"
|
||||
if options.upgrade != nil {
|
||||
callproto = strings.ToUpper(options.upgrade.name)
|
||||
}
|
||||
if sintf != "" {
|
||||
callname = fmt.Sprintf("%s/%s/%s", callproto, saddr, sintf)
|
||||
}
|
||||
if !t.startCalling(callname) {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
// Block new calls for a little while, to mitigate livelock scenarios
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
delay := default_timeout + time.Duration(rand.Intn(10000))*time.Millisecond
|
||||
time.Sleep(delay)
|
||||
t.mutex.Lock()
|
||||
delete(t.calls, callname)
|
||||
t.mutex.Unlock()
|
||||
}()
|
||||
var conn net.Conn
|
||||
var err error
|
||||
if options.socksProxyAddr != "" {
|
||||
if sintf != "" {
|
||||
return
|
||||
}
|
||||
dialerdst, er := net.ResolveTCPAddr("tcp", options.socksProxyAddr)
|
||||
if er != nil {
|
||||
return
|
||||
}
|
||||
var dialer proxy.Dialer
|
||||
dialer, err = proxy.SOCKS5("tcp", dialerdst.String(), options.socksProxyAuth, proxy.Direct)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx, done := context.WithTimeout(t.links.core.ctx, default_timeout)
|
||||
conn, err = dialer.(proxy.ContextDialer).DialContext(ctx, "tcp", saddr)
|
||||
done()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t.waitgroup.Add(1)
|
||||
options.socksPeerAddr = saddr
|
||||
if ch := t.handler(conn, false, options); ch != nil {
|
||||
<-ch
|
||||
}
|
||||
} else {
|
||||
dst, err := net.ResolveTCPAddr("tcp", saddr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if dst.IP.IsLinkLocalUnicast() {
|
||||
dst.Zone = sintf
|
||||
if dst.Zone == "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
dialer := net.Dialer{
|
||||
Control: t.tcpContext,
|
||||
}
|
||||
if sintf != "" {
|
||||
dialer.Control = t.getControl(sintf)
|
||||
ief, err := net.InterfaceByName(sintf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if ief.Flags&net.FlagUp == 0 {
|
||||
return
|
||||
}
|
||||
addrs, err := ief.Addrs()
|
||||
if err == nil {
|
||||
for addrindex, addr := range addrs {
|
||||
src, _, err := net.ParseCIDR(addr.String())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if src.Equal(dst.IP) {
|
||||
continue
|
||||
}
|
||||
if !src.IsGlobalUnicast() && !src.IsLinkLocalUnicast() {
|
||||
continue
|
||||
}
|
||||
bothglobal := src.IsGlobalUnicast() == dst.IP.IsGlobalUnicast()
|
||||
bothlinklocal := src.IsLinkLocalUnicast() == dst.IP.IsLinkLocalUnicast()
|
||||
if !bothglobal && !bothlinklocal {
|
||||
continue
|
||||
}
|
||||
if (src.To4() != nil) != (dst.IP.To4() != nil) {
|
||||
continue
|
||||
}
|
||||
if bothglobal || bothlinklocal || addrindex == len(addrs)-1 {
|
||||
dialer.LocalAddr = &net.TCPAddr{
|
||||
IP: src,
|
||||
Port: 0,
|
||||
Zone: sintf,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if dialer.LocalAddr == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx, done := context.WithTimeout(t.links.core.ctx, default_timeout)
|
||||
conn, err = dialer.DialContext(ctx, "tcp", dst.String())
|
||||
done()
|
||||
if err != nil {
|
||||
t.links.core.log.Debugf("Failed to dial %s: %s", callproto, err)
|
||||
return
|
||||
}
|
||||
t.waitgroup.Add(1)
|
||||
if ch := t.handler(conn, false, options); ch != nil {
|
||||
<-ch
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (t *tcp) handler(sock net.Conn, incoming bool, options tcpOptions) chan struct{} {
|
||||
defer t.waitgroup.Done() // Happens after sock.close
|
||||
defer sock.Close()
|
||||
t.setExtraOptions(sock)
|
||||
var upgraded bool
|
||||
if options.upgrade != nil {
|
||||
var err error
|
||||
if sock, err = options.upgrade.upgrade(sock, &options); err != nil {
|
||||
t.links.core.log.Errorln("TCP handler upgrade failed:", err)
|
||||
return nil
|
||||
}
|
||||
upgraded = true
|
||||
}
|
||||
var name, proto, local, remote string
|
||||
if options.socksProxyAddr != "" {
|
||||
name = "socks://" + sock.RemoteAddr().String() + "/" + options.socksPeerAddr
|
||||
proto = "socks"
|
||||
local, _, _ = net.SplitHostPort(sock.LocalAddr().String())
|
||||
remote, _, _ = net.SplitHostPort(options.socksPeerAddr)
|
||||
} else {
|
||||
if upgraded {
|
||||
proto = options.upgrade.name
|
||||
name = proto + "://" + sock.RemoteAddr().String()
|
||||
} else {
|
||||
proto = "tcp"
|
||||
name = proto + "://" + sock.RemoteAddr().String()
|
||||
}
|
||||
local, _, _ = net.SplitHostPort(sock.LocalAddr().String())
|
||||
remote, _, _ = net.SplitHostPort(sock.RemoteAddr().String())
|
||||
}
|
||||
localIP := net.ParseIP(local)
|
||||
if localIP = localIP.To16(); localIP != nil {
|
||||
var laddr address.Address
|
||||
var lsubnet address.Subnet
|
||||
copy(laddr[:], localIP)
|
||||
copy(lsubnet[:], localIP)
|
||||
if laddr.IsValid() || lsubnet.IsValid() {
|
||||
// The local address is with the network address/prefix range
|
||||
// This would route ygg over ygg, which we don't want
|
||||
// FIXME ideally this check should happen outside of the core library
|
||||
// Maybe dial/listen at the application level
|
||||
// Then pass a net.Conn to the core library (after these kinds of checks are done)
|
||||
t.links.core.log.Debugln("Dropping ygg-tunneled connection", local, remote)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
force := net.ParseIP(strings.Split(remote, "%")[0]).IsLinkLocalUnicast()
|
||||
link, err := t.links.create(sock, name, proto, local, remote, incoming, force, options.linkOptions)
|
||||
if err != nil {
|
||||
t.links.core.log.Println(err)
|
||||
panic(err)
|
||||
}
|
||||
t.links.core.log.Debugln("DEBUG: starting handler for", name)
|
||||
ch, err := link.handler()
|
||||
t.links.core.log.Debugln("DEBUG: stopped handler for", name, err)
|
||||
return ch
|
||||
}
|
126
src/core/tls.go
126
src/core/tls.go
|
@ -1,126 +0,0 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type tcptls struct {
|
||||
tcp *tcp
|
||||
config *tls.Config
|
||||
forDialer *TcpUpgrade
|
||||
forListener *TcpUpgrade
|
||||
}
|
||||
|
||||
func (t *tcptls) init(tcp *tcp) {
|
||||
t.tcp = tcp
|
||||
t.forDialer = &TcpUpgrade{
|
||||
upgrade: t.upgradeDialer,
|
||||
name: "tls",
|
||||
}
|
||||
t.forListener = &TcpUpgrade{
|
||||
upgrade: t.upgradeListener,
|
||||
name: "tls",
|
||||
}
|
||||
|
||||
edpriv := make(ed25519.PrivateKey, ed25519.PrivateKeySize)
|
||||
copy(edpriv[:], tcp.links.core.secret[:])
|
||||
|
||||
certBuf := &bytes.Buffer{}
|
||||
|
||||
// TODO: because NotAfter is finite, we should add some mechanism to regenerate the certificate and restart the listeners periodically for nodes with very high uptimes. Perhaps regenerate certs and restart listeners every few months or so.
|
||||
pubtemp := x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
Subject: pkix.Name{
|
||||
CommonName: hex.EncodeToString(tcp.links.core.public[:]),
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().Add(time.Hour * 24 * 365),
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
|
||||
derbytes, err := x509.CreateCertificate(rand.Reader, &pubtemp, &pubtemp, edpriv.Public(), edpriv)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create certificate: %s", err)
|
||||
}
|
||||
|
||||
if err := pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derbytes}); err != nil {
|
||||
panic("failed to encode certificate into PEM")
|
||||
}
|
||||
|
||||
cpool := x509.NewCertPool()
|
||||
cpool.AppendCertsFromPEM(derbytes)
|
||||
|
||||
t.config = &tls.Config{
|
||||
RootCAs: cpool,
|
||||
Certificates: []tls.Certificate{
|
||||
{
|
||||
Certificate: [][]byte{derbytes},
|
||||
PrivateKey: edpriv,
|
||||
},
|
||||
},
|
||||
InsecureSkipVerify: true,
|
||||
MinVersion: tls.VersionTLS13,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *tcptls) configForOptions(options *tcpOptions) *tls.Config {
|
||||
config := t.config.Clone()
|
||||
config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
||||
if len(rawCerts) != 1 {
|
||||
return errors.New("tls not exactly 1 cert")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(rawCerts[0])
|
||||
if err != nil {
|
||||
return errors.New("tls failed to parse cert")
|
||||
}
|
||||
if cert.PublicKeyAlgorithm != x509.Ed25519 {
|
||||
return errors.New("tls wrong cert algorithm")
|
||||
}
|
||||
pk := cert.PublicKey.(ed25519.PublicKey)
|
||||
var key keyArray
|
||||
copy(key[:], pk)
|
||||
// If options does not have a pinned key, then pin one now
|
||||
if options.pinnedEd25519Keys == nil {
|
||||
options.pinnedEd25519Keys = make(map[keyArray]struct{})
|
||||
options.pinnedEd25519Keys[key] = struct{}{}
|
||||
}
|
||||
if _, isIn := options.pinnedEd25519Keys[key]; !isIn {
|
||||
return errors.New("tls key does not match pinned key")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
func (t *tcptls) upgradeListener(c net.Conn, options *tcpOptions) (net.Conn, error) {
|
||||
config := t.configForOptions(options)
|
||||
conn := tls.Server(c, config)
|
||||
if err := conn.Handshake(); err != nil {
|
||||
return c, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (t *tcptls) upgradeDialer(c net.Conn, options *tcpOptions) (net.Conn, error) {
|
||||
config := t.configForOptions(options)
|
||||
config.ServerName = options.tlsSNI
|
||||
conn := tls.Client(c, config)
|
||||
if err := conn.Handshake(); err != nil {
|
||||
return c, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
|
@ -4,6 +4,9 @@ import "github.com/yggdrasil-network/yggdrasil-go/src/config"
|
|||
|
||||
type MulticastInterfaceConfig = config.MulticastInterfaceConfig
|
||||
|
||||
var defaultConfig = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultConfig=/path/to/config
|
||||
var defaultAdminListen = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultAdminListen=unix://path/to/sock'
|
||||
|
||||
// Defines which parameters are expected by default for configuration on a
|
||||
// specific platform. These values are populated in the relevant defaults_*.go
|
||||
// for the platform being targeted. They must be set.
|
||||
|
@ -17,27 +20,40 @@ type platformDefaultParameters struct {
|
|||
// Multicast interfaces
|
||||
DefaultMulticastInterfaces []MulticastInterfaceConfig
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU uint64
|
||||
DefaultIfMTU uint64
|
||||
DefaultIfName string
|
||||
}
|
||||
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
defaults := getDefaults()
|
||||
if defaultConfig != "" {
|
||||
defaults.DefaultConfigFile = defaultConfig
|
||||
}
|
||||
if defaultAdminListen != "" {
|
||||
defaults.DefaultAdminListen = defaultAdminListen
|
||||
}
|
||||
return defaults
|
||||
}
|
||||
|
||||
// Generates default configuration and returns a pointer to the resulting
|
||||
// NodeConfig. This is used when outputting the -genconf parameter and also when
|
||||
// using -autoconf.
|
||||
func GenerateConfig() *config.NodeConfig {
|
||||
// Get the defaults for the platform.
|
||||
defaults := GetDefaults()
|
||||
// Create a node configuration and populate it.
|
||||
cfg := new(config.NodeConfig)
|
||||
cfg.NewKeys()
|
||||
cfg.Listen = []string{}
|
||||
cfg.AdminListen = GetDefaults().DefaultAdminListen
|
||||
cfg.AdminListen = defaults.DefaultAdminListen
|
||||
cfg.Peers = []string{}
|
||||
cfg.InterfacePeers = map[string][]string{}
|
||||
cfg.AllowedPublicKeys = []string{}
|
||||
cfg.MulticastInterfaces = GetDefaults().DefaultMulticastInterfaces
|
||||
cfg.IfName = GetDefaults().DefaultIfName
|
||||
cfg.IfMTU = GetDefaults().DefaultIfMTU
|
||||
cfg.MulticastInterfaces = defaults.DefaultMulticastInterfaces
|
||||
cfg.IfName = defaults.DefaultIfName
|
||||
cfg.IfMTU = defaults.DefaultIfMTU
|
||||
cfg.NodeInfoPrivacy = false
|
||||
|
||||
return cfg
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the macOS/Darwin platform. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",
|
||||
|
@ -19,7 +19,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: "bridge.*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 65535,
|
||||
DefaultIfMTU: 65535,
|
||||
DefaultIfName: "auto",
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the BSD platforms. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",
|
||||
|
@ -18,7 +18,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: ".*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 32767,
|
||||
DefaultIfMTU: 32767,
|
||||
DefaultIfName: "/dev/tun0",
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the Linux platform. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",
|
||||
|
@ -18,7 +18,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: ".*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 65535,
|
||||
DefaultIfMTU: 65535,
|
||||
DefaultIfName: "auto",
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the BSD platforms. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",
|
||||
|
@ -18,7 +18,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: ".*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 16384,
|
||||
DefaultIfMTU: 16384,
|
||||
DefaultIfName: "tun0",
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the other platforms. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "tcp://localhost:9001",
|
||||
|
@ -18,7 +18,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: ".*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 65535,
|
||||
DefaultIfMTU: 65535,
|
||||
DefaultIfName: "none",
|
||||
|
|
|
@ -5,7 +5,7 @@ package defaults
|
|||
|
||||
// Sane defaults for the Windows platform. The "default" options may be
|
||||
// may be replaced by the running configuration.
|
||||
func GetDefaults() platformDefaultParameters {
|
||||
func getDefaults() platformDefaultParameters {
|
||||
return platformDefaultParameters{
|
||||
// Admin
|
||||
DefaultAdminListen: "tcp://localhost:9001",
|
||||
|
@ -18,7 +18,7 @@ func GetDefaults() platformDefaultParameters {
|
|||
{Regex: ".*", Beacon: true, Listen: true},
|
||||
},
|
||||
|
||||
// TUN/TAP
|
||||
// TUN
|
||||
MaximumIfMTU: 65535,
|
||||
DefaultIfMTU: 65535,
|
||||
DefaultIfName: "Yggdrasil",
|
||||
|
|
|
@ -20,15 +20,18 @@ func (m *Multicast) getMulticastInterfacesHandler(req *GetMulticastInterfacesReq
|
|||
}
|
||||
|
||||
func (m *Multicast) SetupAdminHandlers(a *admin.AdminSocket) {
|
||||
_ = a.AddHandler("getMulticastInterfaces", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetMulticastInterfacesRequest{}
|
||||
res := &GetMulticastInterfacesResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.getMulticastInterfacesHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
_ = a.AddHandler(
|
||||
"getMulticastInterfaces", "Show which interfaces multicast is enabled on", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetMulticastInterfacesRequest{}
|
||||
res := &GetMulticastInterfacesResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.getMulticastInterfacesHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -9,13 +9,11 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/gologme/log"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
"golang.org/x/net/ipv6"
|
||||
)
|
||||
|
@ -27,13 +25,15 @@ import (
|
|||
type Multicast struct {
|
||||
phony.Inbox
|
||||
core *core.Core
|
||||
config *config.NodeConfig
|
||||
log *log.Logger
|
||||
sock *ipv6.PacketConn
|
||||
groupAddr string
|
||||
listeners map[string]*listenerInfo
|
||||
isOpen bool
|
||||
_interfaces map[string]interfaceInfo
|
||||
_isOpen bool
|
||||
_listeners map[string]*listenerInfo
|
||||
_interfaces map[string]*interfaceInfo
|
||||
config struct {
|
||||
_groupAddr GroupAddress
|
||||
_interfaces map[MulticastInterface]struct{}
|
||||
}
|
||||
}
|
||||
|
||||
type interfaceInfo struct {
|
||||
|
@ -45,46 +45,44 @@ type interfaceInfo struct {
|
|||
}
|
||||
|
||||
type listenerInfo struct {
|
||||
listener *core.TcpListener
|
||||
listener *core.Listener
|
||||
time time.Time
|
||||
interval time.Duration
|
||||
port uint16
|
||||
}
|
||||
|
||||
// Init prepares the multicast interface for use.
|
||||
func (m *Multicast) Init(core *core.Core, nc *config.NodeConfig, log *log.Logger, options interface{}) error {
|
||||
m.core = core
|
||||
m.config = nc
|
||||
m.log = log
|
||||
m.listeners = make(map[string]*listenerInfo)
|
||||
m._interfaces = make(map[string]interfaceInfo)
|
||||
m.groupAddr = "[ff02::114]:9001"
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start starts the multicast interface. This launches goroutines which will
|
||||
// listen for multicast beacons from other hosts and will advertise multicast
|
||||
// beacons out to the network.
|
||||
func (m *Multicast) Start() error {
|
||||
func New(core *core.Core, log *log.Logger, opts ...SetupOption) (*Multicast, error) {
|
||||
m := &Multicast{
|
||||
core: core,
|
||||
log: log,
|
||||
_listeners: make(map[string]*listenerInfo),
|
||||
_interfaces: make(map[string]*interfaceInfo),
|
||||
}
|
||||
m.config._interfaces = map[MulticastInterface]struct{}{}
|
||||
m.config._groupAddr = GroupAddress("[ff02::114]:9001")
|
||||
for _, opt := range opts {
|
||||
m._applyOption(opt)
|
||||
}
|
||||
var err error
|
||||
phony.Block(m, func() {
|
||||
err = m._start()
|
||||
})
|
||||
m.log.Debugln("Started multicast module")
|
||||
return err
|
||||
return m, err
|
||||
}
|
||||
|
||||
func (m *Multicast) _start() error {
|
||||
if m.isOpen {
|
||||
if m._isOpen {
|
||||
return fmt.Errorf("multicast module is already started")
|
||||
}
|
||||
m.config.RLock()
|
||||
defer m.config.RUnlock()
|
||||
if len(m.config.MulticastInterfaces) == 0 {
|
||||
if len(m.config._interfaces) == 0 {
|
||||
return nil
|
||||
}
|
||||
m.log.Infoln("Starting multicast module")
|
||||
addr, err := net.ResolveUDPAddr("udp", m.groupAddr)
|
||||
m.log.Debugln("Starting multicast module")
|
||||
defer m.log.Debugln("Started multicast module")
|
||||
addr, err := net.ResolveUDPAddr("udp", string(m.config._groupAddr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -101,7 +99,7 @@ func (m *Multicast) _start() error {
|
|||
// Windows can't set this flag, so we need to handle it in other ways
|
||||
}
|
||||
|
||||
m.isOpen = true
|
||||
m._isOpen = true
|
||||
go m.listen()
|
||||
m.Act(nil, m._multicastStarted)
|
||||
m.Act(nil, m._announce)
|
||||
|
@ -113,7 +111,7 @@ func (m *Multicast) _start() error {
|
|||
func (m *Multicast) IsStarted() bool {
|
||||
var isOpen bool
|
||||
phony.Block(m, func() {
|
||||
isOpen = m.isOpen
|
||||
isOpen = m._isOpen
|
||||
})
|
||||
return isOpen
|
||||
}
|
||||
|
@ -130,7 +128,7 @@ func (m *Multicast) Stop() error {
|
|||
|
||||
func (m *Multicast) _stop() error {
|
||||
m.log.Infoln("Stopping multicast module")
|
||||
m.isOpen = false
|
||||
m._isOpen = false
|
||||
if m.sock != nil {
|
||||
m.sock.Close()
|
||||
}
|
||||
|
@ -138,7 +136,7 @@ func (m *Multicast) _stop() error {
|
|||
}
|
||||
|
||||
func (m *Multicast) _updateInterfaces() {
|
||||
interfaces := m.getAllowedInterfaces()
|
||||
interfaces := m._getAllowedInterfaces()
|
||||
for name, info := range interfaces {
|
||||
addrs, err := info.iface.Addrs()
|
||||
if err != nil {
|
||||
|
@ -163,10 +161,8 @@ func (m *Multicast) Interfaces() map[string]net.Interface {
|
|||
}
|
||||
|
||||
// getAllowedInterfaces returns the currently known/enabled multicast interfaces.
|
||||
func (m *Multicast) getAllowedInterfaces() map[string]interfaceInfo {
|
||||
interfaces := make(map[string]interfaceInfo)
|
||||
// Get interface expressions from config
|
||||
ifcfgs := m.config.MulticastInterfaces
|
||||
func (m *Multicast) _getAllowedInterfaces() map[string]*interfaceInfo {
|
||||
interfaces := make(map[string]*interfaceInfo)
|
||||
// Ask the system for network interfaces
|
||||
allifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
|
@ -176,62 +172,55 @@ func (m *Multicast) getAllowedInterfaces() map[string]interfaceInfo {
|
|||
}
|
||||
// Work out which interfaces to announce on
|
||||
for _, iface := range allifaces {
|
||||
if iface.Flags&net.FlagUp == 0 {
|
||||
// Ignore interfaces that are down
|
||||
continue
|
||||
switch {
|
||||
case iface.Flags&net.FlagUp == 0:
|
||||
continue // Ignore interfaces that are down
|
||||
case iface.Flags&net.FlagMulticast == 0:
|
||||
continue // Ignore non-multicast interfaces
|
||||
case iface.Flags&net.FlagPointToPoint != 0:
|
||||
continue // Ignore point-to-point interfaces
|
||||
}
|
||||
if iface.Flags&net.FlagMulticast == 0 {
|
||||
// Ignore non-multicast interfaces
|
||||
continue
|
||||
}
|
||||
if iface.Flags&net.FlagPointToPoint != 0 {
|
||||
// Ignore point-to-point interfaces
|
||||
continue
|
||||
}
|
||||
for _, ifcfg := range ifcfgs {
|
||||
for ifcfg := range m.config._interfaces {
|
||||
// Compile each regular expression
|
||||
e, err := regexp.Compile(ifcfg.Regex)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Does the interface match the regular expression? Store it if so
|
||||
if e.MatchString(iface.Name) {
|
||||
if ifcfg.Beacon || ifcfg.Listen {
|
||||
info := interfaceInfo{
|
||||
iface: iface,
|
||||
beacon: ifcfg.Beacon,
|
||||
listen: ifcfg.Listen,
|
||||
port: ifcfg.Port,
|
||||
}
|
||||
interfaces[iface.Name] = info
|
||||
}
|
||||
break
|
||||
if !ifcfg.Beacon && !ifcfg.Listen {
|
||||
continue
|
||||
}
|
||||
if !ifcfg.Regex.MatchString(iface.Name) {
|
||||
continue
|
||||
}
|
||||
interfaces[iface.Name] = &interfaceInfo{
|
||||
iface: iface,
|
||||
beacon: ifcfg.Beacon,
|
||||
listen: ifcfg.Listen,
|
||||
port: ifcfg.Port,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return interfaces
|
||||
}
|
||||
|
||||
func (m *Multicast) _announce() {
|
||||
if !m.isOpen {
|
||||
if !m._isOpen {
|
||||
return
|
||||
}
|
||||
m._updateInterfaces()
|
||||
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
||||
groupAddr, err := net.ResolveUDPAddr("udp6", string(m.config._groupAddr))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
destAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
||||
destAddr, err := net.ResolveUDPAddr("udp6", string(m.config._groupAddr))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// There might be interfaces that we configured listeners for but are no
|
||||
// longer up - if that's the case then we should stop the listeners
|
||||
for name, info := range m.listeners {
|
||||
for name, info := range m._listeners {
|
||||
// Prepare our stop function!
|
||||
stop := func() {
|
||||
info.listener.Stop()
|
||||
delete(m.listeners, name)
|
||||
info.listener.Close()
|
||||
delete(m._listeners, name)
|
||||
m.log.Debugln("No longer multicasting on", name)
|
||||
}
|
||||
// If the interface is no longer visible on the system then stop the
|
||||
|
@ -290,7 +279,7 @@ func (m *Multicast) _announce() {
|
|||
}
|
||||
// Try and see if we already have a TCP listener for this interface
|
||||
var linfo *listenerInfo
|
||||
if nfo, ok := m.listeners[iface.Name]; !ok || nfo.listener.Listener == nil {
|
||||
if nfo, ok := m._listeners[iface.Name]; !ok || nfo.listener.Listener == nil {
|
||||
// No listener was found - let's create one
|
||||
urlString := fmt.Sprintf("tls://[%s]:%d", addrIP, info.port)
|
||||
u, err := url.Parse(urlString)
|
||||
|
@ -301,13 +290,13 @@ func (m *Multicast) _announce() {
|
|||
m.log.Debugln("Started multicasting on", iface.Name)
|
||||
// Store the listener so that we can stop it later if needed
|
||||
linfo = &listenerInfo{listener: li, time: time.Now(), port: info.port}
|
||||
m.listeners[iface.Name] = linfo
|
||||
m._listeners[iface.Name] = linfo
|
||||
} else {
|
||||
m.log.Warnln("Not multicasting on", iface.Name, "due to error:", err)
|
||||
}
|
||||
} else {
|
||||
// An existing listener was found
|
||||
linfo = m.listeners[iface.Name]
|
||||
linfo = m._listeners[iface.Name]
|
||||
}
|
||||
// Make sure nothing above failed for some reason
|
||||
if linfo == nil {
|
||||
|
@ -340,7 +329,7 @@ func (m *Multicast) _announce() {
|
|||
}
|
||||
|
||||
func (m *Multicast) listen() {
|
||||
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
||||
groupAddr, err := net.ResolveUDPAddr("udp6", string(m.config._groupAddr))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -388,7 +377,7 @@ func (m *Multicast) listen() {
|
|||
if !from.IP.Equal(addr.IP) {
|
||||
continue
|
||||
}
|
||||
var interfaces map[string]interfaceInfo
|
||||
var interfaces map[string]*interfaceInfo
|
||||
phony.Block(m, func() {
|
||||
interfaces = m._interfaces
|
||||
})
|
||||
|
|
|
@ -31,7 +31,7 @@ import (
|
|||
)
|
||||
|
||||
func (m *Multicast) _multicastStarted() {
|
||||
if !m.isOpen {
|
||||
if !m._isOpen {
|
||||
return
|
||||
}
|
||||
C.StopAWDLBrowsing()
|
||||
|
|
28
src/multicast/options.go
Normal file
28
src/multicast/options.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package multicast
|
||||
|
||||
import "regexp"
|
||||
|
||||
func (m *Multicast) _applyOption(opt SetupOption) {
|
||||
switch v := opt.(type) {
|
||||
case MulticastInterface:
|
||||
m.config._interfaces[v] = struct{}{}
|
||||
case GroupAddress:
|
||||
m.config._groupAddr = v
|
||||
}
|
||||
}
|
||||
|
||||
type SetupOption interface {
|
||||
isSetupOption()
|
||||
}
|
||||
|
||||
type MulticastInterface struct {
|
||||
Regex *regexp.Regexp
|
||||
Beacon bool
|
||||
Listen bool
|
||||
Port uint16
|
||||
}
|
||||
|
||||
type GroupAddress string
|
||||
|
||||
func (a MulticastInterface) isSetupOption() {}
|
||||
func (a GroupAddress) isSetupOption() {}
|
45
src/tun/admin.go
Normal file
45
src/tun/admin.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package tun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||
)
|
||||
|
||||
type GetTUNRequest struct{}
|
||||
type GetTUNResponse struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Name string `json:"name,omitempty"`
|
||||
MTU uint64 `json:"mtu,omitempty"`
|
||||
}
|
||||
|
||||
type TUNEntry struct {
|
||||
MTU uint64 `json:"mtu"`
|
||||
}
|
||||
|
||||
func (t *TunAdapter) getTUNHandler(req *GetTUNRequest, res *GetTUNResponse) error {
|
||||
res.Enabled = t.isEnabled
|
||||
if !t.isEnabled {
|
||||
return nil
|
||||
}
|
||||
res.Name = t.Name()
|
||||
res.MTU = t.MTU()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) {
|
||||
_ = a.AddHandler(
|
||||
"getTun", "Show information about the node's TUN interface", []string{},
|
||||
func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetTUNRequest{}
|
||||
res := &GetTUNResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.getTUNHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
},
|
||||
)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package tuntap
|
||||
package tun
|
||||
|
||||
const TUN_OFFSET_BYTES = 4
|
||||
|
20
src/tun/options.go
Normal file
20
src/tun/options.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package tun
|
||||
|
||||
func (m *TunAdapter) _applyOption(opt SetupOption) {
|
||||
switch v := opt.(type) {
|
||||
case InterfaceName:
|
||||
m.config.name = v
|
||||
case InterfaceMTU:
|
||||
m.config.mtu = v
|
||||
}
|
||||
}
|
||||
|
||||
type SetupOption interface {
|
||||
isSetupOption()
|
||||
}
|
||||
|
||||
type InterfaceName string
|
||||
type InterfaceMTU uint64
|
||||
|
||||
func (a InterfaceName) isSetupOption() {}
|
||||
func (a InterfaceMTU) isSetupOption() {}
|
|
@ -1,10 +1,7 @@
|
|||
package tuntap
|
||||
package tun
|
||||
|
||||
// This manages the tun driver to send/recv packets to/from applications
|
||||
|
||||
// TODO: Crypto-key routing support
|
||||
// TODO: Set MTU of session properly
|
||||
// TODO: Reject packets that exceed session MTU with ICMPv6 for PMTU Discovery
|
||||
// TODO: Connection timeouts (call Conn.Close() when we want to time out)
|
||||
// TODO: Don't block in reader on writes that are pending searches
|
||||
|
||||
|
@ -13,14 +10,11 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
|
||||
//"sync"
|
||||
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/gologme/log"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||
)
|
||||
|
@ -33,8 +27,7 @@ type MTU uint16
|
|||
// calling yggdrasil.Start().
|
||||
type TunAdapter struct {
|
||||
rwc *ipv6rwc.ReadWriteCloser
|
||||
config *config.NodeConfig
|
||||
log *log.Logger
|
||||
log core.Logger
|
||||
addr address.Address
|
||||
subnet address.Subnet
|
||||
mtu uint64
|
||||
|
@ -43,6 +36,10 @@ type TunAdapter struct {
|
|||
//mutex sync.RWMutex // Protects the below
|
||||
isOpen bool
|
||||
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
||||
config struct {
|
||||
name InterfaceName
|
||||
mtu InterfaceMTU
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the maximum supported MTU for the platform based on the defaults in
|
||||
|
@ -93,50 +90,39 @@ func MaximumMTU() uint64 {
|
|||
|
||||
// Init initialises the TUN module. You must have acquired a Listener from
|
||||
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
||||
func (tun *TunAdapter) Init(rwc *ipv6rwc.ReadWriteCloser, config *config.NodeConfig, log *log.Logger, options interface{}) error {
|
||||
tun.rwc = rwc
|
||||
tun.config = config
|
||||
tun.log = log
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start the setup process for the TUN adapter. If successful, starts the
|
||||
// reader actor to handle packets on that interface.
|
||||
func (tun *TunAdapter) Start() error {
|
||||
var err error
|
||||
phony.Block(tun, func() {
|
||||
err = tun._start()
|
||||
})
|
||||
return err
|
||||
func New(rwc *ipv6rwc.ReadWriteCloser, log core.Logger, opts ...SetupOption) (*TunAdapter, error) {
|
||||
tun := &TunAdapter{
|
||||
rwc: rwc,
|
||||
log: log,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
tun._applyOption(opt)
|
||||
}
|
||||
return tun, tun._start()
|
||||
}
|
||||
|
||||
func (tun *TunAdapter) _start() error {
|
||||
if tun.isOpen {
|
||||
return errors.New("TUN module is already started")
|
||||
}
|
||||
if tun.config == nil {
|
||||
return errors.New("no configuration available to TUN")
|
||||
}
|
||||
tun.config.RLock()
|
||||
defer tun.config.RUnlock()
|
||||
tun.addr = tun.rwc.Address()
|
||||
tun.subnet = tun.rwc.Subnet()
|
||||
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
||||
if tun.config.IfName == "none" || tun.config.IfName == "dummy" {
|
||||
if tun.config.name == "none" || tun.config.name == "dummy" {
|
||||
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
|
||||
tun.isEnabled = false
|
||||
go tun.write()
|
||||
return nil
|
||||
}
|
||||
mtu := tun.config.IfMTU
|
||||
mtu := uint64(tun.config.mtu)
|
||||
if tun.rwc.MaxMTU() < mtu {
|
||||
mtu = tun.rwc.MaxMTU()
|
||||
}
|
||||
if err := tun.setup(tun.config.IfName, addr, mtu); err != nil {
|
||||
if err := tun.setup(string(tun.config.name), addr, mtu); err != nil {
|
||||
return err
|
||||
}
|
||||
if tun.MTU() != mtu {
|
||||
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", tun.config.IfMTU, tun.MTU(), MaximumMTU())
|
||||
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", tun.config.mtu, tun.MTU(), MaximumMTU())
|
||||
}
|
||||
tun.rwc.SetMTU(tun.MTU())
|
||||
tun.isOpen = true
|
|
@ -1,7 +1,7 @@
|
|||
//go:build openbsd || freebsd
|
||||
// +build openbsd freebsd
|
||||
|
||||
package tuntap
|
||||
package tun
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
|
@ -1,7 +1,7 @@
|
|||
//go:build !mobile
|
||||
// +build !mobile
|
||||
|
||||
package tuntap
|
||||
package tun
|
||||
|
||||
// The darwin platform specific tun parts
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
//go:build !mobile
|
||||
// +build !mobile
|
||||
|
||||
package tuntap
|
||||
package tun
|
||||
|
||||
// The linux platform specific tun parts
|
||||
|
||||
|
@ -28,7 +28,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
// Configures the TAP adapter with the correct IPv6 address and MTU. Netlink
|
||||
// Configures the TUN adapter with the correct IPv6 address and MTU. Netlink
|
||||
// is used to do this, so there is not a hard requirement on "ip" or "ifconfig"
|
||||
// to exist on the system, but this will fail if Netlink is not present in the
|
||||
// kernel (it nearly always is).
|
|
@ -1,7 +1,7 @@
|
|||
//go:build !linux && !darwin && !windows && !openbsd && !freebsd && !mobile
|
||||
// +build !linux,!darwin,!windows,!openbsd,!freebsd,!mobile
|
||||
|
||||
package tuntap
|
||||
package tun
|
||||
|
||||
// This is to catch unsupported platforms
|
||||
// If your platform supports tun devices, you could try configuring it manually
|
|
@ -1,7 +1,7 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package tuntap
|
||||
package tun
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -50,7 +50,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
})
|
||||
}
|
||||
|
||||
// Sets the MTU of the TAP adapter.
|
||||
// Sets the MTU of the TUN adapter.
|
||||
func (tun *TunAdapter) setupMTU(mtu uint64) error {
|
||||
if tun.iface == nil || tun.Name() == "" {
|
||||
return errors.New("Can't configure MTU as TUN adapter is not present")
|
||||
|
@ -77,7 +77,7 @@ func (tun *TunAdapter) setupMTU(mtu uint64) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Sets the IPv6 address of the TAP adapter.
|
||||
// Sets the IPv6 address of the TUN adapter.
|
||||
func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
if tun.iface == nil || tun.Name() == "" {
|
||||
return errors.New("Can't configure IPv6 address as TUN adapter is not present")
|
|
@ -1,37 +0,0 @@
|
|||
package tuntap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||
)
|
||||
|
||||
type GetTUNRequest struct{}
|
||||
type GetTUNResponse map[string]TUNEntry
|
||||
|
||||
type TUNEntry struct {
|
||||
MTU uint64 `json:"mtu"`
|
||||
}
|
||||
|
||||
func (t *TunAdapter) getTUNHandler(req *GetTUNRequest, res *GetTUNResponse) error {
|
||||
*res = GetTUNResponse{
|
||||
t.Name(): TUNEntry{
|
||||
MTU: t.MTU(),
|
||||
},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) {
|
||||
_ = a.AddHandler("getTunTap", []string{}, func(in json.RawMessage) (interface{}, error) {
|
||||
req := &GetTUNRequest{}
|
||||
res := &GetTUNResponse{}
|
||||
if err := json.Unmarshal(in, &req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.getTUNHandler(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
// Package util contains miscellaneous utilities used by yggdrasil.
|
||||
// In particular, this includes a crypto worker pool, Cancellation machinery, and a sync.Pool used to reuse []byte.
|
||||
package util
|
||||
|
||||
// These are misc. utility functions that didn't really fit anywhere else
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimerStop stops a timer and makes sure the channel is drained, returns true if the timer was stopped before firing.
|
||||
func TimerStop(t *time.Timer) bool {
|
||||
stopped := t.Stop()
|
||||
select {
|
||||
case <-t.C:
|
||||
default:
|
||||
}
|
||||
return stopped
|
||||
}
|
||||
|
||||
// FuncTimeout runs the provided function in a separate goroutine, and returns true if the function finishes executing before the timeout passes, or false if the timeout passes.
|
||||
// It includes no mechanism to stop the function if the timeout fires, so the user is expected to do so on their own (such as with a Cancellation or a context).
|
||||
func FuncTimeout(timeout time.Duration, f func()) bool {
|
||||
success := make(chan struct{})
|
||||
go func() {
|
||||
defer close(success)
|
||||
f()
|
||||
}()
|
||||
timer := time.NewTimer(timeout)
|
||||
defer TimerStop(timer)
|
||||
select {
|
||||
case <-success:
|
||||
return true
|
||||
case <-timer.C:
|
||||
return false
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue