-
Notifications
You must be signed in to change notification settings - Fork 1
/
domain.py
executable file
·153 lines (129 loc) · 5.34 KB
/
domain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# coding=utf-8
"""
@version:0.1
@author: ysicing
@file: ex_domain/domain.py
@time: 2017/11/7 11:29
"""
import sys
import argparse
from resolver import DomainRecorder
from tools import make_wild_name, is_passive_wild_name
from interaction import Input
from config import Config
api_key = Config.api_key
secret_key = Config.secret_key
domain_suffix = Config.domain_suffix
domain_id = Config.domain_id
resolver = DomainRecorder(api_key=api_key, secret_key=secret_key, domain_suffix=domain_suffix, domain_id=domain_id)
def do_stdout(arg, end=True):
sys.stdout.write('{}'.format(arg))
if end:
sys.stdout.write("\n")
sys.stdout.flush()
class ExDomain(object):
def __init__(self, parse_args, *args, **kwargs):
self.ip = parse_args.ip
def get_random_suffix(self):
dr = DomainRecorder(api_key=api_key, secret_key=secret_key, domain_suffix=domain_suffix, domain_id=domain_id)
for count in range(5):
new_suffix = make_wild_name()
if dr.exist_wild(new_suffix):
print("exist wild_suffix {}, retry.".format(new_suffix))
continue
else:
return new_suffix
print("unlucky, test 5 wild_domain failed, please try again.")
return None
@classmethod
def init_domain(cls, parse_args):
obj = cls(parse_args)
if resolver.ping() is False:
print("looks like I can't access internet....")
input_domain = Input.read("Setup wild_domain for this region,like '<name>.goodrain.org'", blank=False)
else:
input_domain = Input('', is_blank=True)
if input_domain.is_blank:
suffix = obj.get_random_suffix()
if suffix is None:
return 1
resolver.add_wild(suffix, parse_args.ip)
if resolver.exist_wild(suffix):
print("prepare domain *.{}.goodrain.org finished".format(suffix))
web_domain = '{}.goodrain.org'.format(suffix)
else:
print("prepare domain *.{}.goodrain.org failed,retry".format(suffix))
else:
if input_domain.value.endswith('.goodrain.org'):
suffix = input_domain.value.replace('.goodrain.org', '').lstrip('*.')
try:
if is_passive_wild_name(suffix):
web_domain = '{}.goodrain.org'.format(suffix)
except Exception as e:
print(e)
return 1
else:
print("Use custom domain:{}".format(input_domain.value))
web_domain = input_domain.value
print("init successful:{}".format(web_domain))
return 0
@classmethod
def del_domain(cls, parse_args):
if parse_args.domain.endswith('.goodrain.org'):
suffix = parse_args.domain.replace('.goodrain.org', '').lstrip('*.')
else:
suffix = parse_args.domain.lstrip('*.')
if resolver.exist_wild(suffix):
print("delete domain:{}.goodrain.org".format(suffix))
resolver.del_wild(suffix)
if resolver.exist_wild(suffix):
print("delete failed...")
return 0
print("delete sucessful:{}.goodrain.org".format(suffix))
else:
print("domain {}.goodrain.org not exist.".format(suffix))
@classmethod
def update_domain(cls, parse_args):
if parse_args.domain.endswith('.goodrain.org'):
suffix = parse_args.domain.replace('.goodrain.org', '').lstrip('*.')
else:
suffix = parse_args.domain.lstrip('*.')
if resolver.exist_wild(suffix):
print("update domain:{}.goodrain.org".format(suffix))
resolver.update_wild(suffix, parse_args.ip)
if resolver.exist_wild(suffix):
print("update sucessful:{}.goodrain.org --> {}".format(suffix, parse_args.ip))
return 0
print("update failed...")
else:
print("domain {}.goodrain.org not exist.".format(suffix))
def main():
parser = argparse.ArgumentParser(description="Goodrain Domain DC-Tools")
subparsers = parser.add_subparsers(metavar='<subcommand>')
init_command = subparsers.add_parser('init', help='init domain resolve')
init_command.add_argument('--ip', action='store', default='127.0.0.1')
init_command.set_defaults(func=ExDomain.init_domain)
del_command = subparsers.add_parser('del', help='del domain resolve')
del_command.add_argument('--domain', action='store', default=None, help='just like www.google.com')
del_command.set_defaults(func=ExDomain.del_domain)
update_command = subparsers.add_parser('update', help='update domain resolve')
update_command.add_argument('--ip', action='store', default='127.0.0.1')
update_command.add_argument('--domain', action='store', default=None, help='just like www.google.com')
update_command.set_defaults(func=ExDomain.update_domain)
args = parser.parse_args()
"""
stdout args
"""
# do_stdout(args)
if hasattr(args, 'func') and args.func is not None:
args.func(args)
else:
print("<subcommand> choose from 'init', 'del', 'update'.")
print("Thank you.")
return 1
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
print("exit...")