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

IP变更信息通过server酱推送到微信 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vcancy
Copy link

@vcancy vcancy commented Dec 6, 2017

IP变更信息通过server酱推送到微信

@vcancy
Copy link
Author

vcancy commented Dec 6, 2017

IP变更信息通过server酱推送到微信

@rfancn
Copy link
Owner

rfancn commented Dec 6, 2017

@vcancy 十分感谢你的PR, 下面是我的一点建议:

  1. 以后所有的feature的配置全部建议放在feature_[feature name]的section里面, 这样逻辑更清晰
[feature_serverchan]
enabled=false
sckey = 
  1. 如果新功能的配置全部放在feature_xxx的section中的话, 需要修改config.py过滤掉feature_开头的section

FROM:

    def get_domain_record_sections(self):
        """
        Get sections other than default, which contains DomainRecord definition
        :return: section list
        """
        return self.parser.sections()

TO:

    def get_domain_record_sections(self):
        """
        Get sections other than default, which contains DomainRecord definition
        :return: section list
        """
        # filter out feature_sections
        sections = self.parser.sections()
        return [ s for s in sections if not s.lower().startswith("feature_") ]
  1. 在读取serverchan配置的时候,因为之前版本的serverchan相关的配置选项不是必须的,为了与以前的配置文件兼容,不会导致用户由于更新了新版本因为读取不到serverchan的配置而出错,建议在config.py这样处理:
if not self.access_id or not self.access_key:
            DDNSUtils.err_and_exit("Invalid access_id or access_key in config file.")

# if we specify section "feature_serverchan", then we need read the required options
if self.parser.has_section("feature_serverchan"):
    self.get_feature_serverchan_options()

def get_feature_serverchan_options():
    section_name = "feature_serverchan"
    try:
        enabled = self.parser.getboolean(section_name, "enabled")
    except ValueError as ex:
        DDNSUtils.err_and_exit("Invalid 'enabled' value in feature serverchan config: {0}".format(ex))
    except ConfigParser.NoOptionError as ex:
        enabled = False

    if enabled:
       try:
           sckey = self.parser.getboolean(section_name, "sckey")
       except ConfigParser.NoOptionError as ex:
           DDNSUtils.err_and_exit("No sckey specified")

    if sckey == "":
        DDNSUtils.err_and_exit("Empty serverchan sckey")

    self.serverchan_enabled = enabled
    self.serverchan_sckey = sckey
  1. 为效率考虑,在ddns.py里面判断self.serverchan_enabled is True的时候才去调用push_serverchan_msg()
if not remote_record:
              DDNSUtils.err("Failed finding remote DomainRecord" \		              DDNSUtils.err("Failed finding remote DomainRecord" \
                            "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))		                            "[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
             if config.serverchan_enabled:
                    DDNSUtils.push_serverchan_msg(config.serverchan_sckey,"更新DNS失败:外网IP[{remote}]".format(remote=remote_record.value))
...
  1. 在push_serverchan_msg()里面需要用try...except处理错误,以防影响主功能的运行。
    try:
         requests.get('https://sc.ftqq.com/{}.send?text={}&desp={}'.format(sckey,title,msg)
    except:
         pass
  1. 建议能不能在Readme里面加入"server酱"功能的基本介绍以及各个参数代表的意思,以及简单示例配置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants