diff --git a/.gitignore b/.gitignore index b07865f..2b3122d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ examples/http-dialer/http-client examples/http-dialer/http-dialer examples/jasmine-teehaus/jasmineteehaus examples/pairchat/pairchat +examples/shared-token/shared-token examples/simple-pipe/simple-pipe examples/timeserve/timeserve examples/withcontext/withcontext diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 22d3e17..ae7a3cd 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -9,6 +9,8 @@ import ( jt "gomod.cblgh.org/jasmine-teehaus/experimental" ) +var DefaultQuicPort uint16 = 7842 + type Option func(*iroh.EndpointBuilder) error func New(options ...Option) (*iroh.Endpoint, error) { @@ -81,6 +83,16 @@ func WithRelayMode(rMode *iroh.RelayMode) Option { } } +func WithSharedToken(vMap *iroh.RelayMap) Option { + return func(endpointBuilder *iroh.EndpointBuilder) error { + // NOTE(d1): pending the more convenient implementation + // https://github.com/n0-computer/iroh-ffi/issues/307 + rMode := iroh.RelayModeCustom(vMap) + endpointBuilder.RelayMode(rMode) + return nil + } +} + func WithSecretKey(secretKey []byte) Option { return func(endpointBuilder *iroh.EndpointBuilder) error { return endpointBuilder.SecretKey(secretKey) diff --git a/examples/jasmine-teehaus/README.md b/examples/jasmine-teehaus/README.md index 68b938f..15f9405 100644 --- a/examples/jasmine-teehaus/README.md +++ b/examples/jasmine-teehaus/README.md @@ -3,3 +3,13 @@ This example shows how to configure the community managed relays of [`jasmine-teehaus`](https://codeberg.org/cblgh/jasmine-teehaus). This approach should generally work for other community relays. + +This code might not run if the Jasmine Teehaus relays are down or protected by +additional authentication methods in the future. Please consult the upstream +repository linked above. + +## Terminal 1 + +``` +./jasmineteehaus +``` diff --git a/examples/shared-token/README.md b/examples/shared-token/README.md new file mode 100644 index 0000000..61ce697 --- /dev/null +++ b/examples/shared-token/README.md @@ -0,0 +1,13 @@ +# shared-token + +This example shows how to connect to a relay which is protected by a shared +token. **Please note**, this example will not work unless you have a valid +shared token from the [Jasmine Teehaus +community](https://codeberg.org/cblgh/jasmine-teehaus). + +## Terminal 1 + +``` +export SHARED_TOKEN=... +./shared-token +``` diff --git a/examples/shared-token/cleanclose b/examples/shared-token/cleanclose new file mode 100755 index 0000000..1bd1126 Binary files /dev/null and b/examples/shared-token/cleanclose differ diff --git a/examples/shared-token/go.mod b/examples/shared-token/go.mod new file mode 100644 index 0000000..fd63fff --- /dev/null +++ b/examples/shared-token/go.mod @@ -0,0 +1,12 @@ +module git.coopcloud.tech/decentral1se/iroh-go/examples/shared-token + +go 1.26.1 + +require git.coopcloud.tech/decentral1se/iroh-go v0.0.0-20260717110820-68ad06fe2a25 + +require ( + golang.org/x/sync v0.24.0 // indirect + gomod.cblgh.org/jasmine-teehaus v0.0.3 // indirect +) + +replace git.coopcloud.tech/decentral1se/iroh-go => ../../ diff --git a/examples/shared-token/go.sum b/examples/shared-token/go.sum new file mode 100644 index 0000000..3a23bc1 --- /dev/null +++ b/examples/shared-token/go.sum @@ -0,0 +1,6 @@ +github.com/magiconair/properties v1.18.12 h1:sT9zQpvTB3B4gzrX0tmZNTEaGyg8Zw55MFYRE32Mr9I= +github.com/magiconair/properties v1.18.12/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +gomod.cblgh.org/jasmine-teehaus v0.0.3 h1:KXdEcJeAo7TqNq98UtPJ/OzmteVuaaoMWy7LK1wZMy4= +gomod.cblgh.org/jasmine-teehaus v0.0.3/go.mod h1:kga45IfEn1aEX2VPxQ8yXA3VcqyDseTIdf68gpWQtrM= diff --git a/examples/shared-token/makefile b/examples/shared-token/makefile new file mode 100644 index 0000000..04b0f86 --- /dev/null +++ b/examples/shared-token/makefile @@ -0,0 +1,2 @@ +default: + @go build -ldflags "-s -w" -v ./... diff --git a/examples/shared-token/shared-token.go b/examples/shared-token/shared-token.go new file mode 100644 index 0000000..00cadc8 --- /dev/null +++ b/examples/shared-token/shared-token.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + + iroh "git.coopcloud.tech/decentral1se/iroh-go" + "git.coopcloud.tech/decentral1se/iroh-go/endpoint" +) + +func main() { + alpn := iroh.Alpn("iroh-go-shared-token/0") + + token := os.Getenv("SHARED_TOKEN") + if token == "" { + fmt.Println("missing SHARED_TOKEN?") + os.Exit(1) + } + + vMap := iroh.RelayMapEmpty() + vMap.Insert(iroh.RelayConfig{ + Url: "https://jasmine.permacomputing.net", + QuicPort: &endpoint.DefaultQuicPort, + AuthToken: &token, + }) + + e, err := endpoint.New( + endpoint.WithAlpns(alpn), + endpoint.WithApplyJasmineTeehaus(), + endpoint.WithSharedToken(vMap), + ) + if err := iroh.As(err); err != nil { + panic(err) + } + + e.Online() + + if err := e.Close(); err != nil { + panic(iroh.As(err)) + } +}