Metadata-Version: 2.1
Name: expose-localhost
Version: 0.4.5
Summary: Expose an app/api running on local host to public internet using AWS EC2
Author-email: Vignesh Sivanandha Rao <svignesh1793@gmail.com>
License: MIT License
        
        Copyright (c) 2021 Vignesh Sivanandha Rao
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/thevickypedia/expose
Project-URL: Docs, https://thevickypedia.github.io/expose/
Project-URL: Source, https://github.com/thevickypedia/expose
Project-URL: Bug Tracker, https://github.com/thevickypedia/expose/issues
Project-URL: Release Notes, https://github.com/thevickypedia/expose/blob/main/release_notes.rst
Keywords: route53,certificate,ec2,ngrok-alternative,tunnel
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Information Technology
Classifier: Operating System :: OS Independent
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: System :: Networking :: Firewalls
Requires-Python: >=3
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: boto3 (>=1.20.24)
Requires-Dist: botocore (>=1.23.24)
Requires-Dist: urllib3 (>=1.26.7)
Requires-Dist: python-dotenv (>=0.19.2)
Requires-Dist: paramiko (>=2.8.1)
Requires-Dist: click (>=8.0.3)
Requires-Dist: psutil (>=5.8.0)
Requires-Dist: requests (>=2.26.0)
Requires-Dist: pyOpenSSL (>=21.0.0)
Requires-Dist: scp (>=0.14.2)
Provides-Extra: dev
Requires-Dist: pre-commit ; extra == 'dev'

# Expose localhost using EC2
Expose an app/api running on local host to public internet using AWS EC2

### Requirements
- Access to an AWS account.
- A `service/app/api` running on a specific port that has to be exposed to public internet.
- **[Optional]** A domain `example.com` hosted on `route53`.

### Setup
#### Environment Variables:
Environment variables can be loaded from a `.env` file.

**Mandatory Arg:**
- `PORT`: Port number that has to be exposed (on which a localhost `service/app/api` is running)

**Optional Args:**
- `AMI_ID`: ID of any public AMI with an Ubuntu OS. Defaults to a region specific image ID.
- `ACCESS_KEY`: Access key to access AWS resources. Defaults to `~/.aws/config`
- `SECRET_KEY`: Secret key to access AWS resources. Defaults to `~/.aws/config`
- `REGION_NAME`: Region name where the instance should live. Defaults to `US-WEST-2`
- `DOMAIN`: If the domain name is registered using `route53`. *Example: `mywebsite.com`*
- `SUBDOMAIN`: Sub-domain that has to be added for the domain name. *Example: `tunnel.mywebsite.com`*
- `EMAIL`: Email address to create the self-signed SSL and private key. Defaults to `USER@expose-localhost.com`
- `ORG`: Organization name for the certificate. Defaults to the AWS endpoint.

<details>
<summary><strong>Setup a custom endpoint</strong></summary>

The public DNS names for EC2 instances are long and messy. To avoid that, an `A` record can be added to the `route53` hosted zone.

:warning: &nbsp; Requires an active hosted zone on `route53`.

- `DOMAIN`: If the domain name is registered using `route53`. *Example: `mywebsite.com`*
- `SUBDOMAIN`: Sub-domain that has to be added for the domain name. *Example: `tunnel.mywebsite.com`*

&nbsp; &nbsp; &nbsp; &nbsp; :bulb: &nbsp; This will be the endpoint to access the localhost.

</details>

#### Certificate:
- Securing the tunnel requires the certificate chain and the key file.
- These two files should be saved as `cert.pem` and `key.pem` in either `~.ssh/*.pem` or within `expose` repository.
- No certs? No problem. [`expose`](https://github.com/thevickypedia/expose/blob/main/expose/helpers/cert.py) will 
generate a self-signed certificate and a private key automatically.

<details>
<summary><strong>Generate private SSL certificate</strong></summary>

Unfortunately not many SSL certificate providers give the liberty to download key files. But `expose`, can use private certificates.

:warning: &nbsp; Some web browsers might throw a warning and some might even block a self-signed certificate/private CA.

To manually generate a self-signed cert:

> `openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout ~/.ssh/key.pem -out ~/.ssh/cert.pem`

[OR]

Simply let `expose` create a self-signed SSL certificate and a private key.

- `EMAIL`: Email address to create the self-signed SSL and private key. Defaults to `USER@expose-localhost.com`
- `ORG`: Organization name for the certificate. Defaults to the AWS endpoint.

</details>

### Usage
###### Installation
```shell
python3 -m pip install expose-localhost
```

###### Start tunneling:
```python
import expose

tunnel = expose.Tunnel()
tunnel.start()
```

###### Stop tunneling:
```python
import expose

tunnel = expose.Tunnel()
tunnel.stop()
```

<details>
<summary><strong>Troubleshooting</strong></summary>

> If `E: Could not get lock /var/lib/dpkg/lock-frontend` occurs during startup, simply rerun the script with start command.
> This occurs when `apt` hasn't released the resources yet. Re-running `tunnel.start()` will simply re-configure the instance.

</details>

## Coding Standards
Docstring format: [`Google`](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) <br>
Styling conventions: [`PEP 8`](https://www.python.org/dev/peps/pep-0008/) <br>
Clean code with pre-commit hooks: [`flake8`](https://flake8.pycqa.org/en/latest/) and 
[`isort`](https://pycqa.github.io/isort/)

## [Release Notes](https://github.com/thevickypedia/expose/blob/main/release_notes.rst)
**Requirement**
```shell
python -m pip install changelog-generator
```

**Usage**
```shell
changelog reverse -f release_notes.rst -t 'Release Notes'
```

## Linting
`PreCommit` will ensure linting, and the doc creation are run on every commit.

**Requirement**
```shell
pip install sphinx==5.1.1 pre-commit recommonmark
```

**Usage**
```shell
pre-commit run --all-files
```

### Pypi Package
[![pypi-module](https://img.shields.io/badge/Software%20Repository-pypi-1f425f.svg)](https://packaging.python.org/tutorials/packaging-projects/)

[https://pypi.org/project/expose-localhost/](https://pypi.org/project/expose-localhost/)

### Runbook
[![made-with-sphinx-doc](https://img.shields.io/badge/Code%20Docs-Sphinx-1f425f.svg)](https://www.sphinx-doc.org/en/master/man/sphinx-autogen.html)

[https://thevickypedia.github.io/expose/](https://thevickypedia.github.io/expose/)

## License & copyright

&copy; Vignesh Sivanandha Rao

Licensed under the [MIT License](https://github.com/thevickypedia/expose/blob/main/LICENSE)
