Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to change the source IP. #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion noipy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def execute_update(args):
response_code = None
response_text = None
if update_ddns:
ip_address = args.ip if args.ip else utils.get_ip()
ip_address = args.ip if args.ip else utils.get_ip(args.sourceip)
if not ip_address:
process_message = 'Unable to get IP address. Check connection.'
exec_result = EXECUTION_RESULT_NOK
Expand Down Expand Up @@ -137,6 +137,7 @@ def create_parser():
parser.add_argument('-u', '--usertoken', help='provider username or token')
parser.add_argument('-p', '--password', help='provider password when apply')
parser.add_argument('-n', '--hostname', help='provider hostname to be updated')
parser.add_argument('-s', '--sourceip', help='source ip to use')
parser.add_argument(
'--provider',
help='DDNS provider plugin (default: {})'.format(dnsupdater.DEFAULT_PLUGIN),
Expand Down
9 changes: 7 additions & 2 deletions noipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import socket

import requests
from requests_toolbelt.adapters import source

HTTPBIN_URL = 'https://httpbin.org/ip'

Expand All @@ -21,10 +22,14 @@ def read_input(message):
return input(message)


def get_ip():
def get_ip(source_ip):
"""Return machine's origin IP address."""
try:
r = requests.get(HTTPBIN_URL)
s = requests.Session()
new_source = source.SourceAddressAdapter(source_ip)
s.mount('http://', new_source)
s.mount('https://', new_source)
r = s.get(HTTPBIN_URL)
return r.json()['origin'] if r.status_code == 200 else None
except requests.exceptions.ConnectionError:
return None
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.27.1
requests-toolbelt=0.10.1