|
@@ -4,6 +4,8 @@ import bubble.ApiConstants; |
|
|
import bubble.model.account.Account; |
|
|
import bubble.model.account.Account; |
|
|
import bubble.model.device.Device; |
|
|
import bubble.model.device.Device; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.cobbzilla.util.io.FileUtil; |
|
|
|
|
|
import org.cobbzilla.util.string.Base64; |
|
|
import org.cobbzilla.wizard.stream.FileSendableResource; |
|
|
import org.cobbzilla.wizard.stream.FileSendableResource; |
|
|
import org.glassfish.jersey.server.ContainerRequest; |
|
|
import org.glassfish.jersey.server.ContainerRequest; |
|
|
|
|
|
|
|
@@ -14,13 +16,13 @@ import javax.ws.rs.Produces; |
|
|
import javax.ws.rs.core.Context; |
|
|
import javax.ws.rs.core.Context; |
|
|
import javax.ws.rs.core.Response; |
|
|
import javax.ws.rs.core.Response; |
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
import static org.cobbzilla.util.http.HttpContentTypes.*; |
|
|
import static org.cobbzilla.util.http.HttpContentTypes.*; |
|
|
import static org.cobbzilla.util.io.FileUtil.abs; |
|
|
import static org.cobbzilla.util.io.FileUtil.abs; |
|
|
import static org.cobbzilla.wizard.resources.ResourceUtil.*; |
|
|
import static org.cobbzilla.wizard.resources.ResourceUtil.*; |
|
|
|
|
|
|
|
|
@Consumes(APPLICATION_JSON) |
|
|
@Consumes(APPLICATION_JSON) |
|
|
@Produces(APPLICATION_JSON) |
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class VpnConfigResource { |
|
|
public class VpnConfigResource { |
|
|
|
|
|
|
|
@@ -30,19 +32,46 @@ public class VpnConfigResource { |
|
|
|
|
|
|
|
|
public VpnConfigResource(Device device) { this.device = device; } |
|
|
public VpnConfigResource(Device device) { this.device = device; } |
|
|
|
|
|
|
|
|
|
|
|
public File getQRfile() { |
|
|
|
|
|
final File qrFile = new File(VPN_CONFIG_PATH+device.getUuid()+".png"); |
|
|
|
|
|
if (!qrFile.exists()) { |
|
|
|
|
|
// todo: try to regenerate algo users? |
|
|
|
|
|
log.error("qrCode: file not found: "+abs(qrFile)); |
|
|
|
|
|
throw invalidEx("err.deviceQRcode.qrCodeFileNotFound"); |
|
|
|
|
|
} |
|
|
|
|
|
return qrFile; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public File getVpnConfFile() { |
|
|
|
|
|
final File confFile = new File(VPN_CONFIG_PATH+device.getUuid()+".conf"); |
|
|
|
|
|
if (!confFile.exists()) { |
|
|
|
|
|
// todo: try to regenerate algo users? |
|
|
|
|
|
log.error("confFile: file not found: "+abs(confFile)); |
|
|
|
|
|
throw invalidEx("err.deviceVpnConf.confFileNotFound"); |
|
|
|
|
|
} |
|
|
|
|
|
return confFile; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@GET @Path("/QR.png") |
|
|
@GET @Path("/QR.png") |
|
|
@Produces(IMAGE_PNG) |
|
|
@Produces(IMAGE_PNG) |
|
|
public Response qrCode(@Context ContainerRequest ctx) { |
|
|
public Response qrCode(@Context ContainerRequest ctx) { |
|
|
final Account caller = userPrincipal(ctx); |
|
|
final Account caller = userPrincipal(ctx); |
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
|
|
|
return send(new FileSendableResource(getQRfile())); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
final File qrFile = new File(VPN_CONFIG_PATH+device.getUuid()+".png"); |
|
|
|
|
|
if (!qrFile.exists()) { |
|
|
|
|
|
// todo: try to regenerate algo users? |
|
|
|
|
|
log.error("qrCode: file not found: "+abs(qrFile)); |
|
|
|
|
|
return notFound(); |
|
|
|
|
|
|
|
|
@GET @Path("/QR.png.base64") |
|
|
|
|
|
@Produces(TEXT_PLAIN) |
|
|
|
|
|
public Response qrCodeBase64(@Context ContainerRequest ctx) { |
|
|
|
|
|
final Account caller = userPrincipal(ctx); |
|
|
|
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
|
|
|
final String data; |
|
|
|
|
|
try { |
|
|
|
|
|
data = Base64.encodeBytes(FileUtil.toBytes(getQRfile())); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
return invalid("err.deviceQRcode.qrCodeError"); |
|
|
} |
|
|
} |
|
|
return send(new FileSendableResource(qrFile)); |
|
|
|
|
|
|
|
|
return ok(data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@GET @Path("/vpn.conf") |
|
|
@GET @Path("/vpn.conf") |
|
@@ -51,15 +80,25 @@ public class VpnConfigResource { |
|
|
final Account caller = userPrincipal(ctx); |
|
|
final Account caller = userPrincipal(ctx); |
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
|
|
|
|
|
|
final File confFile = new File(VPN_CONFIG_PATH+device.getUuid()+".conf"); |
|
|
|
|
|
if (!confFile.exists()) { |
|
|
|
|
|
// todo: try to regenerate algo users? |
|
|
|
|
|
log.error("confFile: file not found: "+abs(confFile)); |
|
|
|
|
|
return notFound(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
final File confFile = getVpnConfFile(); |
|
|
return send(new FileSendableResource(confFile) |
|
|
return send(new FileSendableResource(confFile) |
|
|
.setContentType(APPLICATION_OCTET_STREAM) |
|
|
.setContentType(APPLICATION_OCTET_STREAM) |
|
|
.setForceDownload(true)); |
|
|
.setForceDownload(true)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GET @Path("/vpn.conf.base64") |
|
|
|
|
|
@Produces(TEXT_PLAIN) |
|
|
|
|
|
public Response confFileBase64(@Context ContainerRequest ctx) { |
|
|
|
|
|
final Account caller = userPrincipal(ctx); |
|
|
|
|
|
if (!caller.admin() && !caller.getUuid().equals(device.getAccount())) return forbidden(); |
|
|
|
|
|
|
|
|
|
|
|
final String data; |
|
|
|
|
|
try { |
|
|
|
|
|
data = Base64.encodeBytes(FileUtil.toBytes(getVpnConfFile())); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
return invalid("err.deviceVpnConf.confError"); |
|
|
|
|
|
} |
|
|
|
|
|
return ok(data); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |