Skip to content

Commit

Permalink
feat: Add regular expression support for token.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier9896 committed Jul 6, 2024
1 parent 7a3726c commit ff34f23
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Serve is running on
https://printjs.cn:17521

Please make sure that the ports have been opened in the security group or firewall.
token: vue-plugin-hiprint
token: vue-plugin-hiprint*
```

### `Web` project connecting to `node-hiprint-transit`
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ node ./dist/index
https://printjs.cn:17521

请确保安全组或防火墙已放行端口。
令牌:vue-plugin-hiprint
令牌:vue-plugin-hiprint*
```

### `Web` 项目连接 `node-hiprint-transit`
Expand Down
117 changes: 77 additions & 40 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53826,7 +53826,7 @@ function log(message) {
}

var name = "node-hiprint-transit";
var version = "0.0.3";
var version = "0.0.4";
var description = "A nodejs server for hiprint transit";
var main = "index.js";
var type = "module";
Expand Down Expand Up @@ -53887,7 +53887,7 @@ var packageJson = {
/*
* @Date: 2023-09-28 19:28:42
* @LastEditors: [email protected]
* @LastEditTime: 2024-05-24 11:22:15
* @LastEditTime: 2024-07-06 14:19:42
* @FilePath: /node-hiprint-transit/index.js
*/

Expand All @@ -53902,7 +53902,7 @@ const i18n = new I18n({
defaultLocale: "en",
});

const CLIENT = {};
const CLIENT = new Map();

// Read config first and then start serve
readConfig().then((CONFIG) => {
Expand Down Expand Up @@ -53946,7 +53946,9 @@ readConfig().then((CONFIG) => {

server.listen(port, () => {
log(i18n.__("Serve is start"));
console.log(chalk.green(`node-hiprint-transit version: ${packageJson.version}\n`));
console.log(
chalk.green(`node-hiprint-transit version: ${packageJson.version}\n`)
);
console.log(
i18n.__(
"Serve is running on\n%s\n\nPlease make sure that the ports have been opened in the security group or firewall.\ntoken: %s",
Expand All @@ -53958,7 +53960,8 @@ readConfig().then((CONFIG) => {

// Authentication
io.use((socket, next) => {
if (token && socket.handshake.auth.token === token) {
const tokenRegex = new RegExp(`^${token.replace(/\*/g, "\\S+")}$`);
if (token && tokenRegex.test(socket.handshake.auth.token)) {
next();
} else {
log(i18n.__("Authentication failed for %s", socket.id));
Expand All @@ -53968,43 +53971,58 @@ readConfig().then((CONFIG) => {

// Socket.io Add event listener
io.on("connection", (socket) => {
const sToken = socket.handshake.auth.token;
if (!CLIENT.has(sToken)) {
CLIENT.set(sToken, new Map());
}

if (socket.handshake.query.test !== "true") {
if (socket.handshake.query.client === "electron-hiprint") {
log(i18n.__("Client connected: %s", socket.id + "(electron-hiprint)"));
log(
i18n.__(
"Client connected: %s",
`${socket.id} | ${sToken} | (electron-hiprint)`
)
);
// Join electron-hiprint room
socket.join("electron-hiprint");
socket.join(`${sToken}_electron-hiprint`);

// Send client list to web client
io.to("web-client").emit("clients", CLIENT);
// Send client list to current token web client
io.to(`${sToken}_web-client`).emit("clients", Array.from(CLIENT.get(sToken).values()));

// Send all printer list to web client
var allPrinterList = [];
Object.keys(CLIENT).forEach((key) => {
CLIENT[key].printerList.forEach((printer) => {
CLIENT.get(sToken).forEach((client) => {
client.printerList.forEach((printer) => {
allPrinterList.push({
...printer,
server: Object.assign({}, CLIENT[key], {
server: Object.assign({}, client, {
printerList: undefined,
}),
});
});
});
io.sockets.emit("printerList", allPrinterList);
io.to(`${sToken}_web-client`).emit("printerList", allPrinterList);
} else {
log(i18n.__("Client connected: %s", socket.id + "(web-client)"));
log(
i18n.__(
"Client connected: %s",
`${socket.id} | ${sToken} | (web-client)`
)
);
// Join web-client room
socket.join("web-client");
socket.join(`${token}_web-client`);

// Send client list to web client
socket.emit("clients", CLIENT);
socket.emit("clients", Array.from(CLIENT.get(sToken).values()));

// Send all printer list to web client
var allPrinterList = [];
Object.keys(CLIENT).forEach((key) => {
CLIENT[key].printerList.forEach((printer) => {
CLIENT.get(sToken).forEach((client) => {
client.printerList.forEach((printer) => {
allPrinterList.push({
...printer,
server: Object.assign({}, CLIENT[key], {
server: Object.assign({}, client, {
printerList: undefined,
}),
});
Expand All @@ -54013,40 +54031,45 @@ readConfig().then((CONFIG) => {
socket.emit("printerList", allPrinterList);
}
} else {
log(i18n.__("Client connected: %s", socket.id + " (test)"));
log(i18n.__("Client connected: %s", `${socket.id} | ${sToken} | (test)`));
}

// Get client info
socket.on("clientInfo", (data) => {
CLIENT[socket.id] = Object.assign({}, CLIENT[socket.id], data);
CLIENT.get(sToken).set(
socket.id,
Object.assign({}, CLIENT.get(sToken).get(socket.id), data)
);
});

// Get client printer list
socket.on("printerList", (printerList) => {
CLIENT[socket.id] = Object.assign({}, CLIENT[socket.id], { printerList });
CLIENT.get(sToken).set(
socket.id,
Object.assign({}, CLIENT.get(sToken).get(socket.id), { printerList })
);
});

// Get all client list
socket.on("getClients", () => {
socket.emit("clients", CLIENT);
socket.emit("clients", Array.from(CLIENT.get(sToken).values()));
});

// Get all clients printer list
socket.on("refreshPrinterList", () => {
io.to("electron-hiprint").emit("refreshPrinterList");
io.to(`${sToken}_electron-hiprint`).emit("refreshPrinterList");

// Just wait 2 seconds for the client to update the printer list
// Of course, this is not a good way to do it. But it’s not like it can’t be used 🤪
setTimeout(() => {
var allPrinterList = [];
Object.keys(CLIENT).forEach((key) => {
CLIENT[key].printerList.forEach((printer) => {
CLIENT.get(sToken).forEach((client) => {
client.printerList.forEach((printer) => {
allPrinterList.push({
...printer,
server: Object.assign({}, CLIENT[key], {
server: Object.assign({}, client, {
printerList: undefined,
}),
clientId: key,
});
});
});
Expand All @@ -54065,9 +54088,9 @@ readConfig().then((CONFIG) => {
// Make a ipp print to electron-hiprint client
socket.on("ippPrint", (options) => {
if (options.client) {
if (!CLIENT[options.client]) {
if (!CLIENT.get(sToken).get(options.client)) {
socket.emit("error", {
msg: "Client is not exist."
msg: "Client is not exist.",
});
return;
}
Expand All @@ -54077,7 +54100,7 @@ readConfig().then((CONFIG) => {
log(i18n.__("%s send ippPrint to %s", socket.id, options.client));
} else {
socket.emit("error", {
msg: "Client must be specified."
msg: "Client must be specified.",
});
}
});
Expand All @@ -54099,9 +54122,9 @@ readConfig().then((CONFIG) => {
// Make a ipp request to electron-hiprint client
socket.on("ippRequest", (options) => {
if (options.client) {
if (!CLIENT[options.client]) {
if (!CLIENT.get(sToken).get(options.client)) {
socket.emit("error", {
msg: "Client is not exist."
msg: "Client is not exist.",
});
return;
}
Expand All @@ -54111,7 +54134,7 @@ readConfig().then((CONFIG) => {
log(i18n.__("%s send ippRequest to %s", socket.id, options.client));
} else {
socket.emit("error", {
msg: "Client must be specified."
msg: "Client must be specified.",
});
}
});
Expand All @@ -54126,7 +54149,7 @@ readConfig().then((CONFIG) => {
// Make a news to electron-hiprint client
socket.on("news", (options) => {
if (options.client) {
if (!CLIENT[options.client]) {
if (!CLIENT.get(sToken).get(options.client)) {
socket.emit("error", {
msg: "Client is not exist.",
templateId: options.templateId,
Expand All @@ -54149,15 +54172,27 @@ readConfig().then((CONFIG) => {
socket.on("success", (options) => {
if (options.replyId) {
socket.to(options.replyId).emit("success", options);
log(i18n.__("%s client: print success, templateId: %s", socket.id, options.templateId));
log(
i18n.__(
"%s client: print success, templateId: %s",
socket.id,
options.templateId
)
);
}
});

// Make a error callback to reply client
socket.on("error", (options) => {
if (options.replyId) {
socket.to(options.replyId).emit("error", options);
log(i18n.__("%s client: print error, templateId: %s", socket.id, options.templateId));
log(
i18n.__(
"%s client: print error, templateId: %s",
socket.id,
options.templateId
)
);
}
});

Expand All @@ -54167,9 +54202,9 @@ readConfig().then((CONFIG) => {
log(i18n.__("Client disconnected: %s", socket.id));
// Remove electron-hiprint client from CLIENT
if (socket.handshake.query.client === "electron-hiprint") {
delete CLIENT[socket.id];
CLIENT.get(sToken).delete(socket.id);
// Send client list to web client
io.to("web-client").emit("clients", CLIENT);
io.to("web-client").emit("clients", Array.from(CLIENT.get(sToken).values()));
}
}
});
Expand All @@ -54178,7 +54213,9 @@ readConfig().then((CONFIG) => {
// Retrieve the client print list every 10 minutes.
setInterval(() => {
log(i18n.__("Retrieve the client print list"));
io.to("electron-hiprint").emit("refreshPrinterList");
CLIENT.forEach((_, key) => {
io.to(`${key}_electron-hiprint`).emit("refreshPrinterList");
});
}, 1000 * 60 * 10);
});

Expand Down
2 changes: 1 addition & 1 deletion dist/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -39908,7 +39908,7 @@ function setToken() {
inquirer.prompt([{
name: "token",
type: "input",
message: i18n.__("Set service TOKEN:"),
message: i18n.__("Set service TOKEN (Use the wildcard character (*) to match any character):"),
default: "vue-plugin-hiprint",
validate: (input) => {
if (input && input.length >= 6) {
Expand Down
2 changes: 1 addition & 1 deletion dist/src/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Set serve port %s:": "Set serve port %s:",
"Port must be set between %s": "Port must be set between %s",
"Set service TOKEN:": "Set service TOKEN:",
"Set service TOKEN (Use the wildcard character (*) to match any character):": "Set service TOKEN (Use the wildcard character (*) to match any character):",
"For security reasons, the TOKEN length must be greater than 5": "For security reasons, the TOKEN length must be greater than 5",
"Use SSL:": "Use SSL:",
"Configuration file written successfully": "Configuration file written successfully",
Expand Down
2 changes: 1 addition & 1 deletion dist/src/locales/zh.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Set serve port %s:": "设置服务端口号%s:",
"Port must be set between %s": "端口号必须设置在%s之间",
"Set service TOKEN:": "设置服务 TOKEN:",
"Set service TOKEN (Use the wildcard character (*) to match any character):": "设置服务 TOKEN (使用通配符(*)匹配任意字符)",
"For security reasons, the TOKEN length must be greater than 5": "出于安全考虑 TOKEN 长度必须大于5",
"Use SSL:": "使用 SSL:",
"Configuration file written successfully": "配置文件写入成功",
Expand Down
Loading

0 comments on commit ff34f23

Please sign in to comment.