Skip to content

Commit

Permalink
Merge pull request #2 from Ktilis:main
Browse files Browse the repository at this point in the history
Thanks to Ktilis for this amazing PR#2!
Remapping config strings & unique links for skin files by md5
  • Loading branch information
ms0ur authored Jun 30, 2024
2 parents 2e1dc52 + 9bbd88f commit c8a5133
Show file tree
Hide file tree
Showing 25 changed files with 423 additions and 71 deletions.
25 changes: 17 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"mysql2": "^3.5.2",
"rcon-client": "^4.2.3",
"rimraf": "^5.0.1",
"sharp": "^0.32.4"
"sharp": "^0.32.4",
"ts-md5": "^1.3.1"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
Expand Down
28 changes: 20 additions & 8 deletions src/config/mysql.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
{
"mysql": {
"host": "localhost",
"port": 3310,
"port": 3306,
"user":{
"name": "root",
"password": "root"
"name": "test",
"password": "test"
},
"database": {
"name": "newAuth",
"table": "AUTH",
"columns": {
"username": "NICKNAME",
"password": "HASH"
"name": "hc2_test",
"tables": {
"players": {
"name": "AUTH",
"columns": {
"username": "NICKNAME",
"password": "HASH"
}
},
"skins": {
"name": "SKINS_CACHE",
"columns": {
"username": "NICKNAME",
"hash": "SKIN_HASH",
"last_updated": "LAST_UPDATED"
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/skinSystem.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"skins":{
"path": "C:\\Users\\zxcvb\\Downloads\\ttemp",
"path": "/user/helicraft/skins",
"nameFormat": "%s",
"type": "png"
}
Expand Down
20 changes: 16 additions & 4 deletions src/database/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ database = {
},
database: {
name: mysqlConfig.mysql.database.name,
table: mysqlConfig.mysql.database.table,
columns: {
username: mysqlConfig.mysql.database.columns.username,
password: mysqlConfig.mysql.database.columns.password
tables: {
players: {
name: mysqlConfig.mysql.database.tables.players.name,
columns: {
username: mysqlConfig.mysql.database.tables.players.columns.username,
password: mysqlConfig.mysql.database.tables.players.columns.password
}
},
skins: {
name: mysqlConfig.mysql.database.tables.skins.name,
columns: {
username: mysqlConfig.mysql.database.tables.skins.columns.username,
hash: mysqlConfig.mysql.database.tables.skins.columns.hash,
last_updated: mysqlConfig.mysql.database.tables.skins.columns.last_updated
}
}
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/database/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ interface dbConfig {
},
database: {
name: string,
table: string,
columns: {
username: string,
password: string
tables: {
players: {
name: string,
columns: {
username: string,
password: string
}
},
skins: {
name: string,
columns: {
username: string,
hash: string,
last_updated: string
}
}
}
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/database/functions/changePassword.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

import database from '../database.config';
import connection from './mysqlConnect';

export default async (username: string, HASH: string): Promise<boolean> => {
try {
const result = await connection.execute(
`UPDATE ${database.database.table} SET ${database.database.columns.password} = ? WHERE ${database.database.columns.username} = ?`,
`UPDATE ${database.database.tables.players.name} SET ${database.database.tables.players.columns.password} = ? WHERE ${database.database.tables.players.columns.username} = ?`,
[HASH, username]
);

Expand Down
2 changes: 1 addition & 1 deletion src/database/functions/checkPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function givePassword(username: string): Promise<string> {
throw new Error('User not found');
}

const passwordProperty = database.database.columns.password;
const passwordProperty = database.database.tables.players.columns.password;
const HASH = user[passwordProperty];

if (!HASH) {
Expand Down
6 changes: 4 additions & 2 deletions src/database/functions/createUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mysql from 'mysql2';
import database from '../database.config';
import connection from './mysqlConnect';

Expand All @@ -12,7 +11,10 @@ import connection from './mysqlConnect';
export default async function createUser(username: string, password: string): Promise<boolean> {
try {
const result = await connection.execute(
`INSERT INTO ${database.database.table} (${database.database.columns.username}, ${database.database.columns.password}) VALUES (?, ?)`,
`INSERT INTO ${database.database.tables.players.name}
(${database.database.tables.players.columns.username},
${database.database.tables.players.columns.password})
VALUES (?, ?)`,
[username, password],
);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/database/functions/findUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function getUserByUsername(username: string): Promise<mysql
try {
const result = await new Promise<mysql.RowDataPacket[]>((resolve, reject) => {
connection.execute(
`SELECT * FROM ${database.database.table} WHERE ${database.database.columns.username} = ?`,
`SELECT * FROM ${database.database.tables.players.name} WHERE ${database.database.tables.players.columns.username} = ?`,
[username],
(err, result: mysql.RowDataPacket[]) => {
if (err) {
Expand Down
26 changes: 26 additions & 0 deletions src/database/functions/skins/addNewSkin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import database from '../../database.config';
import connection from '../mysqlConnect';

/**
* Creates a new user in the database with the given username and hash.
*
* @param {string} username - The username of the new user.
* @param {string} hash - The password of the new user.
* @return {Promise<boolean>} A promise that resolves to true if the user was created successfully, or false otherwise.
*/
export default async function addNewSkin(username: string, hash: string, last_updated: string): Promise<boolean> {
try {
const result = await connection.execute(
`INSERT INTO ${database.database.tables.skins.name}
(${database.database.tables.skins.columns.username},
${database.database.tables.skins.columns.hash},
${database.database.tables.skins.columns.last_updated})
VALUES (?, ?, ?)`,
[username, hash, last_updated],
);
return true;
} catch (error) {
console.error('Error creating user:', error);
return false;
}
}
38 changes: 38 additions & 0 deletions src/database/functions/skins/findUserByUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import mysql from 'mysql2';
import database from '../../database.config';
import connection from '../mysqlConnect';

/**
* Is user exists
*
* @param {string} username - The username of the user to find.
* @return {Promise<mysql.RowDataPacket | null>} A promise that resolves true if the user was found, or null if not found.
*/
export default async function isUserExists(username: string): Promise<boolean> {
try {
const result = await new Promise<mysql.RowDataPacket[]>((resolve, reject) => {
connection.execute(
`SELECT *
FROM ${database.database.tables.skins.name}
WHERE ${database.database.tables.skins.columns.username} = ?`,
[username],
(err, result: mysql.RowDataPacket[]) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(result);
}
}
);
});

if (result.length > 0) {
return true;
} else {
return false;
}
} catch (error) {
throw error;
}
}
38 changes: 38 additions & 0 deletions src/database/functions/skins/getHashByUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import mysql from 'mysql2';
import database from '../../database.config';
import connection from '../mysqlConnect';

/**
* Returning player's skin hash
*
* @param {string} hash - The skin hash of the user to find.
* @return {Promise<string | null>} A promise that resolves with the hash if found, or null if not found.
*/
export default async function getHashByUsername(username: string): Promise<string | null> {
try {
const result = await new Promise<mysql.RowDataPacket[]>((resolve, reject) => {
connection.execute(
`SELECT ${database.database.tables.skins.columns.hash}
FROM ${database.database.tables.skins.name}
WHERE ${database.database.tables.skins.columns.username} = ?`,
[username],
(err, result: mysql.RowDataPacket[]) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(result);
}
}
);
});

if (result.length > 0) {
return result[0][database.database.tables.skins.columns.hash];
} else {
return null;
}
} catch (error) {
throw error;
}
}
38 changes: 38 additions & 0 deletions src/database/functions/skins/getUsernameByHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import mysql from 'mysql2';
import database from '../../database.config';
import connection from '../mysqlConnect';

/**
* Returning player's skin hash
*
* @param {string} hash - The skin hash of the user to find.
* @return {Promise<string | null>} A promise that resolves with the hash if found, or null if not found.
*/
export default async function getUserBySkinHash(hash: string): Promise<string | null> {
try {
const result = await new Promise<mysql.RowDataPacket[]>((resolve, reject) => {
connection.execute(
`SELECT ${database.database.tables.skins.columns.username}
FROM ${database.database.tables.skins.name}
WHERE ${database.database.tables.skins.columns.hash} = ?`,
[hash],
(err, result: mysql.RowDataPacket[]) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(result);
}
}
);
});

if (result.length > 0) {
return result[0][database.database.tables.skins.columns.username];
} else {
return null;
}
} catch (error) {
throw error;
}
}
20 changes: 20 additions & 0 deletions src/database/functions/skins/updateUserSkinHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import database from '../../database.config';
import connection from '../mysqlConnect';

export default async function updateUserSkinHash(username: string, hash: string, last_updated: string): Promise<boolean> {
try {
const result = await connection.execute(
`UPDATE ${database.database.tables.skins.name}
SET ${database.database.tables.skins.columns.hash} = ?, ${database.database.tables.skins.columns.last_updated} = ?
WHERE ${database.database.tables.skins.columns.username} = ?`,
[hash, last_updated, username]
);

console.log(`Updated row(s)`);

return true;
} catch (error) {
console.error('Error updating values:', error);
return false;
}
};
Loading

0 comments on commit c8a5133

Please sign in to comment.