Compare commits

..

5 Commits

Author SHA1 Message Date
Nils
55dfb5405a
reworked dockerfile for local building 2021-12-28 05:23:33 +01:00
Nils
0c4db69e08
add docker files 2021-12-28 04:29:37 +01:00
Nils
a62d199c8b
add systemd unit file 2021-12-28 04:11:50 +01:00
Nils
fed9b40c86
add traceroute 2021-12-28 04:02:54 +01:00
Nils
0eb1a032dc
remove help placeholder 2021-12-28 03:54:31 +01:00
4 changed files with 56 additions and 6 deletions

12
docker/Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM golang:latest as builder
WORKDIR /build
COPY . .
RUN go get -u
RUN CGO_ENABLED=0 go build -o probehost2
FROM alpine:latest
RUN apk update
RUN apk add mtr
COPY --from=builder /build/probehost2 /
RUN touch /probehost2.log
CMD ["/probehost2"]

11
docker/docker-compose.yml Normal file
View File

@ -0,0 +1,11 @@
version: '2.3'
services:
probehost2:
container_name: probehost2
image: byreqz/probehost2:latest
restart: unless-stopped
ports:
- 1234:8000
volumes:
- ./probehost2.log:/probehost2.log

23
main.go
View File

@ -14,10 +14,10 @@ import (
var logstdout = log.New()
var logfile = log.New()
var logfilepath string
var listenport int
func init() {
var logfilepath string
flag.StringVar(&logfilepath, "logfilepath", "probehost2.log", "sets the output file for the log")
flag.IntVar(&listenport, "port", 8000, "sets the port to listen on")
flag.Parse()
@ -64,10 +64,6 @@ func runner(remoteip string, command string, args... string) string{
return string(cmd)
}
func showhelp(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "placeholder")
}
func validatehosts(hosts []string) []string{
var valid []string
for _, host := range hosts {
@ -110,10 +106,25 @@ func mtr(w http.ResponseWriter, req *http.Request) {
}
}
func traceroute(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/")
targets := strings.Split(geturl[2], ",")
hosts := validatehosts(targets)
var res string
for _, host := range hosts {
res = fmt.Sprint(res, runner(req.RemoteAddr, "traceroute", host), "\n")
}
if res == "" {
fmt.Fprintln(w, http.StatusInternalServerError)
} else {
fmt.Fprint(w, strings.TrimSpace(res), "\n")
}
}
func main() {
http.HandleFunc("/ping/", ping)
http.HandleFunc("/mtr/", mtr)
http.HandleFunc("/", showhelp)
http.HandleFunc("/tracert/", traceroute)
logstdout.Info("Serving on :", listenport)
logfile.Info("Serving on :", listenport)
http.ListenAndServe(fmt.Sprint(":", listenport), nil)

View File

@ -0,0 +1,16 @@
# systemd service file for probehost2
[Unit]
Description=probehost2 daemon
Wants=network.target
After=network-online.target
StartLimitBurst=5
StartLimitIntervalSec=20
[Service]
Restart=always
RestartSec=1
ExecStart=/bin/probehost2 --logfilepath "/var/log/probehost2.log" --port 8000
[Install]
WantedBy=multi-user.target