Skip to content

Commit

Permalink
Restart containers with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
eiqnepm committed Feb 3, 2024
1 parent 6fd9b42 commit 8ba3e90
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 317 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: ci
on:
push:
branches:
- "dev"
- "restart"

jobs:
docker:
Expand All @@ -26,4 +26,4 @@ jobs:
context: .
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
push: true
tags: eiqnepm/portcheck:dev
tags: eiqnepm/portcheck:restart
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ WORKDIR /usr/src/app

COPY go.mod ./
COPY cmd ./cmd
COPY internal ./internal

RUN go build -ldflags="-s -w" -o /usr/local/bin/app cmd/portcheck/main.go

Expand Down
144 changes: 64 additions & 80 deletions cmd/portcheck/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package main

import (
"encoding/json"
"log"
"net"
u "net/url"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/Eiqnepm/portcheck/internal/deluge"
"github.com/Eiqnepm/portcheck/internal/network"
"github.com/Eiqnepm/portcheck/internal/qbit"
)

func env(key string, defaultValue string) (value string) {
Expand All @@ -23,32 +20,50 @@ func env(key string, defaultValue string) (value string) {
return
}

func main() {
log.SetFlags(log.LstdFlags)
func getLocalAddr() (string, error) {
conn, err := net.Dial("tcp", "255.255.255.255:0")
if err != nil {
return "", nil
}

client := env("CLIENT", "qBittorrent")
defer func(conn *net.Conn) {
err := (*conn).Close()
if err != nil {
log.Println(err)
}
}(&conn)

clientPort, err := strconv.Atoi(env("CLIENT_PORT", "6881"))
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
log.Fatal(err)
return "", err
}

clientWebScheme := env("CLIENT_WEBUI_SCHEME", "http")
clientWebHost := env("CLIENT_WEBUI_HOST", "localhost")
clientWebPort := env("CLIENT_WEBUI_PORT", "8080")
if !strings.EqualFold(client, "qBittorrent") {
clientWebPort = env("CLIENT_WEBUI_PORT", "8112")
return host, nil
}

func queryPort(network string, ip string, port int, timeout time.Duration) error {
conn, err := net.DialTimeout(network, net.JoinHostPort(ip, strconv.Itoa(port)), timeout)
if err != nil {
return err
}
clientWebUrl := u.URL{
Scheme: clientWebScheme,
Host: net.JoinHostPort(clientWebHost, clientWebPort),

if err := conn.Close(); err != nil {
log.Println(err)
}

qbitUsername := env("CLIENT_USERNAME", "admin")
clientPassword := env("CLIENT_PASSWORD", "adminadmin")
if !strings.EqualFold(client, "qBittorrent") {
clientPassword = env("CLIENT_PASSWORD", "deluge")
return nil
}

func main() {
log.SetFlags(log.LstdFlags | log.Llongfile)

port, err := strconv.Atoi(env("PORT", "6881"))
if err != nil {
log.Fatal(err)
}

network := env("NETWORK", "tcp")

t, err := strconv.Atoi(env("TIMEOUT", "300"))
if err != nil {
log.Fatal(err)
Expand All @@ -62,83 +77,52 @@ func main() {

dialTimeout := time.Duration(t) * time.Second

firstLoop := true
for {
if !firstLoop {
time.Sleep(timeout)
}

firstLoop = false

outboundIp, err := network.GetOutboundIP()
for range time.Tick(timeout) {
localAddr, err := getLocalAddr()
if err != nil {
log.Println(err)
continue
}

err = network.QueryPort(outboundIp, clientPort, dialTimeout)
err = queryPort(network, localAddr, port, dialTimeout)
if err == nil {
continue
}

log.Println(err)

if !strings.EqualFold(client, "qBittorrent") {
func() {
sesh, err := deluge.Login(clientWebUrl, clientPassword)
if err != nil {
log.Println(err)
return
}

defer func(sesh deluge.Session) {
err := sesh.Logout()
if err != nil {
log.Println(err)
}
}(sesh)

err = sesh.SetPreference("listen_ports", []int{0})
if err != nil {
log.Println(err)
return
}

err = sesh.SetPreference("listen_ports", []int{clientPort})
if err != nil {
log.Println(err)
return
}
}()
args := []string{
"ps",
"--format",
"json",
"--filter",
"label=io.github.eiqnepm.portcheck.enable=true",
}

output, err := exec.Command("docker", args...).Output()
if err != nil {
log.Println(err)
continue
}

func() {
session, err := qbit.Login(clientWebUrl, qbitUsername, clientPassword)
if err != nil {
log.Println(err)
return
ids := []string{"restart"}
for _, line := range strings.Split(string(output), "\n") {
var container struct {
ID string `json:"ID"`
}

defer func(session qbit.Session) {
err := session.Logout()
if err != nil {
log.Println(err)
}
}(session)

err = session.SetPreference("listen_port", 0)
if err != nil {
if err := json.Unmarshal([]byte(line), &container); err != nil {
log.Println(err)
return
continue
}

err = session.SetPreference("listen_port", clientPort)
if err != nil {
log.Println(err)
return
}
}()
log.Println(container.ID)
ids = append(ids, container.ID)
}

if _, err := exec.Command("docker", ids...).Output(); err != nil {
log.Println(err)
continue
}
}
}
88 changes: 0 additions & 88 deletions internal/deluge/deluge.go

This file was deleted.

40 changes: 0 additions & 40 deletions internal/network/network.go

This file was deleted.

Loading

0 comments on commit 8ba3e90

Please sign in to comment.