-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4481571
commit 895fe17
Showing
8 changed files
with
164 additions
and
15 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
FROM scratch | ||
LABEL maintainer="waterlemons2k <[email protected]>" | ||
|
||
COPY ca-certificates.crt /etc/ssl/certs/ | ||
COPY ddns-go /app/ | ||
COPY Shanghai /usr/share/zoneinfo/Asia/ | ||
|
||
ENV TZ=Asia/Shanghai | ||
EXPOSE 9876 | ||
|
||
ENTRYPOINT ["/app/ddns-go"] | ||
CMD ["-l", ":9876", "-f", "300"] | ||
CMD ["-l", ":9876", "-f", "300"] |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/WaterLemons2k/Docker-ddns-go/tools/json" | ||
"github.com/WaterLemons2k/Docker-ddns-go/tools/untar" | ||
) | ||
|
||
// Release represents a GitHub release. | ||
// | ||
// https://docs.github.com/en/rest/releases/releases#get-a-release-by-tag-name | ||
type Release struct { | ||
Assets []Asset `json:"assets"` | ||
} | ||
|
||
// Asset represents a GitHub release asset. | ||
type Asset struct { | ||
Name string `json:"name"` | ||
URL string `json:"browser_download_url"` | ||
} | ||
|
||
const ( | ||
// assetName indicates that the file with assetName in the asset will be downloaded. | ||
assetName = "linux_x86_64" | ||
|
||
// repo is the name of the GitHub repository without the owner part. | ||
repo = "ddns-go" | ||
) | ||
|
||
var client = &http.Client{Timeout: 10 * time.Second} | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("Usage: go run -C tools . <version>") | ||
return | ||
} | ||
version := os.Args[1] | ||
|
||
url := fmt.Sprintf("https://api.github.com/repos/jeessy2/%s/releases/tags/v%s", repo, version) | ||
|
||
body := getBody(url) | ||
defer body.Close() | ||
|
||
var release Release | ||
if err := json.Parse(body, &release); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, asset := range release.Assets { | ||
// We only focus on asset with assetName | ||
if !strings.Contains(asset.Name, assetName) { | ||
continue | ||
} | ||
|
||
body := getBody(asset.URL) | ||
defer body.Close() | ||
fmt.Println(asset.Name, "downloaded. Extracting", repo, "from the tar.gz file...") | ||
|
||
// Extracts the file with repo from the tar.gz file | ||
untar.SpecificFile(body, repo) | ||
break | ||
} | ||
} | ||
|
||
// getBody gets the response body from the given url. | ||
func getBody(url string) io.ReadCloser { | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return resp.Body | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/WaterLemons2k/Docker-ddns-go/tools | ||
|
||
go 1.20 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
// Parse parses the JSON-encoded body and stores the result in the value pointed to by v. | ||
func Parse(body io.Reader, v any) error { | ||
data, err := io.ReadAll(body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return json.Unmarshal(data, v) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package untar untars a tarball to disk. | ||
package untar | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// SpecificFile extracts the file with the specified filename from | ||
// the tar.gz file in src and writes it to the file. | ||
func SpecificFile(src io.Reader, filename string) { | ||
gz, err := gzip.NewReader(src) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer gz.Close() | ||
|
||
t := tar.NewReader(gz) | ||
for { | ||
header, err := t.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
log.Fatal(err) | ||
} | ||
|
||
// Extracts only regular file with the specified filename | ||
if !(header.Typeflag == tar.TypeReg && strings.Contains(header.Name, filename)) { | ||
continue | ||
} | ||
|
||
// Write the extracted file to the parent directory of the executable | ||
out, err := os.Create(filepath.Join("..", header.Name)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer out.Close() | ||
|
||
if _, err = io.Copy(out, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |