mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-02 13:03:11 +00:00
The error in String should not actually ocurr, as the multiaddr should have been valid to be constructed successfully, and thus should be encoded back to its string rep correctly. This will be bolstered by creating an interface (to prevent messing with the internal bytes)
go-multiaddr
multiaddr implementation in Go.
Example
Simple
import "github.com/jbenet/go-multiaddr"
m := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m.buffer
// <Buffer >
m.String()
// /ip4/127.0.0.1/udp/1234
// construct with Buffer
m = multiaddr.Multiaddr{ Bytes: m.Bytes }
// <Multiaddr /ip4/127.0.0.1/udp/1234>
Protocols
// get the multiaddr protocol description objects
addr.Protocols()
// []*Protocol{
// &Protocol{ Code: 4, Name: 'ip4', Size: 32},
// &Protocol{ Code: 17, Name: 'udp', Size: 16},
// }
Other formats
// handles the stupid url version too
m = multiaddr.NewUrl("udp4://127.0.0.1:1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m.Url(buf)
// udp4://127.0.0.1:1234
En/decapsulate
m.Encapsulate(m.NewMultiaddr("/sctp/5678"))
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
m.Decapsulate(m.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
// <Multiaddr /ip4/127.0.0.1>
Tunneling
Multiaddr allows expressing tunnels very nicely.
printer := multiaddr.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
proxy := multiaddr.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
printerOverProxy := proxy.Encapsulate(printer)
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
proxyAgain := printerOverProxy.Decapsulate(multiaddr.NewMultiaddr("/ip4"))
// <Multiaddr /ip4/10.20.30.40/tcp/443>
Description
Languages
Go
100%