Module: Model::AcmeIdentifier

Extended by:
ActiveSupport::Concern
Included in:
Com::AcmeIdentifier
Defined in:
app/models/com/model/acme_identifier.rb

Instance Method Summary collapse

Instance Method Details

#authorizationObject



118
119
120
121
122
123
124
125
# File 'app/models/com/model/acme_identifier.rb', line 118

def authorization
  auth = acme_order.order.authorizations.find { |i| domain == i.domain && wildcard.present? == i.wildcard.present? }
rescue Acme::Client::Error::BadNonce
  retry
else
  save_auth(auth)
  auth
end

#auto_verifyObject



76
77
78
79
80
81
82
# File 'app/models/com/model/acme_identifier.rb', line 76

def auto_verify
  if file_name.present? && file_content.present?
    file_verify?
  else
    dns_verify?
  end
end

#compute_wildcardObject



39
40
41
42
43
44
45
46
# File 'app/models/com/model/acme_identifier.rb', line 39

def compute_wildcard
  if identifier.start_with?('*.')
    self.wildcard = true
    self.domain = identifier.delete_prefix('*.')
  else
    self.domain = identifier
  end
end

#deactivateObject



127
128
129
# File 'app/models/com/model/acme_identifier.rb', line 127

def deactivate
  acme_order..client.deactivate_authorization(url: url)
end

#dns_hostObject



114
115
116
# File 'app/models/com/model/acme_identifier.rb', line 114

def dns_host
  "#{record_name}.#{domain}"
end

#dns_resolvObject



56
57
58
59
60
61
# File 'app/models/com/model/acme_identifier.rb', line 56

def dns_resolv
  Resolv::DNS.open do |dns|
    records = dns.getresources dns_host, Resolv::DNS::Resource::IN::TXT
    records.map!(&:data)
  end
end

#dns_verify?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/com/model/acme_identifier.rb', line 63

def dns_verify?
  unless dns_resolv.include?(record_content)
    ensure_dns
  end

  auth = authorization
  auth.dns.request_validation
  if auth.reload && auth.status == 'valid'
    self.update dns_valid: true, status: 'valid'
  end
  dns_valid
end

#ensure_dnsObject

todo use aliyun temply



49
50
51
52
53
54
# File 'app/models/com/model/acme_identifier.rb', line 49

def ensure_dns
  r = AliDns.add_acme_record domain, record_content
  if r['RecordId']
    AliDns.check_record(domain, record_content)
  end
end

#file_verify?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/com/model/acme_identifier.rb', line 84

def file_verify?
  file_path = Rails.root.join('public', file_name)

  unless file_path.file? && file_path.read == file_content
    file_path.dirname.exist? || file_path.dirname.mkpath
    File.open(file_path, 'w') do |f|
      f.write file_content
    end
  end

  auth = authorization
  auth.http.request_validation
  if auth.reload && auth.status == 'valid'
    self.update file_valid: true, status: 'valid'
  end

  file_valid
end

#renew_dns_validObject



31
32
33
# File 'app/models/com/model/acme_identifier.rb', line 31

def renew_dns_valid
  self.dns_valid = false
end

#renew_file_validObject



35
36
37
# File 'app/models/com/model/acme_identifier.rb', line 35

def renew_file_valid
  self.file_valid = false
end

#save_auth(auth = authorization) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'app/models/com/model/acme_identifier.rb', line 103

def save_auth(auth = authorization)
  update(
    record_name: auth.dns&.record_name,
    record_content: auth.dns&.record_content,
    file_name: auth.http&.filename,
    file_content: auth.http&.file_content,
    url: auth.url,
    status: auth.status
  )
end