Skip to content

Commit

Permalink
Merge pull request #553 from typerefinery-ai/master
Browse files Browse the repository at this point in the history
Master to Dev
  • Loading branch information
wildone authored Jan 22, 2025
2 parents e9571a8 + 4658bda commit 19034e5
Show file tree
Hide file tree
Showing 55 changed files with 33,071 additions and 36,774 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

strategy:
matrix:
node: [18.3.0, 18.20.4]
node: [20.9.0]
os: [
# macos-latest,
# ubuntu-latest,
Expand All @@ -61,6 +61,8 @@ jobs:
steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
submodules: true

- name: Install Node.js, NPM and Yarn
uses: actions/setup-node@v3
Expand Down Expand Up @@ -198,7 +200,7 @@ jobs:
NODE_ENV: develop
strategy:
matrix:
node: [18.3.0]
node: [20.9.0]
os: [
# macos-latest,
# ubuntu-latest,
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "services/fastapi/generated/os-triage/dialect_data"]
path = services/fastapi/generated/os-triage/dialect_data
url = https://github.com/os-threat/stix2-dialect-definitions.git
branch = main
File renamed without changes.
10 changes: 9 additions & 1 deletion electron/app/main/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// import { get } from "node:http"
import * as winston from "winston"
import { type ILogger } from "update-electron-app"
const { combine, timestamp, label, json } = winston.format

export class Logger {
export class Logger implements ILogger {
readonly #service: string
readonly #logger: winston.Logger
readonly #logsDir: string
Expand Down Expand Up @@ -58,4 +59,11 @@ export class Logger {
// console.log(...args)
// }
}

info(...args: any[]) {
this.#logger.log("info", JSON.stringify(args))
// if (this.#isDev) {
// console.log(...args)
// }
}
}
11 changes: 7 additions & 4 deletions electron/app/main/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { Service, ServiceStatus, type ServiceConfig } from "./Service"
import { Logger } from "./Logger"
import { dataPath, resourceBinary } from "./Resources"
import path from "path"
import config from "../../../package.json" assert { type: "json" }
import fs from "fs"
import process from "node:process"
import { fileURLToPath } from "url"
import { dirname } from "path"
import pkg from "../../../package.json"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
Expand All @@ -24,7 +24,7 @@ const APPDATA =
process.env.APPDATA || (process.platform === "darwin" ? "/Users" : "/home")

let logsDir = isProduction
? path.join(APPDATA, config.name, "logs")
? path.join(APPDATA, pkg.name, "logs")
: path.join(__dirname, "../../../logs")

// create a new logs sub directory with date timestamp everytime the app starts
Expand All @@ -35,7 +35,10 @@ const dateStr = date
.replace(/.Z/g, "")
.replace(/T/g, "_")
logsDir = path.join(logsDir, dateStr)
fs.mkdirSync(logsDir, { recursive: true })
// check if logs directory exists, if not create it
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true })
}

const servicePort = 3001

Expand All @@ -47,7 +50,7 @@ logger.log("isProduction", isProduction)

const servicesPath = path.join(__dirname, "../../../services")
const servicesUserDataPath = isProduction
? path.join(APPDATA, config.name, "services")
? path.join(APPDATA, pkg.name, "services")
: path.join(__dirname, "../../../services")
logger.log("services path", servicesPath)
logger.log("services data path", servicesUserDataPath)
Expand Down
155 changes: 82 additions & 73 deletions electron/app/main/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pkg from "../../../package.json" assert { type: "json" };
import portfinder from "portfinder"
import child_process, { type SpawnOptions } from "node:child_process"
import path from "path"
import fs from "fs"
import http, { type RequestOptions } from "node:http"
import e from "express"
import pkg from "../../../package.json"

export function tryParseInt(text: string, defaultValue: number): number {
try {
Expand Down Expand Up @@ -35,7 +36,6 @@ export async function checkPortFree(port: number, host: string) {
})
}


export async function getPortFree(port: number, host: string) {
return portfinder
.getPortPromise({ host: host, port: port })
Expand All @@ -47,9 +47,13 @@ export async function getPortFree(port: number, host: string) {
})
}

export async function stopProcess(pid: any, force: boolean = false, abortController: AbortController) {
var processCommand = ""
var forceFlag = ""
export async function stopProcess(
pid: any,
force = false,
abortController: AbortController
) {
let processCommand = ""
let forceFlag = ""
//if windows use powershell to get process
if (os.isWindows) {
forceFlag = force ? "-Force" : ""
Expand All @@ -60,7 +64,7 @@ export async function stopProcess(pid: any, force: boolean = false, abortControl
}

//execute command
var stopped = false
let stopped = false
await os.runCommandWithCallBack(
processCommand,
[],
Expand All @@ -69,7 +73,7 @@ export async function stopProcess(pid: any, force: boolean = false, abortControl
if (data) {
const stopOutput = data.trim()
if (os.isWindows) {
var stopOutputJson = JSON.parse(data)
const stopOutputJson = JSON.parse(data)
if (stopOutputJson["HasExited"]) {
stopped = true
return stopped
Expand All @@ -94,9 +98,12 @@ export async function stopProcess(pid: any, force: boolean = false, abortControl
* @param abortController to cancel the process
* @returns pid using a port
*/
export async function getProcessPidForPort(port: number, abortController: AbortController) {
export async function getProcessPidForPort(
port: number,
abortController: AbortController
) {
var processPid = ""
var processCommand = ""
let processCommand = ""
//if windows use powershell to get process
if (os.isWindows) {
processCommand = `powershell -Command "Get-NetTCPConnection -LocalPort ${port} -State Listen -ErrorAction SilentlyContinue | Select-Object -Property OwningProcess | ConvertTo-Json" `
Expand All @@ -115,7 +122,7 @@ export async function getProcessPidForPort(port: number, abortController: AbortC
processPid = data.trim()
if (processPid) {
if (os.isWindows) {
var dataJson = JSON.parse(data)
let dataJson = JSON.parse(data)
//if dataJson is array, get first item
if (Array.isArray(dataJson)) {
dataJson = dataJson[0]
Expand All @@ -126,7 +133,6 @@ export async function getProcessPidForPort(port: number, abortController: AbortC
return processPid
}
}

}
return processPid
}
Expand All @@ -142,9 +148,12 @@ export async function getProcessPidForPort(port: number, abortController: AbortC
* @param abortController to cancel the process
* @returns process path using a port
*/
export async function getProcessPathForPID(pid: number, abortController: AbortController) {
export async function getProcessPathForPID(
pid: number,
abortController: AbortController
) {
var processPath = ""
var processCommand = ""
let processCommand = ""
//if windows use powershell to get process
if (os.isWindows) {
processCommand = `powershell -Command "Get-Process -Id ${pid} | Select-Object -Property Path | ConvertTo-Json "`
Expand All @@ -162,7 +171,7 @@ export async function getProcessPathForPID(pid: number, abortController: AbortCo
(data) => {
if (data) {
if (os.isWindows) {
var dataJson = JSON.parse(data)
const dataJson = JSON.parse(data)
if (dataJson["Path"]) {
processPath = dataJson["Path"]
return processPath
Expand Down Expand Up @@ -306,75 +315,75 @@ export function csvParseRow(data: string) {
return fields
}

/**
*
* @param template template string
* @param args arguments to be replaced in template
* @returns interpolated string
*/
/**
*
* @param template template string
* @param args arguments to be replaced in template
* @returns interpolated string
*/
export function updateTemplateVars(
template: string,
args: { [key: string]: string }
): string {
if (typeof args !== "object") {
return template
}
try {
return new Function(
"return `" + template.replace(/\$\{(.+?)\}/g, "${this.$1}") + "`;"
).call(args)
} catch (e) {
// ES6 syntax not supported
}
Object.keys(args).forEach((key) => {
template = template.replace(
new RegExp("\\$\\{" + key + "\\}", "g"),
args[key]
)
})
template: string,
args: { [key: string]: string }
): string {
if (typeof args !== "object") {
return template
}

/**
* get string timestamp
* @returns timestamp
*/
export function timestamp(): string {
return new Date().toISOString()
try {
return new Function(
"return `" + template.replace(/\$\{(.+?)\}/g, "${this.$1}") + "`;"
).call(args)
} catch (e) {
// ES6 syntax not supported
}
Object.keys(args).forEach((key) => {
template = template.replace(
new RegExp("\\$\\{" + key + "\\}", "g"),
args[key]
)
})
return template
}

/**
* get string timestamp
* @returns timestamp
*/
export function timestamp(): string {
return new Date().toISOString()
}

/*
/*
compile a templarte string for var
*/
export function varTemplate(name: string): string {
return "${" + name + "}"
}
export function varTemplate(name: string): string {
return "${" + name + "}"
}

export function getCallerName() {
const error = new Error();
if (error) {
if (error.stack) {
const stack = error.stack.split('\n');
// The second item in the stack array is the calling function
const callerStackLine = stack[2];
const callerNameMatch = callerStackLine.match(/at (\w+)/);
if (callerNameMatch) {
return callerNameMatch[1];
}
export function getCallerName() {
const error = new Error()
if (error) {
if (error.stack) {
const stack = error.stack.split("\n")
// The second item in the stack array is the calling function
const callerStackLine = stack[2]
const callerNameMatch = callerStackLine.match(/at (\w+)/)
if (callerNameMatch) {
return callerNameMatch[1]
}
}
return null;
}
return null
}

export function getCallStackString() {
const error = new Error();
if (error && error.stack) {
const stack = error.stack.split('\n').slice(2); // Slice to remove 'Error' and the call to getCallStack itself
const functionNames = stack.map(line => {
const match = line.match(/at (\w+)/);
return match ? match[1] : 'anonymous';
});
return functionNames.join(' -> ');
}
return "anonymous";
export function getCallStackString() {
const error = new Error()
if (error && error.stack) {
const stack = error.stack.split("\n").slice(2) // Slice to remove 'Error' and the call to getCallStack itself
const functionNames = stack.map((line) => {
const match = line.match(/at (\w+)/)
return match ? match[1] : "anonymous"
})
return functionNames.join(" -> ")
}
return "anonymous"
}
20 changes: 18 additions & 2 deletions electron/app/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import ElectronWindowState from "electron-window-state"
import { t } from "i18next"
import { type AppIPC, sharedAppIpc } from "../preload/ipc"
import { Service } from "./Service"
import pkg from "../../../package.json"

import updateElectronApp from "update-electron-app"
import { updateElectronApp, UpdateSourceType } from "update-electron-app"

// add support for self signed certificates
app.commandLine.appendSwitch("ignore-certificate-errors", "true")
Expand Down Expand Up @@ -117,11 +118,26 @@ logger.log(
)}`
)

logger.log(`app.isPackaged: ${app.isPackaged}`)
// pkg.repository.url
logger.log(`pkg.repository.url: ${pkg.repository.url}`)
//getConfig("productName")
logger.log(`getConfig("productName"): ${getConfig("productName")}`)
// app.getName()
logger.log(`app.getName(): ${app.getName()}`)
// app.getAppPath()
logger.log(`app.getAppPath(): ${app.getAppPath()}`)

if (!isDev) {
// run auto update
updateElectronApp({
repo: "typerefinery-ai/typerefinery",
updateSource: {
type: UpdateSourceType.ElectronPublicUpdateService,
repo: pkg.repository.url,
host: pkg.repository.url,
},
updateInterval: "1 hour",
logger: logger,
})
}

Expand Down
2 changes: 1 addition & 1 deletion electron/app/preload/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "path"
import { builtinModules } from "module"
import { defineConfig } from "vite"
import pkg from "../../../package.json" assert { type: "json" };
import pkg from "../../../package.json" assert { type: "json" }

export default defineConfig({
root: __dirname,
Expand Down
Loading

0 comments on commit 19034e5

Please sign in to comment.