Skip to content

Commit

Permalink
1. Add Lombok
Browse files Browse the repository at this point in the history
2. Now update key when 1 day reimagining
3. New logic code
4. Some fix

P. S. This update not tested
  • Loading branch information
megoRU committed Dec 29, 2021
1 parent 7f8407c commit 7848552
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 109 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ Plesk trial key auto update

```
CREATE TABLE `Plesk` (
`id` int(6) NOT NULL,
`id` int(6) NOT NULL AUTO_INCREMENT,
`text` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `text` (`text`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
```
INSERT INTO Plesk (id, text) values (0, "Your Key");
INSERT INTO Plesk (id, text) values (1, "Your Key2");
INSERT INTO Plesk (id, text) values (2, "Your Key3");
INSERT INTO Plesk (text) values ("Your Key");
INSERT INTO Plesk (text) values ("Your Key2");
INSERT INTO Plesk (text) values ("Your Key3");
```

## Run on Windows:
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
<version>8.0.27</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
Expand All @@ -62,7 +62,13 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
108 changes: 5 additions & 103 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,114 +1,16 @@
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import db.ConnectionsData;
import threads.ThreadGetDate;

public class Main {

private static final HashMap<Integer, String> keys = new HashMap<>();

public static void main(String[] args) throws Exception {

if (args.length != 7) {
throw new Exception("Wrong number of arguments");
}

final String con = args[0];
final String user = args[1];
final String pass = args[2];
final String sshHost = args[3];
final String sshLogin = args[4];
final String sshPass = args[5];
final String databaseName = args[6];

try {
for (; ; ) {

String query = "SELECT id, text FROM Plesk WHERE id = 0";
String delete = "DELETE FROM Plesk WHERE id = ?";
String update = "UPDATE Plesk SET id = id - 1 WHERE id >= ?";
Connection conn = DriverManager.getConnection(
"jdbc:mysql://" + con + ":3306/" + databaseName
+ "?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", user, pass);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
String id = rs.getString("id");
String text = rs.getString("text");
keys.put(Integer.parseInt(id), text);
}
String command = "cd /; usr/sbin/plesk bin license -i " + keys.get(0).trim();
System.out.println(keys.get(0));
runCommand(command, sshPass, sshLogin, sshHost);

//remove from DB and HashMap and close connections
keys.remove(0);
PreparedStatement preparedStmt = conn.prepareStatement(delete);
PreparedStatement preparedStmt2 = conn.prepareStatement(update);
preparedStmt.setInt(1, 0);
preparedStmt2.setInt(1, 0);
preparedStmt.executeUpdate();
preparedStmt2.executeUpdate();
rs.close();
statement.close();
preparedStmt.close();
preparedStmt2.close();
conn.close();
//sleep 14 days
Thread.sleep(1209600000L);
}
} catch (Exception ex) {
Thread.currentThread().interrupt();
ex.printStackTrace();
}
}

protected static void runCommand(String command, String pass, String login, String host) {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
try {
Session session = jsch.getSession(login, host, 22);
session.setPassword(pass);
session.setConfig(config);
session.connect();
System.out.println("Connected");

Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);

InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ignored) {
Thread.currentThread().interrupt();
}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
} catch (Exception exx) {
exx.printStackTrace();
}
final ConnectionsData connectionsData = new ConnectionsData(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
Thread thread = new ThreadGetDate(connectionsData);
thread.start();
}
}
19 changes: 19 additions & 0 deletions src/main/java/db/ConnectionsData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package db;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@AllArgsConstructor
public class ConnectionsData {

private final String con;
private final String user;
private final String pass;
private final String sshHost;
private final String sshLogin;
private final String sshPass;
private final String databaseName;
}
104 changes: 104 additions & 0 deletions src/main/java/db/DataBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package db;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.AllArgsConstructor;
import model.Plesk;

import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
public class DataBase {

private final ConnectionsData connectionsData;
private static final List<Plesk> keys = new ArrayList<>();

public void getRowsInTable() {
try {
final String query = "SELECT id, text FROM Plesk";
final String delete = "DELETE FROM Plesk WHERE id = ";

Connection conn = DriverManager.getConnection(
"jdbc:mysql://" + connectionsData.getCon() + ":3306/" + connectionsData.getDatabaseName()
+ "?useSSL=false&serverTimezone=UTC&characterEncoding=utf8", connectionsData.getUser(), connectionsData.getPass());

Statement statement = conn.createStatement();

ResultSet rs = statement.executeQuery(query);

while (rs.next()) {
String id = rs.getString("id");
String text = rs.getString("text");
keys.add(new Plesk(id, text));
}

String command = "cd /; usr/sbin/plesk bin license -i " + keys.get(0).getText().trim();

System.out.println(keys.get(0).getText());

runCommand(command, connectionsData.getSshHost(), connectionsData.getSshLogin(), connectionsData.getSshPass());

PreparedStatement preparedStmt = conn.prepareStatement(delete + keys.get(0).getId());
preparedStmt.executeQuery();

//Close con
preparedStmt.close();
rs.close();
statement.close();
keys.clear();

} catch (Exception e) {
e.printStackTrace();
}
}

private void runCommand(String command, String pass, String login, String host) {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
try {
Session session = jsch.getSession(login, host, 22);
session.setPassword(pass);
session.setConfig(config);
session.connect();
System.out.println("Connected");

Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);

InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ignored) {
Thread.currentThread().interrupt();
}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
} catch (Exception exx) {
exx.printStackTrace();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/model/Plesk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Plesk {

private final String id;
private final String text;
}
Loading

0 comments on commit 7848552

Please sign in to comment.