@h3nr1-d14z/nat-gate 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +43 -0
  2. package/scripts/install.js +149 -0
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@h3nr1-d14z/nat-gate",
3
+ "version": "0.1.0",
4
+ "description": "CLI tool for managing iptables port forwarding through Tailscale tunnels",
5
+ "keywords": [
6
+ "iptables",
7
+ "nat",
8
+ "port-forwarding",
9
+ "tailscale",
10
+ "networking",
11
+ "linux"
12
+ ],
13
+ "author": "h3nr1-d14z",
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/h3nr1-d14z/nat-gate.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/h3nr1-d14z/nat-gate/issues"
21
+ },
22
+ "homepage": "https://github.com/h3nr1-d14z/nat-gate#readme",
23
+ "os": [
24
+ "linux"
25
+ ],
26
+ "cpu": [
27
+ "x64",
28
+ "arm64"
29
+ ],
30
+ "bin": {
31
+ "nat-gate": "./bin/nat-gate"
32
+ },
33
+ "scripts": {
34
+ "postinstall": "node scripts/install.js"
35
+ },
36
+ "files": [
37
+ "bin",
38
+ "scripts"
39
+ ],
40
+ "engines": {
41
+ "node": ">=14"
42
+ }
43
+ }
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env node
2
+
3
+ const https = require('https');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const { execSync } = require('child_process');
7
+
8
+ const REPO = 'h3nr1-d14z/nat-gate';
9
+ const BIN_DIR = path.join(__dirname, '..', 'bin');
10
+ const BIN_PATH = path.join(BIN_DIR, 'nat-gate');
11
+
12
+ function getPlatformInfo() {
13
+ const platform = process.platform;
14
+ const arch = process.arch;
15
+
16
+ if (platform !== 'linux') {
17
+ console.error(`Error: nat-gate only supports Linux. Current platform: ${platform}`);
18
+ process.exit(1);
19
+ }
20
+
21
+ let binaryName;
22
+ if (arch === 'x64') {
23
+ binaryName = 'nat-gate-linux-x86_64';
24
+ } else if (arch === 'arm64') {
25
+ binaryName = 'nat-gate-linux-aarch64';
26
+ } else {
27
+ console.error(`Error: Unsupported architecture: ${arch}`);
28
+ process.exit(1);
29
+ }
30
+
31
+ return binaryName;
32
+ }
33
+
34
+ function getLatestRelease() {
35
+ return new Promise((resolve, reject) => {
36
+ const options = {
37
+ hostname: 'api.github.com',
38
+ path: `/repos/${REPO}/releases/latest`,
39
+ headers: {
40
+ 'User-Agent': 'nat-gate-npm-installer'
41
+ }
42
+ };
43
+
44
+ https.get(options, (res) => {
45
+ let data = '';
46
+ res.on('data', chunk => data += chunk);
47
+ res.on('end', () => {
48
+ try {
49
+ const release = JSON.parse(data);
50
+ if (release.tag_name) {
51
+ resolve(release);
52
+ } else {
53
+ reject(new Error('No releases found'));
54
+ }
55
+ } catch (e) {
56
+ reject(e);
57
+ }
58
+ });
59
+ }).on('error', reject);
60
+ });
61
+ }
62
+
63
+ function downloadFile(url, dest) {
64
+ return new Promise((resolve, reject) => {
65
+ const file = fs.createWriteStream(dest);
66
+
67
+ const request = (url) => {
68
+ https.get(url, {
69
+ headers: { 'User-Agent': 'nat-gate-npm-installer' }
70
+ }, (res) => {
71
+ // Handle redirects
72
+ if (res.statusCode === 302 || res.statusCode === 301) {
73
+ request(res.headers.location);
74
+ return;
75
+ }
76
+
77
+ if (res.statusCode !== 200) {
78
+ reject(new Error(`Failed to download: ${res.statusCode}`));
79
+ return;
80
+ }
81
+
82
+ res.pipe(file);
83
+ file.on('finish', () => {
84
+ file.close();
85
+ resolve();
86
+ });
87
+ }).on('error', (err) => {
88
+ fs.unlink(dest, () => {});
89
+ reject(err);
90
+ });
91
+ };
92
+
93
+ request(url);
94
+ });
95
+ }
96
+
97
+ async function main() {
98
+ console.log('Installing nat-gate binary...');
99
+
100
+ const binaryName = getPlatformInfo();
101
+ console.log(`Platform: linux, Architecture: ${process.arch}`);
102
+ console.log(`Binary: ${binaryName}`);
103
+
104
+ // Ensure bin directory exists
105
+ if (!fs.existsSync(BIN_DIR)) {
106
+ fs.mkdirSync(BIN_DIR, { recursive: true });
107
+ }
108
+
109
+ try {
110
+ // Get latest release info
111
+ console.log('Fetching latest release...');
112
+ const release = await getLatestRelease();
113
+ console.log(`Found release: ${release.tag_name}`);
114
+
115
+ // Find the correct asset
116
+ const asset = release.assets.find(a => a.name === binaryName);
117
+ if (!asset) {
118
+ throw new Error(`Binary ${binaryName} not found in release ${release.tag_name}`);
119
+ }
120
+
121
+ // Download the binary
122
+ console.log(`Downloading ${binaryName}...`);
123
+ await downloadFile(asset.browser_download_url, BIN_PATH);
124
+
125
+ // Make executable
126
+ fs.chmodSync(BIN_PATH, 0o755);
127
+
128
+ console.log('nat-gate installed successfully!');
129
+ console.log(`Binary location: ${BIN_PATH}`);
130
+
131
+ // Verify installation
132
+ try {
133
+ const version = execSync(`"${BIN_PATH}" --version`, { encoding: 'utf8' });
134
+ console.log(`Version: ${version.trim()}`);
135
+ } catch (e) {
136
+ // Binary might not run on this platform during npm install
137
+ console.log('Note: Binary downloaded but could not verify (this is normal during cross-platform installs)');
138
+ }
139
+
140
+ } catch (error) {
141
+ console.error('Installation failed:', error.message);
142
+ console.error('');
143
+ console.error('You can manually download from:');
144
+ console.error(` https://github.com/${REPO}/releases/latest`);
145
+ process.exit(1);
146
+ }
147
+ }
148
+
149
+ main();