Created
April 27, 2021 13:26
-
-
Save geektheripper/160aab5bc6873cb0fbeceef56f7e8ca9 to your computer and use it in GitHub Desktop.
阿里云函数计算DDNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:global DDns do={ | |
:local DDnsDomain "$DDnsSubDomain.<your-domain>"; | |
:local DDnsApiURL "https://<account-id>.<region>.fc.aliyuncs.com/2016-08-15/proxy/<your-service>/<your-function>/<your-secert-key>"; | |
:local OldIP [/ip dns static get [find name="$DDnsDomain"] address]; | |
:local NewIPTemp [/ip address get [find interface="$DDnsInterface"] address]; | |
:local NewIP [:pick $NewIPTemp 0 ([:len $NewIPTemp]-3)]; | |
:if ($NewIP != $OldIP) do={ | |
:log info "interface $DDnsInterface ip address $OldIP changed to $NewIP"; | |
/ip dns static set [find name="$DDnsDomain"] address=$NewIP | |
/tool fetch mode=https url="$DDnsApiURL/$DDnsSubDomain/$NewIP" keep-result=no | |
} | |
} | |
:do { | |
$DDns DDnsSubDomain="unicom-01" DDnsInterface="pppoe-unicom-1" | |
} on-error={ :put "unicom-01 ddns setup failed" }; | |
:do { | |
$DDns DDnsSubDomain="unicom-02" DDnsInterface="pppoe-unicom-2" | |
} on-error={ :put "unicom-02 ddns setup failed" }; | |
:do { | |
$DDns DDnsSubDomain="unicom-03" DDnsInterface="pppoe-unicom-3" | |
} on-error={ :put "unicom-03 ddns setup failed" }; | |
:do { | |
$DDns DDnsSubDomain="unicom-04" DDnsInterface="pppoe-unicom-4" | |
} on-error={ :put "unicom-04 ddns setup failed" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Core = require('@alicloud/pop-core'); | |
const client = new Core({ | |
accessKeyId: '<your-access-key-id>', | |
accessKeySecret: '<your-access-key-secret>', | |
endpoint: 'https://alidns.aliyuncs.com', | |
apiVersion: '2015-01-09' | |
}); | |
function apiCall(action, params) { | |
return client.request(action, params, { method: 'POST' }) | |
} | |
async function getRecordId(domain) { | |
const { DomainRecords } = await apiCall('DescribeSubDomainRecords', { | |
RegionId: "cn-hangzhou", | |
SubDomain: domain | |
}) | |
const Record = DomainRecords.Record.filter((r) => r.Status === 'ENABLE' && r.Type === 'A')[0] | |
return Record && Record.RecordId | |
} | |
module.exports.handler = async function (req, resp) { | |
const [secretKey, subDomain, clientIP] = req.path.split('/').filter(i => i); | |
if (secretKey !== process.env.SECRET_KEY) return resp.send('ERROR'); | |
const RecordId = await getRecordId(subDomain + '.<your-domain>') | |
const params = { | |
RegionId: "cn-hangzhou", | |
RecordId, | |
DomainName: "<your-primary-domain>", | |
RR: subDomain + '..<your-sub-domain>', | |
Type: "A", | |
Value: clientIP || req.clientIP | |
}; | |
resp.send(JSON.stringify(await apiCall(RecordId ? 'UpdateDomainRecord' : 'AddDomainRecord', params))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment