|
| 1 | +package io.libp2p.protocol.autonat; |
| 2 | + |
| 3 | +import com.google.protobuf.*; |
| 4 | +import io.libp2p.core.*; |
| 5 | +import io.libp2p.core.Stream; |
| 6 | +import io.libp2p.core.multiformats.*; |
| 7 | +import io.libp2p.core.multistream.*; |
| 8 | +import io.libp2p.protocol.*; |
| 9 | +import io.libp2p.protocol.autonat.pb.*; |
| 10 | +import java.io.*; |
| 11 | +import java.net.*; |
| 12 | +import java.util.*; |
| 13 | +import java.util.concurrent.*; |
| 14 | +import java.util.stream.*; |
| 15 | +import org.jetbrains.annotations.*; |
| 16 | + |
| 17 | +public class AutonatProtocol extends ProtobufProtocolHandler<AutonatProtocol.AutoNatController> { |
| 18 | + |
| 19 | + public static class Binding extends StrictProtocolBinding<AutoNatController> { |
| 20 | + public Binding() { |
| 21 | + super("/libp2p/autonat/v1.0.0", new AutonatProtocol()); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public interface AutoNatController { |
| 26 | + CompletableFuture<Autonat.Message> rpc(Autonat.Message req); |
| 27 | + |
| 28 | + default CompletableFuture<Autonat.Message.DialResponse> requestDial( |
| 29 | + PeerId ourId, List<Multiaddr> us) { |
| 30 | + if (us.isEmpty()) |
| 31 | + throw new IllegalStateException("Requested autonat dial with no addresses!"); |
| 32 | + return rpc(Autonat.Message.newBuilder() |
| 33 | + .setType(Autonat.Message.MessageType.DIAL) |
| 34 | + .setDial( |
| 35 | + Autonat.Message.Dial.newBuilder() |
| 36 | + .setPeer( |
| 37 | + Autonat.Message.PeerInfo.newBuilder() |
| 38 | + .addAllAddrs( |
| 39 | + us.stream() |
| 40 | + .map(a -> ByteString.copyFrom(a.serialize())) |
| 41 | + .collect(Collectors.toList())) |
| 42 | + .setId(ByteString.copyFrom(ourId.getBytes())))) |
| 43 | + .build()) |
| 44 | + .thenApply(msg -> msg.getDialResponse()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static class Sender implements ProtocolMessageHandler<Autonat.Message>, AutoNatController { |
| 49 | + private final Stream stream; |
| 50 | + private final LinkedBlockingDeque<CompletableFuture<Autonat.Message>> queue = |
| 51 | + new LinkedBlockingDeque<>(); |
| 52 | + |
| 53 | + public Sender(Stream stream) { |
| 54 | + this.stream = stream; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onMessage(@NotNull Stream stream, Autonat.Message msg) { |
| 59 | + queue.poll().complete(msg); |
| 60 | + } |
| 61 | + |
| 62 | + public CompletableFuture<Autonat.Message> rpc(Autonat.Message req) { |
| 63 | + CompletableFuture<Autonat.Message> res = new CompletableFuture<>(); |
| 64 | + queue.add(res); |
| 65 | + stream.writeAndFlush(req); |
| 66 | + return res; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean sameIP(Multiaddr a, Multiaddr b) { |
| 71 | + if (a.has(Protocol.IP4)) |
| 72 | + return a.getFirstComponent(Protocol.IP4).equals(b.getFirstComponent(Protocol.IP4)); |
| 73 | + if (a.has(Protocol.IP6)) |
| 74 | + return a.getFirstComponent(Protocol.IP6).equals(b.getFirstComponent(Protocol.IP6)); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean reachableIP(Multiaddr a) { |
| 79 | + try { |
| 80 | + if (a.has(Protocol.IP4)) |
| 81 | + return InetAddress.getByName(a.getFirstComponent(Protocol.IP4).getStringValue()) |
| 82 | + .isReachable(1000); |
| 83 | + if (a.has(Protocol.IP6)) |
| 84 | + return InetAddress.getByName(a.getFirstComponent(Protocol.IP6).getStringValue()) |
| 85 | + .isReachable(1000); |
| 86 | + } catch (IOException e) { |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + public static class Receiver |
| 92 | + implements ProtocolMessageHandler<Autonat.Message>, AutoNatController { |
| 93 | + private final Stream p2pstream; |
| 94 | + |
| 95 | + public Receiver(Stream p2pstream) { |
| 96 | + this.p2pstream = p2pstream; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onMessage(@NotNull Stream stream, Autonat.Message msg) { |
| 101 | + switch (msg.getType()) { |
| 102 | + case DIAL: |
| 103 | + { |
| 104 | + Autonat.Message.Dial dial = msg.getDial(); |
| 105 | + PeerId peerId = new PeerId(dial.getPeer().getId().toByteArray()); |
| 106 | + List<Multiaddr> requestedDials = |
| 107 | + dial.getPeer().getAddrsList().stream() |
| 108 | + .map(s -> Multiaddr.deserialize(s.toByteArray())) |
| 109 | + .collect(Collectors.toList()); |
| 110 | + PeerId streamPeerId = stream.remotePeerId(); |
| 111 | + if (!peerId.equals(streamPeerId)) { |
| 112 | + p2pstream.close(); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + Multiaddr remote = stream.getConnection().remoteAddress(); |
| 117 | + Optional<Multiaddr> reachable = |
| 118 | + requestedDials.stream() |
| 119 | + .filter(a -> sameIP(a, remote)) |
| 120 | + .filter(a -> !a.has(Protocol.P2PCIRCUIT)) |
| 121 | + .filter(a -> reachableIP(a)) |
| 122 | + .findAny(); |
| 123 | + Autonat.Message.Builder resp = |
| 124 | + Autonat.Message.newBuilder().setType(Autonat.Message.MessageType.DIAL_RESPONSE); |
| 125 | + if (reachable.isPresent()) { |
| 126 | + resp = |
| 127 | + resp.setDialResponse( |
| 128 | + Autonat.Message.DialResponse.newBuilder() |
| 129 | + .setStatus(Autonat.Message.ResponseStatus.OK) |
| 130 | + .setAddr(ByteString.copyFrom(reachable.get().serialize()))); |
| 131 | + } else { |
| 132 | + resp = |
| 133 | + resp.setDialResponse( |
| 134 | + Autonat.Message.DialResponse.newBuilder() |
| 135 | + .setStatus(Autonat.Message.ResponseStatus.E_DIAL_ERROR)); |
| 136 | + } |
| 137 | + p2pstream.writeAndFlush(resp); |
| 138 | + } |
| 139 | + default: |
| 140 | + { |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public CompletableFuture<Autonat.Message> rpc(Autonat.Message msg) { |
| 146 | + return CompletableFuture.failedFuture( |
| 147 | + new IllegalStateException("Cannot send form a receiver!")); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static final int TRAFFIC_LIMIT = 2 * 1024; |
| 152 | + |
| 153 | + public AutonatProtocol() { |
| 154 | + super(Autonat.Message.getDefaultInstance(), TRAFFIC_LIMIT, TRAFFIC_LIMIT); |
| 155 | + } |
| 156 | + |
| 157 | + @NotNull |
| 158 | + @Override |
| 159 | + protected CompletableFuture<AutoNatController> onStartInitiator(@NotNull Stream stream) { |
| 160 | + Sender replyPropagator = new Sender(stream); |
| 161 | + stream.pushHandler(replyPropagator); |
| 162 | + return CompletableFuture.completedFuture(replyPropagator); |
| 163 | + } |
| 164 | + |
| 165 | + @NotNull |
| 166 | + @Override |
| 167 | + protected CompletableFuture<AutoNatController> onStartResponder(@NotNull Stream stream) { |
| 168 | + Receiver dialer = new Receiver(stream); |
| 169 | + stream.pushHandler(dialer); |
| 170 | + return CompletableFuture.completedFuture(dialer); |
| 171 | + } |
| 172 | +} |
0 commit comments