package autorelayimport (mamanet)// This function cleans up a relay's address set to remove private addresses and curtail// addrsplosion.// TODO: Remove this, we don't need this. The current method tries to select the// best address for the relay. Instead we should rely on the addresses provided by the// relay in response to the reservation request.func cleanupAddressSet( []ma.Multiaddr) []ma.Multiaddr {var , []ma.Multiaddrfor , := range {ifisRelayAddr() {continue }ifmanet.IsPublicAddr() { = append(, )continue }// discard unroutable addrsifmanet.IsPrivateAddr() { = append(, ) } }if !hasAddrsplosion() {return }returnsanitizeAddrsplodedSet(, )}func isRelayAddr( ma.Multiaddr) bool { := falsema.ForEach(, func( ma.Component) bool {switch .Protocol().Code {casema.P_CIRCUIT: = truereturnfalsedefault:returntrue } })return}// we have addrsplosion if for some protocol we advertise multiple ports on// the same base address.func hasAddrsplosion( []ma.Multiaddr) bool { := make(map[string]int)for , := range { , := addrKeyAndPort() , := []if && != {returntrue } [] = }returnfalse}func addrKeyAndPort( ma.Multiaddr) (string, int) {var (stringint )ma.ForEach(, func( ma.Component) bool {switch .Protocol().Code {casema.P_TCP, ma.P_UDP: = int(binary.BigEndian.Uint16(.RawValue())) += "/" + .Protocol().Namedefault: := .Value()if == "" { = .Protocol().Name } += "/" + }returntrue })return , }// clean up addrsplosion// the following heuristic is used:// - for each base address/protocol combination, if there are multiple ports advertised then// only accept the default port if present.// - If the default port is not present, we check for non-standard ports by tracking// private port bindings if present.// - If there is no default or private port binding, then we can't infer the correct// port and give up and return all addrs (for that base address)func sanitizeAddrsplodedSet(, []ma.Multiaddr) []ma.Multiaddr {typestruct {ma.Multiaddrint } := make(map[int]struct{}) := make(map[string][])for , := range { , := addrKeyAndPort() [] = struct{}{} }for , := range { , := addrKeyAndPort() [] = append([], {: , : }) }var []ma.Multiaddrfor , := range {iflen() == 1 {// it's not addrsploded = append(, [0].)continue } := falsefor , := range {if , := [.]; {// it matches a privately bound port, use it = append(, .) = truecontinue }if . == 4001 || . == 4002 {// it's a default port, use it = append(, .) = true } }if ! {// we weren't able to select a port; bite the bullet and use them allfor , := range { = append(, .) } } }return}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.