Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
gutenye committed Feb 16, 2016
0 parents commit efca67d
Showing 9 changed files with 406 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.tmp
/aliyundns
/dist
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
Aliyundns, a simple cli to list and update Aliyun DNS record.
================

[Homepage](https://github.com/gutenye/aliyundns) |
[Documentation](https://github.com/gutenye/aliyundns/wiki) |
[Issue Tracker](https://github.com/gutenye/aliyundns/issues) |
[MIT License](http://choosealicense.com/licenses/mit) |
[by Guten](http://guten.me) |
[Gratipay](https://gratipay.com/gutenye) |
[Bountysource](https://www.bountysource.com/teams/gutenye)

| | |
|----------------|------------------------------------------------------------|
| | **Install** |
| ArchLinux | `$ pacaur -S xx` |
| Manually | `$ go get github.com/gutenye/aliyundns` |

...

Getting started
---------------

Used in OpenWRT

Start a new server in your personal server, then use OpenWRT ddns

```
$ aliyundns server 3000
$ edit ddns custom url in OpenWRT: <server-ip>:3000/?id=12345679&rr=www&value=[IP]&access_key_id=[USERNAME]&access_key_secret=[PASSWORD]
```

Other Usages

```
$ edit ~/.aliyundnsrc
ACCESS_KEY_ID = "x"
ACCESS_KEY_SECRET = "x"
$ aliyundns list example.com
12345678 www 1.1.1.1 example.com A
$ aliyundns update 12345678 www 2.2.2.2
```

Development
===========

Contributing
-------------

* Submit any bugs/features/ideas to github issue tracker.
* Thanks to [all contributors](https://github.com/gutenye/aliyundns/contributors?type=a).

Resource
--------

* [Mickyxing/aliyundns](https://github.com/Mickyxing/aliyundns), similar project, but written in Python.

Copyright
---------

The MIT License

Copyright (c) 2015 Guten Ye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
133 changes: 133 additions & 0 deletions ake
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env bash
#
# Usage
#
# ./ake release # build, package, and upload

APP="aliyundns"
EXTRA_FILES=""
HOST="root@box"

RELEASE="linux/amd64"
VERSION=$(cat VERSION)
DESTDIR=".tmp/$APP-$VERSION"
declare -A OS_MAP=(
[homebrew]="darwin"
)
declare -A DIR_MAP=(
[homebrew]="/usr/share/$APP"
)

platform=""
os=""
arch=""
assets_dir=""

cgo_enabled() { [ "$1" == "$GOHOSTOS" ] && echo 1 || echo 0; }
ext() { [ $1 == "windows" ] && echo .exe || echo ""; }

main() {
go run *.go "$@"
}

release() {
GOROOT="/home/guten/dev/src/go/go"
rm *.zip *.tar.gz 2>/dev/null
for release in $RELEASE; do
platform=${release%/*}; arch=${release#*/}; os=${OS_MAP[$platform]-$platform}
assets_dir=${DIR_MAP[$platform]-$o_assets_dir}
build
package
done
upload *.zip *.tar.gz
}

######
# lib
#####

get_deps() {
go get -v ./...
}

update-version() {
sed -i "s/app.Version.*/app.Version = \"$VERSION\"/" main.go
}

# dist{platform, os, arch, assets_dir}
prepare() {
rm -r .tmp 2>/dev/null
mkdir -p $DESTDIR
mkdir -p dist

if [ ! -z $EXTRA_FILES ]; then
cp -r $EXTRA_FILES $DESTDIR
fi
}

# build{platform, os, arch, assets_dir}
build() {
prepare
update-version

#rsync -a --del --exclude '.*' . /tmp/$APP/

echo -e "\nBuilding $platform/$arch"
#CGO_ENABLED=$(cgo_enabled $os) GOOS=$os GOARCH=$arch $GOROOT/bin/go build -o "$DESTDIR/$APP$(ext $os)"
GOOS=$os GOARCH=$arch $GOROOT/bin/go build -o "$DESTDIR/$APP$(ext $os)"

#rsync -a /tmp/$APP/ .
}

# package{platform, os, arch}
package() {
echo "Packing $platform/$arch"
case $os in
linux | darwin ) tar zcvf dist/$APP.$platform.$arch-$VERSION.tar.gz -C .tmp $APP-$VERSION;;
windows ) cd .tmp && zip -r ../dist/$APP.$platform.$arch-$VERSION.zip $APP-$VERSION && cd .. ;;
esac
}

upload() {
#for file in $*; do
#s3cmd put --acl-public $file s3://downloads.gutenye.com/$APP/
#current=$(echo $file | sed -r 's/(.*)-[0-9.]+(\..*)$/\1\2/')
#s3cmd copy s3://downloads.gutenye.com/$APP/$file s3://downloads.gutenye.com/$APP/$current
#done
:
}

clean() {
rm -r dist/*
}

deploy() {
platform="linux"; arch="amd64" os="linux"
build
scp misc/aliyundns.service "$HOST:/etc/systemd/system"
scp $DESTDIR/aliyundns "$HOST:/usr/bin"
}

############
# main
############

eval "$(go env)"

o_assets_dir="runtime"
o_package=false
while getopts "d:p" opt; do
case $opt in
d ) o_assets_dir=$OPTARG ;;
p ) o_package=true ;;
esac
done
shift $(( OPTIND - 1 ))

case $1 in
release ) shift && release "$@" ;;
u | upload ) shift && upload "$@";;
c | clean ) shift && clean "$@" ;;
d | deploy) shift && deploy"$@" ;;
* ) main "$@";;
esac
32 changes: 32 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"github.com/denverdino/aliyungo/dns"
"github.com/gutengo/shell"
)

func List(domain string) {
client := dns.NewClient(rc.ACCESS_KEY_ID, rc.ACCESS_KEY_SECRET)
res, err := client.DescribeDomainRecords(&dns.DescribeDomainRecordsArgs{
DomainName: domain,
})
if err != nil {
shell.ErrorExit(err)
}

for _, v := range res.DomainRecords.Record {
fmt.Println(v.RecordId, v.RR, v.Value, v.DomainName, v.Type)
}
}

func Update(recordId, rr, value string) error {
client := dns.NewClient(rc.ACCESS_KEY_ID, rc.ACCESS_KEY_SECRET)
_, err := client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{
RecordId: recordId,
RR: rr,
Value: value,
Type: dns.ARecord,
})
return err
}
99 changes: 99 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
"github.com/gutengo/fil"
"github.com/gutengo/shell"
"os"
)

var pd = fmt.Println

type Rc struct {
ACCESS_KEY_ID string
ACCESS_KEY_SECRET string
}

var rc Rc

func main() {
cli.AppHelpTemplate = `{{.Name}} v{{.Version}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[options] {{end}}<command> [arguments]
COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
GLOBAL OPTIONS:
{{range .Flags}}{{.}}
{{end}}{{end}}
`

app := cli.NewApp()
app.Name = "aliyundns"
app.Usage = "list and update dns record"
app.Version = "0.0.1"

app.Action = func(c *cli.Context) {
cli.ShowAppHelp(c)
}

app.Commands = []cli.Command{
{
Name: "list",
Usage: "<domain>",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
shell.ErrorExit("Missing domain argument.\n\nExample:\n\n $ aliyundns list example.com")
}
List(args.Get(0))
},
},
{
Name: "update",
Usage: "<record_id> <rr> <value>",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 3 {
shell.ErrorExit("Must provide 3 arguments.\n\nExample:\n\n $ aliyundns update 1234567 www 192.168.1.1")
}
err := Update(args.Get(0), args.Get(1), args.Get(2))
if err != nil {
shell.ErrorExit(err)
}
fmt.Println("Success")
},
},
{
Name: "server",
Usage: "<port>",
Action: func(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
shell.ErrorExit("Must provide 2 arguments.\n\nExample:\n\n $ aliyundns server 3000")
}
Server(args.Get(0))
},
},
}

rc = loadRc()
app.Run(os.Args)
}

func loadRc() (ret Rc) {
file := os.Getenv("HOME") + "/.aliyundnsrc"

if ok, _ := fil.IsNotExist(file); ok {
return Rc{}
}

if _, err := toml.DecodeFile(file, &ret); err != nil {
shell.ErrorExit("%s: %s\n", "Load "+file, err)
}
return ret
}
9 changes: 9 additions & 0 deletions misc/aliyundns.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Aliyun DNS
After=network.target

[Service]
ExecStart=/usr/bin/aliyundns server 2014

[Install]
WantedBy=multi-user.target
28 changes: 28 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"log"
"net/http"
)

func Server(port string) {
http.HandleFunc("/", homeHandler)
fmt.Println(">> Listen on " + port)
err := http.ListenAndServe(":"+port, nil)
log.Fatal(err)
}

func homeHandler(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
rc.ACCESS_KEY_ID = req.FormValue("access_key_id")
rc.ACCESS_KEY_SECRET = req.FormValue("access_key_secret")
err := Update(req.FormValue("id"), req.FormValue("rr"), req.FormValue("value"))
if err != nil {
http.Error(res, err.Error(), 400)
} else {
fmt.Fprint(res, "Success")
}
}
}

0 comments on commit efca67d

Please sign in to comment.