| 1 |
#!/usr/bin/env gjs |
| 2 |
// |
| 3 |
// Copyright (c) 2021, Sonny Piers <[email protected]> |
| 4 |
// |
| 5 |
// All rights reserved. |
| 6 |
// |
| 7 |
// Redistribution and use in source and binary forms, with or without |
| 8 |
// modification, are permitted provided that the following conditions are met: |
| 9 |
// |
| 10 |
// 1. Redistributions of source code must retain the above copyright notice, this |
| 11 |
// list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
// this list of conditions and the following disclaimer in the documentation |
| 15 |
// and/or other materials provided with the distribution. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 |
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 |
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 |
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 26 |
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 27 |
// DAMAGE. |
| 28 |
|
| 29 |
const { GUPnPIgd, GLib } = imports.gi; |
| 30 |
const { system } = imports; |
| 31 |
|
| 32 |
const { SimpleIgd } = GUPnPIgd; |
| 33 |
const { MainLoop } = GLib; |
| 34 |
|
| 35 |
const mainloop = new MainLoop(null, false); |
| 36 |
|
| 37 |
const protocol = "TCP"; |
| 38 |
const external_port = 0; // 0 means local_port if available otherwise random port |
| 39 |
const local_ip = ARGV[0]; |
| 40 |
const local_port = ARGV[1]; |
| 41 |
const lease_duration = 3000; // auto-renewed until port mapping is removed or process exits |
| 42 |
const description = "gupnp-igd JavaScript example"; |
| 43 |
|
| 44 |
let exit_code = 0; |
| 45 |
|
| 46 |
if (!local_ip || !local_port) { |
| 47 |
print("Usage: ./map-port.js IP PORT"); |
| 48 |
system.exit(1); |
| 49 |
} |
| 50 |
|
| 51 |
const simpleIgd = new SimpleIgd(); |
| 52 |
simpleIgd.connect( |
| 53 |
"error-mapping-port", |
| 54 |
(self, error, proto, external_port, local_ip, local_port, description) => { |
| 55 |
printerr(error); |
| 56 |
exit_code = 1; |
| 57 |
mainloop.quit(); |
| 58 |
} |
| 59 |
); |
| 60 |
|
| 61 |
simpleIgd.connect( |
| 62 |
"mapped-external-port", |
| 63 |
( |
| 64 |
self, |
| 65 |
proto, |
| 66 |
external_ip, |
| 67 |
replaces_external_ip, |
| 68 |
external_port, |
| 69 |
local_ip, |
| 70 |
local_port, |
| 71 |
description |
| 72 |
) => { |
| 73 |
print( |
| 74 |
`success ${external_ip}:${external_port} -> ${local_ip}:${local_port}` |
| 75 |
); |
| 76 |
} |
| 77 |
); |
| 78 |
|
| 79 |
simpleIgd.add_port( |
| 80 |
protocol, |
| 81 |
external_port, |
| 82 |
local_ip, |
| 83 |
local_port, |
| 84 |
lease_duration, |
| 85 |
description |
| 86 |
); |
| 87 |
|
| 88 |
mainloop.run(); |
| 89 |
system.exit(exit_code); |