Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
767646feae | |||
c28694c878 | |||
18ce6548f6 | |||
b41505a2b6 | |||
091bb8ac4e | |||
d6f8e2cbcf | |||
9c8fec5fd6 | |||
2c08a08003 | |||
1bcdae67f2 | |||
2a5351484a | |||
982ff08f11 | |||
05cc42b240 |
@ -16,6 +16,9 @@ import oshi.hardware.HardwareAbstractionLayer;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -85,14 +88,21 @@ public final class LicenseExample {
|
||||
JsonElement description = json.get("description");
|
||||
JsonElement ownerSnowflake = json.get("ownerSnowflake");
|
||||
JsonElement ownerName = json.get("ownerName");
|
||||
JsonElement duration = json.get("duration");
|
||||
|
||||
// Parsing the expiration date if we have one
|
||||
JsonElement expires = json.get("expires");
|
||||
Date expiresDate = null;
|
||||
if (!expires.isJsonNull()) {
|
||||
OffsetDateTime offsetDateTime = OffsetDateTime.parse(expires.getAsString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
expiresDate = Date.from(offsetDateTime.toInstant());
|
||||
}
|
||||
|
||||
// Return the license response
|
||||
return new LicenseResponse(200, null,
|
||||
description.isJsonNull() ? null : description.getAsString(),
|
||||
ownerSnowflake.isJsonNull() ? -1 : ownerSnowflake.getAsLong(),
|
||||
ownerName.isJsonNull() ? null : ownerName.getAsString(),
|
||||
duration.isJsonNull() ? -1 : duration.getAsLong()
|
||||
expires.isJsonNull() ? null : expiresDate
|
||||
);
|
||||
} else {
|
||||
ResponseBody errorBody = response.body(); // Get the error body
|
||||
@ -177,12 +187,9 @@ public final class LicenseExample {
|
||||
private String ownerName;
|
||||
|
||||
/**
|
||||
* The duration of the license, present if valid.
|
||||
* <p>
|
||||
* If -1, the license will be permanent.
|
||||
* </p>
|
||||
* The optional expiration {@link Date} of the license.
|
||||
*/
|
||||
private long duration;
|
||||
private Date expires;
|
||||
|
||||
public LicenseResponse(long status, @NonNull String error) {
|
||||
this.status = status;
|
||||
@ -204,7 +211,7 @@ public final class LicenseExample {
|
||||
* @return true if permanent, otherwise false
|
||||
*/
|
||||
public boolean isPermanent() {
|
||||
return duration == -1;
|
||||
return expires == null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,11 @@
|
||||
package me.braydon.example;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Braydon
|
||||
*/
|
||||
public final class Main {
|
||||
public static void main(String[] args) {
|
||||
LicenseExample.LicenseResponse response = LicenseExample.check("XXXX-XXXX-XXXX-XXXX", "Example");
|
||||
LicenseExample.LicenseResponse response = LicenseExample.check("C45E-40F6-924C-753B", "CloudSpigot");
|
||||
if (!response.isValid()) { // License isn't valid
|
||||
System.err.println("Invalid license: " + response.getError());
|
||||
return;
|
||||
@ -22,9 +20,8 @@ public final class Main {
|
||||
}
|
||||
if (response.isPermanent()) { // License is permanent
|
||||
System.out.println("Your license is permanent");
|
||||
} else { // License has a duration
|
||||
long durationSeconds = TimeUnit.SECONDS.toMillis(response.getDuration()); // The duration in seconds
|
||||
System.out.println("Your license will expire in " + durationSeconds + " seconds");
|
||||
} else { // License has an expiration date
|
||||
System.out.printf("Your license will expire at: %s%n", response.getExpires().toInstant());
|
||||
}
|
||||
}
|
||||
}
|
64
README.md
64
README.md
@ -1,3 +1,65 @@
|
||||
# LicenseServer
|
||||
|
||||
A simple open-source licensing server for your products.
|
||||
A simple open-source licensing server for your products.
|
||||
|
||||
## Discord Preview
|
||||
|
||||
![License Usage Log](https://cdn.rainnny.club/SagsCD0I.png)
|
||||
|
||||
## API Reference
|
||||
|
||||
### Check License
|
||||
|
||||
```http
|
||||
POST /check
|
||||
```
|
||||
|
||||
#### Body
|
||||
|
||||
| Key | Type | Description |
|
||||
|:----------|:---------|:-----------------------------------------------|
|
||||
| `key` | `string` | **Required**. Your license key |
|
||||
| `product` | `string` | **Required**. The product the license is for |
|
||||
| `hwid` | `string` | **Required**. The hardware id of the requester |
|
||||
|
||||
#### Response
|
||||
|
||||
##### Error
|
||||
|
||||
```json
|
||||
{
|
||||
"error": "Error message"
|
||||
}
|
||||
```
|
||||
|
||||
##### Success
|
||||
|
||||
```json
|
||||
{
|
||||
"description": "Testing",
|
||||
"ownerSnowflake": 504147739131641857,
|
||||
"ownerName": "Braydon#2712",
|
||||
"duration": -1
|
||||
}
|
||||
```
|
||||
|
||||
## Deployment
|
||||
|
||||
### Docker
|
||||
|
||||
```bash
|
||||
docker run -d -p 7500:7500 -v "$(pwd)/data/application.yml:/usr/local/app/application.yml" git.rainnny.club/rainnny/licenseserver:latest
|
||||
```
|
||||
|
||||
### Docker Compose
|
||||
|
||||
```yml
|
||||
version: '3'
|
||||
services:
|
||||
app:
|
||||
image: git.rainnny.club/rainnny/licenseserver:latest
|
||||
volumes:
|
||||
- ./data/application.yml:/usr/local/app/application.yml
|
||||
ports:
|
||||
- "7500:7500"
|
||||
```
|
@ -1,8 +0,0 @@
|
||||
version: '3'
|
||||
services:
|
||||
app:
|
||||
image: git.rainnny.club/rainnny/licenseserver:latest
|
||||
volumes:
|
||||
- ./data:/usr/local/app
|
||||
ports:
|
||||
- "7500:7500"
|
2
pom.xml
2
pom.xml
@ -12,7 +12,7 @@
|
||||
|
||||
<groupId>me.braydon</groupId>
|
||||
<artifactId>LicenseServer</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
<description>A simple open-source licensing server for your products.</description>
|
||||
|
||||
<properties>
|
||||
|
@ -74,7 +74,7 @@ public final class LicenseController {
|
||||
license.getDescription(),
|
||||
license.getOwnerSnowflake(),
|
||||
license.getOwnerName(),
|
||||
license.getDuration()
|
||||
license.getExpires()
|
||||
));
|
||||
} catch (APIException ex) { // Handle the exception
|
||||
return ResponseEntity.status(ex.getStatus())
|
||||
|
@ -5,6 +5,8 @@ import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import me.braydon.license.model.License;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* A data transfer object for a {@link License}.
|
||||
*
|
||||
@ -34,10 +36,7 @@ public class LicenseDTO {
|
||||
private String ownerName;
|
||||
|
||||
/**
|
||||
* The duration that this licensee is valid for.
|
||||
* <p>
|
||||
* If -1, the license will be permanent.
|
||||
* </p>
|
||||
* The optional expiration {@link Date} of this license.
|
||||
*/
|
||||
private long duration;
|
||||
private Date expires;
|
||||
}
|
||||
|
@ -80,12 +80,9 @@ public class License {
|
||||
private int hwidLimit;
|
||||
|
||||
/**
|
||||
* The duration that this licensee is valid for.
|
||||
* <p>
|
||||
* If -1, the license will be permanent.
|
||||
* </p>
|
||||
* The optional expiration {@link Date} of this license.
|
||||
*/
|
||||
private long duration;
|
||||
private Date expires;
|
||||
|
||||
/**
|
||||
* The {@link Date} this license was last used.
|
||||
@ -113,7 +110,7 @@ public class License {
|
||||
return false;
|
||||
}
|
||||
// Check if the license has expired
|
||||
return System.currentTimeMillis() - created.getTime() >= duration;
|
||||
return expires.before(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,7 +119,7 @@ public class License {
|
||||
* @return true if permanent, otherwise false
|
||||
*/
|
||||
public boolean isPermanent() {
|
||||
return duration == -1L;
|
||||
return expires == null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,12 +6,16 @@ import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.braydon.license.common.TimeUtils;
|
||||
import me.braydon.license.model.License;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.OnlineStatus;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
|
||||
import net.dv8tion.jda.api.requests.ErrorResponse;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -33,6 +37,7 @@ public final class DiscordService {
|
||||
*/
|
||||
@Value("${spring.application.name}")
|
||||
@NonNull private String applicationName;
|
||||
|
||||
/**
|
||||
* The token to the Discord bot.
|
||||
*/
|
||||
@ -69,6 +74,24 @@ public final class DiscordService {
|
||||
@Value("${discord.logs.expired}") @Getter
|
||||
private boolean logHwidLimitExceeded;
|
||||
|
||||
/**
|
||||
* Should new IPs be sent to the license owner?
|
||||
*/
|
||||
@Value("${discord.owner-logs.newIp}") @Getter
|
||||
private boolean logNewIpsToOwner;
|
||||
|
||||
/**
|
||||
* Should new HWIDs be sent to the license owner?
|
||||
*/
|
||||
@Value("${discord.owner-logs.newHwid}") @Getter
|
||||
private boolean logNewHwidsToOwner;
|
||||
|
||||
/**
|
||||
* Should the license owner be notified when their license is about to expire?
|
||||
*/
|
||||
@Value("${discord.owner-logs.expiringSoon}") @Getter
|
||||
private boolean logExpiringSoonToOwner;
|
||||
|
||||
/**
|
||||
* The {@link JDA} instance of the bot.
|
||||
*/
|
||||
@ -125,9 +148,33 @@ public final class DiscordService {
|
||||
throw new IllegalArgumentException("Log channel %s wasn't found".formatted(logsChannel));
|
||||
}
|
||||
// Send the log
|
||||
textChannel.sendMessageEmbeds(embed.setFooter("%s v%s - %s".formatted(
|
||||
applicationName, applicationVersion, TimeUtils.dateTime()
|
||||
)).build()).queue();
|
||||
textChannel.sendMessageEmbeds(buildEmbed(embed)).queue();
|
||||
}
|
||||
|
||||
public void sendOwnerLog(@NonNull License license, @NonNull EmbedBuilder embed) {
|
||||
// We need an owner for the license
|
||||
if (license.getOwnerSnowflake() <= 0L) {
|
||||
return;
|
||||
}
|
||||
// Lookup the owner of the license
|
||||
jda.retrieveUserById(license.getOwnerSnowflake()).queue(owner -> {
|
||||
if (owner == null) { // Couldn't locate the owner of the license
|
||||
return;
|
||||
}
|
||||
owner.openPrivateChannel().queue(channel -> {
|
||||
channel.sendMessageEmbeds(buildEmbed(embed)).queue(null, ex -> {
|
||||
// Ignore the ex if the owner has priv msgs turned off, we don't care
|
||||
if (((ErrorResponseException) ex).getErrorResponse() != ErrorResponse.CANNOT_SEND_TO_USER) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
});
|
||||
}, ex -> {
|
||||
// Ignore the ex if the owner isn't found, we don't care
|
||||
if (((ErrorResponseException) ex).getErrorResponse() != ErrorResponse.UNKNOWN_USER) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,4 +185,17 @@ public final class DiscordService {
|
||||
public boolean isReady() {
|
||||
return jda != null && (jda.getStatus() == JDA.Status.CONNECTED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the given embed.
|
||||
*
|
||||
* @param embedBuilder the embed builder
|
||||
* @return the built embed
|
||||
*/
|
||||
@NonNull
|
||||
private MessageEmbed buildEmbed(@NonNull EmbedBuilder embedBuilder) {
|
||||
return embedBuilder.setFooter("%s v%s - %s".formatted(
|
||||
applicationName, applicationVersion, TimeUtils.dateTime()
|
||||
)).build();
|
||||
}
|
||||
}
|
@ -63,12 +63,12 @@ public final class LicenseService {
|
||||
* @param ownerName the optional owner name of the license
|
||||
* @param ipLimit the IP limit of the license
|
||||
* @param hwidLimit the HWID limit of the license
|
||||
* @param duration the duration of the license, -1 for permanent
|
||||
* @param expires the optional expiration date of the license
|
||||
* @return the created license
|
||||
* @see License for license
|
||||
*/
|
||||
public License create(@NonNull String key, @NonNull String product, String description, long ownerSnowflake,
|
||||
String ownerName, int ipLimit, int hwidLimit, long duration) {
|
||||
String ownerName, int ipLimit, int hwidLimit, Date expires) {
|
||||
// Create the new license
|
||||
License license = new License();
|
||||
license.setKey(BCrypt.hashpw(key, licensesSalt)); // Hash the key
|
||||
@ -80,7 +80,7 @@ public final class LicenseService {
|
||||
license.setHwids(new HashSet<>());
|
||||
license.setIpLimit(ipLimit); // Use the given IP limit
|
||||
license.setHwidLimit(hwidLimit); // Use the given HWID limit
|
||||
license.setDuration(duration);
|
||||
license.setExpires(expires);
|
||||
license.setCreated(new Date());
|
||||
repository.insert(license); // Insert the newly created license
|
||||
return license;
|
||||
@ -107,12 +107,13 @@ public final class LicenseService {
|
||||
}
|
||||
License license = optionalLicense.get(); // The license found
|
||||
String hashedIp = BCrypt.hashpw(ip, ipsSalt); // Hash the IP
|
||||
String obfuscateKey = MiscUtils.obfuscateKey(key); // Obfuscate the key
|
||||
boolean newIp = !license.getIps().contains(hashedIp); // Is the IP new?
|
||||
boolean newHwid = !license.getHwids().contains(hwid); // Is the HWID new?
|
||||
|
||||
// Log the license being used, if enabled
|
||||
if (discordService.isLogUses()) {
|
||||
// god i hate sending discord embeds, it's so big and ugly :(
|
||||
boolean newIp = !license.getIps().contains(hashedIp); // Is the IP new?
|
||||
boolean newHwid = !license.getHwids().contains(hwid); // Is the HWID new?
|
||||
|
||||
// Constructing tags
|
||||
StringBuilder tags = new StringBuilder();
|
||||
@ -125,22 +126,15 @@ public final class LicenseService {
|
||||
}
|
||||
tags.append("HWID");
|
||||
}
|
||||
long expirationDate = (license.getCreated().getTime() + license.getDuration()) / 1000L;
|
||||
long expires = license.isPermanent() ? -1L : license.getExpires().getTime() / 1000L;
|
||||
int ipCount = license.getIps().size();
|
||||
int hwidCount = license.getHwids().size();
|
||||
discordService.sendLog(new EmbedBuilder()
|
||||
.setColor(Color.BLUE)
|
||||
.setTitle("License Used" + (!tags.isEmpty() ? " (" + tags + ")" : ""))
|
||||
.addField("License",
|
||||
"`" + MiscUtils.obfuscateKey(key) + "`",
|
||||
true
|
||||
)
|
||||
.addField("Product",
|
||||
license.getProduct(),
|
||||
true
|
||||
)
|
||||
.addField("Description",
|
||||
license.getDescription(),
|
||||
true
|
||||
)
|
||||
.addField("License", "`" + obfuscateKey + "`", true)
|
||||
.addField("Product", license.getProduct(), true)
|
||||
.addField("Description", license.getDescription(), true)
|
||||
.addField("Owner ID",
|
||||
license.getOwnerSnowflake() <= 0L ? "N/A" : String.valueOf(license.getOwnerSnowflake()),
|
||||
true
|
||||
@ -150,23 +144,17 @@ public final class LicenseService {
|
||||
true
|
||||
)
|
||||
.addField("Expires",
|
||||
license.isPermanent() ? "Never" : "<t:" + expirationDate + ":R>",
|
||||
expires == -1L ? "Never" : "<t:" + expires + ":R>",
|
||||
true
|
||||
)
|
||||
.addField("IP",
|
||||
ip,
|
||||
true
|
||||
)
|
||||
.addField("HWID",
|
||||
"```" + hwid + "```",
|
||||
false
|
||||
)
|
||||
.addField("IP", ip, true)
|
||||
.addField("HWID", "```" + hwid + "```", false)
|
||||
.addField("IPs",
|
||||
license.getIps().size() + "/" + license.getIpLimit(),
|
||||
(newIp ? ipCount + 1 : ipCount) + "/" + license.getIpLimit(),
|
||||
true
|
||||
)
|
||||
.addField("HWIDs",
|
||||
license.getHwids().size() + "/" + license.getHwidLimit(),
|
||||
(newHwid ? hwidCount + 1 : hwidCount) + "/" + license.getHwidLimit(),
|
||||
true
|
||||
)
|
||||
);
|
||||
@ -178,13 +166,37 @@ public final class LicenseService {
|
||||
discordService.sendLog(new EmbedBuilder()
|
||||
.setColor(Color.RED)
|
||||
.setTitle("License Expired")
|
||||
.setDescription("License `%s` is expired".formatted(MiscUtils.obfuscateKey(key)))
|
||||
.setDescription("License `%s` is expired".formatted(obfuscateKey))
|
||||
);
|
||||
}
|
||||
throw new LicenseExpiredException();
|
||||
}
|
||||
|
||||
// Sending new IP log to the license owner
|
||||
if (newIp && discordService.isLogNewIpsToOwner()) {
|
||||
discordService.sendOwnerLog(license, new EmbedBuilder()
|
||||
.setColor(0xF2781B)
|
||||
.setTitle("New IP")
|
||||
.setDescription("One of your licenses has been used on a new IP:")
|
||||
.addField("License", "`" + obfuscateKey + "`", true)
|
||||
.addField("Product", license.getProduct(), true)
|
||||
.addField("IP", "```" + ip + "```", false)
|
||||
);
|
||||
}
|
||||
// Sending new HWID log to the license owner
|
||||
if (newHwid && discordService.isLogNewHwidsToOwner()) {
|
||||
discordService.sendOwnerLog(license, new EmbedBuilder()
|
||||
.setColor(0xF2781B)
|
||||
.setTitle("New HWID")
|
||||
.setDescription("One of your licenses has been used on a new HWID:")
|
||||
.addField("License", "`" + obfuscateKey + "`", true)
|
||||
.addField("Product", license.getProduct(), true)
|
||||
.addField("HWID", "```" + hwid + "```", false)
|
||||
);
|
||||
}
|
||||
// Use the license
|
||||
try {
|
||||
license.use(hashedIp, hwid); // Use the license
|
||||
license.use(hashedIp, hwid);
|
||||
repository.save(license); // Save the used license
|
||||
log.info("License key '{}' for product '{}' was used by {} (HWID: {})", key, product, ip, hwid);
|
||||
return license;
|
||||
@ -195,7 +207,7 @@ public final class LicenseService {
|
||||
.setColor(Color.RED)
|
||||
.setTitle("License IP Limit Reached")
|
||||
.setDescription("License `%s` has reached it's IP limit: **%s**".formatted(
|
||||
MiscUtils.obfuscateKey(key),
|
||||
obfuscateKey,
|
||||
license.getIpLimit()
|
||||
))
|
||||
);
|
||||
@ -204,7 +216,7 @@ public final class LicenseService {
|
||||
.setColor(Color.RED)
|
||||
.setTitle("License HWID Limit Reached")
|
||||
.setDescription("License `%s` has reached it's HWID limit: **%s**".formatted(
|
||||
MiscUtils.obfuscateKey(key),
|
||||
obfuscateKey,
|
||||
license.getHwidLimit()
|
||||
))
|
||||
);
|
||||
|
@ -12,6 +12,8 @@ salts:
|
||||
# Discord Bot Configuration
|
||||
discord:
|
||||
token: ""
|
||||
|
||||
# Global Logs
|
||||
logs:
|
||||
channel: 0 # The channel ID to log to, leave as 0 to disable
|
||||
uses: true # Should used licenses be logged?
|
||||
@ -19,6 +21,11 @@ discord:
|
||||
ipLimitExceeded: true # Should IP limited licenses be logged when used?
|
||||
hwidLimitExceeded: true # Should HWID limited licenses be logged when used?
|
||||
|
||||
# License Owner Logs
|
||||
owner-logs:
|
||||
newIp: true # Should new IPs be sent to the license owner?
|
||||
newHwid: true # Should new HWIDs be sent to the license owner?
|
||||
|
||||
# Log Configuration
|
||||
logging:
|
||||
file:
|
||||
|
Reference in New Issue
Block a user