Skip to content

Commit ec7cf7f

Browse files
Merge branch 'develop'
2 parents c8cef80 + d4af70a commit ec7cf7f

18 files changed

Lines changed: 304 additions & 179 deletions

File tree

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.env
2+
.gitignore
3+
.github
4+
.idea

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Aliddns
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
install:
7+
name: Install
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/setup-go@v2
11+
- uses: actions/checkout@v2
12+
- uses: actions/cache@v2
13+
with:
14+
path: ~/go/pkg/mod
15+
key: ${{ runner.os }}-${{ hashFiles('**/go.sum') }}
16+
restore-keys: ${{ runner.os }}-
17+
- name: Install dependencies
18+
run: go get -v -t -d ./...
19+
lint:
20+
needs: install
21+
name: Lint
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v2
25+
- uses: golangci/golangci-lint-action@v1
26+
with:
27+
version: v1.27

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ linters:
5454
- nakedret
5555
- unconvert
5656
- unparam
57-
fast: true
57+
fast: false

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ FROM golang:alpine as builder
22

33
ADD . /aliddns-src
44

5+
ENV GOPROXY='https://goproxy.cn/,direct'
6+
ENV GOSUMDB=off
7+
58
RUN cp /etc/apk/repositories /etc/apk/repositories.backup && \
69
sed -i -E "s|http://.+/alpine|http://mirrors\.aliyun\.com/alpine|" /etc/apk/repositories && \
7-
apk add --no-cache --virtual .build-deps git && \
10+
apk add --no-cache git make && \
811
cd /aliddns-src && \
9-
go build -gcflags=-trimpath=${GOPATH} -asmflags=-trimpath=${GOPATH} -ldflags '-s -w' -o aliddns && \
12+
make build && \
1013
cp aliddns /
1114

1215
FROM alpine:latest

Makefile

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
.PHONY: all test clean vendor
22

3-
ldflags = -ldflags="-s -w"
3+
buildTime = `date +%Y-%m-%dT%T%z`
4+
gitCommit = `git rev-parse --short HEAD`
5+
gitTag = `git describe --tags --abbrev=0 2> /dev/null`
6+
7+
ldflags = -ldflags="-s -w -X main.buildTime=${buildTime} -X main.gitCommit=${gitCommit} -X main.gitTag=${gitTag}"
48
gcflags = -gcflags="-trimpath=${PWD}"
59
output = -o=aliddns
610

7-
build: # build
11+
build:
812
CGO_ENABLED=0 go build ${ldflags} ${gcflags} -v ${output}
913

10-
build-linux:
11-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${ldflags} ${gcflags} -v ${output}
12-
1314
lint:
14-
CGO_ENABLED=0 GOGC=15 golangci-lint run
15-
16-
lint-ci:
17-
CGO_ENABLED=0 GOGC=15 golangci-lint run -v
18-
19-
lint-fix:
20-
CGO_ENABLED=0 GOGC=15 golangci-lint run --fix
15+
CGO_ENABLED=0 golangci-lint run --concurrency=2
2116

2217
tidy:
2318
go mod tidy
19+
20+
image = "darthminion/aliddns"
21+
22+
build-image:
23+
docker build -t ${image} .
24+
25+
push-image:
26+
docekr push ${image}

README.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Ali DDNS
22

3+
![Aliddns](https://github.com/DarthPestilane/aliddns/workflows/Aliddns/badge.svg)
4+
35
调用阿里云提供的API,刷新域名解析,将其解析到当前IP地址。
46

57
本来想用php写的,但是php版本的sdk写的太烂了(大家可以感受一下:[github/aliyun-stupid-php-sdk](https://github.com/aliyun/aliyun-openapi-php-sdk)),没有composer,连注释也没有,不想用,故换成golang。

app/app.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app
2+
3+
import "github.com/DarthPestilane/aliddns/app/logger"
4+
5+
func Log() *logger.Logger {
6+
return logger.Provide()
7+
}

app/cmd/cmd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"github.com/DarthPestilane/aliddns/app/cmd/server"
5+
"github.com/DarthPestilane/aliddns/app/cmd/version"
6+
"github.com/urfave/cli"
7+
"os"
8+
)
9+
10+
func Run(buildTime, gitCommit, gitTag string) {
11+
info := &version.BuildInfo{
12+
BuildTime: buildTime,
13+
GitCommit: gitCommit,
14+
GitTag: gitTag,
15+
}
16+
if info.GitTag == "" {
17+
info.GitTag = "in-dev"
18+
}
19+
20+
app := cli.NewApp()
21+
app.Version = info.GitTag
22+
app.Commands = []cli.Command{
23+
server.Command(),
24+
version.Command(info),
25+
}
26+
if err := app.Run(os.Args); err != nil {
27+
panic(err)
28+
}
29+
}

cmd_run.go renamed to app/cmd/server/server.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
package main
1+
package server
22

33
import (
4-
"encoding/json"
54
"fmt"
5+
"github.com/DarthPestilane/aliddns/app"
6+
"github.com/DarthPestilane/aliddns/app/dns"
7+
"github.com/DarthPestilane/aliddns/app/helper"
8+
jsoniter "github.com/json-iterator/go"
69
"github.com/urfave/cli"
710
"net/http"
811
"strconv"
912
)
1013

11-
func cmdRun() cli.Command {
12-
defaultPort, err := strconv.Atoi(env("PORT", "8888"))
14+
func Command() cli.Command {
15+
defaultPort, err := strconv.Atoi(helper.Env("PORT", "8888"))
1316
if err != nil {
1417
panic(fmt.Errorf("parse env PORT failed: %v", err))
1518
}
@@ -24,7 +27,7 @@ func cmdRun() cli.Command {
2427
},
2528
Action: func(ctx *cli.Context) {
2629
port := ctx.Int("port")
27-
Log.Info(fmt.Sprintf("listening at port %d", port))
30+
app.Log().Info(fmt.Sprintf("listening at port %d", port))
2831
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2932
w.Header().Set("Content-Type", "application/json")
3033

@@ -35,7 +38,7 @@ func cmdRun() cli.Command {
3538
var domainName string
3639
if domains, has := query["domain_name"]; !has || domains[0] == "" {
3740
w.WriteHeader(422)
38-
b, _ := json.Marshal(map[string]interface{}{
41+
b, _ := jsoniter.Marshal(map[string]interface{}{
3942
"success": false,
4043
"errors": "domain_name is required",
4144
})
@@ -52,13 +55,13 @@ func cmdRun() cli.Command {
5255
}
5356

5457
// ip
55-
currentIP := ip(r)
58+
currentIP := helper.IP(r)
5659

57-
// bind
58-
dns := NewDns(domainName, currentIP, rr)
59-
Log.Info("=====")
60-
if err := dns.Bind(); err != nil {
61-
b, _ := json.Marshal(map[string]interface{}{
60+
// bind dns
61+
dnsHandler := dns.New(domainName, currentIP, rr)
62+
app.Log().Info("=====")
63+
if err := dnsHandler.Bind(); err != nil {
64+
b, _ := jsoniter.Marshal(map[string]interface{}{
6265
"success": false,
6366
"errors": err.Error(),
6467
})
@@ -68,18 +71,18 @@ func cmdRun() cli.Command {
6871
}
6972

7073
w.WriteHeader(200)
71-
b, err := json.Marshal(map[string]interface{}{
74+
b, err := jsoniter.Marshal(map[string]interface{}{
7275
"success": true,
7376
"message": fmt.Sprintf("set ip of '%s.%s' to %s", rr, domainName, currentIP),
7477
})
7578
if err != nil {
76-
Log.Error("decode response failed", err)
79+
app.Log().Error("decode response failed", err)
7780
return
7881
}
7982
_, _ = w.Write(b)
8083
})
8184
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
82-
panic(fmt.Errorf("start http server failed: %v", err))
85+
panic(fmt.Errorf("start http server failed: %s", err))
8386
}
8487
},
8588
}

app/cmd/version/version.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli"
6+
"runtime"
7+
"strings"
8+
)
9+
10+
const tpl = `
11+
Version: %s
12+
Go version: %s
13+
Git commit: %s
14+
Built: %s
15+
OS/Arch: %s/%s
16+
`
17+
18+
func Command(info *BuildInfo) cli.Command {
19+
return cli.Command{
20+
Name: "version",
21+
Usage: "Shows app version",
22+
Action: func(ctx *cli.Context) {
23+
fmt.Println(strings.Trim(fmt.Sprintf(tpl,
24+
info.GitTag,
25+
runtime.Version(),
26+
info.GitCommit,
27+
info.BuildTime,
28+
runtime.GOOS, runtime.GOARCH,
29+
), "\n"))
30+
},
31+
}
32+
}
33+
34+
type BuildInfo struct {
35+
BuildTime string `json:"build_time"`
36+
GitCommit string `json:"git_commit"`
37+
GitTag string `json:"git_tag"`
38+
}

0 commit comments

Comments
 (0)