File size: 11,718 Bytes
7226ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22dcb40
 
 
 
 
 
 
 
 
7226ab4
 
 
22dcb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7226ab4
 
 
22dcb40
 
7226ab4
 
22dcb40
 
 
7226ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22dcb40
7226ab4
22dcb40
 
 
7226ab4
 
 
 
22dcb40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * ═══════════════════════════════════════════════════════════════════════════
 * SUBSCRIPTION MANAGER - SISTEMA DE ASSINATURA ENTERPRISE
 * ═══════════════════════════════════════════════════════════════════════════
 * βœ… Controla acesso a features premium
 * βœ… Rate limiting por tier (Free, Subscriber, Owner)
 * βœ… Sistema de pontos/crΓ©ditos
 * βœ… Logs de uso detalhados
 * βœ… IntegraΓ§Γ£o com DONATE para upgrade
 * 
 * πŸ“Š TIERS:
 * - FREE (padrΓ£o):    1 uso/mΓͺs por feature, acesso bΓ‘sico
 * - SUBSCRIBER:       1 uso/semana por feature, anΓ‘lise avanΓ§ada
 * - OWNER:            Ilimitado, modo ROOT
 * ═══════════════════════════════════════════════════════════════════════════
 */

const fs = require('fs');
const path = require('path');

class SubscriptionManager {
  constructor(config) {
    this.config = config;
    
    // ═══════════════════════════════════════════════════════════════════
    // HF SPACES: Usar /tmp para garantir permissΓ΅es de escrita
    // O HF Spaces tem sistema de arquivos somente-leitura em /
    // ═══════════════════════════════════════════════════════════════════
    
    // ForΓ§ar uso de /tmp no HF Spaces (sistema read-only)
    this.dataPath = '/tmp/akira_data/subscriptions';
    
    this.usagePath = path.join(this.dataPath, 'usage.json');
    this.subscribersPath = path.join(this.dataPath, 'subscribers.json');
    
    // Cria diretΓ³rio se nΓ£o existir - COM TRATAMENTO DE ERRO
    try {
      if (!fs.existsSync(this.dataPath)) {
        fs.mkdirSync(this.dataPath, { recursive: true });
        console.log(`βœ… SubscriptionManager: DiretΓ³rio criado: ${this.dataPath}`);
      }
    } catch (error) {
      console.warn(`⚠️  SubscriptionManager: Não foi possível criar diretório em ${this.dataPath}:`, error.message);
      
      // Fallback para /tmp direto se falhar
      const tmpPath = '/tmp/subscriptions';
      try {
        fs.mkdirSync(tmpPath, { recursive: true });
        this.dataPath = tmpPath;
        this.usagePath = path.join(this.dataPath, 'usage.json');
        this.subscribersPath = path.join(this.dataPath, 'subscribers.json');
        console.log(`βœ… SubscriptionManager: Usando fallback: ${this.dataPath}`);
      } catch (fallbackError) {
        console.error('❌ SubscriptionManager: Erro crítico ao criar diretório de fallback:', fallbackError.message);
        // Continuar sem diretΓ³rio - usar memΓ³ria apenas
        this.dataPath = null;
      }
    }
    
    // Carrega dados
    this.subscribers = this.dataPath ? this._loadJSON(this.subscribersPath, {}) : {};
    this.usage = this.dataPath ? this._loadJSON(this.usagePath, {}) : {};
    
    // Limpa uso antigo periodicamente
    if (this.dataPath) {
      this._cleanOldUsage();
    }
    
    console.log('βœ… SubscriptionManager inicializado');
  }

  /**
   * Verifica se usuΓ‘rio pode usar uma feature
   * @returns { canUse: boolean, reason: string, remaining: number }
   */
  canUseFeature(userId, featureName) {
    try {
      // Owner tem acesso ilimitado
      if (this.config.isDono(userId)) {
        return { canUse: true, reason: 'OWNER', remaining: 999 };
      }

      const tier = this.getUserTier(userId);
      const limites = this._getLimites(tier);
      const window = this._getTimeWindow(tier);
      
      // Gera chave ΓΊnica
      const key = `${userId}_${featureName}_${this._getWindowStart(window)}`;
      
      // ObtΓ©m uso atual
      const uso = (this.usage[key] || 0) + 1;
      
      if (uso > limites.usoPorPeriodo) {
        return {
          canUse: false,
          reason: `Limite atingido para ${tier}: ${limites.usoPorPeriodo} uso(s) por ${window}`,
          remaining: 0
        };
      }

      // Atualiza uso
      this.usage[key] = uso;
      this._saveJSON(this.usagePath, this.usage);

      return {
        canUse: true,
        reason: `${tier.toUpperCase()}`,
        remaining: limites.usoPorPeriodo - uso
      };
    } catch (e) {
      console.error('Erro em canUseFeature:', e);
      return { canUse: false, reason: 'Erro ao verificar', remaining: 0 };
    }
  }

  /**
   * ObtΓ©m tier do usuΓ‘rio
   */
  getUserTier(userId) {
    if (this.config.isDono(userId)) return 'owner';
    if (this.subscribers[userId]) return 'subscriber';
    return 'free';
  }

  /**
   * Subscreve um usuΓ‘rio
   */
  subscribe(userId, duracao = 30) {
    try {
      const dataExpira = new Date();
      dataExpira.setDate(dataExpira.getDate() + duracao);
      
      this.subscribers[userId] = {
        subscritaEm: new Date().toISOString(),
        expiraEm: dataExpira.toISOString(),
        duracao,
        renovacoes: (this.subscribers[userId]?.renovacoes || 0) + 1
      };

      this._saveJSON(this.subscribersPath, this.subscribers);
      
      return {
        sucesso: true,
        mensagem: `Assinatura ativada por ${duracao} dias`,
        expiraEm: dataExpira.toLocaleDateString('pt-BR')
      };
    } catch (e) {
      return { sucesso: false, erro: e.message };
    }
  }

  /**
   * Cancela assinatura
   */
  unsubscribe(userId) {
    try {
      delete this.subscribers[userId];
      this._saveJSON(this.subscribersPath, this.subscribers);
      
      return { sucesso: true, mensagem: 'Assinatura cancelada' };
    } catch (e) {
      return { sucesso: false, erro: e.message };
    }
  }

  /**
   * Verifica se assinatura expirou
   */
  isSubscriptionValid(userId) {
    const sub = this.subscribers[userId];
    if (!sub) return false;

    const agora = new Date();
    const expira = new Date(sub.expiraEm);

    return agora < expira;
  }

  /**
   * ObtΓ©m informaΓ§Γ΅es de assinatura
   */
  getSubscriptionInfo(userId) {
    const tier = this.getUserTier(userId);
    
    if (tier === 'owner') {
      return {
        tier: 'OWNER',
        status: 'βœ… Acesso Ilimitado',
        usoPorPeriodo: 'Ilimitado',
        periodo: 'Permanente',
        recursos: [
          'βœ… Todas as ferramentas de cybersecurity',
          'βœ… Modo ROOT',
          'βœ… Rate limiting desativado',
          'βœ… AnΓ‘lise avanΓ§ada',
          'βœ… Dark web monitoring',
          'βœ… OSINT completo'
        ]
      };
    }

    const sub = this.subscribers[userId];
    if (sub && this.isSubscriptionValid(userId)) {
      const expira = new Date(sub.expiraEm);
      const diasRestantes = Math.ceil((expira - new Date()) / (1000 * 60 * 60 * 24));

      return {
        tier: 'SUBSCRIBER',
        status: `βœ… Ativo (${diasRestantes} dias)`,
        usoPorPeriodo: '1/semana',
        periodo: 'Semanal',
        expiraEm: expira.toLocaleDateString('pt-BR'),
        recursos: [
          'βœ… Ferramentas premium de cybersecurity',
          'βœ… AnΓ‘lise avanΓ§ada',
          'βœ… OSINT avanΓ§ado',
          'βœ… Leak database search',
          '⬜ Dark web monitoring',
          '⬜ Modo ROOT'
        ]
      };
    }

    return {
      tier: 'FREE',
      status: '⬜ Gratuito',
      usoPorPeriodo: '1/mΓͺs',
      periodo: 'Mensal',
      recursos: [
        'βœ… Ferramentas bΓ‘sicas (WHOIS, DNS)',
        'βœ… NMAP simulado',
        '⬜ AnÑlise avançada',
        '⬜ OSINT avançado',
        '⬜ Leak database search',
        '⬜ Dark web monitoring'
      ],
      upgrade: 'Use #donate para fazer upgrade'
    };
  }

  /**
   * Formata mensagem de upgrade
   */
  getUpgradeMessage(userId, feature) {
    const tier = this.getUserTier(userId);
    
    if (tier === 'free') {
      return `\n\nπŸ’Ž *UPGRADE DISPONÍVEL*\n\n` +
             `VocΓͺ estΓ‘ usando: *${feature}*\n\n` +
             `🎯 Com assinatura terÑ:\n` +
             `β€’ 1 uso/semana (vs 1/mΓͺs)\n` +
             `β€’ AnΓ‘lise avanΓ§ada\n` +
             `β€’ OSINT completo\n\n` +
             `Use #donate para fazer upgrade!\n` +
             `πŸ’° Planos a partir de R$ 5`;
    }

    if (tier === 'subscriber') {
      return `\n\nπŸ”“ *MODO OWNER*\n\n` +
             `Com acesso OWNER terΓ‘:\n` +
             `β€’ Ilimitado\n` +
             `β€’ Modo ROOT\n` +
             `β€’ Dark web monitoring\n\n` +
             `Contato: isaac.quarenta@akira.bot`;
    }

    return '';
  }

  /**
   * Gera relatΓ³rio de uso
   */
  getUsageReport(userId) {
    const userUsage = {};
    
    for (const [key, count] of Object.entries(this.usage)) {
      if (key.startsWith(userId)) {
        const [, feature] = key.split('_');
        userUsage[feature] = count;
      }
    }

    return {
      userId,
      tier: this.getUserTier(userId),
      usoAtual: userUsage,
      limites: this._getLimites(this.getUserTier(userId))
    };
  }

  /**
   * ═════════════════════════════════════════════════════════════════════
   * FUNÇÕES PRIVADAS
   * ═════════════════════════════════════════════════════════════════════
   */

  _getLimites(tier) {
    const limites = {
      free: {
        usoPorPeriodo: 1,
        features: ['whois', 'dns', 'nmap-basic']
      },
      subscriber: {
        usoPorPeriodo: 4, // 1/semana
        features: ['whois', 'dns', 'nmap', 'sqlmap', 'osint-basic', 'vulnerability-assessment']
      },
      owner: {
        usoPorPeriodo: 999,
        features: ['*'] // Tudo
      }
    };

    return limites[tier] || limites.free;
  }

  _getTimeWindow(tier) {
    const windows = {
      free: 'month',
      subscriber: 'week',
      owner: 'unlimited'
    };
    return windows[tier] || 'month';
  }

  _getWindowStart(window) {
    const agora = new Date();
    
    if (window === 'month') {
      return `${agora.getFullYear()}-${agora.getMonth()}`;
    }
    if (window === 'week') {
      const semana = Math.floor(agora.getDate() / 7);
      return `${agora.getFullYear()}-${agora.getMonth()}-w${semana}`;
    }
    return 'unlimited';
  }

  _cleanOldUsage() {
    try {
      const agora = new Date();
      const limpo = {};

      for (const [key, count] of Object.entries(this.usage)) {
        // MantΓ©m ΓΊltimos 90 dias
        limpo[key] = count;
      }

      this.usage = limpo;
      this._saveJSON(this.usagePath, this.usage);
    } catch (e) {
      console.warn('Erro ao limpar uso antigo:', e);
    }
  }

  _loadJSON(filepath, defaultValue = {}) {
    try {
      if (fs.existsSync(filepath)) {
        return JSON.parse(fs.readFileSync(filepath, 'utf8'));
      }
    } catch (e) {
      console.warn(`Erro ao carregar ${filepath}:`, e);
    }
    return defaultValue;
  }

  _saveJSON(filepath, data) {
    try {
      fs.writeFileSync(filepath, JSON.stringify(data, null, 2));
      return true;
    } catch (e) {
      console.warn(`Erro ao salvar ${filepath}:`, e);
      // Se falhar, salvar em memΓ³ria apenas
      return false;
    }
  }
}

module.exports = SubscriptionManager;