Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tor: Implement HsDescEventParser #2270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bisq.tor.controller;

import bisq.tor.controller.events.events.HsDescCreatedEvent;
import bisq.tor.controller.events.events.HsDescEvent;
import bisq.tor.controller.events.events.HsDescUploadEvent;
import bisq.tor.controller.events.events.HsDescUploadedEventV2;

import java.util.Optional;

public class HsDescEventParser {
public static Optional<HsDescEvent> tryParse(String[] parts) {
if (HsDescEvent.Action.CREATED.isAction(parts)) {
// 650 HS_DESC CREATED <onion_address> UNKNOWN UNKNOWN <descriptor_id>
HsDescCreatedEvent hsDescEvent = HsDescCreatedEvent.builder()
.action(HsDescEvent.Action.CREATED)
.hsAddress(parts[3])
.authType(parts[4])
.hsDir(parts[5])
.descriptorId(parts[6])
.build();
return Optional.of(hsDescEvent);

} else if (HsDescEvent.Action.UPLOAD.isAction(parts)) {
// 650 HS_DESC UPLOAD <onion_address> UNKNOWN <hs_dir> <descriptor_id> HSDIR_INDEX=<index>
HsDescUploadEvent hsDescEvent = HsDescUploadEvent.builder()
.action(HsDescEvent.Action.UPLOAD)
.hsAddress(parts[3])
.authType(parts[4])
.hsDir(parts[5])
.descriptorId(parts[6])
.hsDirIndex(parts[7])
.build();
return Optional.of(hsDescEvent);

} else if (HsDescEvent.Action.UPLOADED.isAction(parts)) {
// 650 HS_DESC UPLOADED <onion_address> UNKNOWN <hs_dir>
HsDescUploadedEventV2 hsDescEvent = HsDescUploadedEventV2.builder()
.action(HsDescEvent.Action.UPLOADED)
.hsAddress(parts[3])
.authType(parts[4])
.hsDir(parts[5])
.build();
return Optional.of(hsDescEvent);
} else {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public class TorControlProtocol implements AutoCloseable {
private final OutputStream outputStream;

// MidReplyLine = StatusCode "-" ReplyLine
private final Pattern midReplyLinePattern = Pattern.compile("^\\d+-.+");
// DataReplyLine = StatusCode "+" ReplyLine CmdData
private final Pattern dataReplyLinePattern = Pattern.compile("^\\d+\\+.+");
private final Pattern multiLineReplyPattern = Pattern.compile("^\\d+[-+].+");

public TorControlProtocol(int port) throws IOException {
controlSocket = new Socket("127.0.0.1", port);
Expand Down Expand Up @@ -148,7 +147,7 @@ private String tryReadNextReply() {
}

private boolean isMultilineReply(String reply) {
return midReplyLinePattern.matcher(reply).matches() || dataReplyLinePattern.matcher(reply).matches();
return multiLineReplyPattern.matcher(reply).matches();
}

private String assertTwoLineOkReply(Stream<String> replyStream, String commandName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package bisq.tor.controller.events.events;

import lombok.Getter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;


@SuperBuilder
@Getter
@ToString(callSuper = true)
public class HsDescCreatedEvent extends HsDescEvent {
private final String descriptorId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bisq.tor.controller.events.events;

import lombok.Getter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
@ToString
public abstract class HsDescEvent {

@Getter
public enum Action {
CREATED(7),
UPLOAD(8),
UPLOADED(6);

private final int numberOfArgs;

Action(int numberOfArgs) {
this.numberOfArgs = numberOfArgs;
}

public boolean isAction(String[] parts) {
// Example: 650 HS_DESC CREATED <onion_address> UNKNOWN UNKNOWN <descriptor_id>
return parts[2].equals(this.toString()) && parts.length == numberOfArgs;
}
}

protected final Action action;
protected final String hsAddress;
protected final String authType;
protected final String hsDir;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bisq.tor.controller.events.events;

import lombok.Getter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
@ToString(callSuper = true)
public class HsDescUploadEvent extends HsDescCreatedEvent {
private final String hsDirIndex;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bisq.tor.controller.events.events;

import lombok.Getter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
@ToString(callSuper = true)
public class HsDescUploadedEventV2 extends HsDescEvent {
}
Loading