ssh-bridge-ai
Version:
One Command Magic SSH with Invisible Analytics - Connect to any server instantly with 'sshbridge user@server'. Zero setup, zero friction, pure magic. Industry-standard security with behind-the-scenes business intelligence.
414 lines (353 loc) • 9.61 kB
JavaScript
const crypto = require('crypto');
const fs = require('fs-extra');
const path = require('path');
/**
* Feature Gate System for SSHBridge
*
* Controls access to features based on:
* - License validation
* - Feature tiers
* - Usage quotas
* - Security level
*/
class FeatureGate {
constructor() {
this.features = new Map();
this.license = null;
this.quota = {
commands: 0,
maxCommands: 50, // Free tier default
resetDate: null
};
// Initialize feature definitions
this.initializeFeatures();
// Load license if available
this.loadLicense();
// Setup quota reset
this.setupQuotaReset();
}
/**
* Initialize feature definitions with tiers
*/
initializeFeatures() {
// Free tier features (always available)
this.features.set('basic_ssh', {
tier: 'free',
description: 'Basic SSH connections',
enabled: true,
quota: null
});
this.features.set('key_management', {
tier: 'free',
description: 'SSH key generation and management',
enabled: true,
quota: null
});
this.features.set('config_management', {
tier: 'free',
description: 'Basic configuration management',
enabled: true,
quota: null
});
// Pro tier features (require license)
this.features.set('ai_commands', {
tier: 'pro',
description: 'AI-powered command suggestions',
enabled: false,
quota: 'commands',
minQuota: 1
});
this.features.set('advanced_security', {
tier: 'pro',
description: 'Advanced security features',
enabled: false,
quota: null
});
this.features.set('enterprise_integrations', {
tier: 'pro',
description: 'Enterprise integrations (LDAP, SSO)',
enabled: false,
quota: null
});
this.features.set('audit_logging', {
tier: 'pro',
description: 'Comprehensive audit logging',
enabled: false,
quota: null
});
this.features.set('team_management', {
tier: 'pro',
description: 'Team and role management',
enabled: false,
quota: null
});
// Enterprise tier features (require enterprise license)
this.features.set('compliance_reporting', {
tier: 'enterprise',
description: 'Compliance and regulatory reporting',
enabled: false,
quota: null
});
this.features.set('advanced_analytics', {
tier: 'enterprise',
description: 'Advanced usage analytics',
enabled: false,
quota: null
});
this.features.set('custom_integrations', {
tier: 'enterprise',
description: 'Custom integration development',
enabled: false,
quota: null
});
}
/**
* Load and validate license
*/
loadLicense() {
try {
const licensePath = this.getLicensePath();
if (fs.existsSync(licensePath)) {
const licenseData = fs.readFileSync(licensePath, 'utf8');
this.license = this.validateLicense(licenseData);
if (this.license) {
this.enableFeaturesForLicense();
}
}
} catch (error) {
// Silently fail - use free tier
}
}
/**
* Get license file path
*/
getLicensePath() {
const homeDir = process.env.HOME || process.env.USERPROFILE;
return path.join(homeDir, '.sshbridge', 'license.key');
}
/**
* Validate license data
*/
validateLicense(licenseData) {
try {
// Decode license (this would be more sophisticated in production)
const decoded = Buffer.from(licenseData, 'base64').toString('utf8');
const license = JSON.parse(decoded);
// Validate license structure
if (!this.isValidLicenseStructure(license)) {
return null;
}
// Check expiration
if (license.expiresAt && new Date(license.expiresAt) < new Date()) {
return null;
}
// Validate signature (if present)
if (license.signature && !this.validateSignature(license)) {
return null;
}
return license;
} catch (error) {
return null;
}
}
/**
* Check if license has valid structure
*/
isValidLicenseStructure(license) {
return license &&
license.tier &&
license.customerId &&
license.issuedAt &&
['pro', 'enterprise'].includes(license.tier);
}
/**
* Validate license signature
*/
validateSignature(license) {
try {
// This would validate against your public key
// For now, just check if signature exists
return license.signature && license.signature.length > 0;
} catch (error) {
return false;
}
}
/**
* Enable features based on license tier
*/
enableFeaturesForLicense() {
if (!this.license) return;
const tier = this.license.tier;
// Enable features for the licensed tier
for (const [featureName, feature] of this.features) {
if (feature.tier === tier || feature.tier === 'free') {
feature.enabled = true;
}
}
// Set quota based on license
if (tier === 'pro') {
this.quota.maxCommands = 1000;
} else if (tier === 'enterprise') {
this.quota.maxCommands = 100000;
}
}
/**
* Setup quota reset (monthly)
*/
setupQuotaReset() {
const now = new Date();
const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
this.quota.resetDate = nextMonth;
// Check if we need to reset quota
if (now > this.quota.resetDate) {
this.resetQuota();
}
}
/**
* Reset usage quota
*/
resetQuota() {
this.quota.commands = 0;
this.quota.resetDate = new Date(this.quota.resetDate.getFullYear(), this.quota.resetDate.getMonth() + 1, 1);
}
/**
* Check if a feature is available
*/
isFeatureAvailable(featureName) {
const feature = this.features.get(featureName);
if (!feature) {
return false;
}
if (!feature.enabled) {
return false;
}
// Check quota if feature has one
if (feature.quota && feature.minQuota) {
return this.quota[feature.quota] >= feature.minQuota;
}
return true;
}
/**
* Use a quota (e.g., for AI commands)
*/
useQuota(quotaType, amount = 1) {
if (this.quota[quotaType] !== undefined) {
this.quota[quotaType] += amount;
// Check if quota exceeded
if (this.quota[quotaType] > this.quota.maxCommands) {
throw new Error(`Quota exceeded for ${quotaType}. Limit: ${this.quota.maxCommands}`);
}
}
}
/**
* Get feature information
*/
getFeatureInfo(featureName) {
const feature = this.features.get(featureName);
if (!feature) {
return null;
}
return {
...feature,
available: this.isFeatureAvailable(featureName),
quotaInfo: feature.quota ? {
current: this.quota[feature.quota],
max: this.quota.maxCommands,
resetDate: this.quota.resetDate
} : null
};
}
/**
* Get all features for current license
*/
getAvailableFeatures() {
const available = [];
for (const [featureName, feature] of this.features) {
if (this.isFeatureAvailable(featureName)) {
available.push({
name: featureName,
...feature
});
}
}
return available;
}
/**
* Get upgrade information
*/
getUpgradeInfo() {
const currentTier = this.license ? this.license.tier : 'free';
if (currentTier === 'enterprise') {
return null; // Already at highest tier
}
const nextTier = currentTier === 'free' ? 'pro' : 'enterprise';
const nextTierFeatures = [];
for (const [featureName, feature] of this.features) {
if (feature.tier === nextTier) {
nextTierFeatures.push({
name: featureName,
description: feature.description
});
}
}
return {
currentTier,
nextTier,
features: nextTierFeatures,
pricing: this.getPricingInfo(nextTier)
};
}
/**
* Get pricing information
*/
getPricingInfo(tier) {
const pricing = {
pro: {
monthly: 29,
yearly: 290,
features: ['AI Commands', 'Advanced Security', 'Enterprise Integrations', 'Audit Logging', 'Team Management']
},
enterprise: {
monthly: 99,
yearly: 990,
features: ['Compliance Reporting', 'Advanced Analytics', 'Custom Integrations', 'Priority Support', 'SLA Guarantee']
}
};
return pricing[tier] || null;
}
/**
* Get current license status
*/
getLicenseStatus() {
if (!this.license) {
return {
tier: 'free',
status: 'unlicensed',
features: this.getAvailableFeatures(),
quota: this.quota
};
}
return {
tier: this.license.tier,
status: 'licensed',
customerId: this.license.customerId,
issuedAt: this.license.issuedAt,
expiresAt: this.license.expiresAt,
features: this.getAvailableFeatures(),
quota: this.quota
};
}
/**
* Check if user can access enterprise features
*/
canAccessEnterpriseFeatures() {
return this.license && this.license.tier === 'enterprise';
}
/**
* Check if user can access pro features
*/
canAccessProFeatures() {
return this.license && ['pro', 'enterprise'].includes(this.license.tier);
}
}
module.exports = { FeatureGate };