Skip to content

Commit f88ef3e

Browse files
author
Tetsuya NAKAMURA
committed
First develop
1 parent c808a7a commit f88ef3e

8 files changed

+338
-0
lines changed

.gitignore

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
# Created by https://www.gitignore.io/api/go,vim,macos,visualstudiocode
3+
4+
### Go ###
5+
# Binaries for programs and plugins
6+
*.exe
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
18+
.glide/
19+
20+
### macOS ###
21+
*.DS_Store
22+
.AppleDouble
23+
.LSOverride
24+
25+
# Icon must end with two \r
26+
Icon
27+
28+
# Thumbnails
29+
._*
30+
31+
# Files that might appear in the root of a volume
32+
.DocumentRevisions-V100
33+
.fseventsd
34+
.Spotlight-V100
35+
.TemporaryItems
36+
.Trashes
37+
.VolumeIcon.icns
38+
.com.apple.timemachine.donotpresent
39+
40+
# Directories potentially created on remote AFP share
41+
.AppleDB
42+
.AppleDesktop
43+
Network Trash Folder
44+
Temporary Items
45+
.apdisk
46+
47+
### Vim ###
48+
# swap
49+
[._]*.s[a-v][a-z]
50+
[._]*.sw[a-p]
51+
[._]s[a-v][a-z]
52+
[._]sw[a-p]
53+
# session
54+
Session.vim
55+
# temporary
56+
.netrwhist
57+
*~
58+
# auto-generated tag files
59+
tags
60+
61+
### VisualStudioCode ###
62+
.vscode/*
63+
!.vscode/settings.json
64+
!.vscode/tasks.json
65+
!.vscode/launch.json
66+
!.vscode/extensions.json
67+
.history
68+
69+
# End of https://www.gitignore.io/api/go,vim,macos,visualstudiocode
70+
71+
### build ###
72+
/check-cert-expire
73+
/check-cert-expire.exe
74+
/release
75+
/build

Makefile

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
NAME := check-cert-expire
2+
VERSION := 0.1.0
3+
4+
.PHONY: \
5+
all \
6+
release_dir \
7+
linux64 \
8+
linux386 \
9+
darwin64 \
10+
darwin386 \
11+
windows64 \
12+
windows386 \
13+
source \
14+
clean
15+
16+
all: linux64 linux386 darwin64 darwin386 windows64 windows386
17+
18+
release_dir:
19+
mkdir -p ./release
20+
21+
linux64: release_dir
22+
$(eval GOOS := linux)
23+
$(eval GOARCH := amd64)
24+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
25+
mkdir -p ./build/${TARGET}
26+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}
27+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
28+
mv ./build/${TARGET}.zip ./release/
29+
30+
linux386: release_dir
31+
$(eval GOOS := linux)
32+
$(eval GOARCH := 386)
33+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
34+
mkdir -p ./build/${TARGET}
35+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}
36+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
37+
mv ./build/${TARGET}.zip ./release/
38+
39+
darwin64: release_dir
40+
$(eval GOOS := darwin)
41+
$(eval GOARCH := amd64)
42+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
43+
mkdir -p ./build/${TARGET}
44+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}
45+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
46+
mv ./build/${TARGET}.zip ./release/
47+
48+
darwin386: release_dir
49+
$(eval GOOS := darwin)
50+
$(eval GOARCH := 386)
51+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
52+
mkdir -p ./build/${TARGET}
53+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}
54+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
55+
mv ./build/${TARGET}.zip ./release/
56+
57+
windows64: release_dir
58+
$(eval GOOS := windows)
59+
$(eval GOARCH := amd64)
60+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
61+
mkdir -p ./build/${TARGET}
62+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}.exe
63+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
64+
mv ./build/${TARGET}.zip ./release/
65+
66+
windows386: release_dir
67+
$(eval GOOS := windows)
68+
$(eval GOARCH := 386)
69+
$(eval TARGET := ${NAME}_${VERSION}_${GOOS}_${GOARCH})
70+
mkdir -p ./build/${TARGET}
71+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o ./build/${TARGET}/${NAME}.exe
72+
cd ./build && zip -r ${TARGET}.zip ${TARGET}
73+
mv ./build/${TARGET}.zip ./release/
74+
75+
clean:
76+
go clean
77+
rm -rf ./build ./release

dial.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"time"
6+
)
7+
8+
func Dial(srvName string, hostName string, port string, isInsecure bool) (*time.Time, error) {
9+
10+
tlsCfg := &tls.Config{
11+
ServerName: srvName,
12+
InsecureSkipVerify: isInsecure,
13+
}
14+
15+
conn, err := tls.Dial("tcp", hostName+":"+port, tlsCfg)
16+
if err != nil {
17+
return nil, err
18+
}
19+
defer conn.Close()
20+
21+
state := conn.ConnectionState()
22+
certs := state.PeerCertificates
23+
expire := &certs[0].NotAfter
24+
25+
return expire, nil
26+
}

dial_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestDial(t *testing.T) {
9+
10+
expire, err := Dial("google.com", "google.com", "443", false)
11+
12+
if err != nil {
13+
fmt.Println(err)
14+
t.Errorf("google.com's cert should not have error")
15+
}
16+
17+
fmt.Println(expire)
18+
}

exit_status.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
const (
4+
ExitError = 64
5+
ExitExpire = 65
6+
)

help_template.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
const HelpTemplate = `NAME:
4+
{{.Name}} - {{.Usage}}
5+
6+
USAGE:
7+
{{.HelpName}} [options] servername
8+
{{if len .Authors}}
9+
AUTHOR:
10+
{{range .Authors}}{{ . }}{{end}}
11+
{{end}}{{if .Commands}}
12+
OPTIONS:
13+
{{range .VisibleFlags}}{{.}}
14+
{{end}}{{end}}{{if .Copyright }}
15+
COPYRIGHT:
16+
{{.Copyright}}
17+
{{end}}{{if .Version}}
18+
VERSION:
19+
{{.Version}}
20+
{{end}}
21+
`

main.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/urfave/cli"
9+
)
10+
11+
func main() {
12+
app := cli.NewApp()
13+
14+
// App meta
15+
app.Name = Name
16+
app.Version = Version
17+
app.Authors = []cli.Author{
18+
cli.Author{
19+
Name: AuthorName,
20+
Email: AuthorEmail,
21+
},
22+
}
23+
app.Usage = Usage
24+
app.UsageText = fmt.Sprintf("%s [options] servername", Name)
25+
26+
// Flags
27+
var (
28+
srvName string
29+
hostName string
30+
port string
31+
delay int
32+
isInsecure bool
33+
isSilent bool
34+
)
35+
36+
app.Flags = []cli.Flag{
37+
cli.StringFlag{
38+
Name: "host, H",
39+
Usage: "target host for scanning",
40+
Destination: &hostName,
41+
},
42+
cli.StringFlag{
43+
Name: "port, p",
44+
Usage: "ssl port",
45+
Value: "443",
46+
Destination: &port,
47+
},
48+
cli.IntFlag{
49+
Name: "delay, d",
50+
Usage: "delay of expire",
51+
Value: 0,
52+
Destination: &delay,
53+
},
54+
cli.BoolFlag{
55+
Name: "insecure, k",
56+
Usage: "permit insecure cert",
57+
Destination: &isInsecure,
58+
},
59+
cli.BoolFlag{
60+
Name: "silent, s",
61+
Usage: "enable silent mode",
62+
Destination: &isSilent,
63+
},
64+
}
65+
66+
// Set HelpTemplate
67+
cli.AppHelpTemplate = HelpTemplate
68+
69+
// Main action
70+
app.Action = func(c *cli.Context) error {
71+
72+
// Args
73+
if c.NArg() != 1 {
74+
cli.ShowAppHelp(c)
75+
return nil
76+
}
77+
srvName = c.Args().Get(0)
78+
79+
if hostName == "" {
80+
hostName = srvName
81+
}
82+
83+
// Dial
84+
expire, err := Dial(srvName, hostName, port, isInsecure)
85+
if err != nil {
86+
if !isSilent {
87+
fmt.Println(err)
88+
}
89+
return cli.NewExitError("", ExitError)
90+
}
91+
92+
if !isSilent {
93+
fmt.Println(expire)
94+
}
95+
96+
// Check expire
97+
limit := time.Now().AddDate(0, 0, delay).UTC()
98+
if !expire.After(limit) {
99+
return cli.NewExitError("", ExitExpire)
100+
}
101+
102+
return nil
103+
}
104+
105+
app.Run(os.Args)
106+
}

meta.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
const (
4+
Name = "check-cert-expire"
5+
Version = "0.1.0"
6+
Usage = "Check weather a cert expires or not."
7+
AuthorName = "Tetsuya Nakamura"
8+
AuthorEmail = "[email protected]"
9+
)

0 commit comments

Comments
 (0)