@ufdevsllc/authme2.0
Version:
SDK for license management and remote monitoring with automatic system tracking, license validation, and remote control capabilities
145 lines (139 loc) • 4.59 kB
JavaScript
import mongoose from 'mongoose';
const systemTrackingSchema = new mongoose.Schema({
licenseKey: {
type: String,
required: [true, 'License key is required'],
trim: true,
ref: 'License'
},
systemInfo: {
os: {
type: String,
required: [true, 'Operating system is required'],
trim: true,
maxlength: [50, 'OS name cannot exceed 50 characters']
},
platform: {
type: String,
required: [true, 'Platform is required'],
trim: true,
maxlength: [50, 'Platform cannot exceed 50 characters']
},
arch: {
type: String,
required: [true, 'Architecture is required'],
trim: true,
maxlength: [20, 'Architecture cannot exceed 20 characters']
},
memory: {
type: Number,
required: [true, 'Memory information is required'],
min: [0, 'Memory cannot be negative']
},
cpu: {
model: {
type: String,
trim: true,
maxlength: [100, 'CPU model cannot exceed 100 characters']
},
cores: {
type: Number,
min: [1, 'CPU cores must be at least 1'],
max: [256, 'CPU cores cannot exceed 256']
},
speed: {
type: Number,
min: [0, 'CPU speed cannot be negative']
}
}
},
deploymentInfo: {
ipAddress: {
type: String,
required: [true, 'IP address is required'],
trim: true,
validate: {
validator: function (value) {
// Basic IP validation (IPv4 and IPv6)
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const ipv6Regex = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
return ipv4Regex.test(value) || ipv6Regex.test(value);
},
message: 'Invalid IP address format'
}
},
serverLocation: {
type: String,
trim: true,
maxlength: [100, 'Server location cannot exceed 100 characters']
},
environment: {
type: String,
required: [true, 'Environment is required'],
enum: {
values: ['development', 'staging', 'production', 'testing'],
message: 'Environment must be one of: development, staging, production, testing'
}
},
timestamp: {
type: Date,
default: Date.now,
required: true
}
},
corsSettings: {
allowedOrigins: [{
type: String,
trim: true,
maxlength: [200, 'Origin cannot exceed 200 characters']
}],
methods: [{
type: String,
trim: true,
enum: {
values: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'],
message: 'Invalid HTTP method'
}
}],
headers: [{
type: String,
trim: true,
maxlength: [100, 'Header name cannot exceed 100 characters']
}]
},
lastSeen: {
type: Date,
default: Date.now,
required: true
},
isActive: {
type: Boolean,
default: true,
required: true
}
}, {
timestamps: true,
collection: 'systemTracking'
});
// Indexes for efficient queries
systemTrackingSchema.index({ licenseKey: 1 });
systemTrackingSchema.index({ 'deploymentInfo.ipAddress': 1 });
systemTrackingSchema.index({ isActive: 1 });
systemTrackingSchema.index({ lastSeen: 1 });
systemTrackingSchema.index({ 'deploymentInfo.environment': 1 });
// Virtual to check if system is recently active (within last 24 hours)
systemTrackingSchema.virtual('isRecentlyActive').get(function () {
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
return this.lastSeen > twentyFourHoursAgo;
});
// Method to update last seen timestamp
systemTrackingSchema.methods.updateLastSeen = function () {
this.lastSeen = new Date();
return this.save();
};
// Method to deactivate system tracking
systemTrackingSchema.methods.deactivate = function () {
this.isActive = false;
return this.save();
};
export default mongoose.model('SystemTracking', systemTrackingSchema);