Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
initial hack
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Apr 5, 2017
0 parents commit a90bf80
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist/
build/
*.egg*
*.pyc
__pycache__
.cache/
*.swp
.tox/
.idea/
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions README.rst
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 added local_cname/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions local_cname/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from local_cname.cli import main

main()
52 changes: 52 additions & 0 deletions local_cname/cli.py
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)
35 changes: 35 additions & 0 deletions setup.py
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']}
)

0 comments on commit a90bf80

Please sign in to comment.