summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client/SamProxyThread.java
blob: 29384558fe9b5ee4370b0d6c58d72a081232d1bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ganarchy.friendcode.client;

import ganarchy.friendcode.FriendCode;
import ganarchy.friendcode.sam.I2PSamControl;
import ganarchy.friendcode.sam.I2PSamStreamConnector;

import java.io.IOException;
import java.net.Inet6Address;
import java.net.ServerSocket;
import java.net.Socket;

public class SamProxyThread extends Thread {
    private final String target;
    private volatile int localPort;
    private volatile Status status = Status.IDLE;
    private volatile boolean running = true;
    private volatile boolean isConnecting = false;

    public SamProxyThread(String target) {
        this.target = target;
        this.setDaemon(true);
        this.setName("SAM Proxy Control Thread");
    }

    @Override
    public void run() {
        if (this.running) {
            tryRun(true);
        }
        if (this.running) {
            tryRun(false);
        }
        this.status = Status.SETUP_FAILED;
    }

    private void tryRun(final boolean zeroHop) {
        // this is all very inefficient but we don't particularly care
        // who cares if you have 3 threads just to connect to a minecraft server/friend code
        // it's only 3 threads
        // the "server" is far more efficient, only requiring one
        try (final I2PSamControl control = new I2PSamControl(zeroHop, null)) {
            if (!control.connect()) {
                this.running = false;
                this.status = Status.CONNECTION_FAILED;
                return;
            }
            if (!control.start()) {
                this.running = false;
                this.status = Status.SETUP_FAILED;
                return;
            }
            switch (control.checkName(this.target)) {
                case UNKNOWN, INVALID -> {
                    this.running = false;
                    this.status = Status.RESOLUTION_FAILED;
                    return;
                }
                case FAILED -> {
                    return;
                }
            }
            // we can't distinguish if we should retry the connection
            // (e.g. because building tunnels took too long, destination is known but gateway/peer is not, etc)
            // vs if we should try with 1hop (because we can't reach peer from our connection,
            // e.g. ipv4-only client vs ipv6-only friend, symmetric NAT, etc)
            // so we just try to "warm up" the connection and hope it works.
            I2PSamStreamConnector warmup = control.connectStream(this.target);
            if (!warmup.connect()) {
                this.running = false;
                this.status = Status.CONNECTION_FAILED;
                return;
            }
            warmup.start();
            warmup.close();
            I2PSamStreamConnector stream = control.connectStream(this.target);
            if (!stream.connect()) {
                this.running = false;
                this.status = Status.CONNECTION_FAILED;
                return;
            }
            this.isConnecting = true;
            if (!stream.start()) {
                FriendCode.LOGGER.warn("[SAM error] {}", stream.getStatus());
                this.isConnecting = false;
                if (!zeroHop) {
                    this.running = false;
                    this.status = Status.SETUP_FAILED;
                }
                return;
            }
            try (
                    Socket socket = stream.unwrap();
                    ServerSocket server = new ServerSocket(0, 8, Inet6Address.getByAddress("localhost", new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 0));
            ) {
                this.localPort = server.getLocalPort();
                server.setSoTimeout(3000);
                this.status = Status.RUNNING;
                try (Socket single = server.accept()) {
                    socket.setTcpNoDelay(true);
                    single.setTcpNoDelay(true);
                    Thread clientThread = new Thread("SAM Proxy CTS Thread") {
                        @Override
                        public void run() {
                            try (
                                var clientRead = single.getInputStream();
                                var serverWrite = socket.getOutputStream();
                            ) {
                                byte[] buffer = new byte[128*1024];
                                while (SamProxyThread.this.running) {
                                    int read = clientRead.read(buffer);
                                    if (read > 0) {
                                        serverWrite.write(buffer, 0, read);
                                    }
                                }
                            } catch (IOException ignored) {
                            }
                        }
                    };
                    Thread serverThread = new Thread("SAM Proxy STC Thread") {
                        @Override
                        public void run() {
                            try (
                                var serverRead = socket.getInputStream();
                                var clientWrite = single.getOutputStream();
                            ) {
                                byte[] buffer = new byte[128*1024];
                                while (SamProxyThread.this.running) {
                                    int read = serverRead.read(buffer);
                                    if (read > 0) {
                                        clientWrite.write(buffer, 0, read);
                                    }
                                }
                            } catch (IOException ignored) {
                            }
                        }
                    };
                    clientThread.setDaemon(true);
                    clientThread.start();
                    serverThread.setDaemon(true);
                    serverThread.start();
                    while (!this.isInterrupted() && this.running) {
                        if (control.sendPing() < 0) {
                            this.running = false;
                        }
                        try {
                            Thread.sleep(1500L);
                        } catch (InterruptedException ignored) {
                        }
                    }
                } finally {
                    this.running = false;
                }
            }
        } catch (IOException e) {
            // don't really need to do anything
        }
    }

    public void stopProxy() {
        this.running = false;
        // FIXME this is wrong, need to actually close sockets.
        this.interrupt();
    }

    public Status status() {
        return this.status;
    }

    public int port() {
        return this.localPort;
    }

    public boolean isConnecting() {
        return this.isConnecting;
    }

    public enum Status {
        IDLE,
        RUNNING,
        CONNECTION_FAILED,
        SETUP_FAILED,
        RESOLUTION_FAILED;
    }
}