This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a90bf80
Showing
7 changed files
with
813 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
dist/ | ||
build/ | ||
*.egg* | ||
*.pyc | ||
__pycache__ | ||
.cache/ | ||
*.swp | ||
.tox/ | ||
.idea/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
=========== | ||
Local CNAME | ||
=========== | ||
|
||
.. image:: https://img.shields.io/pypi/v/local-cname.svg | ||
:target: https://pypi.python.org/pypi/local-cname | ||
:alt: Latest Version | ||
|
||
.. image:: https://img.shields.io/pypi/status/local-cname.svg | ||
:target: https://pypi.python.org/pypi/local-cname | ||
:alt: Development Status | ||
|
||
.. image:: https://img.shields.io/pypi/pyversions/local-cname.svg | ||
:target: https://pypi.python.org/pypi/local-cname | ||
:alt: Python Versions | ||
|
||
.. image:: https://img.shields.io/pypi/l/local-cname.svg | ||
:target: https://github.com/hjacobs/local-cname/blob/master/LICENSE | ||
:alt: License | ||
|
||
Installation: | ||
|
||
.. code-block:: | ||
$ sudo pip install -U local-cname | ||
Usage: | ||
|
||
.. code-block:: | ||
$ sudo local-cname FROM_DNS_NAME TO_DNS_NAME | ||
Example: | ||
|
||
.. code-block:: | ||
$ sudo local-cname google.com duckduckgo.com | ||
$ ping google.com | ||
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from local_cname.cli import main | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import argparse | ||
import socket | ||
import time | ||
from pathlib import Path | ||
|
||
from clickclick import Action, info | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('from') | ||
parser.add_argument('to') | ||
args = parser.parse_args() | ||
|
||
hosts_file = Path('/etc/hosts') | ||
|
||
with hosts_file.open() as fd: | ||
old_contents = fd.read() | ||
|
||
backup_file = hosts_file.with_suffix('.local-cname-backup') | ||
with backup_file.open('w') as fd: | ||
fd.write(old_contents) | ||
|
||
try: | ||
while True: | ||
entries = [] | ||
|
||
with Action('Resolving {} ..'.format(args.to)): | ||
results = socket.getaddrinfo(args.to, 80, type=socket.SOCK_STREAM) | ||
for result in results: | ||
family, type, proto, canonname, sockaddr = result | ||
if family in (socket.AF_INET, socket.AF_INET6): | ||
ip = sockaddr[0] | ||
entries.append((getattr(args, 'from'), ip)) | ||
|
||
info('Current entries:') | ||
for hostname, ip in entries: | ||
info('{} -> {}'.format(hostname, ip)) | ||
|
||
with Action('Writing {} ..'.format(hosts_file)): | ||
with hosts_file.open('w') as fd: | ||
fd.write(old_contents) | ||
fd.write('#### Start of entries generated by local-cname\n') | ||
for hostname, ip in entries: | ||
fd.write('{} {}\n'.format(ip, hostname)) | ||
|
||
time.sleep(60) | ||
except KeyboardInterrupt: | ||
# ignore, do not print stacktrace | ||
pass | ||
finally: | ||
backup_file.rename(hosts_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
from setuptools import find_packages, setup | ||
|
||
|
||
def readme(): | ||
try: | ||
return open('README.rst', encoding='utf-8').read() | ||
except TypeError: | ||
return open('README.rst').read() | ||
|
||
|
||
setup( | ||
name='local-cname', | ||
packages=find_packages(), | ||
version='0.2', | ||
description='Local CNAME: ', | ||
long_description=readme(), | ||
author='[email protected]', | ||
url='https://github.com/hjacobs/local-cname', | ||
keywords='dns hosts local', | ||
license='GNU General Public License v3 (GPLv3)', | ||
setup_requires=['flake8'], | ||
install_requires=['clickclick'], | ||
tests_require=[], | ||
classifiers=[ | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 3.4', | ||
'Programming Language :: Python :: 3.5', | ||
'Intended Audience :: Developers', | ||
'Operating System :: OS Independent', | ||
], | ||
entry_points={'console_scripts': ['local-cname = local_cname.cli:main']} | ||
) |