diff --git a/docker-compose.yml b/docker-compose.yml index 363e258..7f1a63c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,18 +13,14 @@ services: ports: - "6752:6752" volumes: - - ./CaddyfileDev:/etc/caddy/Caddyfile - # - ./CaddyfileMaintance:/etc/caddy/Caddyfile + - ./CaddyfileDev:/etc/caddy/Caddyfile # - ./CaddyfileMaintance:/etc/caddy/Caddyfile - caddy_data:/data - caddy_config:/config + - ./static:/static networks: - caddy_net healthcheck: - test: - [ - "CMD-SHELL", - "curl -f http://caddy_freedium:6752/ --max-time 80 --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Safari/605.1.15'" - ] + test: [ "CMD-SHELL", "curl -f http://localhost:6752/ --max-time 80 --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Safari/605.1.15'" ] interval: 30s start_period: 20s timeout: 80s @@ -32,78 +28,13 @@ services: # restart: always stop_grace_period: 2m - wgcf1: - image: neilpang/wgcf-docker:latest - container_name: wgcf1 - volumes: - # - ./wgcf:/wgcf - - /lib/modules:/lib/modules - - ./scripts/entryWGCF.sh:/entry.sh - - /etc/localtime:/etc/localtime:ro - privileged: true - sysctls: - net.ipv6.conf.all.disable_ipv6: 0 - cap_add: - - NET_ADMIN - # ports: - # - "9681:1080" - restart: always - networks: - - caddy_net - healthcheck: - test: curl -fs https://www.cloudflare.com/cdn-cgi/trace | grep -q -E 'warp=(on|plus)' && exit 0 || exit 1 - interval: 5s - timeout: 2s - retries: 10 - - dante_1: - image: shturman/dante:1.4.2 - container_name: dante_1 - # build: - # context: ./ - # dockerfile: ./DockerfileDante - volumes: - - ./scripts/dante.config:/etc/sockd.conf - - /etc/localtime:/etc/localtime:ro - restart: always - environment: - - CFGFILE=/etc/sockd.conf - network_mode: "service:wgcf1" - depends_on: - wgcf1: - condition: service_healthy - # healthcheck: - # test: curl --proxy socks5://localhost:1080 https://google.com - # interval: 5s - # timeout: 2s - # retries: 5 - - wgcf1_healthcare_service: - build: - context: . - dockerfile: DockerfileHealthcare - environment: - - TG_TOKEN=${TELEGRAM_BOT_TOKEN} - - TG_CHAT_ID=${TELEGRAM_ADMIN_ID} - - PROXY_HOST=wgcf1 - - PROXY_PORT=1080 - depends_on: - - dante_1 - networks: - - caddy_net - volumes: - - /var/run/docker.sock:/var/run/docker.sock - - /etc/localtime:/etc/localtime:ro - - ./scripts/service_up.py:/scripts/service_up.py - # network_mode: "host" - web: build: context: ./ dockerfile: ./Dockerfile command: python3 -m server server environment: - - "PROXY_LIST=socks5://wgcf1:1080" + - "PROXY_LIST=socks5://wgcf1:1080,socks5://wgcf2:1080" volumes: # - ./.env:/app/.env # - ./server/user_data/logs:/app/server/user_data/logs @@ -142,18 +73,159 @@ services: mem_limit: 4g stop_grace_period: 2m - # redis_service: - # image: redis:latest - # networks: - # - caddy_net - # healthcheck: - # test: ["CMD", "redis-cli", "ping"] - # interval: 30s - # start_period: 20s - # timeout: 10s - # retries: 3 - # restart: always - # stop_grace_period: 2m + ########## WGCF PROXY ----1---- ########## + + wgcf1: + image: neilpang/wgcf-docker:latest + container_name: wgcf1 + volumes: + # - ./wgcf:/wgcf + - /lib/modules:/lib/modules + - ./scripts/entryWGCF.sh:/entry.sh + - /etc/localtime:/etc/localtime:ro + privileged: true + sysctls: + net.ipv6.conf.all.disable_ipv6: 0 + cap_add: + - NET_ADMIN + # ports: + # - "9681:1080" + restart: always + networks: + - caddy_net + healthcheck: + test: curl -fs https://www.cloudflare.com/cdn-cgi/trace | grep -q -E 'warp=(on|plus)' && exit 0 || exit 1 + interval: 5s + timeout: 2s + retries: 10000 + + dante_1: + image: shturman/dante:1.4.2 + container_name: dante_1 + # build: + # context: ./ + # dockerfile: ./DockerfileDante + volumes: + - ./scripts/dante.config:/etc/sockd.conf + - /etc/localtime:/etc/localtime:ro + restart: always + environment: + - CFGFILE=/etc/sockd.conf + network_mode: "service:wgcf1" + depends_on: + wgcf1: + condition: service_healthy + # healthcheck: + # test: curl --proxy socks5://localhost:1080 https://google.com + # interval: 5s + # timeout: 2s + # retries: 5 + + wgcf1_healthcare_service: + build: + context: ./scripts/wgcf-healthcare + dockerfile: Dockerfile + environment: + - TG_TOKEN=${TELEGRAM_BOT_TOKEN} + - TG_CHAT_ID=${TELEGRAM_ADMIN_ID} + - CONTAINERS_TO_RESTART=wgcf1,dante_1 + - PROXY_HOST=wgcf1 + - PROXY_PORT=1080 + - SLEEP_DURATION=30 + # depends_on: + # - dante_1 + restart: always + networks: + - caddy_net + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - /etc/localtime:/etc/localtime:ro + # network_mode: "host" + + ########## WGCF PROXY ----2---- ########## + + wgcf2: + image: neilpang/wgcf-docker:latest + container_name: wgcf2 + volumes: + # - ./wgcf:/wgcf + - /lib/modules:/lib/modules + - ./scripts/entryWGCF.sh:/entry.sh + - /etc/localtime:/etc/localtime:ro + privileged: true + sysctls: + net.ipv6.conf.all.disable_ipv6: 0 + depends_on: + wgcf1: + condition: service_healthy + cap_add: + - NET_ADMIN + # ports: + # - "9681:1080" + restart: always + networks: + - caddy_net + healthcheck: + test: curl -fs https://www.cloudflare.com/cdn-cgi/trace | grep -q -E 'warp=(on|plus)' && exit 0 || exit 1 + interval: 5s + timeout: 2s + retries: 10000 + + dante_2: + image: shturman/dante:1.4.2 + container_name: dante_2 + # build: + # context: ./ + # dockerfile: ./DockerfileDante + volumes: + - ./scripts/dante.config:/etc/sockd.conf + - /etc/localtime:/etc/localtime:ro + restart: always + environment: + - CFGFILE=/etc/sockd.conf + network_mode: "service:wgcf2" + depends_on: + wgcf2: + condition: service_healthy + # healthcheck: + # test: curl --proxy socks5://localhost:1080 https://google.com + # interval: 5s + # timeout: 2s + # retries: 5 + + wgcf2_healthcare_service: + build: + context: ./scripts/wgcf-healthcare + dockerfile: Dockerfile + environment: + - TG_TOKEN=${TELEGRAM_BOT_TOKEN} + - TG_CHAT_ID=${TELEGRAM_ADMIN_ID} + - CONTAINERS_TO_RESTART=wgcf2,dante_2 + - PROXY_HOST=wgcf2 + - PROXY_PORT=1080 + - SLEEP_DURATION=40 + # depends_on: + # - dante_2 + restart: always + networks: + - caddy_net + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - /etc/localtime:/etc/localtime:ro + # network_mode: "host" + + # redis_service: + # image: redis:latest + # networks: + # - caddy_net + # healthcheck: + # test: ["CMD", "redis-cli", "ping"] + # interval: 30s + # start_period: 20s + # timeout: 10s + # retries: 3 + # restart: always + # stop_grace_period: 2m redis_service: image: 'docker.dragonflydb.io/dragonflydb/dragonfly' @@ -173,7 +245,7 @@ services: retries: 3 restart: always stop_grace_period: 2m - # mem_limit: 1g + mem_limit: 2g postgres: image: postgres:16.3-alpine3.20 diff --git a/scripts/wgcf-healthcare/Dockerfile b/scripts/wgcf-healthcare/Dockerfile new file mode 100644 index 0000000..be2626d --- /dev/null +++ b/scripts/wgcf-healthcare/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:1.23.1-alpine3.20 AS builder + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . ./ + +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /main ./main.go + +FROM alpine:3.20 +WORKDIR /app + +# COPY .env . +COPY --from=builder /main . + +ENTRYPOINT ["/app/main"] \ No newline at end of file diff --git a/scripts/wgcf-healthcare/go.mod b/scripts/wgcf-healthcare/go.mod new file mode 100644 index 0000000..fc383e6 --- /dev/null +++ b/scripts/wgcf-healthcare/go.mod @@ -0,0 +1,25 @@ +module wgcf-checkhealth + +go 1.23.1 + +require ( + github.com/Microsoft/go-winio v0.4.14 // indirect + github.com/distribution/reference v0.6.0 // indirect + github.com/docker/docker v27.2.1+incompatible // indirect + github.com/docker/go-connections v0.5.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/moby/docker-image-spec v1.3.1 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect + go.opentelemetry.io/otel v1.30.0 // indirect + go.opentelemetry.io/otel/metric v1.30.0 // indirect + go.opentelemetry.io/otel/trace v1.30.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/scripts/wgcf-healthcare/go.sum b/scripts/wgcf-healthcare/go.sum new file mode 100644 index 0000000..281a086 --- /dev/null +++ b/scripts/wgcf-healthcare/go.sum @@ -0,0 +1,78 @@ +github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= +github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v27.2.1+incompatible h1:fQdiLfW7VLscyoeYEBz7/J8soYFDZV1u6VW6gJEjNMI= +github.com/docker/docker v27.2.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= +github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= +github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 h1:ZIg3ZT/aQ7AfKqdwp7ECpOK6vHqquXXuyTjIO8ZdmPs= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0/go.mod h1:DQAwmETtZV00skUwgD6+0U89g80NKsJE3DCKeLLPQMI= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/scripts/wgcf-healthcare/main.go b/scripts/wgcf-healthcare/main.go new file mode 100644 index 0000000..af3c04f --- /dev/null +++ b/scripts/wgcf-healthcare/main.go @@ -0,0 +1,206 @@ +package main + +import ( + "context" + "fmt" + "io/ioutil" + "log" + "net" + "net/http" + "net/url" + "os" + "strconv" + "strings" + "time" + + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/client" + "golang.org/x/net/proxy" +) + +const ( + errorMsg = "[Cloudflare proxy] Freedium proxy is not working, restarting containers..." + proxyTestURL = "http://ipinfo.io" + telegramAPIBaseURL = "https://api.telegram.org/bot" +) + +var ( + userName = os.Getenv("USER_NAME") + userPassword = os.Getenv("USER_PASSWORD") + proxyHost = getEnvOrDefault("PROXY_HOST", "localhost") + proxyPort = getEnvOrDefault("PROXY_PORT", "9681") + sleepDurationVar = getEnvOrDefault("SLEEP_DURATION", "35") + sleepDuration int + authFormatted = formatAuth(userName, userPassword) + containersToRestart = strings.Split(os.Getenv("CONTAINERS_TO_RESTART"), ",") // []string{"dante_1", "wgcf1"} +) + +func getEnvOrDefault(key, defaultValue string) string { + if value := os.Getenv(key); value != "" { + log.Printf("Using environment variable for %s: %s", key, value) + return value + } + log.Printf("Using default value for %s: %s", key, defaultValue) + return defaultValue +} + +func formatAuth(username, password string) string { + if username != "" && password != "" { + log.Printf("Formatting auth with username: %s", username) + return fmt.Sprintf("%s:%s@", username, password) + } + log.Println("No authentication provided") + return "" +} + +func restartContainers() { + log.Println("Starting container restart process") + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + log.Printf("Error creating Docker client: %v", err) + return + } + defer cli.Close() + + stopTimeout := 10 // seconds + stopOptions := container.StopOptions{ + Timeout: &stopTimeout, + } + log.Printf("Stop timeout set to %d seconds", stopTimeout) + + for _, containerName := range containersToRestart { + log.Printf("Attempting to restart container: %s", containerName) + if err := cli.ContainerRestart(context.Background(), containerName, stopOptions); err != nil { + log.Printf("Error restarting %s: %v", containerName, err) + } else { + log.Printf("%s restarted successfully.", containerName) + } + } + log.Println("Container restart process completed") +} + +func sendMsg(text string, maxRetries int) bool { + log.Printf("Attempting to send message: %s", text) + tgToken := os.Getenv("TG_TOKEN") + chatID := os.Getenv("TG_CHAT_ID") + + if tgToken == "" || chatID == "" { + log.Println("TG_TOKEN or TG_CHAT_ID is not set, skipping message sending") + return false + } + + urlReq := fmt.Sprintf("%s%s/sendMessage?chat_id=%s&text=%s", telegramAPIBaseURL, tgToken, chatID, url.QueryEscape(text)) + + for attempt := 0; attempt < maxRetries; attempt++ { + log.Printf("Sending message attempt %d/%d", attempt+1, maxRetries) + resp, err := http.Get(urlReq) + if err != nil { + log.Printf("Error sending message (attempt %d/%d): %v", attempt+1, maxRetries, err) + if attempt < maxRetries-1 { + log.Println("Retrying in 5 seconds...") + time.Sleep(5 * time.Second) + } + continue + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + log.Println("Message sent successfully.") + return true + } + log.Printf("Unexpected status code: %d", resp.StatusCode) + } + + log.Println("All retry attempts failed. Message not sent.") + return false +} + +func checkProxy(maxRetries int, retryDelay time.Duration) bool { + proxyURL, err := url.Parse(fmt.Sprintf("socks5://%s%s:%s", authFormatted, proxyHost, proxyPort)) + if err != nil { + log.Printf("Error parsing proxy URL: %v", err) + return false + } + log.Printf("Checking proxy: %s", proxyURL) + + dialer, err := proxy.FromURL(proxyURL, proxy.Direct) + if err != nil { + log.Printf("Error creating proxy dialer: %v", err) + return false + } + + httpTransport := &http.Transport{ + Dial: dialer.Dial, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr) + }, + } + client := &http.Client{Transport: httpTransport, Timeout: 10 * time.Second} + log.Printf("HTTP client created with timeout: %v", client.Timeout) + + for attempt := 0; attempt < maxRetries; attempt++ { + log.Printf("Proxy check attempt %d/%d", attempt+1, maxRetries) + resp, err := client.Get(proxyTestURL) + if err != nil { + log.Printf("Error checking proxy (attempt %d/%d): %v", attempt+1, maxRetries, err) + if attempt < maxRetries-1 { + log.Printf("Retrying in %v seconds...", retryDelay.Seconds()) + time.Sleep(retryDelay) + } + continue + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Printf("Error reading response body: %v", err) + continue + } + + log.Printf("Proxy is working: Status Code %d, Body: %s", resp.StatusCode, string(body)) + return true + } + + log.Println("All retry attempts failed. Proxy is not working.") + return false +} + +func main() { + log.Println("Starting proxy monitoring service") + + if len(containersToRestart) == 0 { + log.Fatal("CONTAINERS_TO_RESTART is not set") + } + + log.Printf("Containers to restart: %v", containersToRestart) + log.Printf("Proxy host: %s", proxyHost) + log.Printf("Proxy port: %s", proxyPort) + log.Printf("Auth formatted: %s", authFormatted) + + if proxyHost == "" || proxyPort == "" { + log.Fatal("PROXY_HOST and PROXY_PORT must be set") + } + + log.Printf("Proxy configuration: Host=%s, Port=%s", proxyHost, proxyPort) + + var err error + sleepDuration, err = strconv.Atoi(sleepDurationVar) + if err != nil { + log.Fatalf("Invalid sleep duration: %v", err) + } + + for { + log.Printf("Waiting for %d seconds before next proxy check", sleepDuration) + time.Sleep(time.Duration(sleepDuration) * time.Second) + log.Println("Initiating proxy check...") + if !checkProxy(3, 5*time.Second) { + log.Println(errorMsg) + log.Println("Sending error message via Telegram") + sendMsg(errorMsg, 3) + log.Println("Initiating container restart process") + restartContainers() + } else { + log.Println("Proxy check successful") + } + } +}