summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
blob: 1813511f79fb2a3af0be3abc3f032519df8e0537 (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
package ganarchy.friendcode.sam;

import com.google.common.collect.ImmutableMap;

import java.io.IOException;
import java.net.*;

public class I2PSamStreamForwarder extends I2PSamStateMachine {
    private final String id;
    private final String port;
    private final SocketAddress socketAddress;

    public I2PSamStreamForwarder(SocketAddress socketAddress, String id, String port) {
        this.id = id;
        this.port = port;
        this.socketAddress = socketAddress;
    }

    @Override
    public boolean connect() {
        try {
            Socket samSocket = new Socket();
            samSocket.connect(this.socketAddress, 3000);
            return this.connect(samSocket);
        } catch (IOException e) {
            return false;
        }
    }

    public boolean start() {
        if (!super.start()) {
            return false;
        }
        try {
            this.sendCommand(new I2PSamCommand(
                "STREAM", "FORWARD",
                ImmutableMap.of(
                    "ID", this.id,
                    "PORT", this.port,
                    "SILENT", "true"
                )
            ));
            return "OK".equals(this.getCommand("STREAM", "STATUS").parameters().get("RESULT"));
        } catch (IOException e) {
            return false;
        }
    }
}