Skip to content

enhance: add functions for daemon tools to do mTLS #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path"
import child_process from "child_process"
import {fileURLToPath} from "url"
import {gunzipSync} from "zlib"
import https from "https"

export interface GlobalOpts {
URL?: string
Expand Down Expand Up @@ -1283,3 +1284,48 @@ export interface DatasetElement {
contents?: string
binaryContents?: ArrayBuffer
}

// Functions for use in daemon tools:

export function createServer(listener: http.RequestListener<typeof http.IncomingMessage, typeof http.ServerResponse>): https.Server {
const certB64 = process.env.CERT
const privateKeyB64 = process.env.PRIVATE_KEY
const gptscriptCertB64 = process.env.GPTSCRIPT_CERT

if (!certB64) {
console.log('Missing CERT env var')
process.exit(1)
} else if (!privateKeyB64) {
console.log('Missing PRIVATE_KEY env var')
process.exit(1)
} else if (!gptscriptCertB64) {
console.log('Missing GPTSCRIPT_CERT env var')
process.exit(1)
}

const cert = Buffer.from(certB64, 'base64').toString('utf-8')
const privateKey = Buffer.from(privateKeyB64, 'base64').toString('utf-8')
const gptscriptCert = Buffer.from(gptscriptCertB64, 'base64').toString('utf-8')

const options = {
key: privateKey,
cert: cert,
ca: gptscriptCert,
requestCert: true,
rejectUnauthorized: true,
}

return https.createServer(options, listener)
}

export function startServer(server: https.Server) {
const port = process.env.PORT
if (!port) {
console.log('Missing PORT env var')
process.exit(1)
}

server.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
}
Loading