add worker

This commit is contained in:
nin0dev 2024-11-14 07:57:50 -05:00
parent 9e3b3e5ee3
commit 2f98e577c5
9 changed files with 2303 additions and 0 deletions

12
worker/.editorconfig Normal file
View file

@ -0,0 +1,12 @@
# http://editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space

172
worker/.gitignore vendored Normal file
View file

@ -0,0 +1,172 @@
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
# wrangler project
.dev.vars
.wrangler/

7
worker/.prettierrc.json Normal file
View file

@ -0,0 +1,7 @@
{
"printWidth": 140,
"singleQuote": false,
"semi": true,
"useTabs": true,
"tabWidth": 4
}

18
worker/package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "tsfetch",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"test": "vitest",
"cf-typegen": "wrangler types"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.5.2",
"@cloudflare/workers-types": "^4.20241112.0",
"typescript": "^5.5.2",
"wrangler": "^3.60.3"
}
}

1856
worker/pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load diff

74
worker/src/index.ts Normal file
View file

@ -0,0 +1,74 @@
const sh = `#!/bin/bash
# Dependency check
command -v jq > /dev/null && true || printf "\\033[1;31mInstall jq, then come back.\\n"
command -v jq > /dev/null && true || exit
command -v tailscale > /dev/null && true || printf "\\033[1;31mInstall Tailscale, then come back.\\n"
command -v tailscale > /dev/null && true || exit
# Define useful variables
tailscaleStatus=\`tailscale status --json\`
isTailscaleUp=\`echo $tailscaleStatus | jq .Self.Online\`
tailnetName=\`echo $tailscaleStatus | jq -r .CurrentTailnet.Name\`
tailscaleVersion=\`echo $tailscaleStatus | jq -r .Version\`
tailscaleIPs=\`echo $tailscaleStatus | jq -r '.TailscaleIPs | join(", ")'\`
tailscalePeerCount=\`echo $tailscaleStatus | jq -r '.Peer | length'\`
tailscaleOnlinePeerCount=\`echo $tailscaleStatus | jq -r '[.Peer[] | select(.Online == true)] | length'\`
tailscaleOfflinePeerCount=\`echo $tailscaleStatus | jq -r '[.Peer[] | select(.Online == false)] | length'\`
tailscaleExitNodesCount=\`echo $tailscaleStatus | jq -r '[.Peer[] | select(.ExitNodeOption == true)] | length'\`
tailscaleIsExitNode=\`echo $tailscaleStatus | jq -r .Self.ExitNode\`
tailscaleCanBeExitNode=\`echo $tailscaleStatus | jq -r .Self.ExitNodeOption\`
tailscaleDeviceID=\`echo $tailscaleStatus | jq -r .Self.ID\`
tailscaleUserID=\`echo $tailscaleStatus | jq -r .Self.UserID\`
tailscaleIsMagicDNSEnabled=\`echo $tailscaleStatus | jq -r .CurrentTailnet.MagicDNSEnabled\`
tailscaleMagicDNSHostname=\`echo $tailscaleStatus | jq -r .CurrentTailnet.MagicDNSSuffix\`
tailscaleLoginName=\`echo $tailscaleStatus | jq -r '.Self.UserID as $userID | .User[] | select(.ID == $userID) | .LoginName'\`
tailscaleUserCount=\`echo $tailscaleStatus | jq -r '.Peer | length'\`
tailscaleAPIURL=\`echo $tailscaleStatus | jq -r '.Self.PeerAPIURL[0]'\`
# Show content
printf "\\\\033[0;38;5;248m oooooooo oooooooo oooooooo \\\\033[1;37m$tailnetName\\\\033[0;38;5;248m\\\\n"
printf " oooooooooo oooooooooo oooooooooo \\\\033[1;37m--------------\\\\033[0;38;5;248m\\\\n"
printf " oooooooooooo oooooooooooo oooooooooooo \\\\033[1;37mVersion: \\\\033[0;37m$tailscaleVersion\\\\033[0;38;5;248m\\\\n"
printf " oooooooooo oooooooooo oooooooooo \\\\033[1;37mIP addresses: \\\\033[0;37m$tailscaleIPs\\\\033[0;38;5;248m\\\\n"
printf " oooooooo oooooooo oooooooo \\\\033[1;37mDevice count: \\\\033[0;37m$tailscalePeerCount ($tailscaleOnlinePeerCount online, $tailscaleOfflinePeerCount offline, $tailscaleExitNodesCount exit node/s)\\\\033[0;38;5;248m\\\\n"
printf " \\\\033[1;37mIs exit node: \\\\033[0;37m$tailscaleIsExitNode\\\\033[0;38;5;248m\\\\n"
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || printf "\\\\033[0;38;5;248m"
printf " oooooooo oooooooo oooooooo \\\\033[1;37mAllowed to be exit node: \\\\033[0;37m$tailscaleCanBeExitNode\\\\033[0;38;5;248m\\\\n"
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || printf "\\\\033[0;38;5;248m"
printf " oooooooooo oooooooooo oooooooooo \\\\033[1;37mDevice ID: \\\\033[0;37m$tailscaleDeviceID\\\\033[0;38;5;248m\\\\n"
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || printf "\\\\033[0;38;5;248m"
printf " oooooooooooo oooooooooooo oooooooooooo \\\\033[1;37mUser ID: \\\\033[0;37m$tailscaleUserID\\\\033[0;38;5;248m\\\\n"
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || printf "\\\\033[0;38;5;248m"
printf " oooooooooo oooooooooo oooooooooo \\\\033[1;37mMagicDNS enabled: \\\\033[0;37m$tailscaleIsMagicDNSEnabled\\\\033[0;38;5;248m\\\\n"
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || printf "\\\\033[0;38;5;248m"
printf " oooooooo oooooooo oooooooo \\\\033[1;37mMagicDNS hostname: \\\\033[0;37m$tailscaleMagicDNSHostname\\\\033[0;38;5;248m\\\\n"
printf " \\\\033[1;37mUser login name: \\\\033[0;37m$tailscaleLoginName\\\\033[0;38;5;248m\\\\n"
printf "\\\\033[0;38;5;248m oooooooo "
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || false
printf "oooooooo "
printf "\\\\033[0;38;5;248moooooooo \\\\033[1;37mTailnet user count: \\\\033[0;37m$tailscaleUserCount\\\\033[0;38;5;248m\\\\n"
printf "\\\\033[0;38;5;248m oooooooooo "
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || false
printf "oooooooooo "
printf "\\\\033[0;38;5;248moooooooooo \\\\033[1;37mPeer API URL: \\\\033[0;37m$tailscaleAPIURL\\\\033[0;38;5;248m\\\\n"
printf "\\\\033[0;38;5;248m oooooooooooo "
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || false
printf "oooooooooooo "
printf "\\\\033[0;38;5;248moooooooooooo\\\\n"
printf "\\\\033[0;38;5;248m oooooooooo "
[[ "$isTailscaleUp" == "true" ]] && printf "\\\\033[1;37m" || false
printf "oooooooooo "
printf "\\\\033[0;38;5;248moooooooooo \\\\033[40m \\\\033[41m \\\\033[42m \\\\033[43m \\\\033[44m \\\\033[45m \\\\033[46m \\\\033[47m \\\\033[m\\\\n"
printf "\\033[0;38;5;248m oooooooo "
[[ "$isTailscaleUp" == "true" ]] && printf "\\033[1;37m" || false
printf "oooooooo "
printf "\\033[0;38;5;248moooooooo \\033[100m \\033[101m \\033[102m \\033[103m \\033[104m \\033[105m \\033[106m \\033[107m \\033[m\\n\\n\\n"
`;
export default {
async fetch(request, env, ctx): Promise<Response> {
return new Response(sh);
},
} satisfies ExportedHandler<Env>;

46
worker/tsconfig.json Normal file
View file

@ -0,0 +1,46 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2021",
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
"lib": ["es2021"],
/* Specify what JSX code is generated. */
"jsx": "react-jsx",
/* Specify what module code is generated. */
"module": "es2022",
/* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Bundler",
/* Specify type package names to be included without being referenced in a source file. */
"types": [
"@cloudflare/workers-types/2023-07-01"
],
/* Enable importing .json files */
"resolveJsonModule": true,
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"allowJs": true,
/* Enable error reporting in type-checked JavaScript files. */
"checkJs": false,
/* Disable emitting files from a compilation. */
"noEmit": true,
/* Ensure that each file can be safely transpiled without relying on other imports. */
"isolatedModules": true,
/* Allow 'import x from y' when a module doesn't have a default export. */
"allowSyntheticDefaultImports": true,
/* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true,
/* Enable all strict type-checking options. */
"strict": true,
/* Skip type checking all .d.ts files. */
"skipLibCheck": true
},
"exclude": ["test"],
"include": ["worker-configuration.d.ts", "src/**/*.ts"]
}

4
worker/worker-configuration.d.ts vendored Normal file
View file

@ -0,0 +1,4 @@
// Generated by Wrangler
// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen`
interface Env {
}

114
worker/wrangler.toml Normal file
View file

@ -0,0 +1,114 @@
#:schema node_modules/wrangler/config-schema.json
name = "tsfetch"
main = "src/index.ts"
compatibility_date = "2024-11-12"
compatibility_flags = ["nodejs_compat"]
# Workers Logs
# Docs: https://developers.cloudflare.com/workers/observability/logs/workers-logs/
# Configuration: https://developers.cloudflare.com/workers/observability/logs/workers-logs/#enable-workers-logs
[observability]
enabled = true
# Automatically place your workloads in an optimal location to minimize latency.
# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure
# rather than the end user may result in better performance.
# Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
# [placement]
# mode = "smart"
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Docs:
# - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
# Note: Use secrets to store sensitive data.
# - https://developers.cloudflare.com/workers/configuration/secrets/
# [vars]
# MY_VARIABLE = "production_value"
# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflares global network
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai
# [ai]
# binding = "AI"
# Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets
# [[analytics_engine_datasets]]
# binding = "MY_DATASET"
# Bind a headless browser instance running on Cloudflare's global network.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering
# [browser]
# binding = "MY_BROWSER"
# Bind a D1 database. D1 is Cloudflares native serverless SQL database.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases
# [[d1_databases]]
# binding = "MY_DB"
# database_name = "my-database"
# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms
# [[dispatch_namespaces]]
# binding = "MY_DISPATCHER"
# namespace = "my-namespace"
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects
# [[durable_objects.bindings]]
# name = "MY_DURABLE_OBJECT"
# class_name = "MyDurableObject"
# Durable Object migrations.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
# [[migrations]]
# tag = "v1"
# new_classes = ["MyDurableObject"]
# Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive
# [[hyperdrive]]
# binding = "MY_HYPERDRIVE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces
# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Bind an mTLS certificate. Use to present a client certificate when communicating with another service.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates
# [[mtls_certificates]]
# binding = "MY_CERTIFICATE"
# certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
# [[queues.producers]]
# binding = "MY_QUEUE"
# queue = "my-queue"
# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
# [[queues.consumers]]
# queue = "my-queue"
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"
# Bind another Worker service. Use this binding to call another Worker without network overhead.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings
# [[services]]
# binding = "MY_SERVICE"
# service = "my-service"
# Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases.
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes
# [[vectorize]]
# binding = "MY_INDEX"
# index_name = "my-index"