Skip to content

Commit 91ad84e

Browse files
committed
Added new method for refreshing auth info
Also included the ability to handle unauthenticated requests, which is useful in debugging, and made some additonal changes to make debugging easier.
1 parent ad17e76 commit 91ad84e

File tree

7 files changed

+53
-21
lines changed

7 files changed

+53
-21
lines changed

src/main/java/com/beowulfe/hap/HomekitRoot.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void start() {
100100
webHandler.start(new HomekitClientConnectionFactoryImpl(authInfo,
101101
registry, subscriptions, advertiser)).thenAccept(port -> {
102102
try {
103-
advertiser.setDiscoverable(!authInfo.hasUser());
103+
refreshAuthInfo();
104104
advertiser.advertise(label, authInfo.getMac(), port);
105105
} catch (Exception e) {
106106
throw new RuntimeException(e);
@@ -117,6 +117,25 @@ public void stop() {
117117
started = false;
118118
}
119119

120+
/**
121+
* Refreshes auth info after it has been changed outside this library
122+
*
123+
* @throws IOException if the info cannot be read
124+
*/
125+
public void refreshAuthInfo() throws IOException {
126+
advertiser.setDiscoverable(!authInfo.hasUser());
127+
}
128+
129+
/**
130+
* By default, most homekit requests require that the client be paired. Allowing unauthenticated requests
131+
* can be useful for debugging, but should not be used in production.
132+
*
133+
* @param allow whether to allow unauthenticated requests
134+
*/
135+
public void allowUnauthenticatedRequests(boolean allow) {
136+
registry.setAllowUnauthenticatedRequests(allow);
137+
}
138+
120139
HomekitRegistry getRegistry() {
121140
return registry;
122141
}

src/main/java/com/beowulfe/hap/impl/HomekitRegistry.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class HomekitRegistry {
2424
private final Map<Integer, HomekitAccessory> accessories;
2525
private final Map<HomekitAccessory, List<Service>> services = new HashMap<>();
2626
private final Map<HomekitAccessory, Map<Integer, Characteristic>> characteristics = new HashMap<>();
27+
private boolean isAllowUnauthenticatedRequests = false;
2728

2829
public HomekitRegistry(String label) {
2930
this.label = label;
@@ -81,5 +82,13 @@ public void add(HomekitAccessory accessory) {
8182
public void remove(HomekitAccessory accessory) {
8283
accessories.remove(accessory.getId());
8384
}
85+
86+
public boolean isAllowUnauthenticatedRequests() {
87+
return isAllowUnauthenticatedRequests;
88+
}
89+
90+
public void setAllowUnauthenticatedRequests(boolean allow) {
91+
this.isAllowUnauthenticatedRequests = allow;
92+
}
8493

8594
}

src/main/java/com/beowulfe/hap/impl/connections/HttpSession.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ public HttpResponse handleRequest(HttpRequest request) throws IOException {
5454
return handlePairVerify(request);
5555

5656
default:
57-
logger.info("Unrecognized request for "+request.getUri());
58-
return new NotFoundResponse();
59-
60-
// Enable this to allow unauthenticated access. Useful for debugging.
61-
//return handleAuthenticatedRequest(request);
57+
if (registry.isAllowUnauthenticatedRequests()) {
58+
return handleAuthenticatedRequest(request);
59+
} else {
60+
logger.info("Unrecognized request for "+request.getUri());
61+
return new NotFoundResponse();
62+
}
6263
}
6364
}
6465

src/main/java/com/beowulfe/hap/impl/http/impl/BinaryHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
5757
}
5858

5959
private void traceData(String msg, byte[] b, ChannelHandlerContext ctx) throws Exception {
60-
if (logger.isTraceEnabled()) {
60+
if (logger.isTraceEnabled() && b.length > 0) {
6161
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
6262
HexDump.dump(b, 0, stream, 0);
6363
stream.flush();

src/main/java/com/beowulfe/hap/impl/http/impl/LoggingHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ public void write(ChannelHandlerContext ctx, Object msg,
3939
}
4040

4141
private void logBytes(String type, ByteBuf buf, ChannelHandlerContext ctx) throws IOException {
42-
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
43-
byte[] bytes = new byte[buf.readableBytes()];
44-
buf.getBytes(0, bytes, 0, bytes.length);
45-
HexDump.dump(bytes, 0, stream, 0);
46-
stream.flush();
47-
logger.trace(String.format("%s %s [%s]:\n%s\n", type, buf, ctx.channel().remoteAddress().toString(),
48-
stream.toString()));
42+
if (buf.readableBytes() > 0) {
43+
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
44+
byte[] bytes = new byte[buf.readableBytes()];
45+
buf.getBytes(0, bytes, 0, bytes.length);
46+
HexDump.dump(bytes, 0, stream, 0);
47+
stream.flush();
48+
logger.trace(String.format("%s %s [%s]:\n%s\n", type, buf, ctx.channel().remoteAddress().toString(),
49+
stream.toString()));
50+
}
4951
}
5052
}
5153
}

src/main/java/com/beowulfe/hap/impl/jmdns/JmdnsHomekitAdvertiser.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ public void stop() {
5353
}
5454

5555
public void setDiscoverable(boolean discoverable) throws IOException {
56-
this.discoverable = discoverable;
57-
if (isAdvertising) {
58-
logger.info("Re-creating service due to change in discoverability to "+discoverable);
59-
jmdns.unregisterAllServices();
60-
registerService();
56+
if (this.discoverable != discoverable) {
57+
this.discoverable = discoverable;
58+
if (isAdvertising) {
59+
logger.info("Re-creating service due to change in discoverability to "+discoverable);
60+
jmdns.unregisterAllServices();
61+
registerService();
62+
}
6163
}
6264
}
6365

6466
private void registerService() throws IOException {
6567
logger.info("Registering "+SERVICE_TYPE+" on port "+port);
6668
Map<String, String> props = new HashMap<>();
6769
props.put("sf", discoverable ? "1" : "0");
68-
props.put("pv", "1.0");
6970
props.put("id", mac);
7071
props.put("md", label);
7172
props.put("c#", "1");

src/main/java/com/beowulfe/hap/impl/pairing/PairingUpdateController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public HttpResponse handle(HttpRequest request) throws IOException {
3030
byte[] username = d.getBytes(MessageType.USERNAME);
3131
authInfo.removeUser(authInfo.getMac()+new String(username));
3232
if (!authInfo.hasUser()) {
33-
advertiser.setDiscoverable(false);
33+
advertiser.setDiscoverable(true);
3434
}
3535
} else {
3636
throw new RuntimeException("Unrecognized method: "+method);

0 commit comments

Comments
 (0)